blob: bce4de32d54d7875af66d42ac5ffb057b4d553a9 [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 };
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000445 const uint32_t num_instructions = code_item_->insns_size_in_code_units_;
Vladimir Marko69d310e2017-10-09 14:12:23 +0100446 ArenaBitVector* locations = ArenaBitVector::Create(local_allocator_,
447 num_instructions,
448 /* expandable */ false,
449 kArenaAllocGraphBuilder);
450 locations->ClearAllBits();
Nicolas Geoffray58cc1cb2017-11-20 13:27:29 +0000451 uint32_t debug_info_offset = OatFile::GetDebugInfoOffset(*dex_file_, code_item_);
452 dex_file_->DecodeDebugPositionInfo(code_item_, debug_info_offset, Callback::Position, locations);
David Brazdildee58d62016-04-07 09:54:26 +0000453 // Instruction-specific tweaks.
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000454 IterationRange<DexInstructionIterator> instructions = code_item_->Instructions();
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800455 for (const DexInstructionPcPair& inst : instructions) {
456 switch (inst->Opcode()) {
David Brazdildee58d62016-04-07 09:54:26 +0000457 case Instruction::MOVE_EXCEPTION: {
458 // Stop in native debugger after the exception has been moved.
459 // The compiler also expects the move at the start of basic block so
460 // we do not want to interfere by inserting native-debug-info before it.
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800461 locations->ClearBit(inst.DexPc());
462 DexInstructionIterator next = std::next(DexInstructionIterator(inst));
463 DCHECK(next.DexPc() != inst.DexPc());
Mathieu Chartier2b2bef22017-10-26 17:10:19 -0700464 if (next != instructions.end()) {
465 locations->SetBit(next.DexPc());
David Brazdildee58d62016-04-07 09:54:26 +0000466 }
467 break;
468 }
469 default:
470 break;
471 }
472 }
Vladimir Marko69d310e2017-10-09 14:12:23 +0100473 return locations;
David Brazdildee58d62016-04-07 09:54:26 +0000474}
475
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100476HInstruction* HInstructionBuilder::LoadLocal(uint32_t reg_number, DataType::Type type) const {
David Brazdildee58d62016-04-07 09:54:26 +0000477 HInstruction* value = (*current_locals_)[reg_number];
478 DCHECK(value != nullptr);
479
480 // If the operation requests a specific type, we make sure its input is of that type.
481 if (type != value->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100482 if (DataType::IsFloatingPointType(type)) {
Aart Bik31883642016-06-06 15:02:44 -0700483 value = ssa_builder_->GetFloatOrDoubleEquivalent(value, type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100484 } else if (type == DataType::Type::kReference) {
Aart Bik31883642016-06-06 15:02:44 -0700485 value = ssa_builder_->GetReferenceTypeEquivalent(value);
David Brazdildee58d62016-04-07 09:54:26 +0000486 }
Aart Bik31883642016-06-06 15:02:44 -0700487 DCHECK(value != nullptr);
David Brazdildee58d62016-04-07 09:54:26 +0000488 }
489
490 return value;
491}
492
493void HInstructionBuilder::UpdateLocal(uint32_t reg_number, HInstruction* stored_value) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100494 DataType::Type stored_type = stored_value->GetType();
495 DCHECK_NE(stored_type, DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +0000496
497 // Storing into vreg `reg_number` may implicitly invalidate the surrounding
498 // registers. Consider the following cases:
499 // (1) Storing a wide value must overwrite previous values in both `reg_number`
500 // and `reg_number+1`. We store `nullptr` in `reg_number+1`.
501 // (2) If vreg `reg_number-1` holds a wide value, writing into `reg_number`
502 // must invalidate it. We store `nullptr` in `reg_number-1`.
503 // Consequently, storing a wide value into the high vreg of another wide value
504 // will invalidate both `reg_number-1` and `reg_number+1`.
505
506 if (reg_number != 0) {
507 HInstruction* local_low = (*current_locals_)[reg_number - 1];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100508 if (local_low != nullptr && DataType::Is64BitType(local_low->GetType())) {
David Brazdildee58d62016-04-07 09:54:26 +0000509 // The vreg we are storing into was previously the high vreg of a pair.
510 // We need to invalidate its low vreg.
511 DCHECK((*current_locals_)[reg_number] == nullptr);
512 (*current_locals_)[reg_number - 1] = nullptr;
513 }
514 }
515
516 (*current_locals_)[reg_number] = stored_value;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100517 if (DataType::Is64BitType(stored_type)) {
David Brazdildee58d62016-04-07 09:54:26 +0000518 // We are storing a pair. Invalidate the instruction in the high vreg.
519 (*current_locals_)[reg_number + 1] = nullptr;
520 }
521}
522
523void HInstructionBuilder::InitializeParameters() {
524 DCHECK(current_block_->IsEntryBlock());
525
Vladimir Marko69d310e2017-10-09 14:12:23 +0100526 // outer_compilation_unit_ is null only when unit testing.
527 if (outer_compilation_unit_ == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +0000528 return;
529 }
530
531 const char* shorty = dex_compilation_unit_->GetShorty();
532 uint16_t number_of_parameters = graph_->GetNumberOfInVRegs();
533 uint16_t locals_index = graph_->GetNumberOfLocalVRegs();
534 uint16_t parameter_index = 0;
535
536 const DexFile::MethodId& referrer_method_id =
537 dex_file_->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
538 if (!dex_compilation_unit_->IsStatic()) {
539 // Add the implicit 'this' argument, not expressed in the signature.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100540 HParameterValue* parameter = new (allocator_) HParameterValue(*dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +0000541 referrer_method_id.class_idx_,
542 parameter_index++,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100543 DataType::Type::kReference,
Igor Murashkind01745e2017-04-05 16:40:31 -0700544 /* is_this */ true);
David Brazdildee58d62016-04-07 09:54:26 +0000545 AppendInstruction(parameter);
546 UpdateLocal(locals_index++, parameter);
547 number_of_parameters--;
Igor Murashkind01745e2017-04-05 16:40:31 -0700548 current_this_parameter_ = parameter;
549 } else {
550 DCHECK(current_this_parameter_ == nullptr);
David Brazdildee58d62016-04-07 09:54:26 +0000551 }
552
553 const DexFile::ProtoId& proto = dex_file_->GetMethodPrototype(referrer_method_id);
554 const DexFile::TypeList* arg_types = dex_file_->GetProtoParameters(proto);
555 for (int i = 0, shorty_pos = 1; i < number_of_parameters; i++) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100556 HParameterValue* parameter = new (allocator_) HParameterValue(
David Brazdildee58d62016-04-07 09:54:26 +0000557 *dex_file_,
558 arg_types->GetTypeItem(shorty_pos - 1).type_idx_,
559 parameter_index++,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100560 DataType::FromShorty(shorty[shorty_pos]),
Igor Murashkind01745e2017-04-05 16:40:31 -0700561 /* is_this */ false);
David Brazdildee58d62016-04-07 09:54:26 +0000562 ++shorty_pos;
563 AppendInstruction(parameter);
564 // Store the parameter value in the local that the dex code will use
565 // to reference that parameter.
566 UpdateLocal(locals_index++, parameter);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100567 if (DataType::Is64BitType(parameter->GetType())) {
David Brazdildee58d62016-04-07 09:54:26 +0000568 i++;
569 locals_index++;
570 parameter_index++;
571 }
572 }
573}
574
575template<typename T>
576void HInstructionBuilder::If_22t(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100577 HInstruction* first = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
578 HInstruction* second = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100579 T* comparison = new (allocator_) T(first, second, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000580 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100581 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000582 current_block_ = nullptr;
583}
584
585template<typename T>
586void HInstructionBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100587 HInstruction* value = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100588 T* comparison = new (allocator_) T(value, graph_->GetIntConstant(0, dex_pc), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000589 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100590 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000591 current_block_ = nullptr;
592}
593
594template<typename T>
595void HInstructionBuilder::Unop_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100596 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000597 uint32_t dex_pc) {
598 HInstruction* first = LoadLocal(instruction.VRegB(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100599 AppendInstruction(new (allocator_) T(type, first, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000600 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
601}
602
603void HInstructionBuilder::Conversion_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100604 DataType::Type input_type,
605 DataType::Type result_type,
David Brazdildee58d62016-04-07 09:54:26 +0000606 uint32_t dex_pc) {
607 HInstruction* first = LoadLocal(instruction.VRegB(), input_type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100608 AppendInstruction(new (allocator_) HTypeConversion(result_type, first, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000609 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
610}
611
612template<typename T>
613void HInstructionBuilder::Binop_23x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100614 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000615 uint32_t dex_pc) {
616 HInstruction* first = LoadLocal(instruction.VRegB(), type);
617 HInstruction* second = LoadLocal(instruction.VRegC(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100618 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000619 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
620}
621
622template<typename T>
623void HInstructionBuilder::Binop_23x_shift(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100624 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000625 uint32_t dex_pc) {
626 HInstruction* first = LoadLocal(instruction.VRegB(), type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100627 HInstruction* second = LoadLocal(instruction.VRegC(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100628 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000629 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
630}
631
632void HInstructionBuilder::Binop_23x_cmp(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100633 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000634 ComparisonBias bias,
635 uint32_t dex_pc) {
636 HInstruction* first = LoadLocal(instruction.VRegB(), type);
637 HInstruction* second = LoadLocal(instruction.VRegC(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100638 AppendInstruction(new (allocator_) HCompare(type, first, second, bias, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000639 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
640}
641
642template<typename T>
643void HInstructionBuilder::Binop_12x_shift(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100644 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000645 uint32_t dex_pc) {
646 HInstruction* first = LoadLocal(instruction.VRegA(), type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100647 HInstruction* second = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100648 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000649 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
650}
651
652template<typename T>
653void HInstructionBuilder::Binop_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100654 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000655 uint32_t dex_pc) {
656 HInstruction* first = LoadLocal(instruction.VRegA(), type);
657 HInstruction* second = LoadLocal(instruction.VRegB(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100658 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000659 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
660}
661
662template<typename T>
663void HInstructionBuilder::Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100664 HInstruction* first = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000665 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s(), dex_pc);
666 if (reverse) {
667 std::swap(first, second);
668 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100669 AppendInstruction(new (allocator_) T(DataType::Type::kInt32, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000670 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
671}
672
673template<typename T>
674void HInstructionBuilder::Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100675 HInstruction* first = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000676 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b(), dex_pc);
677 if (reverse) {
678 std::swap(first, second);
679 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100680 AppendInstruction(new (allocator_) T(DataType::Type::kInt32, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000681 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
682}
683
Igor Murashkind01745e2017-04-05 16:40:31 -0700684// Does the method being compiled need any constructor barriers being inserted?
685// (Always 'false' for methods that aren't <init>.)
Mathieu Chartierc4ae9162016-04-07 13:19:19 -0700686static bool RequiresConstructorBarrier(const DexCompilationUnit* cu, CompilerDriver* driver) {
Igor Murashkin032cacd2017-04-06 14:40:08 -0700687 // Can be null in unit tests only.
688 if (UNLIKELY(cu == nullptr)) {
689 return false;
690 }
691
David Brazdildee58d62016-04-07 09:54:26 +0000692 Thread* self = Thread::Current();
693 return cu->IsConstructor()
Igor Murashkind01745e2017-04-05 16:40:31 -0700694 && !cu->IsStatic()
695 // RequiresConstructorBarrier must only be queried for <init> methods;
696 // it's effectively "false" for every other method.
697 //
698 // See CompilerDriver::RequiresConstructBarrier for more explanation.
Mathieu Chartierc4ae9162016-04-07 13:19:19 -0700699 && driver->RequiresConstructorBarrier(self, cu->GetDexFile(), cu->GetClassDefIndex());
David Brazdildee58d62016-04-07 09:54:26 +0000700}
701
702// Returns true if `block` has only one successor which starts at the next
703// dex_pc after `instruction` at `dex_pc`.
704static bool IsFallthroughInstruction(const Instruction& instruction,
705 uint32_t dex_pc,
706 HBasicBlock* block) {
707 uint32_t next_dex_pc = dex_pc + instruction.SizeInCodeUnits();
708 return block->GetSingleSuccessor()->GetDexPc() == next_dex_pc;
709}
710
711void HInstructionBuilder::BuildSwitch(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100712 HInstruction* value = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000713 DexSwitchTable table(instruction, dex_pc);
714
715 if (table.GetNumEntries() == 0) {
716 // Empty Switch. Code falls through to the next block.
717 DCHECK(IsFallthroughInstruction(instruction, dex_pc, current_block_));
Vladimir Markoca6fff82017-10-03 14:49:14 +0100718 AppendInstruction(new (allocator_) HGoto(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000719 } else if (table.ShouldBuildDecisionTree()) {
720 for (DexSwitchTableIterator it(table); !it.Done(); it.Advance()) {
721 HInstruction* case_value = graph_->GetIntConstant(it.CurrentKey(), dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100722 HEqual* comparison = new (allocator_) HEqual(value, case_value, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000723 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100724 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000725
726 if (!it.IsLast()) {
727 current_block_ = FindBlockStartingAt(it.GetDexPcForCurrentIndex());
728 }
729 }
730 } else {
731 AppendInstruction(
Vladimir Markoca6fff82017-10-03 14:49:14 +0100732 new (allocator_) HPackedSwitch(table.GetEntryAt(0), table.GetNumEntries(), value, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000733 }
734
735 current_block_ = nullptr;
736}
737
738void HInstructionBuilder::BuildReturn(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100739 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000740 uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100741 if (type == DataType::Type::kVoid) {
Igor Murashkind01745e2017-04-05 16:40:31 -0700742 // Only <init> (which is a return-void) could possibly have a constructor fence.
Igor Murashkin032cacd2017-04-06 14:40:08 -0700743 // This may insert additional redundant constructor fences from the super constructors.
744 // TODO: remove redundant constructor fences (b/36656456).
745 if (RequiresConstructorBarrier(dex_compilation_unit_, compiler_driver_)) {
Igor Murashkind01745e2017-04-05 16:40:31 -0700746 // Compiling instance constructor.
Vladimir Markoba118822017-06-12 15:41:56 +0100747 DCHECK_STREQ("<init>", graph_->GetMethodName());
Igor Murashkind01745e2017-04-05 16:40:31 -0700748
749 HInstruction* fence_target = current_this_parameter_;
750 DCHECK(fence_target != nullptr);
751
Vladimir Markoca6fff82017-10-03 14:49:14 +0100752 AppendInstruction(new (allocator_) HConstructorFence(fence_target, dex_pc, allocator_));
Igor Murashkin6ef45672017-08-08 13:59:55 -0700753 MaybeRecordStat(
754 compilation_stats_,
755 MethodCompilationStat::kConstructorFenceGeneratedFinal);
David Brazdildee58d62016-04-07 09:54:26 +0000756 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100757 AppendInstruction(new (allocator_) HReturnVoid(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000758 } else {
Igor Murashkind01745e2017-04-05 16:40:31 -0700759 DCHECK(!RequiresConstructorBarrier(dex_compilation_unit_, compiler_driver_));
David Brazdildee58d62016-04-07 09:54:26 +0000760 HInstruction* value = LoadLocal(instruction.VRegA(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100761 AppendInstruction(new (allocator_) HReturn(value, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000762 }
763 current_block_ = nullptr;
764}
765
766static InvokeType GetInvokeTypeFromOpCode(Instruction::Code opcode) {
767 switch (opcode) {
768 case Instruction::INVOKE_STATIC:
769 case Instruction::INVOKE_STATIC_RANGE:
770 return kStatic;
771 case Instruction::INVOKE_DIRECT:
772 case Instruction::INVOKE_DIRECT_RANGE:
773 return kDirect;
774 case Instruction::INVOKE_VIRTUAL:
775 case Instruction::INVOKE_VIRTUAL_QUICK:
776 case Instruction::INVOKE_VIRTUAL_RANGE:
777 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
778 return kVirtual;
779 case Instruction::INVOKE_INTERFACE:
780 case Instruction::INVOKE_INTERFACE_RANGE:
781 return kInterface;
782 case Instruction::INVOKE_SUPER_RANGE:
783 case Instruction::INVOKE_SUPER:
784 return kSuper;
785 default:
786 LOG(FATAL) << "Unexpected invoke opcode: " << opcode;
787 UNREACHABLE();
788 }
789}
790
791ArtMethod* HInstructionBuilder::ResolveMethod(uint16_t method_idx, InvokeType invoke_type) {
792 ScopedObjectAccess soa(Thread::Current());
David Brazdildee58d62016-04-07 09:54:26 +0000793
794 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000795 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100796
Vladimir Markoba118822017-06-12 15:41:56 +0100797 ArtMethod* resolved_method =
798 class_linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
799 *dex_compilation_unit_->GetDexFile(),
800 method_idx,
801 dex_compilation_unit_->GetDexCache(),
802 class_loader,
803 graph_->GetArtMethod(),
804 invoke_type);
David Brazdildee58d62016-04-07 09:54:26 +0000805
806 if (UNLIKELY(resolved_method == nullptr)) {
807 // Clean up any exception left by type resolution.
808 soa.Self()->ClearException();
809 return nullptr;
810 }
811
Vladimir Markoba118822017-06-12 15:41:56 +0100812 // The referrer may be unresolved for AOT if we're compiling a class that cannot be
813 // resolved because, for example, we don't find a superclass in the classpath.
814 if (graph_->GetArtMethod() == nullptr) {
815 // The class linker cannot check access without a referrer, so we have to do it.
816 // Fall back to HInvokeUnresolved if the method isn't public.
David Brazdildee58d62016-04-07 09:54:26 +0000817 if (!resolved_method->IsPublic()) {
818 return nullptr;
819 }
David Brazdildee58d62016-04-07 09:54:26 +0000820 }
821
822 // We have to special case the invoke-super case, as ClassLinker::ResolveMethod does not.
823 // We need to look at the referrer's super class vtable. We need to do this to know if we need to
824 // make this an invoke-unresolved to handle cross-dex invokes or abstract super methods, both of
825 // which require runtime handling.
826 if (invoke_type == kSuper) {
Vladimir Markoba118822017-06-12 15:41:56 +0100827 ObjPtr<mirror::Class> compiling_class = GetCompilingClass();
Andreas Gampefa4333d2017-02-14 11:10:34 -0800828 if (compiling_class == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +0000829 // We could not determine the method's class we need to wait until runtime.
830 DCHECK(Runtime::Current()->IsAotCompiler());
831 return nullptr;
832 }
Vladimir Markoba118822017-06-12 15:41:56 +0100833 ObjPtr<mirror::Class> referenced_class = class_linker->LookupResolvedType(
834 *dex_compilation_unit_->GetDexFile(),
835 dex_compilation_unit_->GetDexFile()->GetMethodId(method_idx).class_idx_,
836 dex_compilation_unit_->GetDexCache().Get(),
837 class_loader.Get());
838 DCHECK(referenced_class != nullptr); // We have already resolved a method from this class.
839 if (!referenced_class->IsAssignableFrom(compiling_class)) {
Aart Bikf663e342016-04-04 17:28:59 -0700840 // We cannot statically determine the target method. The runtime will throw a
841 // NoSuchMethodError on this one.
842 return nullptr;
843 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100844 ArtMethod* actual_method;
Vladimir Markoba118822017-06-12 15:41:56 +0100845 if (referenced_class->IsInterface()) {
846 actual_method = referenced_class->FindVirtualMethodForInterfaceSuper(
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100847 resolved_method, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000848 } else {
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100849 uint16_t vtable_index = resolved_method->GetMethodIndex();
850 actual_method = compiling_class->GetSuperClass()->GetVTableEntry(
851 vtable_index, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000852 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100853 if (actual_method != resolved_method &&
854 !IsSameDexFile(*actual_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
855 // The back-end code generator relies on this check in order to ensure that it will not
856 // attempt to read the dex_cache with a dex_method_index that is not from the correct
857 // dex_file. If we didn't do this check then the dex_method_index will not be updated in the
858 // builder, which means that the code-generator (and compiler driver during sharpening and
859 // inliner, maybe) might invoke an incorrect method.
860 // TODO: The actual method could still be referenced in the current dex file, so we
861 // could try locating it.
862 // TODO: Remove the dex_file restriction.
863 return nullptr;
864 }
865 if (!actual_method->IsInvokable()) {
866 // Fail if the actual method cannot be invoked. Otherwise, the runtime resolution stub
867 // could resolve the callee to the wrong method.
868 return nullptr;
869 }
870 resolved_method = actual_method;
David Brazdildee58d62016-04-07 09:54:26 +0000871 }
872
David Brazdildee58d62016-04-07 09:54:26 +0000873 return resolved_method;
874}
875
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100876static bool IsStringConstructor(ArtMethod* method) {
877 ScopedObjectAccess soa(Thread::Current());
878 return method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
879}
880
David Brazdildee58d62016-04-07 09:54:26 +0000881bool HInstructionBuilder::BuildInvoke(const Instruction& instruction,
882 uint32_t dex_pc,
883 uint32_t method_idx,
884 uint32_t number_of_vreg_arguments,
885 bool is_range,
886 uint32_t* args,
887 uint32_t register_index) {
888 InvokeType invoke_type = GetInvokeTypeFromOpCode(instruction.Opcode());
889 const char* descriptor = dex_file_->GetMethodShorty(method_idx);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100890 DataType::Type return_type = DataType::FromShorty(descriptor[0]);
David Brazdildee58d62016-04-07 09:54:26 +0000891
892 // Remove the return type from the 'proto'.
893 size_t number_of_arguments = strlen(descriptor) - 1;
894 if (invoke_type != kStatic) { // instance call
895 // One extra argument for 'this'.
896 number_of_arguments++;
897 }
898
David Brazdildee58d62016-04-07 09:54:26 +0000899 ArtMethod* resolved_method = ResolveMethod(method_idx, invoke_type);
900
901 if (UNLIKELY(resolved_method == nullptr)) {
Igor Murashkin1e065a52017-08-09 13:20:34 -0700902 MaybeRecordStat(compilation_stats_,
903 MethodCompilationStat::kUnresolvedMethod);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100904 HInvoke* invoke = new (allocator_) HInvokeUnresolved(allocator_,
905 number_of_arguments,
906 return_type,
907 dex_pc,
908 method_idx,
909 invoke_type);
David Brazdildee58d62016-04-07 09:54:26 +0000910 return HandleInvoke(invoke,
911 number_of_vreg_arguments,
912 args,
913 register_index,
914 is_range,
915 descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -0700916 nullptr, /* clinit_check */
917 true /* is_unresolved */);
David Brazdildee58d62016-04-07 09:54:26 +0000918 }
919
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100920 // Replace calls to String.<init> with StringFactory.
921 if (IsStringConstructor(resolved_method)) {
922 uint32_t string_init_entry_point = WellKnownClasses::StringInitToEntryPoint(resolved_method);
923 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
924 HInvokeStaticOrDirect::MethodLoadKind::kStringInit,
925 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000926 dchecked_integral_cast<uint64_t>(string_init_entry_point)
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100927 };
928 MethodReference target_method(dex_file_, method_idx);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100929 HInvoke* invoke = new (allocator_) HInvokeStaticOrDirect(
930 allocator_,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100931 number_of_arguments - 1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100932 DataType::Type::kReference /*return_type */,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100933 dex_pc,
934 method_idx,
935 nullptr,
936 dispatch_info,
937 invoke_type,
938 target_method,
939 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit);
940 return HandleStringInit(invoke,
941 number_of_vreg_arguments,
942 args,
943 register_index,
944 is_range,
945 descriptor);
946 }
947
David Brazdildee58d62016-04-07 09:54:26 +0000948 // Potential class initialization check, in the case of a static method call.
949 HClinitCheck* clinit_check = nullptr;
950 HInvoke* invoke = nullptr;
951 if (invoke_type == kDirect || invoke_type == kStatic || invoke_type == kSuper) {
952 // By default, consider that the called method implicitly requires
953 // an initialization check of its declaring method.
954 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement
955 = HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
956 ScopedObjectAccess soa(Thread::Current());
957 if (invoke_type == kStatic) {
958 clinit_check = ProcessClinitCheckForInvoke(
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000959 dex_pc, resolved_method, &clinit_check_requirement);
David Brazdildee58d62016-04-07 09:54:26 +0000960 } else if (invoke_type == kSuper) {
961 if (IsSameDexFile(*resolved_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100962 // Update the method index to the one resolved. Note that this may be a no-op if
David Brazdildee58d62016-04-07 09:54:26 +0000963 // we resolved to the method referenced by the instruction.
964 method_idx = resolved_method->GetDexMethodIndex();
David Brazdildee58d62016-04-07 09:54:26 +0000965 }
966 }
967
968 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
Vladimir Markoe7197bf2017-06-02 17:00:23 +0100969 HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall,
David Brazdildee58d62016-04-07 09:54:26 +0000970 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000971 0u
David Brazdildee58d62016-04-07 09:54:26 +0000972 };
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100973 MethodReference target_method(resolved_method->GetDexFile(),
974 resolved_method->GetDexMethodIndex());
Vladimir Markoca6fff82017-10-03 14:49:14 +0100975 invoke = new (allocator_) HInvokeStaticOrDirect(allocator_,
976 number_of_arguments,
977 return_type,
978 dex_pc,
979 method_idx,
980 resolved_method,
981 dispatch_info,
982 invoke_type,
983 target_method,
984 clinit_check_requirement);
David Brazdildee58d62016-04-07 09:54:26 +0000985 } else if (invoke_type == kVirtual) {
986 ScopedObjectAccess soa(Thread::Current()); // Needed for the method index
Vladimir Markoca6fff82017-10-03 14:49:14 +0100987 invoke = new (allocator_) HInvokeVirtual(allocator_,
988 number_of_arguments,
989 return_type,
990 dex_pc,
991 method_idx,
992 resolved_method,
993 resolved_method->GetMethodIndex());
David Brazdildee58d62016-04-07 09:54:26 +0000994 } else {
995 DCHECK_EQ(invoke_type, kInterface);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100996 ScopedObjectAccess soa(Thread::Current()); // Needed for the IMT index.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100997 invoke = new (allocator_) HInvokeInterface(allocator_,
998 number_of_arguments,
999 return_type,
1000 dex_pc,
1001 method_idx,
1002 resolved_method,
1003 ImTable::GetImtIndex(resolved_method));
David Brazdildee58d62016-04-07 09:54:26 +00001004 }
1005
1006 return HandleInvoke(invoke,
1007 number_of_vreg_arguments,
1008 args,
1009 register_index,
1010 is_range,
1011 descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -07001012 clinit_check,
1013 false /* is_unresolved */);
David Brazdildee58d62016-04-07 09:54:26 +00001014}
1015
Orion Hodsonac141392017-01-13 11:53:47 +00001016bool HInstructionBuilder::BuildInvokePolymorphic(const Instruction& instruction ATTRIBUTE_UNUSED,
1017 uint32_t dex_pc,
1018 uint32_t method_idx,
1019 uint32_t proto_idx,
1020 uint32_t number_of_vreg_arguments,
1021 bool is_range,
1022 uint32_t* args,
1023 uint32_t register_index) {
1024 const char* descriptor = dex_file_->GetShorty(proto_idx);
1025 DCHECK_EQ(1 + ArtMethod::NumArgRegisters(descriptor), number_of_vreg_arguments);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001026 DataType::Type return_type = DataType::FromShorty(descriptor[0]);
Orion Hodsonac141392017-01-13 11:53:47 +00001027 size_t number_of_arguments = strlen(descriptor);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001028 HInvoke* invoke = new (allocator_) HInvokePolymorphic(allocator_,
1029 number_of_arguments,
1030 return_type,
1031 dex_pc,
1032 method_idx);
Orion Hodsonac141392017-01-13 11:53:47 +00001033 return HandleInvoke(invoke,
1034 number_of_vreg_arguments,
1035 args,
1036 register_index,
1037 is_range,
1038 descriptor,
1039 nullptr /* clinit_check */,
1040 false /* is_unresolved */);
1041}
1042
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001043HNewInstance* HInstructionBuilder::BuildNewInstance(dex::TypeIndex type_index, uint32_t dex_pc) {
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001044 ScopedObjectAccess soa(Thread::Current());
David Brazdildee58d62016-04-07 09:54:26 +00001045
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001046 HLoadClass* load_class = BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001047
David Brazdildee58d62016-04-07 09:54:26 +00001048 HInstruction* cls = load_class;
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001049 Handle<mirror::Class> klass = load_class->GetClass();
1050
1051 if (!IsInitialized(klass)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001052 cls = new (allocator_) HClinitCheck(load_class, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001053 AppendInstruction(cls);
1054 }
1055
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001056 // Only the access check entrypoint handles the finalizable class case. If we
1057 // need access checks, then we haven't resolved the method and the class may
1058 // again be finalizable.
1059 QuickEntrypointEnum entrypoint = kQuickAllocObjectInitialized;
1060 if (load_class->NeedsAccessCheck() || klass->IsFinalizable() || !klass->IsInstantiable()) {
1061 entrypoint = kQuickAllocObjectWithChecks;
1062 }
1063
1064 // Consider classes we haven't resolved as potentially finalizable.
Andreas Gampefa4333d2017-02-14 11:10:34 -08001065 bool finalizable = (klass == nullptr) || klass->IsFinalizable();
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001066
Vladimir Markoca6fff82017-10-03 14:49:14 +01001067 HNewInstance* new_instance = new (allocator_) HNewInstance(
David Brazdildee58d62016-04-07 09:54:26 +00001068 cls,
David Brazdildee58d62016-04-07 09:54:26 +00001069 dex_pc,
1070 type_index,
1071 *dex_compilation_unit_->GetDexFile(),
David Brazdildee58d62016-04-07 09:54:26 +00001072 finalizable,
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001073 entrypoint);
1074 AppendInstruction(new_instance);
1075
1076 return new_instance;
1077}
1078
1079void HInstructionBuilder::BuildConstructorFenceForAllocation(HInstruction* allocation) {
1080 DCHECK(allocation != nullptr &&
George Burgess IVf2072992017-05-23 15:36:41 -07001081 (allocation->IsNewInstance() ||
1082 allocation->IsNewArray())); // corresponding to "new" keyword in JLS.
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001083
1084 if (allocation->IsNewInstance()) {
1085 // STRING SPECIAL HANDLING:
1086 // -------------------------------
1087 // Strings have a real HNewInstance node but they end up always having 0 uses.
1088 // All uses of a String HNewInstance are always transformed to replace their input
1089 // of the HNewInstance with an input of the invoke to StringFactory.
1090 //
1091 // Do not emit an HConstructorFence here since it can inhibit some String new-instance
1092 // optimizations (to pass checker tests that rely on those optimizations).
1093 HNewInstance* new_inst = allocation->AsNewInstance();
1094 HLoadClass* load_class = new_inst->GetLoadClass();
1095
1096 Thread* self = Thread::Current();
1097 ScopedObjectAccess soa(self);
1098 StackHandleScope<1> hs(self);
1099 Handle<mirror::Class> klass = load_class->GetClass();
1100 if (klass != nullptr && klass->IsStringClass()) {
1101 return;
1102 // Note: Do not use allocation->IsStringAlloc which requires
1103 // a valid ReferenceTypeInfo, but that doesn't get made until after reference type
1104 // propagation (and instruction builder is too early).
1105 }
1106 // (In terms of correctness, the StringFactory needs to provide its own
1107 // default initialization barrier, see below.)
1108 }
1109
1110 // JLS 17.4.5 "Happens-before Order" describes:
1111 //
1112 // The default initialization of any object happens-before any other actions (other than
1113 // default-writes) of a program.
1114 //
1115 // In our implementation the default initialization of an object to type T means
1116 // setting all of its initial data (object[0..size)) to 0, and setting the
1117 // object's class header (i.e. object.getClass() == T.class).
1118 //
1119 // In practice this fence ensures that the writes to the object header
1120 // are visible to other threads if this object escapes the current thread.
1121 // (and in theory the 0-initializing, but that happens automatically
1122 // when new memory pages are mapped in by the OS).
1123 HConstructorFence* ctor_fence =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001124 new (allocator_) HConstructorFence(allocation, allocation->GetDexPc(), allocator_);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001125 AppendInstruction(ctor_fence);
Igor Murashkin6ef45672017-08-08 13:59:55 -07001126 MaybeRecordStat(
1127 compilation_stats_,
1128 MethodCompilationStat::kConstructorFenceGeneratedNew);
David Brazdildee58d62016-04-07 09:54:26 +00001129}
1130
Vladimir Marko28e012a2017-12-07 11:22:59 +00001131static bool IsSubClass(ObjPtr<mirror::Class> to_test, ObjPtr<mirror::Class> super_class)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001132 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdildee58d62016-04-07 09:54:26 +00001133 return to_test != nullptr && !to_test->IsInterface() && to_test->IsSubClass(super_class);
1134}
1135
1136bool HInstructionBuilder::IsInitialized(Handle<mirror::Class> cls) const {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001137 if (cls == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +00001138 return false;
1139 }
1140
1141 // `CanAssumeClassIsLoaded` will return true if we're JITting, or will
1142 // check whether the class is in an image for the AOT compilation.
1143 if (cls->IsInitialized() &&
1144 compiler_driver_->CanAssumeClassIsLoaded(cls.Get())) {
1145 return true;
1146 }
1147
1148 if (IsSubClass(GetOutermostCompilingClass(), cls.Get())) {
1149 return true;
1150 }
1151
1152 // TODO: We should walk over the inlined methods, but we don't pass
1153 // that information to the builder.
1154 if (IsSubClass(GetCompilingClass(), cls.Get())) {
1155 return true;
1156 }
1157
1158 return false;
1159}
1160
1161HClinitCheck* HInstructionBuilder::ProcessClinitCheckForInvoke(
1162 uint32_t dex_pc,
1163 ArtMethod* resolved_method,
David Brazdildee58d62016-04-07 09:54:26 +00001164 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001165 Handle<mirror::Class> klass = handles_->NewHandle(resolved_method->GetDeclaringClass());
David Brazdildee58d62016-04-07 09:54:26 +00001166
1167 HClinitCheck* clinit_check = nullptr;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001168 if (IsInitialized(klass)) {
David Brazdildee58d62016-04-07 09:54:26 +00001169 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001170 } else {
1171 HLoadClass* cls = BuildLoadClass(klass->GetDexTypeIndex(),
1172 klass->GetDexFile(),
1173 klass,
1174 dex_pc,
1175 /* needs_access_check */ false);
1176 if (cls != nullptr) {
1177 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
Vladimir Markoca6fff82017-10-03 14:49:14 +01001178 clinit_check = new (allocator_) HClinitCheck(cls, dex_pc);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001179 AppendInstruction(clinit_check);
1180 }
David Brazdildee58d62016-04-07 09:54:26 +00001181 }
1182 return clinit_check;
1183}
1184
1185bool HInstructionBuilder::SetupInvokeArguments(HInvoke* invoke,
1186 uint32_t number_of_vreg_arguments,
1187 uint32_t* args,
1188 uint32_t register_index,
1189 bool is_range,
1190 const char* descriptor,
1191 size_t start_index,
1192 size_t* argument_index) {
1193 uint32_t descriptor_index = 1; // Skip the return type.
1194
1195 for (size_t i = start_index;
1196 // Make sure we don't go over the expected arguments or over the number of
1197 // dex registers given. If the instruction was seen as dead by the verifier,
1198 // it hasn't been properly checked.
1199 (i < number_of_vreg_arguments) && (*argument_index < invoke->GetNumberOfArguments());
1200 i++, (*argument_index)++) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001201 DataType::Type type = DataType::FromShorty(descriptor[descriptor_index++]);
1202 bool is_wide = (type == DataType::Type::kInt64) || (type == DataType::Type::kFloat64);
David Brazdildee58d62016-04-07 09:54:26 +00001203 if (!is_range
1204 && is_wide
1205 && ((i + 1 == number_of_vreg_arguments) || (args[i] + 1 != args[i + 1]))) {
1206 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
1207 // reject any class where this is violated. However, the verifier only does these checks
1208 // on non trivially dead instructions, so we just bailout the compilation.
1209 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001210 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001211 << " because of non-sequential dex register pair in wide argument";
Igor Murashkin1e065a52017-08-09 13:20:34 -07001212 MaybeRecordStat(compilation_stats_,
1213 MethodCompilationStat::kNotCompiledMalformedOpcode);
David Brazdildee58d62016-04-07 09:54:26 +00001214 return false;
1215 }
1216 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
1217 invoke->SetArgumentAt(*argument_index, arg);
1218 if (is_wide) {
1219 i++;
1220 }
1221 }
1222
1223 if (*argument_index != invoke->GetNumberOfArguments()) {
1224 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001225 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001226 << " because of wrong number of arguments in invoke instruction";
Igor Murashkin1e065a52017-08-09 13:20:34 -07001227 MaybeRecordStat(compilation_stats_,
1228 MethodCompilationStat::kNotCompiledMalformedOpcode);
David Brazdildee58d62016-04-07 09:54:26 +00001229 return false;
1230 }
1231
1232 if (invoke->IsInvokeStaticOrDirect() &&
1233 HInvokeStaticOrDirect::NeedsCurrentMethodInput(
1234 invoke->AsInvokeStaticOrDirect()->GetMethodLoadKind())) {
1235 invoke->SetArgumentAt(*argument_index, graph_->GetCurrentMethod());
1236 (*argument_index)++;
1237 }
1238
1239 return true;
1240}
1241
1242bool HInstructionBuilder::HandleInvoke(HInvoke* invoke,
1243 uint32_t number_of_vreg_arguments,
1244 uint32_t* args,
1245 uint32_t register_index,
1246 bool is_range,
1247 const char* descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -07001248 HClinitCheck* clinit_check,
1249 bool is_unresolved) {
David Brazdildee58d62016-04-07 09:54:26 +00001250 DCHECK(!invoke->IsInvokeStaticOrDirect() || !invoke->AsInvokeStaticOrDirect()->IsStringInit());
1251
1252 size_t start_index = 0;
1253 size_t argument_index = 0;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001254 if (invoke->GetInvokeType() != InvokeType::kStatic) { // Instance call.
Aart Bik296fbb42016-06-07 13:49:12 -07001255 uint32_t obj_reg = is_range ? register_index : args[0];
1256 HInstruction* arg = is_unresolved
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001257 ? LoadLocal(obj_reg, DataType::Type::kReference)
Aart Bik296fbb42016-06-07 13:49:12 -07001258 : LoadNullCheckedLocal(obj_reg, invoke->GetDexPc());
David Brazdilc120bbe2016-04-22 16:57:00 +01001259 invoke->SetArgumentAt(0, arg);
David Brazdildee58d62016-04-07 09:54:26 +00001260 start_index = 1;
1261 argument_index = 1;
1262 }
1263
1264 if (!SetupInvokeArguments(invoke,
1265 number_of_vreg_arguments,
1266 args,
1267 register_index,
1268 is_range,
1269 descriptor,
1270 start_index,
1271 &argument_index)) {
1272 return false;
1273 }
1274
1275 if (clinit_check != nullptr) {
1276 // Add the class initialization check as last input of `invoke`.
1277 DCHECK(invoke->IsInvokeStaticOrDirect());
1278 DCHECK(invoke->AsInvokeStaticOrDirect()->GetClinitCheckRequirement()
1279 == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit);
1280 invoke->SetArgumentAt(argument_index, clinit_check);
1281 argument_index++;
1282 }
1283
1284 AppendInstruction(invoke);
1285 latest_result_ = invoke;
1286
1287 return true;
1288}
1289
1290bool HInstructionBuilder::HandleStringInit(HInvoke* invoke,
1291 uint32_t number_of_vreg_arguments,
1292 uint32_t* args,
1293 uint32_t register_index,
1294 bool is_range,
1295 const char* descriptor) {
1296 DCHECK(invoke->IsInvokeStaticOrDirect());
1297 DCHECK(invoke->AsInvokeStaticOrDirect()->IsStringInit());
1298
1299 size_t start_index = 1;
1300 size_t argument_index = 0;
1301 if (!SetupInvokeArguments(invoke,
1302 number_of_vreg_arguments,
1303 args,
1304 register_index,
1305 is_range,
1306 descriptor,
1307 start_index,
1308 &argument_index)) {
1309 return false;
1310 }
1311
1312 AppendInstruction(invoke);
1313
1314 // This is a StringFactory call, not an actual String constructor. Its result
1315 // replaces the empty String pre-allocated by NewInstance.
1316 uint32_t orig_this_reg = is_range ? register_index : args[0];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001317 HInstruction* arg_this = LoadLocal(orig_this_reg, DataType::Type::kReference);
David Brazdildee58d62016-04-07 09:54:26 +00001318
1319 // Replacing the NewInstance might render it redundant. Keep a list of these
1320 // to be visited once it is clear whether it is has remaining uses.
1321 if (arg_this->IsNewInstance()) {
1322 ssa_builder_->AddUninitializedString(arg_this->AsNewInstance());
1323 } else {
1324 DCHECK(arg_this->IsPhi());
1325 // NewInstance is not the direct input of the StringFactory call. It might
1326 // be redundant but optimizing this case is not worth the effort.
1327 }
1328
1329 // Walk over all vregs and replace any occurrence of `arg_this` with `invoke`.
1330 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
1331 if ((*current_locals_)[vreg] == arg_this) {
1332 (*current_locals_)[vreg] = invoke;
1333 }
1334 }
1335
1336 return true;
1337}
1338
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001339static DataType::Type GetFieldAccessType(const DexFile& dex_file, uint16_t field_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001340 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
1341 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001342 return DataType::FromShorty(type[0]);
David Brazdildee58d62016-04-07 09:54:26 +00001343}
1344
1345bool HInstructionBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
1346 uint32_t dex_pc,
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001347 bool is_put,
1348 size_t quicken_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001349 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1350 uint32_t obj_reg = instruction.VRegB_22c();
1351 uint16_t field_index;
1352 if (instruction.IsQuickened()) {
1353 if (!CanDecodeQuickenedInfo()) {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00001354 VLOG(compiler) << "Not compiled: Could not decode quickened instruction "
1355 << instruction.Opcode();
David Brazdildee58d62016-04-07 09:54:26 +00001356 return false;
1357 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001358 field_index = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00001359 } else {
1360 field_index = instruction.VRegC_22c();
1361 }
1362
1363 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001364 ArtField* resolved_field = ResolveField(field_index, /* is_static */ false, is_put);
David Brazdildee58d62016-04-07 09:54:26 +00001365
Aart Bik14154132016-06-02 17:53:58 -07001366 // Generate an explicit null check on the reference, unless the field access
1367 // is unresolved. In that case, we rely on the runtime to perform various
1368 // checks first, followed by a null check.
1369 HInstruction* object = (resolved_field == nullptr)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001370 ? LoadLocal(obj_reg, DataType::Type::kReference)
Aart Bik14154132016-06-02 17:53:58 -07001371 : LoadNullCheckedLocal(obj_reg, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001372
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001373 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001374 if (is_put) {
1375 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1376 HInstruction* field_set = nullptr;
1377 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001378 MaybeRecordStat(compilation_stats_,
1379 MethodCompilationStat::kUnresolvedField);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001380 field_set = new (allocator_) HUnresolvedInstanceFieldSet(object,
1381 value,
1382 field_type,
1383 field_index,
1384 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001385 } else {
1386 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Vladimir Markoca6fff82017-10-03 14:49:14 +01001387 field_set = new (allocator_) HInstanceFieldSet(object,
1388 value,
1389 resolved_field,
1390 field_type,
1391 resolved_field->GetOffset(),
1392 resolved_field->IsVolatile(),
1393 field_index,
1394 class_def_index,
1395 *dex_file_,
1396 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001397 }
1398 AppendInstruction(field_set);
1399 } else {
1400 HInstruction* field_get = nullptr;
1401 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001402 MaybeRecordStat(compilation_stats_,
1403 MethodCompilationStat::kUnresolvedField);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001404 field_get = new (allocator_) HUnresolvedInstanceFieldGet(object,
1405 field_type,
1406 field_index,
1407 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001408 } else {
1409 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Vladimir Markoca6fff82017-10-03 14:49:14 +01001410 field_get = new (allocator_) HInstanceFieldGet(object,
1411 resolved_field,
1412 field_type,
1413 resolved_field->GetOffset(),
1414 resolved_field->IsVolatile(),
1415 field_index,
1416 class_def_index,
1417 *dex_file_,
1418 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001419 }
1420 AppendInstruction(field_get);
1421 UpdateLocal(source_or_dest_reg, field_get);
1422 }
1423
1424 return true;
1425}
1426
Vladimir Marko28e012a2017-12-07 11:22:59 +00001427static ObjPtr<mirror::Class> GetClassFrom(CompilerDriver* driver,
David Brazdildee58d62016-04-07 09:54:26 +00001428 const DexCompilationUnit& compilation_unit) {
1429 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001430 Handle<mirror::ClassLoader> class_loader = compilation_unit.GetClassLoader();
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001431 Handle<mirror::DexCache> dex_cache = compilation_unit.GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00001432
1433 return driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, &compilation_unit);
1434}
1435
Vladimir Marko28e012a2017-12-07 11:22:59 +00001436ObjPtr<mirror::Class> HInstructionBuilder::GetOutermostCompilingClass() const {
David Brazdildee58d62016-04-07 09:54:26 +00001437 return GetClassFrom(compiler_driver_, *outer_compilation_unit_);
1438}
1439
Vladimir Marko28e012a2017-12-07 11:22:59 +00001440ObjPtr<mirror::Class> HInstructionBuilder::GetCompilingClass() const {
David Brazdildee58d62016-04-07 09:54:26 +00001441 return GetClassFrom(compiler_driver_, *dex_compilation_unit_);
1442}
1443
Andreas Gampea5b09a62016-11-17 15:21:22 -08001444bool HInstructionBuilder::IsOutermostCompilingClass(dex::TypeIndex type_index) const {
David Brazdildee58d62016-04-07 09:54:26 +00001445 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001446 StackHandleScope<2> hs(soa.Self());
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001447 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001448 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
David Brazdildee58d62016-04-07 09:54:26 +00001449 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
1450 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
1451 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
1452
1453 // GetOutermostCompilingClass returns null when the class is unresolved
1454 // (e.g. if it derives from an unresolved class). This is bogus knowing that
1455 // we are compiling it.
1456 // When this happens we cannot establish a direct relation between the current
1457 // class and the outer class, so we return false.
1458 // (Note that this is only used for optimizing invokes and field accesses)
Andreas Gampefa4333d2017-02-14 11:10:34 -08001459 return (cls != nullptr) && (outer_class.Get() == cls.Get());
David Brazdildee58d62016-04-07 09:54:26 +00001460}
1461
1462void HInstructionBuilder::BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001463 uint32_t dex_pc,
1464 bool is_put,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001465 DataType::Type field_type) {
David Brazdildee58d62016-04-07 09:54:26 +00001466 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1467 uint16_t field_index = instruction.VRegB_21c();
1468
1469 if (is_put) {
1470 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1471 AppendInstruction(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001472 new (allocator_) HUnresolvedStaticFieldSet(value, field_type, field_index, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001473 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001474 AppendInstruction(new (allocator_) HUnresolvedStaticFieldGet(field_type, field_index, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001475 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1476 }
1477}
1478
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001479ArtField* HInstructionBuilder::ResolveField(uint16_t field_idx, bool is_static, bool is_put) {
1480 ScopedObjectAccess soa(Thread::Current());
1481 StackHandleScope<2> hs(soa.Self());
1482
1483 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001484 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001485 Handle<mirror::Class> compiling_class(hs.NewHandle(GetCompilingClass()));
1486
Vladimir Markoe11dd502017-12-08 14:09:45 +00001487 ArtField* resolved_field = class_linker->ResolveField(field_idx,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001488 dex_compilation_unit_->GetDexCache(),
1489 class_loader,
1490 is_static);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001491 if (UNLIKELY(resolved_field == nullptr)) {
1492 // Clean up any exception left by type resolution.
1493 soa.Self()->ClearException();
1494 return nullptr;
1495 }
1496
1497 // Check static/instance. The class linker has a fast path for looking into the dex cache
1498 // and does not check static/instance if it hits it.
1499 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
1500 return nullptr;
1501 }
1502
1503 // Check access.
Andreas Gampefa4333d2017-02-14 11:10:34 -08001504 if (compiling_class == nullptr) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001505 if (!resolved_field->IsPublic()) {
1506 return nullptr;
1507 }
1508 } else if (!compiling_class->CanAccessResolvedField(resolved_field->GetDeclaringClass(),
1509 resolved_field,
1510 dex_compilation_unit_->GetDexCache().Get(),
1511 field_idx)) {
1512 return nullptr;
1513 }
1514
1515 if (is_put &&
1516 resolved_field->IsFinal() &&
1517 (compiling_class.Get() != resolved_field->GetDeclaringClass())) {
1518 // Final fields can only be updated within their own class.
1519 // TODO: Only allow it in constructors. b/34966607.
1520 return nullptr;
1521 }
1522
1523 return resolved_field;
1524}
1525
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00001526void HInstructionBuilder::BuildStaticFieldAccess(const Instruction& instruction,
David Brazdildee58d62016-04-07 09:54:26 +00001527 uint32_t dex_pc,
1528 bool is_put) {
1529 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1530 uint16_t field_index = instruction.VRegB_21c();
1531
1532 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001533 ArtField* resolved_field = ResolveField(field_index, /* is_static */ true, is_put);
David Brazdildee58d62016-04-07 09:54:26 +00001534
1535 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001536 MaybeRecordStat(compilation_stats_,
1537 MethodCompilationStat::kUnresolvedField);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001538 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001539 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00001540 return;
David Brazdildee58d62016-04-07 09:54:26 +00001541 }
1542
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001543 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001544
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001545 Handle<mirror::Class> klass = handles_->NewHandle(resolved_field->GetDeclaringClass());
1546 HLoadClass* constant = BuildLoadClass(klass->GetDexTypeIndex(),
1547 klass->GetDexFile(),
1548 klass,
1549 dex_pc,
1550 /* needs_access_check */ false);
1551
1552 if (constant == nullptr) {
1553 // The class cannot be referenced from this compiled code. Generate
1554 // an unresolved access.
Igor Murashkin1e065a52017-08-09 13:20:34 -07001555 MaybeRecordStat(compilation_stats_,
1556 MethodCompilationStat::kUnresolvedFieldNotAFastAccess);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001557 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00001558 return;
David Brazdildee58d62016-04-07 09:54:26 +00001559 }
1560
David Brazdildee58d62016-04-07 09:54:26 +00001561 HInstruction* cls = constant;
David Brazdildee58d62016-04-07 09:54:26 +00001562 if (!IsInitialized(klass)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001563 cls = new (allocator_) HClinitCheck(constant, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001564 AppendInstruction(cls);
1565 }
1566
1567 uint16_t class_def_index = klass->GetDexClassDefIndex();
1568 if (is_put) {
1569 // We need to keep the class alive before loading the value.
1570 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1571 DCHECK_EQ(HPhi::ToPhiType(value->GetType()), HPhi::ToPhiType(field_type));
Vladimir Markoca6fff82017-10-03 14:49:14 +01001572 AppendInstruction(new (allocator_) HStaticFieldSet(cls,
1573 value,
1574 resolved_field,
1575 field_type,
1576 resolved_field->GetOffset(),
1577 resolved_field->IsVolatile(),
1578 field_index,
1579 class_def_index,
1580 *dex_file_,
1581 dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001582 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001583 AppendInstruction(new (allocator_) HStaticFieldGet(cls,
1584 resolved_field,
1585 field_type,
1586 resolved_field->GetOffset(),
1587 resolved_field->IsVolatile(),
1588 field_index,
1589 class_def_index,
1590 *dex_file_,
1591 dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001592 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1593 }
David Brazdildee58d62016-04-07 09:54:26 +00001594}
1595
1596void HInstructionBuilder::BuildCheckedDivRem(uint16_t out_vreg,
Vladimir Markoca6fff82017-10-03 14:49:14 +01001597 uint16_t first_vreg,
1598 int64_t second_vreg_or_constant,
1599 uint32_t dex_pc,
1600 DataType::Type type,
1601 bool second_is_constant,
1602 bool isDiv) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001603 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
David Brazdildee58d62016-04-07 09:54:26 +00001604
1605 HInstruction* first = LoadLocal(first_vreg, type);
1606 HInstruction* second = nullptr;
1607 if (second_is_constant) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001608 if (type == DataType::Type::kInt32) {
David Brazdildee58d62016-04-07 09:54:26 +00001609 second = graph_->GetIntConstant(second_vreg_or_constant, dex_pc);
1610 } else {
1611 second = graph_->GetLongConstant(second_vreg_or_constant, dex_pc);
1612 }
1613 } else {
1614 second = LoadLocal(second_vreg_or_constant, type);
1615 }
1616
1617 if (!second_is_constant
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001618 || (type == DataType::Type::kInt32 && second->AsIntConstant()->GetValue() == 0)
1619 || (type == DataType::Type::kInt64 && second->AsLongConstant()->GetValue() == 0)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001620 second = new (allocator_) HDivZeroCheck(second, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001621 AppendInstruction(second);
1622 }
1623
1624 if (isDiv) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001625 AppendInstruction(new (allocator_) HDiv(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001626 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001627 AppendInstruction(new (allocator_) HRem(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001628 }
1629 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
1630}
1631
1632void HInstructionBuilder::BuildArrayAccess(const Instruction& instruction,
1633 uint32_t dex_pc,
1634 bool is_put,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001635 DataType::Type anticipated_type) {
David Brazdildee58d62016-04-07 09:54:26 +00001636 uint8_t source_or_dest_reg = instruction.VRegA_23x();
1637 uint8_t array_reg = instruction.VRegB_23x();
1638 uint8_t index_reg = instruction.VRegC_23x();
1639
David Brazdilc120bbe2016-04-22 16:57:00 +01001640 HInstruction* object = LoadNullCheckedLocal(array_reg, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001641 HInstruction* length = new (allocator_) HArrayLength(object, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001642 AppendInstruction(length);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001643 HInstruction* index = LoadLocal(index_reg, DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001644 index = new (allocator_) HBoundsCheck(index, length, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001645 AppendInstruction(index);
1646 if (is_put) {
1647 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
1648 // TODO: Insert a type check node if the type is Object.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001649 HArraySet* aset = new (allocator_) HArraySet(object, index, value, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001650 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1651 AppendInstruction(aset);
1652 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001653 HArrayGet* aget = new (allocator_) HArrayGet(object, index, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001654 ssa_builder_->MaybeAddAmbiguousArrayGet(aget);
1655 AppendInstruction(aget);
1656 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1657 }
1658 graph_->SetHasBoundsChecks(true);
1659}
1660
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001661HNewArray* HInstructionBuilder::BuildFilledNewArray(uint32_t dex_pc,
1662 dex::TypeIndex type_index,
1663 uint32_t number_of_vreg_arguments,
1664 bool is_range,
1665 uint32_t* args,
1666 uint32_t register_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001667 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments, dex_pc);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001668 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001669 HNewArray* const object = new (allocator_) HNewArray(cls, length, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001670 AppendInstruction(object);
1671
1672 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1673 DCHECK_EQ(descriptor[0], '[') << descriptor;
1674 char primitive = descriptor[1];
1675 DCHECK(primitive == 'I'
1676 || primitive == 'L'
1677 || primitive == '[') << descriptor;
1678 bool is_reference_array = (primitive == 'L') || (primitive == '[');
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001679 DataType::Type type = is_reference_array ? DataType::Type::kReference : DataType::Type::kInt32;
David Brazdildee58d62016-04-07 09:54:26 +00001680
1681 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
1682 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
1683 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001684 HArraySet* aset = new (allocator_) HArraySet(object, index, value, type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001685 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1686 AppendInstruction(aset);
1687 }
1688 latest_result_ = object;
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001689
1690 return object;
David Brazdildee58d62016-04-07 09:54:26 +00001691}
1692
1693template <typename T>
1694void HInstructionBuilder::BuildFillArrayData(HInstruction* object,
1695 const T* data,
1696 uint32_t element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001697 DataType::Type anticipated_type,
David Brazdildee58d62016-04-07 09:54:26 +00001698 uint32_t dex_pc) {
1699 for (uint32_t i = 0; i < element_count; ++i) {
1700 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1701 HInstruction* value = graph_->GetIntConstant(data[i], dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001702 HArraySet* aset = new (allocator_) HArraySet(object, index, value, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001703 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1704 AppendInstruction(aset);
1705 }
1706}
1707
1708void HInstructionBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
David Brazdilc120bbe2016-04-22 16:57:00 +01001709 HInstruction* array = LoadNullCheckedLocal(instruction.VRegA_31t(), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001710
1711 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
1712 const Instruction::ArrayDataPayload* payload =
Vladimir Marko92f7f3c2017-10-31 11:38:30 +00001713 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_item_->insns_ + payload_offset);
David Brazdildee58d62016-04-07 09:54:26 +00001714 const uint8_t* data = payload->data;
1715 uint32_t element_count = payload->element_count;
1716
Vladimir Markoc69fba22016-09-06 16:49:15 +01001717 if (element_count == 0u) {
1718 // For empty payload we emit only the null check above.
1719 return;
1720 }
1721
Vladimir Markoca6fff82017-10-03 14:49:14 +01001722 HInstruction* length = new (allocator_) HArrayLength(array, dex_pc);
Vladimir Markoc69fba22016-09-06 16:49:15 +01001723 AppendInstruction(length);
1724
David Brazdildee58d62016-04-07 09:54:26 +00001725 // Implementation of this DEX instruction seems to be that the bounds check is
1726 // done before doing any stores.
1727 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001728 AppendInstruction(new (allocator_) HBoundsCheck(last_index, length, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001729
1730 switch (payload->element_width) {
1731 case 1:
David Brazdilc120bbe2016-04-22 16:57:00 +01001732 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001733 reinterpret_cast<const int8_t*>(data),
1734 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001735 DataType::Type::kInt8,
David Brazdildee58d62016-04-07 09:54:26 +00001736 dex_pc);
1737 break;
1738 case 2:
David Brazdilc120bbe2016-04-22 16:57:00 +01001739 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001740 reinterpret_cast<const int16_t*>(data),
1741 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001742 DataType::Type::kInt16,
David Brazdildee58d62016-04-07 09:54:26 +00001743 dex_pc);
1744 break;
1745 case 4:
David Brazdilc120bbe2016-04-22 16:57:00 +01001746 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001747 reinterpret_cast<const int32_t*>(data),
1748 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001749 DataType::Type::kInt32,
David Brazdildee58d62016-04-07 09:54:26 +00001750 dex_pc);
1751 break;
1752 case 8:
David Brazdilc120bbe2016-04-22 16:57:00 +01001753 BuildFillWideArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001754 reinterpret_cast<const int64_t*>(data),
1755 element_count,
1756 dex_pc);
1757 break;
1758 default:
1759 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1760 }
1761 graph_->SetHasBoundsChecks(true);
1762}
1763
1764void HInstructionBuilder::BuildFillWideArrayData(HInstruction* object,
1765 const int64_t* data,
1766 uint32_t element_count,
1767 uint32_t dex_pc) {
1768 for (uint32_t i = 0; i < element_count; ++i) {
1769 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1770 HInstruction* value = graph_->GetLongConstant(data[i], dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001771 HArraySet* aset =
1772 new (allocator_) HArraySet(object, index, value, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001773 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1774 AppendInstruction(aset);
1775 }
1776}
1777
1778static TypeCheckKind ComputeTypeCheckKind(Handle<mirror::Class> cls)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001779 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001780 if (cls == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +00001781 return TypeCheckKind::kUnresolvedCheck;
1782 } else if (cls->IsInterface()) {
1783 return TypeCheckKind::kInterfaceCheck;
1784 } else if (cls->IsArrayClass()) {
1785 if (cls->GetComponentType()->IsObjectClass()) {
1786 return TypeCheckKind::kArrayObjectCheck;
1787 } else if (cls->CannotBeAssignedFromOtherTypes()) {
1788 return TypeCheckKind::kExactCheck;
1789 } else {
1790 return TypeCheckKind::kArrayCheck;
1791 }
1792 } else if (cls->IsFinal()) {
1793 return TypeCheckKind::kExactCheck;
1794 } else if (cls->IsAbstract()) {
1795 return TypeCheckKind::kAbstractClassCheck;
1796 } else {
1797 return TypeCheckKind::kClassHierarchyCheck;
1798 }
1799}
1800
Vladimir Marko28e012a2017-12-07 11:22:59 +00001801void HInstructionBuilder::BuildLoadString(dex::StringIndex string_index, uint32_t dex_pc) {
1802 HLoadString* load_string =
1803 new (allocator_) HLoadString(graph_->GetCurrentMethod(), string_index, *dex_file_, dex_pc);
1804 HSharpening::ProcessLoadString(load_string,
1805 code_generator_,
1806 compiler_driver_,
1807 *dex_compilation_unit_,
1808 handles_);
1809 AppendInstruction(load_string);
1810}
1811
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001812HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index, uint32_t dex_pc) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001813 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001814 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001815 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00001816 Handle<mirror::Class> klass = handles_->NewHandle(compiler_driver_->ResolveClass(
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001817 soa, dex_compilation_unit_->GetDexCache(), class_loader, type_index, dex_compilation_unit_));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00001818
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001819 bool needs_access_check = true;
Andreas Gampefa4333d2017-02-14 11:10:34 -08001820 if (klass != nullptr) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001821 if (klass->IsPublic()) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001822 needs_access_check = false;
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001823 } else {
Vladimir Marko28e012a2017-12-07 11:22:59 +00001824 ObjPtr<mirror::Class> compiling_class = GetCompilingClass();
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001825 if (compiling_class != nullptr && compiling_class->CanAccess(klass.Get())) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001826 needs_access_check = false;
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001827 }
1828 }
1829 }
1830
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001831 return BuildLoadClass(type_index, dex_file, klass, dex_pc, needs_access_check);
1832}
1833
1834HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index,
1835 const DexFile& dex_file,
1836 Handle<mirror::Class> klass,
1837 uint32_t dex_pc,
1838 bool needs_access_check) {
1839 // Try to find a reference in the compiling dex file.
1840 const DexFile* actual_dex_file = &dex_file;
1841 if (!IsSameDexFile(dex_file, *dex_compilation_unit_->GetDexFile())) {
1842 dex::TypeIndex local_type_index =
1843 klass->FindTypeIndexInOtherDexFile(*dex_compilation_unit_->GetDexFile());
1844 if (local_type_index.IsValid()) {
1845 type_index = local_type_index;
1846 actual_dex_file = dex_compilation_unit_->GetDexFile();
1847 }
1848 }
1849
1850 // Note: `klass` must be from `handles_`.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001851 HLoadClass* load_class = new (allocator_) HLoadClass(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001852 graph_->GetCurrentMethod(),
1853 type_index,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001854 *actual_dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001855 klass,
Andreas Gampefa4333d2017-02-14 11:10:34 -08001856 klass != nullptr && (klass.Get() == GetOutermostCompilingClass()),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001857 dex_pc,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001858 needs_access_check);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001859
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00001860 HLoadClass::LoadKind load_kind = HSharpening::ComputeLoadClassKind(load_class,
1861 code_generator_,
1862 compiler_driver_,
1863 *dex_compilation_unit_);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001864
1865 if (load_kind == HLoadClass::LoadKind::kInvalid) {
1866 // We actually cannot reference this class, we're forced to bail.
1867 return nullptr;
1868 }
Vladimir Marko28e012a2017-12-07 11:22:59 +00001869 // Load kind must be set before inserting the instruction into the graph.
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001870 load_class->SetLoadKind(load_kind);
Vladimir Marko28e012a2017-12-07 11:22:59 +00001871 AppendInstruction(load_class);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001872 return load_class;
1873}
1874
David Brazdildee58d62016-04-07 09:54:26 +00001875void HInstructionBuilder::BuildTypeCheck(const Instruction& instruction,
1876 uint8_t destination,
1877 uint8_t reference,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001878 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +00001879 uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001880 HInstruction* object = LoadLocal(reference, DataType::Type::kReference);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001881 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001882
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001883 ScopedObjectAccess soa(Thread::Current());
1884 TypeCheckKind check_kind = ComputeTypeCheckKind(cls->GetClass());
David Brazdildee58d62016-04-07 09:54:26 +00001885 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001886 AppendInstruction(new (allocator_) HInstanceOf(object, cls, check_kind, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001887 UpdateLocal(destination, current_block_->GetLastInstruction());
1888 } else {
1889 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
1890 // We emit a CheckCast followed by a BoundType. CheckCast is a statement
1891 // which may throw. If it succeeds BoundType sets the new type of `object`
1892 // for all subsequent uses.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001893 AppendInstruction(new (allocator_) HCheckCast(object, cls, check_kind, dex_pc));
1894 AppendInstruction(new (allocator_) HBoundType(object, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001895 UpdateLocal(reference, current_block_->GetLastInstruction());
1896 }
1897}
1898
Vladimir Marko0b66d612017-03-13 14:50:04 +00001899bool HInstructionBuilder::NeedsAccessCheck(dex::TypeIndex type_index, bool* finalizable) const {
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001900 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
1901 LookupReferrerClass(), LookupResolvedType(type_index, *dex_compilation_unit_), finalizable);
David Brazdildee58d62016-04-07 09:54:26 +00001902}
1903
1904bool HInstructionBuilder::CanDecodeQuickenedInfo() const {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001905 return !quicken_info_.IsNull();
David Brazdildee58d62016-04-07 09:54:26 +00001906}
1907
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001908uint16_t HInstructionBuilder::LookupQuickenedInfo(uint32_t quicken_index) {
1909 DCHECK(CanDecodeQuickenedInfo());
1910 return quicken_info_.GetData(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00001911}
1912
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001913bool HInstructionBuilder::ProcessDexInstruction(const Instruction& instruction,
1914 uint32_t dex_pc,
1915 size_t quicken_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001916 switch (instruction.Opcode()) {
1917 case Instruction::CONST_4: {
1918 int32_t register_index = instruction.VRegA();
1919 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n(), dex_pc);
1920 UpdateLocal(register_index, constant);
1921 break;
1922 }
1923
1924 case Instruction::CONST_16: {
1925 int32_t register_index = instruction.VRegA();
1926 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s(), dex_pc);
1927 UpdateLocal(register_index, constant);
1928 break;
1929 }
1930
1931 case Instruction::CONST: {
1932 int32_t register_index = instruction.VRegA();
1933 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i(), dex_pc);
1934 UpdateLocal(register_index, constant);
1935 break;
1936 }
1937
1938 case Instruction::CONST_HIGH16: {
1939 int32_t register_index = instruction.VRegA();
1940 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16, dex_pc);
1941 UpdateLocal(register_index, constant);
1942 break;
1943 }
1944
1945 case Instruction::CONST_WIDE_16: {
1946 int32_t register_index = instruction.VRegA();
1947 // Get 16 bits of constant value, sign extended to 64 bits.
1948 int64_t value = instruction.VRegB_21s();
1949 value <<= 48;
1950 value >>= 48;
1951 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1952 UpdateLocal(register_index, constant);
1953 break;
1954 }
1955
1956 case Instruction::CONST_WIDE_32: {
1957 int32_t register_index = instruction.VRegA();
1958 // Get 32 bits of constant value, sign extended to 64 bits.
1959 int64_t value = instruction.VRegB_31i();
1960 value <<= 32;
1961 value >>= 32;
1962 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1963 UpdateLocal(register_index, constant);
1964 break;
1965 }
1966
1967 case Instruction::CONST_WIDE: {
1968 int32_t register_index = instruction.VRegA();
1969 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l(), dex_pc);
1970 UpdateLocal(register_index, constant);
1971 break;
1972 }
1973
1974 case Instruction::CONST_WIDE_HIGH16: {
1975 int32_t register_index = instruction.VRegA();
1976 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
1977 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1978 UpdateLocal(register_index, constant);
1979 break;
1980 }
1981
1982 // Note that the SSA building will refine the types.
1983 case Instruction::MOVE:
1984 case Instruction::MOVE_FROM16:
1985 case Instruction::MOVE_16: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001986 HInstruction* value = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +00001987 UpdateLocal(instruction.VRegA(), value);
1988 break;
1989 }
1990
1991 // Note that the SSA building will refine the types.
1992 case Instruction::MOVE_WIDE:
1993 case Instruction::MOVE_WIDE_FROM16:
1994 case Instruction::MOVE_WIDE_16: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001995 HInstruction* value = LoadLocal(instruction.VRegB(), DataType::Type::kInt64);
David Brazdildee58d62016-04-07 09:54:26 +00001996 UpdateLocal(instruction.VRegA(), value);
1997 break;
1998 }
1999
2000 case Instruction::MOVE_OBJECT:
2001 case Instruction::MOVE_OBJECT_16:
2002 case Instruction::MOVE_OBJECT_FROM16: {
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01002003 // The verifier has no notion of a null type, so a move-object of constant 0
2004 // will lead to the same constant 0 in the destination register. To mimic
2005 // this behavior, we just pretend we haven't seen a type change (int to reference)
2006 // for the 0 constant and phis. We rely on our type propagation to eventually get the
2007 // types correct.
2008 uint32_t reg_number = instruction.VRegB();
2009 HInstruction* value = (*current_locals_)[reg_number];
2010 if (value->IsIntConstant()) {
2011 DCHECK_EQ(value->AsIntConstant()->GetValue(), 0);
2012 } else if (value->IsPhi()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002013 DCHECK(value->GetType() == DataType::Type::kInt32 ||
2014 value->GetType() == DataType::Type::kReference);
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01002015 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002016 value = LoadLocal(reg_number, DataType::Type::kReference);
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01002017 }
David Brazdildee58d62016-04-07 09:54:26 +00002018 UpdateLocal(instruction.VRegA(), value);
2019 break;
2020 }
2021
2022 case Instruction::RETURN_VOID_NO_BARRIER:
2023 case Instruction::RETURN_VOID: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002024 BuildReturn(instruction, DataType::Type::kVoid, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002025 break;
2026 }
2027
2028#define IF_XX(comparison, cond) \
2029 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
2030 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
2031
2032 IF_XX(HEqual, EQ);
2033 IF_XX(HNotEqual, NE);
2034 IF_XX(HLessThan, LT);
2035 IF_XX(HLessThanOrEqual, LE);
2036 IF_XX(HGreaterThan, GT);
2037 IF_XX(HGreaterThanOrEqual, GE);
2038
2039 case Instruction::GOTO:
2040 case Instruction::GOTO_16:
2041 case Instruction::GOTO_32: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002042 AppendInstruction(new (allocator_) HGoto(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002043 current_block_ = nullptr;
2044 break;
2045 }
2046
2047 case Instruction::RETURN: {
2048 BuildReturn(instruction, return_type_, dex_pc);
2049 break;
2050 }
2051
2052 case Instruction::RETURN_OBJECT: {
2053 BuildReturn(instruction, return_type_, dex_pc);
2054 break;
2055 }
2056
2057 case Instruction::RETURN_WIDE: {
2058 BuildReturn(instruction, return_type_, dex_pc);
2059 break;
2060 }
2061
2062 case Instruction::INVOKE_DIRECT:
2063 case Instruction::INVOKE_INTERFACE:
2064 case Instruction::INVOKE_STATIC:
2065 case Instruction::INVOKE_SUPER:
2066 case Instruction::INVOKE_VIRTUAL:
2067 case Instruction::INVOKE_VIRTUAL_QUICK: {
2068 uint16_t method_idx;
2069 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_QUICK) {
2070 if (!CanDecodeQuickenedInfo()) {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002071 VLOG(compiler) << "Not compiled: Could not decode quickened instruction "
2072 << instruction.Opcode();
David Brazdildee58d62016-04-07 09:54:26 +00002073 return false;
2074 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002075 method_idx = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00002076 } else {
2077 method_idx = instruction.VRegB_35c();
2078 }
2079 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
2080 uint32_t args[5];
2081 instruction.GetVarArgs(args);
2082 if (!BuildInvoke(instruction, dex_pc, method_idx,
2083 number_of_vreg_arguments, false, args, -1)) {
2084 return false;
2085 }
2086 break;
2087 }
2088
2089 case Instruction::INVOKE_DIRECT_RANGE:
2090 case Instruction::INVOKE_INTERFACE_RANGE:
2091 case Instruction::INVOKE_STATIC_RANGE:
2092 case Instruction::INVOKE_SUPER_RANGE:
2093 case Instruction::INVOKE_VIRTUAL_RANGE:
2094 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2095 uint16_t method_idx;
2096 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK) {
2097 if (!CanDecodeQuickenedInfo()) {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002098 VLOG(compiler) << "Not compiled: Could not decode quickened instruction "
2099 << instruction.Opcode();
David Brazdildee58d62016-04-07 09:54:26 +00002100 return false;
2101 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002102 method_idx = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00002103 } else {
2104 method_idx = instruction.VRegB_3rc();
2105 }
2106 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
2107 uint32_t register_index = instruction.VRegC();
2108 if (!BuildInvoke(instruction, dex_pc, method_idx,
2109 number_of_vreg_arguments, true, nullptr, register_index)) {
2110 return false;
2111 }
2112 break;
2113 }
2114
Orion Hodsonac141392017-01-13 11:53:47 +00002115 case Instruction::INVOKE_POLYMORPHIC: {
2116 uint16_t method_idx = instruction.VRegB_45cc();
2117 uint16_t proto_idx = instruction.VRegH_45cc();
2118 uint32_t number_of_vreg_arguments = instruction.VRegA_45cc();
2119 uint32_t args[5];
2120 instruction.GetVarArgs(args);
2121 return BuildInvokePolymorphic(instruction,
2122 dex_pc,
2123 method_idx,
2124 proto_idx,
2125 number_of_vreg_arguments,
2126 false,
2127 args,
2128 -1);
2129 }
2130
2131 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
2132 uint16_t method_idx = instruction.VRegB_4rcc();
2133 uint16_t proto_idx = instruction.VRegH_4rcc();
2134 uint32_t number_of_vreg_arguments = instruction.VRegA_4rcc();
2135 uint32_t register_index = instruction.VRegC_4rcc();
2136 return BuildInvokePolymorphic(instruction,
2137 dex_pc,
2138 method_idx,
2139 proto_idx,
2140 number_of_vreg_arguments,
2141 true,
2142 nullptr,
2143 register_index);
2144 }
2145
David Brazdildee58d62016-04-07 09:54:26 +00002146 case Instruction::NEG_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002147 Unop_12x<HNeg>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002148 break;
2149 }
2150
2151 case Instruction::NEG_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002152 Unop_12x<HNeg>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002153 break;
2154 }
2155
2156 case Instruction::NEG_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002157 Unop_12x<HNeg>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002158 break;
2159 }
2160
2161 case Instruction::NEG_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002162 Unop_12x<HNeg>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002163 break;
2164 }
2165
2166 case Instruction::NOT_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002167 Unop_12x<HNot>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002168 break;
2169 }
2170
2171 case Instruction::NOT_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002172 Unop_12x<HNot>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002173 break;
2174 }
2175
2176 case Instruction::INT_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002177 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002178 break;
2179 }
2180
2181 case Instruction::INT_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002182 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002183 break;
2184 }
2185
2186 case Instruction::INT_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002187 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002188 break;
2189 }
2190
2191 case Instruction::LONG_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002192 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002193 break;
2194 }
2195
2196 case Instruction::LONG_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002197 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002198 break;
2199 }
2200
2201 case Instruction::LONG_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002202 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002203 break;
2204 }
2205
2206 case Instruction::FLOAT_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002207 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002208 break;
2209 }
2210
2211 case Instruction::FLOAT_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002212 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002213 break;
2214 }
2215
2216 case Instruction::FLOAT_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002217 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002218 break;
2219 }
2220
2221 case Instruction::DOUBLE_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002222 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002223 break;
2224 }
2225
2226 case Instruction::DOUBLE_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002227 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002228 break;
2229 }
2230
2231 case Instruction::DOUBLE_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002232 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002233 break;
2234 }
2235
2236 case Instruction::INT_TO_BYTE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002237 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt8, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002238 break;
2239 }
2240
2241 case Instruction::INT_TO_SHORT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002242 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt16, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002243 break;
2244 }
2245
2246 case Instruction::INT_TO_CHAR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002247 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kUint16, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002248 break;
2249 }
2250
2251 case Instruction::ADD_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002252 Binop_23x<HAdd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002253 break;
2254 }
2255
2256 case Instruction::ADD_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002257 Binop_23x<HAdd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002258 break;
2259 }
2260
2261 case Instruction::ADD_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002262 Binop_23x<HAdd>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002263 break;
2264 }
2265
2266 case Instruction::ADD_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002267 Binop_23x<HAdd>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002268 break;
2269 }
2270
2271 case Instruction::SUB_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002272 Binop_23x<HSub>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002273 break;
2274 }
2275
2276 case Instruction::SUB_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002277 Binop_23x<HSub>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002278 break;
2279 }
2280
2281 case Instruction::SUB_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002282 Binop_23x<HSub>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002283 break;
2284 }
2285
2286 case Instruction::SUB_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002287 Binop_23x<HSub>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002288 break;
2289 }
2290
2291 case Instruction::ADD_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002292 Binop_12x<HAdd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002293 break;
2294 }
2295
2296 case Instruction::MUL_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002297 Binop_23x<HMul>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002298 break;
2299 }
2300
2301 case Instruction::MUL_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002302 Binop_23x<HMul>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002303 break;
2304 }
2305
2306 case Instruction::MUL_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002307 Binop_23x<HMul>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002308 break;
2309 }
2310
2311 case Instruction::MUL_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002312 Binop_23x<HMul>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002313 break;
2314 }
2315
2316 case Instruction::DIV_INT: {
2317 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002318 dex_pc, DataType::Type::kInt32, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002319 break;
2320 }
2321
2322 case Instruction::DIV_LONG: {
2323 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002324 dex_pc, DataType::Type::kInt64, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002325 break;
2326 }
2327
2328 case Instruction::DIV_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002329 Binop_23x<HDiv>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002330 break;
2331 }
2332
2333 case Instruction::DIV_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002334 Binop_23x<HDiv>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002335 break;
2336 }
2337
2338 case Instruction::REM_INT: {
2339 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002340 dex_pc, DataType::Type::kInt32, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002341 break;
2342 }
2343
2344 case Instruction::REM_LONG: {
2345 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002346 dex_pc, DataType::Type::kInt64, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002347 break;
2348 }
2349
2350 case Instruction::REM_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002351 Binop_23x<HRem>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002352 break;
2353 }
2354
2355 case Instruction::REM_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002356 Binop_23x<HRem>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002357 break;
2358 }
2359
2360 case Instruction::AND_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002361 Binop_23x<HAnd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002362 break;
2363 }
2364
2365 case Instruction::AND_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002366 Binop_23x<HAnd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002367 break;
2368 }
2369
2370 case Instruction::SHL_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002371 Binop_23x_shift<HShl>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002372 break;
2373 }
2374
2375 case Instruction::SHL_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002376 Binop_23x_shift<HShl>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002377 break;
2378 }
2379
2380 case Instruction::SHR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002381 Binop_23x_shift<HShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002382 break;
2383 }
2384
2385 case Instruction::SHR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002386 Binop_23x_shift<HShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002387 break;
2388 }
2389
2390 case Instruction::USHR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002391 Binop_23x_shift<HUShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002392 break;
2393 }
2394
2395 case Instruction::USHR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002396 Binop_23x_shift<HUShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002397 break;
2398 }
2399
2400 case Instruction::OR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002401 Binop_23x<HOr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002402 break;
2403 }
2404
2405 case Instruction::OR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002406 Binop_23x<HOr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002407 break;
2408 }
2409
2410 case Instruction::XOR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002411 Binop_23x<HXor>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002412 break;
2413 }
2414
2415 case Instruction::XOR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002416 Binop_23x<HXor>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002417 break;
2418 }
2419
2420 case Instruction::ADD_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002421 Binop_12x<HAdd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002422 break;
2423 }
2424
2425 case Instruction::ADD_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002426 Binop_12x<HAdd>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002427 break;
2428 }
2429
2430 case Instruction::ADD_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002431 Binop_12x<HAdd>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002432 break;
2433 }
2434
2435 case Instruction::SUB_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002436 Binop_12x<HSub>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002437 break;
2438 }
2439
2440 case Instruction::SUB_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002441 Binop_12x<HSub>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002442 break;
2443 }
2444
2445 case Instruction::SUB_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002446 Binop_12x<HSub>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002447 break;
2448 }
2449
2450 case Instruction::SUB_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002451 Binop_12x<HSub>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002452 break;
2453 }
2454
2455 case Instruction::MUL_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002456 Binop_12x<HMul>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002457 break;
2458 }
2459
2460 case Instruction::MUL_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002461 Binop_12x<HMul>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002462 break;
2463 }
2464
2465 case Instruction::MUL_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002466 Binop_12x<HMul>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002467 break;
2468 }
2469
2470 case Instruction::MUL_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002471 Binop_12x<HMul>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002472 break;
2473 }
2474
2475 case Instruction::DIV_INT_2ADDR: {
2476 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002477 dex_pc, DataType::Type::kInt32, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002478 break;
2479 }
2480
2481 case Instruction::DIV_LONG_2ADDR: {
2482 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002483 dex_pc, DataType::Type::kInt64, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002484 break;
2485 }
2486
2487 case Instruction::REM_INT_2ADDR: {
2488 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002489 dex_pc, DataType::Type::kInt32, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002490 break;
2491 }
2492
2493 case Instruction::REM_LONG_2ADDR: {
2494 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002495 dex_pc, DataType::Type::kInt64, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002496 break;
2497 }
2498
2499 case Instruction::REM_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002500 Binop_12x<HRem>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002501 break;
2502 }
2503
2504 case Instruction::REM_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002505 Binop_12x<HRem>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002506 break;
2507 }
2508
2509 case Instruction::SHL_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002510 Binop_12x_shift<HShl>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002511 break;
2512 }
2513
2514 case Instruction::SHL_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002515 Binop_12x_shift<HShl>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002516 break;
2517 }
2518
2519 case Instruction::SHR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002520 Binop_12x_shift<HShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002521 break;
2522 }
2523
2524 case Instruction::SHR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002525 Binop_12x_shift<HShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002526 break;
2527 }
2528
2529 case Instruction::USHR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002530 Binop_12x_shift<HUShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002531 break;
2532 }
2533
2534 case Instruction::USHR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002535 Binop_12x_shift<HUShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002536 break;
2537 }
2538
2539 case Instruction::DIV_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002540 Binop_12x<HDiv>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002541 break;
2542 }
2543
2544 case Instruction::DIV_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002545 Binop_12x<HDiv>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002546 break;
2547 }
2548
2549 case Instruction::AND_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002550 Binop_12x<HAnd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002551 break;
2552 }
2553
2554 case Instruction::AND_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002555 Binop_12x<HAnd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002556 break;
2557 }
2558
2559 case Instruction::OR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002560 Binop_12x<HOr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002561 break;
2562 }
2563
2564 case Instruction::OR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002565 Binop_12x<HOr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002566 break;
2567 }
2568
2569 case Instruction::XOR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002570 Binop_12x<HXor>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002571 break;
2572 }
2573
2574 case Instruction::XOR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002575 Binop_12x<HXor>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002576 break;
2577 }
2578
2579 case Instruction::ADD_INT_LIT16: {
2580 Binop_22s<HAdd>(instruction, false, dex_pc);
2581 break;
2582 }
2583
2584 case Instruction::AND_INT_LIT16: {
2585 Binop_22s<HAnd>(instruction, false, dex_pc);
2586 break;
2587 }
2588
2589 case Instruction::OR_INT_LIT16: {
2590 Binop_22s<HOr>(instruction, false, dex_pc);
2591 break;
2592 }
2593
2594 case Instruction::XOR_INT_LIT16: {
2595 Binop_22s<HXor>(instruction, false, dex_pc);
2596 break;
2597 }
2598
2599 case Instruction::RSUB_INT: {
2600 Binop_22s<HSub>(instruction, true, dex_pc);
2601 break;
2602 }
2603
2604 case Instruction::MUL_INT_LIT16: {
2605 Binop_22s<HMul>(instruction, false, dex_pc);
2606 break;
2607 }
2608
2609 case Instruction::ADD_INT_LIT8: {
2610 Binop_22b<HAdd>(instruction, false, dex_pc);
2611 break;
2612 }
2613
2614 case Instruction::AND_INT_LIT8: {
2615 Binop_22b<HAnd>(instruction, false, dex_pc);
2616 break;
2617 }
2618
2619 case Instruction::OR_INT_LIT8: {
2620 Binop_22b<HOr>(instruction, false, dex_pc);
2621 break;
2622 }
2623
2624 case Instruction::XOR_INT_LIT8: {
2625 Binop_22b<HXor>(instruction, false, dex_pc);
2626 break;
2627 }
2628
2629 case Instruction::RSUB_INT_LIT8: {
2630 Binop_22b<HSub>(instruction, true, dex_pc);
2631 break;
2632 }
2633
2634 case Instruction::MUL_INT_LIT8: {
2635 Binop_22b<HMul>(instruction, false, dex_pc);
2636 break;
2637 }
2638
2639 case Instruction::DIV_INT_LIT16:
2640 case Instruction::DIV_INT_LIT8: {
2641 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002642 dex_pc, DataType::Type::kInt32, true, true);
David Brazdildee58d62016-04-07 09:54:26 +00002643 break;
2644 }
2645
2646 case Instruction::REM_INT_LIT16:
2647 case Instruction::REM_INT_LIT8: {
2648 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002649 dex_pc, DataType::Type::kInt32, true, false);
David Brazdildee58d62016-04-07 09:54:26 +00002650 break;
2651 }
2652
2653 case Instruction::SHL_INT_LIT8: {
2654 Binop_22b<HShl>(instruction, false, dex_pc);
2655 break;
2656 }
2657
2658 case Instruction::SHR_INT_LIT8: {
2659 Binop_22b<HShr>(instruction, false, dex_pc);
2660 break;
2661 }
2662
2663 case Instruction::USHR_INT_LIT8: {
2664 Binop_22b<HUShr>(instruction, false, dex_pc);
2665 break;
2666 }
2667
2668 case Instruction::NEW_INSTANCE: {
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002669 HNewInstance* new_instance =
2670 BuildNewInstance(dex::TypeIndex(instruction.VRegB_21c()), dex_pc);
2671 DCHECK(new_instance != nullptr);
2672
David Brazdildee58d62016-04-07 09:54:26 +00002673 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002674 BuildConstructorFenceForAllocation(new_instance);
David Brazdildee58d62016-04-07 09:54:26 +00002675 break;
2676 }
2677
2678 case Instruction::NEW_ARRAY: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002679 dex::TypeIndex type_index(instruction.VRegC_22c());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002680 HInstruction* length = LoadLocal(instruction.VRegB_22c(), DataType::Type::kInt32);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002681 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002682
Vladimir Markoca6fff82017-10-03 14:49:14 +01002683 HNewArray* new_array = new (allocator_) HNewArray(cls, length, dex_pc);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002684 AppendInstruction(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002685 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002686 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002687 break;
2688 }
2689
2690 case Instruction::FILLED_NEW_ARRAY: {
2691 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002692 dex::TypeIndex type_index(instruction.VRegB_35c());
David Brazdildee58d62016-04-07 09:54:26 +00002693 uint32_t args[5];
2694 instruction.GetVarArgs(args);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002695 HNewArray* new_array = BuildFilledNewArray(dex_pc,
2696 type_index,
2697 number_of_vreg_arguments,
2698 /* is_range */ false,
2699 args,
2700 /* register_index */ 0);
2701 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002702 break;
2703 }
2704
2705 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2706 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002707 dex::TypeIndex type_index(instruction.VRegB_3rc());
David Brazdildee58d62016-04-07 09:54:26 +00002708 uint32_t register_index = instruction.VRegC_3rc();
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002709 HNewArray* new_array = BuildFilledNewArray(dex_pc,
2710 type_index,
2711 number_of_vreg_arguments,
2712 /* is_range */ true,
2713 /* args*/ nullptr,
2714 register_index);
2715 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002716 break;
2717 }
2718
2719 case Instruction::FILL_ARRAY_DATA: {
2720 BuildFillArrayData(instruction, dex_pc);
2721 break;
2722 }
2723
2724 case Instruction::MOVE_RESULT:
2725 case Instruction::MOVE_RESULT_WIDE:
2726 case Instruction::MOVE_RESULT_OBJECT: {
2727 DCHECK(latest_result_ != nullptr);
2728 UpdateLocal(instruction.VRegA(), latest_result_);
2729 latest_result_ = nullptr;
2730 break;
2731 }
2732
2733 case Instruction::CMP_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002734 Binop_23x_cmp(instruction, DataType::Type::kInt64, ComparisonBias::kNoBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002735 break;
2736 }
2737
2738 case Instruction::CMPG_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002739 Binop_23x_cmp(instruction, DataType::Type::kFloat32, ComparisonBias::kGtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002740 break;
2741 }
2742
2743 case Instruction::CMPG_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002744 Binop_23x_cmp(instruction, DataType::Type::kFloat64, ComparisonBias::kGtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002745 break;
2746 }
2747
2748 case Instruction::CMPL_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002749 Binop_23x_cmp(instruction, DataType::Type::kFloat32, ComparisonBias::kLtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002750 break;
2751 }
2752
2753 case Instruction::CMPL_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002754 Binop_23x_cmp(instruction, DataType::Type::kFloat64, ComparisonBias::kLtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002755 break;
2756 }
2757
2758 case Instruction::NOP:
2759 break;
2760
2761 case Instruction::IGET:
2762 case Instruction::IGET_QUICK:
2763 case Instruction::IGET_WIDE:
2764 case Instruction::IGET_WIDE_QUICK:
2765 case Instruction::IGET_OBJECT:
2766 case Instruction::IGET_OBJECT_QUICK:
2767 case Instruction::IGET_BOOLEAN:
2768 case Instruction::IGET_BOOLEAN_QUICK:
2769 case Instruction::IGET_BYTE:
2770 case Instruction::IGET_BYTE_QUICK:
2771 case Instruction::IGET_CHAR:
2772 case Instruction::IGET_CHAR_QUICK:
2773 case Instruction::IGET_SHORT:
2774 case Instruction::IGET_SHORT_QUICK: {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002775 if (!BuildInstanceFieldAccess(instruction, dex_pc, /* is_put */ false, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00002776 return false;
2777 }
2778 break;
2779 }
2780
2781 case Instruction::IPUT:
2782 case Instruction::IPUT_QUICK:
2783 case Instruction::IPUT_WIDE:
2784 case Instruction::IPUT_WIDE_QUICK:
2785 case Instruction::IPUT_OBJECT:
2786 case Instruction::IPUT_OBJECT_QUICK:
2787 case Instruction::IPUT_BOOLEAN:
2788 case Instruction::IPUT_BOOLEAN_QUICK:
2789 case Instruction::IPUT_BYTE:
2790 case Instruction::IPUT_BYTE_QUICK:
2791 case Instruction::IPUT_CHAR:
2792 case Instruction::IPUT_CHAR_QUICK:
2793 case Instruction::IPUT_SHORT:
2794 case Instruction::IPUT_SHORT_QUICK: {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002795 if (!BuildInstanceFieldAccess(instruction, dex_pc, /* is_put */ true, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00002796 return false;
2797 }
2798 break;
2799 }
2800
2801 case Instruction::SGET:
2802 case Instruction::SGET_WIDE:
2803 case Instruction::SGET_OBJECT:
2804 case Instruction::SGET_BOOLEAN:
2805 case Instruction::SGET_BYTE:
2806 case Instruction::SGET_CHAR:
2807 case Instruction::SGET_SHORT: {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002808 BuildStaticFieldAccess(instruction, dex_pc, /* is_put */ false);
David Brazdildee58d62016-04-07 09:54:26 +00002809 break;
2810 }
2811
2812 case Instruction::SPUT:
2813 case Instruction::SPUT_WIDE:
2814 case Instruction::SPUT_OBJECT:
2815 case Instruction::SPUT_BOOLEAN:
2816 case Instruction::SPUT_BYTE:
2817 case Instruction::SPUT_CHAR:
2818 case Instruction::SPUT_SHORT: {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002819 BuildStaticFieldAccess(instruction, dex_pc, /* is_put */ true);
David Brazdildee58d62016-04-07 09:54:26 +00002820 break;
2821 }
2822
2823#define ARRAY_XX(kind, anticipated_type) \
2824 case Instruction::AGET##kind: { \
2825 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
2826 break; \
2827 } \
2828 case Instruction::APUT##kind: { \
2829 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
2830 break; \
2831 }
2832
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002833 ARRAY_XX(, DataType::Type::kInt32);
2834 ARRAY_XX(_WIDE, DataType::Type::kInt64);
2835 ARRAY_XX(_OBJECT, DataType::Type::kReference);
2836 ARRAY_XX(_BOOLEAN, DataType::Type::kBool);
2837 ARRAY_XX(_BYTE, DataType::Type::kInt8);
2838 ARRAY_XX(_CHAR, DataType::Type::kUint16);
2839 ARRAY_XX(_SHORT, DataType::Type::kInt16);
David Brazdildee58d62016-04-07 09:54:26 +00002840
2841 case Instruction::ARRAY_LENGTH: {
David Brazdilc120bbe2016-04-22 16:57:00 +01002842 HInstruction* object = LoadNullCheckedLocal(instruction.VRegB_12x(), dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002843 AppendInstruction(new (allocator_) HArrayLength(object, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002844 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
2845 break;
2846 }
2847
2848 case Instruction::CONST_STRING: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08002849 dex::StringIndex string_index(instruction.VRegB_21c());
Vladimir Marko28e012a2017-12-07 11:22:59 +00002850 BuildLoadString(string_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002851 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2852 break;
2853 }
2854
2855 case Instruction::CONST_STRING_JUMBO: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08002856 dex::StringIndex string_index(instruction.VRegB_31c());
Vladimir Marko28e012a2017-12-07 11:22:59 +00002857 BuildLoadString(string_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002858 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
2859 break;
2860 }
2861
2862 case Instruction::CONST_CLASS: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002863 dex::TypeIndex type_index(instruction.VRegB_21c());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002864 BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002865 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2866 break;
2867 }
2868
2869 case Instruction::MOVE_EXCEPTION: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002870 AppendInstruction(new (allocator_) HLoadException(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002871 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
Vladimir Markoca6fff82017-10-03 14:49:14 +01002872 AppendInstruction(new (allocator_) HClearException(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002873 break;
2874 }
2875
2876 case Instruction::THROW: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002877 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002878 AppendInstruction(new (allocator_) HThrow(exception, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002879 // We finished building this block. Set the current block to null to avoid
2880 // adding dead instructions to it.
2881 current_block_ = nullptr;
2882 break;
2883 }
2884
2885 case Instruction::INSTANCE_OF: {
2886 uint8_t destination = instruction.VRegA_22c();
2887 uint8_t reference = instruction.VRegB_22c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002888 dex::TypeIndex type_index(instruction.VRegC_22c());
David Brazdildee58d62016-04-07 09:54:26 +00002889 BuildTypeCheck(instruction, destination, reference, type_index, dex_pc);
2890 break;
2891 }
2892
2893 case Instruction::CHECK_CAST: {
2894 uint8_t reference = instruction.VRegA_21c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002895 dex::TypeIndex type_index(instruction.VRegB_21c());
David Brazdildee58d62016-04-07 09:54:26 +00002896 BuildTypeCheck(instruction, -1, reference, type_index, dex_pc);
2897 break;
2898 }
2899
2900 case Instruction::MONITOR_ENTER: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002901 AppendInstruction(new (allocator_) HMonitorOperation(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002902 LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference),
David Brazdildee58d62016-04-07 09:54:26 +00002903 HMonitorOperation::OperationKind::kEnter,
2904 dex_pc));
2905 break;
2906 }
2907
2908 case Instruction::MONITOR_EXIT: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002909 AppendInstruction(new (allocator_) HMonitorOperation(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002910 LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference),
David Brazdildee58d62016-04-07 09:54:26 +00002911 HMonitorOperation::OperationKind::kExit,
2912 dex_pc));
2913 break;
2914 }
2915
2916 case Instruction::SPARSE_SWITCH:
2917 case Instruction::PACKED_SWITCH: {
2918 BuildSwitch(instruction, dex_pc);
2919 break;
2920 }
2921
2922 default:
2923 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07002924 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00002925 << " because of unhandled instruction "
2926 << instruction.Name();
Igor Murashkin1e065a52017-08-09 13:20:34 -07002927 MaybeRecordStat(compilation_stats_,
2928 MethodCompilationStat::kNotCompiledUnhandledInstruction);
David Brazdildee58d62016-04-07 09:54:26 +00002929 return false;
2930 }
2931 return true;
2932} // NOLINT(readability/fn_size)
2933
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002934ObjPtr<mirror::Class> HInstructionBuilder::LookupResolvedType(
2935 dex::TypeIndex type_index,
2936 const DexCompilationUnit& compilation_unit) const {
2937 return ClassLinker::LookupResolvedType(
2938 type_index, compilation_unit.GetDexCache().Get(), compilation_unit.GetClassLoader().Get());
2939}
2940
2941ObjPtr<mirror::Class> HInstructionBuilder::LookupReferrerClass() const {
2942 // TODO: Cache the result in a Handle<mirror::Class>.
2943 const DexFile::MethodId& method_id =
2944 dex_compilation_unit_->GetDexFile()->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
2945 return LookupResolvedType(method_id.class_idx_, *dex_compilation_unit_);
2946}
2947
David Brazdildee58d62016-04-07 09:54:26 +00002948} // namespace art