blob: 9c6576bc18f1397b804da86ebf83d39a30c4d557 [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"
Andreas Gampe85f1c572018-11-21 13:52:48 -080022#include "base/logging.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010023#include "block_builder.h"
Vladimir Marko3b506202018-10-31 14:33:58 +000024#include "class_linker-inl.h"
25#include "code_generator.h"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010026#include "data_type-inl.h"
David Sehr312f3b22018-03-19 08:39:26 -070027#include "dex/bytecode_utils.h"
David Sehr9e734c72018-01-04 17:56:19 -080028#include "dex/dex_instruction-inl.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010029#include "driver/dex_compilation_unit.h"
David Brazdildee58d62016-04-07 09:54:26 +000030#include "driver/compiler_options.h"
Andreas Gampe75a7db62016-09-26 12:04:26 -070031#include "imtable-inl.h"
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +000032#include "jit/jit.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010033#include "mirror/dex_cache.h"
Nicolas Geoffray58cc1cb2017-11-20 13:27:29 +000034#include "oat_file.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010035#include "optimizing_compiler_stats.h"
Mathieu Chartierde4b08f2017-07-10 14:13:41 -070036#include "quicken_info.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070037#include "scoped_thread_state_change-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070038#include "sharpening.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010039#include "ssa_builder.h"
Andreas Gampea7c83ac2017-09-11 08:14:23 -070040#include "well_known_classes.h"
David Brazdildee58d62016-04-07 09:54:26 +000041
Vladimir Marko0a516052019-10-14 13:00:44 +000042namespace art {
David Brazdildee58d62016-04-07 09:54:26 +000043
Mathieu Chartier808c7a52017-12-15 11:19:33 -080044HInstructionBuilder::HInstructionBuilder(HGraph* graph,
45 HBasicBlockBuilder* block_builder,
46 SsaBuilder* ssa_builder,
47 const DexFile* dex_file,
48 const CodeItemDebugInfoAccessor& accessor,
49 DataType::Type return_type,
50 const DexCompilationUnit* dex_compilation_unit,
51 const DexCompilationUnit* outer_compilation_unit,
Mathieu Chartier808c7a52017-12-15 11:19:33 -080052 CodeGenerator* code_generator,
Mathieu Chartier210531f2018-01-12 10:15:51 -080053 ArrayRef<const uint8_t> interpreter_metadata,
Mathieu Chartier808c7a52017-12-15 11:19:33 -080054 OptimizingCompilerStats* compiler_stats,
55 VariableSizedHandleScope* handles,
56 ScopedArenaAllocator* local_allocator)
57 : allocator_(graph->GetAllocator()),
58 graph_(graph),
59 handles_(handles),
60 dex_file_(dex_file),
61 code_item_accessor_(accessor),
62 return_type_(return_type),
63 block_builder_(block_builder),
64 ssa_builder_(ssa_builder),
Mathieu Chartier808c7a52017-12-15 11:19:33 -080065 code_generator_(code_generator),
66 dex_compilation_unit_(dex_compilation_unit),
67 outer_compilation_unit_(outer_compilation_unit),
68 quicken_info_(interpreter_metadata),
69 compilation_stats_(compiler_stats),
70 local_allocator_(local_allocator),
71 locals_for_(local_allocator->Adapter(kArenaAllocGraphBuilder)),
72 current_block_(nullptr),
73 current_locals_(nullptr),
74 latest_result_(nullptr),
75 current_this_parameter_(nullptr),
Vladimir Marko3b506202018-10-31 14:33:58 +000076 loop_headers_(local_allocator->Adapter(kArenaAllocGraphBuilder)),
77 class_cache_(std::less<dex::TypeIndex>(), local_allocator->Adapter(kArenaAllocGraphBuilder)) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -080078 loop_headers_.reserve(kDefaultNumberOfLoops);
79}
80
David Brazdildee58d62016-04-07 09:54:26 +000081HBasicBlock* HInstructionBuilder::FindBlockStartingAt(uint32_t dex_pc) const {
82 return block_builder_->GetBlockAt(dex_pc);
83}
84
Vladimir Marko69d310e2017-10-09 14:12:23 +010085inline ScopedArenaVector<HInstruction*>* HInstructionBuilder::GetLocalsFor(HBasicBlock* block) {
86 ScopedArenaVector<HInstruction*>* locals = &locals_for_[block->GetBlockId()];
David Brazdildee58d62016-04-07 09:54:26 +000087 const size_t vregs = graph_->GetNumberOfVRegs();
Mingyao Yang01b47b02017-02-03 12:09:57 -080088 if (locals->size() == vregs) {
89 return locals;
90 }
91 return GetLocalsForWithAllocation(block, locals, vregs);
92}
David Brazdildee58d62016-04-07 09:54:26 +000093
Vladimir Marko69d310e2017-10-09 14:12:23 +010094ScopedArenaVector<HInstruction*>* HInstructionBuilder::GetLocalsForWithAllocation(
Mingyao Yang01b47b02017-02-03 12:09:57 -080095 HBasicBlock* block,
Vladimir Marko69d310e2017-10-09 14:12:23 +010096 ScopedArenaVector<HInstruction*>* locals,
Mingyao Yang01b47b02017-02-03 12:09:57 -080097 const size_t vregs) {
98 DCHECK_NE(locals->size(), vregs);
99 locals->resize(vregs, nullptr);
100 if (block->IsCatchBlock()) {
101 // We record incoming inputs of catch phis at throwing instructions and
102 // must therefore eagerly create the phis. Phis for undefined vregs will
103 // be deleted when the first throwing instruction with the vreg undefined
104 // is encountered. Unused phis will be removed by dead phi analysis.
105 for (size_t i = 0; i < vregs; ++i) {
106 // No point in creating the catch phi if it is already undefined at
107 // the first throwing instruction.
108 HInstruction* current_local_value = (*current_locals_)[i];
109 if (current_local_value != nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100110 HPhi* phi = new (allocator_) HPhi(
111 allocator_,
Mingyao Yang01b47b02017-02-03 12:09:57 -0800112 i,
113 0,
114 current_local_value->GetType());
115 block->AddPhi(phi);
116 (*locals)[i] = phi;
David Brazdildee58d62016-04-07 09:54:26 +0000117 }
118 }
119 }
120 return locals;
121}
122
Mingyao Yang01b47b02017-02-03 12:09:57 -0800123inline HInstruction* HInstructionBuilder::ValueOfLocalAt(HBasicBlock* block, size_t local) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100124 ScopedArenaVector<HInstruction*>* locals = GetLocalsFor(block);
David Brazdildee58d62016-04-07 09:54:26 +0000125 return (*locals)[local];
126}
127
128void HInstructionBuilder::InitializeBlockLocals() {
129 current_locals_ = GetLocalsFor(current_block_);
130
131 if (current_block_->IsCatchBlock()) {
132 // Catch phis were already created and inputs collected from throwing sites.
133 if (kIsDebugBuild) {
134 // Make sure there was at least one throwing instruction which initialized
135 // locals (guaranteed by HGraphBuilder) and that all try blocks have been
136 // visited already (from HTryBoundary scoping and reverse post order).
137 bool catch_block_visited = false;
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100138 for (HBasicBlock* current : graph_->GetReversePostOrder()) {
David Brazdildee58d62016-04-07 09:54:26 +0000139 if (current == current_block_) {
140 catch_block_visited = true;
141 } else if (current->IsTryBlock()) {
142 const HTryBoundary& try_entry = current->GetTryCatchInformation()->GetTryEntry();
143 if (try_entry.HasExceptionHandler(*current_block_)) {
144 DCHECK(!catch_block_visited) << "Catch block visited before its try block.";
145 }
146 }
147 }
148 DCHECK_EQ(current_locals_->size(), graph_->GetNumberOfVRegs())
149 << "No instructions throwing into a live catch block.";
150 }
151 } else if (current_block_->IsLoopHeader()) {
152 // If the block is a loop header, we know we only have visited the pre header
153 // because we are visiting in reverse post order. We create phis for all initialized
154 // locals from the pre header. Their inputs will be populated at the end of
155 // the analysis.
156 for (size_t local = 0; local < current_locals_->size(); ++local) {
157 HInstruction* incoming =
158 ValueOfLocalAt(current_block_->GetLoopInformation()->GetPreHeader(), local);
159 if (incoming != nullptr) {
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 0,
164 incoming->GetType());
165 current_block_->AddPhi(phi);
166 (*current_locals_)[local] = phi;
167 }
168 }
169
170 // Save the loop header so that the last phase of the analysis knows which
171 // blocks need to be updated.
172 loop_headers_.push_back(current_block_);
173 } else if (current_block_->GetPredecessors().size() > 0) {
174 // All predecessors have already been visited because we are visiting in reverse post order.
175 // We merge the values of all locals, creating phis if those values differ.
176 for (size_t local = 0; local < current_locals_->size(); ++local) {
177 bool one_predecessor_has_no_value = false;
178 bool is_different = false;
179 HInstruction* value = ValueOfLocalAt(current_block_->GetPredecessors()[0], local);
180
181 for (HBasicBlock* predecessor : current_block_->GetPredecessors()) {
182 HInstruction* current = ValueOfLocalAt(predecessor, local);
183 if (current == nullptr) {
184 one_predecessor_has_no_value = true;
185 break;
186 } else if (current != value) {
187 is_different = true;
188 }
189 }
190
191 if (one_predecessor_has_no_value) {
192 // If one predecessor has no value for this local, we trust the verifier has
193 // successfully checked that there is a store dominating any read after this block.
194 continue;
195 }
196
197 if (is_different) {
198 HInstruction* first_input = ValueOfLocalAt(current_block_->GetPredecessors()[0], local);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100199 HPhi* phi = new (allocator_) HPhi(
200 allocator_,
David Brazdildee58d62016-04-07 09:54:26 +0000201 local,
202 current_block_->GetPredecessors().size(),
203 first_input->GetType());
204 for (size_t i = 0; i < current_block_->GetPredecessors().size(); i++) {
205 HInstruction* pred_value = ValueOfLocalAt(current_block_->GetPredecessors()[i], local);
206 phi->SetRawInputAt(i, pred_value);
207 }
208 current_block_->AddPhi(phi);
209 value = phi;
210 }
211 (*current_locals_)[local] = value;
212 }
213 }
214}
215
216void HInstructionBuilder::PropagateLocalsToCatchBlocks() {
217 const HTryBoundary& try_entry = current_block_->GetTryCatchInformation()->GetTryEntry();
218 for (HBasicBlock* catch_block : try_entry.GetExceptionHandlers()) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100219 ScopedArenaVector<HInstruction*>* handler_locals = GetLocalsFor(catch_block);
David Brazdildee58d62016-04-07 09:54:26 +0000220 DCHECK_EQ(handler_locals->size(), current_locals_->size());
221 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
222 HInstruction* handler_value = (*handler_locals)[vreg];
223 if (handler_value == nullptr) {
224 // Vreg was undefined at a previously encountered throwing instruction
225 // and the catch phi was deleted. Do not record the local value.
226 continue;
227 }
228 DCHECK(handler_value->IsPhi());
229
230 HInstruction* local_value = (*current_locals_)[vreg];
231 if (local_value == nullptr) {
232 // This is the first instruction throwing into `catch_block` where
233 // `vreg` is undefined. Delete the catch phi.
234 catch_block->RemovePhi(handler_value->AsPhi());
235 (*handler_locals)[vreg] = nullptr;
236 } else {
237 // Vreg has been defined at all instructions throwing into `catch_block`
238 // encountered so far. Record the local value in the catch phi.
239 handler_value->AsPhi()->AddInput(local_value);
240 }
241 }
242 }
243}
244
245void HInstructionBuilder::AppendInstruction(HInstruction* instruction) {
246 current_block_->AddInstruction(instruction);
247 InitializeInstruction(instruction);
248}
249
250void HInstructionBuilder::InsertInstructionAtTop(HInstruction* instruction) {
251 if (current_block_->GetInstructions().IsEmpty()) {
252 current_block_->AddInstruction(instruction);
253 } else {
254 current_block_->InsertInstructionBefore(instruction, current_block_->GetFirstInstruction());
255 }
256 InitializeInstruction(instruction);
257}
258
259void HInstructionBuilder::InitializeInstruction(HInstruction* instruction) {
260 if (instruction->NeedsEnvironment()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100261 HEnvironment* environment = new (allocator_) HEnvironment(
262 allocator_,
David Brazdildee58d62016-04-07 09:54:26 +0000263 current_locals_->size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000264 graph_->GetArtMethod(),
David Brazdildee58d62016-04-07 09:54:26 +0000265 instruction->GetDexPc(),
David Brazdildee58d62016-04-07 09:54:26 +0000266 instruction);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100267 environment->CopyFrom(ArrayRef<HInstruction* const>(*current_locals_));
David Brazdildee58d62016-04-07 09:54:26 +0000268 instruction->SetRawEnvironment(environment);
269 }
270}
271
David Brazdilc120bbe2016-04-22 16:57:00 +0100272HInstruction* HInstructionBuilder::LoadNullCheckedLocal(uint32_t register_index, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100273 HInstruction* ref = LoadLocal(register_index, DataType::Type::kReference);
David Brazdilc120bbe2016-04-22 16:57:00 +0100274 if (!ref->CanBeNull()) {
275 return ref;
276 }
277
Vladimir Markoca6fff82017-10-03 14:49:14 +0100278 HNullCheck* null_check = new (allocator_) HNullCheck(ref, dex_pc);
David Brazdilc120bbe2016-04-22 16:57:00 +0100279 AppendInstruction(null_check);
280 return null_check;
281}
282
David Brazdildee58d62016-04-07 09:54:26 +0000283void HInstructionBuilder::SetLoopHeaderPhiInputs() {
284 for (size_t i = loop_headers_.size(); i > 0; --i) {
285 HBasicBlock* block = loop_headers_[i - 1];
286 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
287 HPhi* phi = it.Current()->AsPhi();
288 size_t vreg = phi->GetRegNumber();
289 for (HBasicBlock* predecessor : block->GetPredecessors()) {
290 HInstruction* value = ValueOfLocalAt(predecessor, vreg);
291 if (value == nullptr) {
292 // Vreg is undefined at this predecessor. Mark it dead and leave with
293 // fewer inputs than predecessors. SsaChecker will fail if not removed.
294 phi->SetDead();
295 break;
296 } else {
297 phi->AddInput(value);
298 }
299 }
300 }
301 }
302}
303
304static bool IsBlockPopulated(HBasicBlock* block) {
305 if (block->IsLoopHeader()) {
306 // Suspend checks were inserted into loop headers during building of dominator tree.
307 DCHECK(block->GetFirstInstruction()->IsSuspendCheck());
308 return block->GetFirstInstruction() != block->GetLastInstruction();
309 } else {
310 return !block->GetInstructions().IsEmpty();
311 }
312}
313
314bool HInstructionBuilder::Build() {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800315 DCHECK(code_item_accessor_.HasCodeItem());
Vladimir Marko69d310e2017-10-09 14:12:23 +0100316 locals_for_.resize(
317 graph_->GetBlocks().size(),
318 ScopedArenaVector<HInstruction*>(local_allocator_->Adapter(kArenaAllocGraphBuilder)));
David Brazdildee58d62016-04-07 09:54:26 +0000319
320 // Find locations where we want to generate extra stackmaps for native debugging.
321 // This allows us to generate the info only at interesting points (for example,
322 // at start of java statement) rather than before every dex instruction.
Vladimir Marko3b506202018-10-31 14:33:58 +0000323 const bool native_debuggable = code_generator_ != nullptr &&
324 code_generator_->GetCompilerOptions().GetNativeDebuggable();
David Brazdildee58d62016-04-07 09:54:26 +0000325 ArenaBitVector* native_debug_info_locations = nullptr;
326 if (native_debuggable) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100327 native_debug_info_locations = FindNativeDebugInfoLocations();
David Brazdildee58d62016-04-07 09:54:26 +0000328 }
329
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100330 for (HBasicBlock* block : graph_->GetReversePostOrder()) {
331 current_block_ = block;
David Brazdildee58d62016-04-07 09:54:26 +0000332 uint32_t block_dex_pc = current_block_->GetDexPc();
333
334 InitializeBlockLocals();
335
336 if (current_block_->IsEntryBlock()) {
337 InitializeParameters();
Vladimir Markoca6fff82017-10-03 14:49:14 +0100338 AppendInstruction(new (allocator_) HSuspendCheck(0u));
339 AppendInstruction(new (allocator_) HGoto(0u));
David Brazdildee58d62016-04-07 09:54:26 +0000340 continue;
341 } else if (current_block_->IsExitBlock()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100342 AppendInstruction(new (allocator_) HExit());
David Brazdildee58d62016-04-07 09:54:26 +0000343 continue;
344 } else if (current_block_->IsLoopHeader()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100345 HSuspendCheck* suspend_check = new (allocator_) HSuspendCheck(current_block_->GetDexPc());
David Brazdildee58d62016-04-07 09:54:26 +0000346 current_block_->GetLoopInformation()->SetSuspendCheck(suspend_check);
347 // This is slightly odd because the loop header might not be empty (TryBoundary).
348 // But we're still creating the environment with locals from the top of the block.
349 InsertInstructionAtTop(suspend_check);
350 }
351
352 if (block_dex_pc == kNoDexPc || current_block_ != block_builder_->GetBlockAt(block_dex_pc)) {
353 // Synthetic block that does not need to be populated.
354 DCHECK(IsBlockPopulated(current_block_));
355 continue;
356 }
357
358 DCHECK(!IsBlockPopulated(current_block_));
359
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700360 uint32_t quicken_index = 0;
361 if (CanDecodeQuickenedInfo()) {
362 quicken_index = block_builder_->GetQuickenIndex(block_dex_pc);
363 }
364
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800365 for (const DexInstructionPcPair& pair : code_item_accessor_.InstructionsFrom(block_dex_pc)) {
David Brazdildee58d62016-04-07 09:54:26 +0000366 if (current_block_ == nullptr) {
367 // The previous instruction ended this block.
368 break;
369 }
370
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800371 const uint32_t dex_pc = pair.DexPc();
David Brazdildee58d62016-04-07 09:54:26 +0000372 if (dex_pc != block_dex_pc && FindBlockStartingAt(dex_pc) != nullptr) {
373 // This dex_pc starts a new basic block.
374 break;
375 }
376
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800377 if (current_block_->IsTryBlock() && IsThrowingDexInstruction(pair.Inst())) {
David Brazdildee58d62016-04-07 09:54:26 +0000378 PropagateLocalsToCatchBlocks();
379 }
380
381 if (native_debuggable && native_debug_info_locations->IsBitSet(dex_pc)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100382 AppendInstruction(new (allocator_) HNativeDebugInfo(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000383 }
384
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800385 if (!ProcessDexInstruction(pair.Inst(), dex_pc, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +0000386 return false;
387 }
Nicolas Geoffrayfbf53b52020-04-01 15:20:14 +0100388 DCHECK(!Thread::Current()->IsExceptionPending())
389 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
390 << pair.Inst().Name() << "@" << dex_pc;
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700391
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800392 if (QuickenInfoTable::NeedsIndexForInstruction(&pair.Inst())) {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700393 ++quicken_index;
394 }
David Brazdildee58d62016-04-07 09:54:26 +0000395 }
396
397 if (current_block_ != nullptr) {
398 // Branching instructions clear current_block, so we know the last
399 // instruction of the current block is not a branching instruction.
400 // We add an unconditional Goto to the next block.
401 DCHECK_EQ(current_block_->GetSuccessors().size(), 1u);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100402 AppendInstruction(new (allocator_) HGoto());
David Brazdildee58d62016-04-07 09:54:26 +0000403 }
404 }
405
406 SetLoopHeaderPhiInputs();
407
408 return true;
409}
410
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000411void HInstructionBuilder::BuildIntrinsic(ArtMethod* method) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800412 DCHECK(!code_item_accessor_.HasCodeItem());
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000413 DCHECK(method->IsIntrinsic());
414
415 locals_for_.resize(
416 graph_->GetBlocks().size(),
417 ScopedArenaVector<HInstruction*>(local_allocator_->Adapter(kArenaAllocGraphBuilder)));
418
419 // Fill the entry block. Do not add suspend check, we do not want a suspend
420 // check in intrinsics; intrinsic methods are supposed to be fast.
421 current_block_ = graph_->GetEntryBlock();
422 InitializeBlockLocals();
423 InitializeParameters();
424 AppendInstruction(new (allocator_) HGoto(0u));
425
426 // Fill the body.
427 current_block_ = current_block_->GetSingleSuccessor();
428 InitializeBlockLocals();
429 DCHECK(!IsBlockPopulated(current_block_));
430
431 // Add the invoke and return instruction. Use HInvokeStaticOrDirect even
432 // for methods that would normally use an HInvokeVirtual (sharpen the call).
433 size_t in_vregs = graph_->GetNumberOfInVRegs();
434 size_t number_of_arguments =
435 in_vregs - std::count(current_locals_->end() - in_vregs, current_locals_->end(), nullptr);
436 uint32_t method_idx = dex_compilation_unit_->GetDexMethodIndex();
437 MethodReference target_method(dex_file_, method_idx);
438 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
439 HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall,
440 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Andreas Gampe3db70682018-12-26 15:12:03 -0800441 /* method_load_data= */ 0u
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000442 };
443 InvokeType invoke_type = dex_compilation_unit_->IsStatic() ? kStatic : kDirect;
444 HInvokeStaticOrDirect* invoke = new (allocator_) HInvokeStaticOrDirect(
445 allocator_,
446 number_of_arguments,
447 return_type_,
448 kNoDexPc,
449 method_idx,
450 method,
451 dispatch_info,
452 invoke_type,
453 target_method,
454 HInvokeStaticOrDirect::ClinitCheckRequirement::kNone);
Treehugger Robot2c5827a2018-05-17 22:26:08 +0000455 RangeInstructionOperands operands(graph_->GetNumberOfVRegs() - in_vregs, in_vregs);
Andreas Gampe3db70682018-12-26 15:12:03 -0800456 HandleInvoke(invoke, operands, dex_file_->GetMethodShorty(method_idx), /* is_unresolved= */ false);
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000457
458 // Add the return instruction.
459 if (return_type_ == DataType::Type::kVoid) {
460 AppendInstruction(new (allocator_) HReturnVoid());
461 } else {
462 AppendInstruction(new (allocator_) HReturn(invoke));
463 }
464
465 // Fill the exit block.
466 DCHECK_EQ(current_block_->GetSingleSuccessor(), graph_->GetExitBlock());
467 current_block_ = graph_->GetExitBlock();
468 InitializeBlockLocals();
469 AppendInstruction(new (allocator_) HExit());
470}
471
Vladimir Marko69d310e2017-10-09 14:12:23 +0100472ArenaBitVector* HInstructionBuilder::FindNativeDebugInfoLocations() {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100473 ArenaBitVector* locations = ArenaBitVector::Create(local_allocator_,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800474 code_item_accessor_.InsnsSizeInCodeUnits(),
Andreas Gampe3db70682018-12-26 15:12:03 -0800475 /* expandable= */ false,
Vladimir Marko69d310e2017-10-09 14:12:23 +0100476 kArenaAllocGraphBuilder);
477 locations->ClearAllBits();
Mathieu Chartier3e2e1232018-09-11 12:35:30 -0700478 // The visitor gets called when the line number changes.
479 // In other words, it marks the start of new java statement.
480 code_item_accessor_.DecodeDebugPositionInfo([&](const DexFile::PositionInfo& entry) {
481 locations->SetBit(entry.address_);
482 return false;
483 });
David Brazdildee58d62016-04-07 09:54:26 +0000484 // Instruction-specific tweaks.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800485 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800486 switch (inst->Opcode()) {
David Brazdildee58d62016-04-07 09:54:26 +0000487 case Instruction::MOVE_EXCEPTION: {
488 // Stop in native debugger after the exception has been moved.
489 // The compiler also expects the move at the start of basic block so
490 // we do not want to interfere by inserting native-debug-info before it.
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800491 locations->ClearBit(inst.DexPc());
492 DexInstructionIterator next = std::next(DexInstructionIterator(inst));
493 DCHECK(next.DexPc() != inst.DexPc());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800494 if (next != code_item_accessor_.end()) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -0700495 locations->SetBit(next.DexPc());
David Brazdildee58d62016-04-07 09:54:26 +0000496 }
497 break;
498 }
499 default:
500 break;
501 }
502 }
Vladimir Marko69d310e2017-10-09 14:12:23 +0100503 return locations;
David Brazdildee58d62016-04-07 09:54:26 +0000504}
505
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100506HInstruction* HInstructionBuilder::LoadLocal(uint32_t reg_number, DataType::Type type) const {
David Brazdildee58d62016-04-07 09:54:26 +0000507 HInstruction* value = (*current_locals_)[reg_number];
508 DCHECK(value != nullptr);
509
510 // If the operation requests a specific type, we make sure its input is of that type.
511 if (type != value->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100512 if (DataType::IsFloatingPointType(type)) {
Aart Bik31883642016-06-06 15:02:44 -0700513 value = ssa_builder_->GetFloatOrDoubleEquivalent(value, type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100514 } else if (type == DataType::Type::kReference) {
Aart Bik31883642016-06-06 15:02:44 -0700515 value = ssa_builder_->GetReferenceTypeEquivalent(value);
David Brazdildee58d62016-04-07 09:54:26 +0000516 }
Aart Bik31883642016-06-06 15:02:44 -0700517 DCHECK(value != nullptr);
David Brazdildee58d62016-04-07 09:54:26 +0000518 }
519
520 return value;
521}
522
523void HInstructionBuilder::UpdateLocal(uint32_t reg_number, HInstruction* stored_value) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100524 DataType::Type stored_type = stored_value->GetType();
525 DCHECK_NE(stored_type, DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +0000526
527 // Storing into vreg `reg_number` may implicitly invalidate the surrounding
528 // registers. Consider the following cases:
529 // (1) Storing a wide value must overwrite previous values in both `reg_number`
530 // and `reg_number+1`. We store `nullptr` in `reg_number+1`.
531 // (2) If vreg `reg_number-1` holds a wide value, writing into `reg_number`
532 // must invalidate it. We store `nullptr` in `reg_number-1`.
533 // Consequently, storing a wide value into the high vreg of another wide value
534 // will invalidate both `reg_number-1` and `reg_number+1`.
535
536 if (reg_number != 0) {
537 HInstruction* local_low = (*current_locals_)[reg_number - 1];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100538 if (local_low != nullptr && DataType::Is64BitType(local_low->GetType())) {
David Brazdildee58d62016-04-07 09:54:26 +0000539 // The vreg we are storing into was previously the high vreg of a pair.
540 // We need to invalidate its low vreg.
541 DCHECK((*current_locals_)[reg_number] == nullptr);
542 (*current_locals_)[reg_number - 1] = nullptr;
543 }
544 }
545
546 (*current_locals_)[reg_number] = stored_value;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100547 if (DataType::Is64BitType(stored_type)) {
David Brazdildee58d62016-04-07 09:54:26 +0000548 // We are storing a pair. Invalidate the instruction in the high vreg.
549 (*current_locals_)[reg_number + 1] = nullptr;
550 }
551}
552
553void HInstructionBuilder::InitializeParameters() {
554 DCHECK(current_block_->IsEntryBlock());
555
Vladimir Marko69d310e2017-10-09 14:12:23 +0100556 // outer_compilation_unit_ is null only when unit testing.
557 if (outer_compilation_unit_ == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +0000558 return;
559 }
560
561 const char* shorty = dex_compilation_unit_->GetShorty();
562 uint16_t number_of_parameters = graph_->GetNumberOfInVRegs();
563 uint16_t locals_index = graph_->GetNumberOfLocalVRegs();
564 uint16_t parameter_index = 0;
565
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800566 const dex::MethodId& referrer_method_id =
David Brazdildee58d62016-04-07 09:54:26 +0000567 dex_file_->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
568 if (!dex_compilation_unit_->IsStatic()) {
569 // Add the implicit 'this' argument, not expressed in the signature.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100570 HParameterValue* parameter = new (allocator_) HParameterValue(*dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +0000571 referrer_method_id.class_idx_,
572 parameter_index++,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100573 DataType::Type::kReference,
Andreas Gampe3db70682018-12-26 15:12:03 -0800574 /* is_this= */ true);
David Brazdildee58d62016-04-07 09:54:26 +0000575 AppendInstruction(parameter);
576 UpdateLocal(locals_index++, parameter);
577 number_of_parameters--;
Igor Murashkind01745e2017-04-05 16:40:31 -0700578 current_this_parameter_ = parameter;
579 } else {
580 DCHECK(current_this_parameter_ == nullptr);
David Brazdildee58d62016-04-07 09:54:26 +0000581 }
582
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800583 const dex::ProtoId& proto = dex_file_->GetMethodPrototype(referrer_method_id);
584 const dex::TypeList* arg_types = dex_file_->GetProtoParameters(proto);
David Brazdildee58d62016-04-07 09:54:26 +0000585 for (int i = 0, shorty_pos = 1; i < number_of_parameters; i++) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100586 HParameterValue* parameter = new (allocator_) HParameterValue(
David Brazdildee58d62016-04-07 09:54:26 +0000587 *dex_file_,
588 arg_types->GetTypeItem(shorty_pos - 1).type_idx_,
589 parameter_index++,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100590 DataType::FromShorty(shorty[shorty_pos]),
Andreas Gampe3db70682018-12-26 15:12:03 -0800591 /* is_this= */ false);
David Brazdildee58d62016-04-07 09:54:26 +0000592 ++shorty_pos;
593 AppendInstruction(parameter);
594 // Store the parameter value in the local that the dex code will use
595 // to reference that parameter.
596 UpdateLocal(locals_index++, parameter);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100597 if (DataType::Is64BitType(parameter->GetType())) {
David Brazdildee58d62016-04-07 09:54:26 +0000598 i++;
599 locals_index++;
600 parameter_index++;
601 }
602 }
603}
604
605template<typename T>
606void HInstructionBuilder::If_22t(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100607 HInstruction* first = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
608 HInstruction* second = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100609 T* comparison = new (allocator_) T(first, second, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000610 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100611 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000612 current_block_ = nullptr;
613}
614
615template<typename T>
616void HInstructionBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100617 HInstruction* value = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100618 T* comparison = new (allocator_) T(value, graph_->GetIntConstant(0, dex_pc), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000619 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100620 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000621 current_block_ = nullptr;
622}
623
624template<typename T>
625void HInstructionBuilder::Unop_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100626 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000627 uint32_t dex_pc) {
628 HInstruction* first = LoadLocal(instruction.VRegB(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100629 AppendInstruction(new (allocator_) T(type, first, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000630 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
631}
632
633void HInstructionBuilder::Conversion_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100634 DataType::Type input_type,
635 DataType::Type result_type,
David Brazdildee58d62016-04-07 09:54:26 +0000636 uint32_t dex_pc) {
637 HInstruction* first = LoadLocal(instruction.VRegB(), input_type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100638 AppendInstruction(new (allocator_) HTypeConversion(result_type, first, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000639 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
640}
641
642template<typename T>
643void HInstructionBuilder::Binop_23x(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.VRegB(), type);
647 HInstruction* second = LoadLocal(instruction.VRegC(), type);
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_23x_shift(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.VRegB(), type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100657 HInstruction* second = LoadLocal(instruction.VRegC(), DataType::Type::kInt32);
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
662void HInstructionBuilder::Binop_23x_cmp(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100663 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000664 ComparisonBias bias,
665 uint32_t dex_pc) {
666 HInstruction* first = LoadLocal(instruction.VRegB(), type);
667 HInstruction* second = LoadLocal(instruction.VRegC(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100668 AppendInstruction(new (allocator_) HCompare(type, first, second, bias, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000669 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
670}
671
672template<typename T>
673void HInstructionBuilder::Binop_12x_shift(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100674 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000675 uint32_t dex_pc) {
676 HInstruction* first = LoadLocal(instruction.VRegA(), type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100677 HInstruction* second = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100678 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000679 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
680}
681
682template<typename T>
683void HInstructionBuilder::Binop_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100684 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000685 uint32_t dex_pc) {
686 HInstruction* first = LoadLocal(instruction.VRegA(), type);
687 HInstruction* second = LoadLocal(instruction.VRegB(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100688 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000689 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
690}
691
692template<typename T>
693void HInstructionBuilder::Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100694 HInstruction* first = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000695 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s(), dex_pc);
696 if (reverse) {
697 std::swap(first, second);
698 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100699 AppendInstruction(new (allocator_) T(DataType::Type::kInt32, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000700 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
701}
702
703template<typename T>
704void HInstructionBuilder::Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100705 HInstruction* first = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000706 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b(), dex_pc);
707 if (reverse) {
708 std::swap(first, second);
709 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100710 AppendInstruction(new (allocator_) T(DataType::Type::kInt32, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000711 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
712}
713
Igor Murashkind01745e2017-04-05 16:40:31 -0700714// Does the method being compiled need any constructor barriers being inserted?
715// (Always 'false' for methods that aren't <init>.)
Vladimir Markoc1c34522018-10-31 13:56:49 +0000716static bool RequiresConstructorBarrier(const DexCompilationUnit* cu) {
Igor Murashkin032cacd2017-04-06 14:40:08 -0700717 // Can be null in unit tests only.
718 if (UNLIKELY(cu == nullptr)) {
719 return false;
720 }
721
Vladimir Markoc1c34522018-10-31 13:56:49 +0000722 // Constructor barriers are applicable only for <init> methods.
723 if (LIKELY(!cu->IsConstructor() || cu->IsStatic())) {
724 return false;
725 }
726
727 return cu->RequiresConstructorBarrier();
David Brazdildee58d62016-04-07 09:54:26 +0000728}
729
730// Returns true if `block` has only one successor which starts at the next
731// dex_pc after `instruction` at `dex_pc`.
732static bool IsFallthroughInstruction(const Instruction& instruction,
733 uint32_t dex_pc,
734 HBasicBlock* block) {
735 uint32_t next_dex_pc = dex_pc + instruction.SizeInCodeUnits();
736 return block->GetSingleSuccessor()->GetDexPc() == next_dex_pc;
737}
738
739void HInstructionBuilder::BuildSwitch(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100740 HInstruction* value = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000741 DexSwitchTable table(instruction, dex_pc);
742
743 if (table.GetNumEntries() == 0) {
744 // Empty Switch. Code falls through to the next block.
745 DCHECK(IsFallthroughInstruction(instruction, dex_pc, current_block_));
Vladimir Markoca6fff82017-10-03 14:49:14 +0100746 AppendInstruction(new (allocator_) HGoto(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000747 } else if (table.ShouldBuildDecisionTree()) {
748 for (DexSwitchTableIterator it(table); !it.Done(); it.Advance()) {
749 HInstruction* case_value = graph_->GetIntConstant(it.CurrentKey(), dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100750 HEqual* comparison = new (allocator_) HEqual(value, case_value, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000751 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100752 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000753
754 if (!it.IsLast()) {
755 current_block_ = FindBlockStartingAt(it.GetDexPcForCurrentIndex());
756 }
757 }
758 } else {
759 AppendInstruction(
Vladimir Markoca6fff82017-10-03 14:49:14 +0100760 new (allocator_) HPackedSwitch(table.GetEntryAt(0), table.GetNumEntries(), value, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000761 }
762
763 current_block_ = nullptr;
764}
765
766void HInstructionBuilder::BuildReturn(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100767 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000768 uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100769 if (type == DataType::Type::kVoid) {
Igor Murashkind01745e2017-04-05 16:40:31 -0700770 // Only <init> (which is a return-void) could possibly have a constructor fence.
Igor Murashkin032cacd2017-04-06 14:40:08 -0700771 // This may insert additional redundant constructor fences from the super constructors.
772 // TODO: remove redundant constructor fences (b/36656456).
Vladimir Markoc1c34522018-10-31 13:56:49 +0000773 if (RequiresConstructorBarrier(dex_compilation_unit_)) {
Igor Murashkind01745e2017-04-05 16:40:31 -0700774 // Compiling instance constructor.
Vladimir Markoba118822017-06-12 15:41:56 +0100775 DCHECK_STREQ("<init>", graph_->GetMethodName());
Igor Murashkind01745e2017-04-05 16:40:31 -0700776
777 HInstruction* fence_target = current_this_parameter_;
778 DCHECK(fence_target != nullptr);
779
Vladimir Markoca6fff82017-10-03 14:49:14 +0100780 AppendInstruction(new (allocator_) HConstructorFence(fence_target, dex_pc, allocator_));
Igor Murashkin6ef45672017-08-08 13:59:55 -0700781 MaybeRecordStat(
782 compilation_stats_,
783 MethodCompilationStat::kConstructorFenceGeneratedFinal);
David Brazdildee58d62016-04-07 09:54:26 +0000784 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100785 AppendInstruction(new (allocator_) HReturnVoid(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000786 } else {
Vladimir Markoc1c34522018-10-31 13:56:49 +0000787 DCHECK(!RequiresConstructorBarrier(dex_compilation_unit_));
David Brazdildee58d62016-04-07 09:54:26 +0000788 HInstruction* value = LoadLocal(instruction.VRegA(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100789 AppendInstruction(new (allocator_) HReturn(value, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000790 }
791 current_block_ = nullptr;
792}
793
794static InvokeType GetInvokeTypeFromOpCode(Instruction::Code opcode) {
795 switch (opcode) {
796 case Instruction::INVOKE_STATIC:
797 case Instruction::INVOKE_STATIC_RANGE:
798 return kStatic;
799 case Instruction::INVOKE_DIRECT:
800 case Instruction::INVOKE_DIRECT_RANGE:
801 return kDirect;
802 case Instruction::INVOKE_VIRTUAL:
803 case Instruction::INVOKE_VIRTUAL_QUICK:
804 case Instruction::INVOKE_VIRTUAL_RANGE:
805 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
806 return kVirtual;
807 case Instruction::INVOKE_INTERFACE:
808 case Instruction::INVOKE_INTERFACE_RANGE:
809 return kInterface;
810 case Instruction::INVOKE_SUPER_RANGE:
811 case Instruction::INVOKE_SUPER:
812 return kSuper;
813 default:
814 LOG(FATAL) << "Unexpected invoke opcode: " << opcode;
815 UNREACHABLE();
816 }
817}
818
819ArtMethod* HInstructionBuilder::ResolveMethod(uint16_t method_idx, InvokeType invoke_type) {
820 ScopedObjectAccess soa(Thread::Current());
David Brazdildee58d62016-04-07 09:54:26 +0000821
822 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000823 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100824
Vladimir Markoba118822017-06-12 15:41:56 +0100825 ArtMethod* resolved_method =
826 class_linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
Vladimir Markoba118822017-06-12 15:41:56 +0100827 method_idx,
828 dex_compilation_unit_->GetDexCache(),
829 class_loader,
830 graph_->GetArtMethod(),
831 invoke_type);
David Brazdildee58d62016-04-07 09:54:26 +0000832
833 if (UNLIKELY(resolved_method == nullptr)) {
834 // Clean up any exception left by type resolution.
835 soa.Self()->ClearException();
836 return nullptr;
837 }
838
Vladimir Markoba118822017-06-12 15:41:56 +0100839 // The referrer may be unresolved for AOT if we're compiling a class that cannot be
840 // resolved because, for example, we don't find a superclass in the classpath.
841 if (graph_->GetArtMethod() == nullptr) {
842 // The class linker cannot check access without a referrer, so we have to do it.
843 // Fall back to HInvokeUnresolved if the method isn't public.
David Brazdildee58d62016-04-07 09:54:26 +0000844 if (!resolved_method->IsPublic()) {
845 return nullptr;
846 }
David Brazdildee58d62016-04-07 09:54:26 +0000847 }
848
849 // We have to special case the invoke-super case, as ClassLinker::ResolveMethod does not.
850 // We need to look at the referrer's super class vtable. We need to do this to know if we need to
851 // make this an invoke-unresolved to handle cross-dex invokes or abstract super methods, both of
852 // which require runtime handling.
853 if (invoke_type == kSuper) {
Vladimir Markoa2c211c2018-11-01 09:50:52 +0000854 ObjPtr<mirror::Class> compiling_class = dex_compilation_unit_->GetCompilingClass().Get();
Andreas Gampefa4333d2017-02-14 11:10:34 -0800855 if (compiling_class == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +0000856 // We could not determine the method's class we need to wait until runtime.
857 DCHECK(Runtime::Current()->IsAotCompiler());
858 return nullptr;
859 }
Vladimir Markoba118822017-06-12 15:41:56 +0100860 ObjPtr<mirror::Class> referenced_class = class_linker->LookupResolvedType(
Vladimir Markoba118822017-06-12 15:41:56 +0100861 dex_compilation_unit_->GetDexFile()->GetMethodId(method_idx).class_idx_,
862 dex_compilation_unit_->GetDexCache().Get(),
863 class_loader.Get());
864 DCHECK(referenced_class != nullptr); // We have already resolved a method from this class.
865 if (!referenced_class->IsAssignableFrom(compiling_class)) {
Aart Bikf663e342016-04-04 17:28:59 -0700866 // We cannot statically determine the target method. The runtime will throw a
867 // NoSuchMethodError on this one.
868 return nullptr;
869 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100870 ArtMethod* actual_method;
Vladimir Markoba118822017-06-12 15:41:56 +0100871 if (referenced_class->IsInterface()) {
872 actual_method = referenced_class->FindVirtualMethodForInterfaceSuper(
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100873 resolved_method, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000874 } else {
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100875 uint16_t vtable_index = resolved_method->GetMethodIndex();
876 actual_method = compiling_class->GetSuperClass()->GetVTableEntry(
877 vtable_index, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000878 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100879 if (actual_method != resolved_method &&
880 !IsSameDexFile(*actual_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
881 // The back-end code generator relies on this check in order to ensure that it will not
882 // attempt to read the dex_cache with a dex_method_index that is not from the correct
883 // dex_file. If we didn't do this check then the dex_method_index will not be updated in the
Vladimir Markoc1c34522018-10-31 13:56:49 +0000884 // builder, which means that the code-generator (and sharpening and inliner, maybe)
885 // might invoke an incorrect method.
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100886 // TODO: The actual method could still be referenced in the current dex file, so we
887 // could try locating it.
888 // TODO: Remove the dex_file restriction.
889 return nullptr;
890 }
891 if (!actual_method->IsInvokable()) {
892 // Fail if the actual method cannot be invoked. Otherwise, the runtime resolution stub
893 // could resolve the callee to the wrong method.
894 return nullptr;
895 }
896 resolved_method = actual_method;
David Brazdildee58d62016-04-07 09:54:26 +0000897 }
898
David Brazdildee58d62016-04-07 09:54:26 +0000899 return resolved_method;
900}
901
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100902static bool IsStringConstructor(ArtMethod* method) {
903 ScopedObjectAccess soa(Thread::Current());
904 return method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
905}
906
David Brazdildee58d62016-04-07 09:54:26 +0000907bool HInstructionBuilder::BuildInvoke(const Instruction& instruction,
908 uint32_t dex_pc,
909 uint32_t method_idx,
Treehugger Robot2c5827a2018-05-17 22:26:08 +0000910 const InstructionOperands& operands) {
David Brazdildee58d62016-04-07 09:54:26 +0000911 InvokeType invoke_type = GetInvokeTypeFromOpCode(instruction.Opcode());
Orion Hodson4c8e12e2018-05-18 08:33:20 +0100912 const char* shorty = dex_file_->GetMethodShorty(method_idx);
913 DataType::Type return_type = DataType::FromShorty(shorty[0]);
David Brazdildee58d62016-04-07 09:54:26 +0000914
915 // Remove the return type from the 'proto'.
Orion Hodson4c8e12e2018-05-18 08:33:20 +0100916 size_t number_of_arguments = strlen(shorty) - 1;
David Brazdildee58d62016-04-07 09:54:26 +0000917 if (invoke_type != kStatic) { // instance call
918 // One extra argument for 'this'.
919 number_of_arguments++;
920 }
921
David Brazdildee58d62016-04-07 09:54:26 +0000922 ArtMethod* resolved_method = ResolveMethod(method_idx, invoke_type);
923
924 if (UNLIKELY(resolved_method == nullptr)) {
Igor Murashkin1e065a52017-08-09 13:20:34 -0700925 MaybeRecordStat(compilation_stats_,
926 MethodCompilationStat::kUnresolvedMethod);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100927 HInvoke* invoke = new (allocator_) HInvokeUnresolved(allocator_,
928 number_of_arguments,
929 return_type,
930 dex_pc,
931 method_idx,
932 invoke_type);
Andreas Gampe3db70682018-12-26 15:12:03 -0800933 return HandleInvoke(invoke, operands, shorty, /* is_unresolved= */ true);
David Brazdildee58d62016-04-07 09:54:26 +0000934 }
935
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100936 // Replace calls to String.<init> with StringFactory.
937 if (IsStringConstructor(resolved_method)) {
938 uint32_t string_init_entry_point = WellKnownClasses::StringInitToEntryPoint(resolved_method);
939 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
940 HInvokeStaticOrDirect::MethodLoadKind::kStringInit,
941 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000942 dchecked_integral_cast<uint64_t>(string_init_entry_point)
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100943 };
Nicolas Geoffrayf665f842018-02-15 12:29:06 +0000944 ScopedObjectAccess soa(Thread::Current());
945 MethodReference target_method(resolved_method->GetDexFile(),
946 resolved_method->GetDexMethodIndex());
947 // We pass null for the resolved_method to ensure optimizations
948 // don't rely on it.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100949 HInvoke* invoke = new (allocator_) HInvokeStaticOrDirect(
950 allocator_,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100951 number_of_arguments - 1,
Andreas Gampe3db70682018-12-26 15:12:03 -0800952 /* return_type= */ DataType::Type::kReference,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100953 dex_pc,
954 method_idx,
Andreas Gampe3db70682018-12-26 15:12:03 -0800955 /* resolved_method= */ nullptr,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100956 dispatch_info,
957 invoke_type,
958 target_method,
959 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit);
Orion Hodson4c8e12e2018-05-18 08:33:20 +0100960 return HandleStringInit(invoke, operands, shorty);
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100961 }
962
David Brazdildee58d62016-04-07 09:54:26 +0000963 // Potential class initialization check, in the case of a static method call.
964 HClinitCheck* clinit_check = nullptr;
965 HInvoke* invoke = nullptr;
966 if (invoke_type == kDirect || invoke_type == kStatic || invoke_type == kSuper) {
967 // By default, consider that the called method implicitly requires
968 // an initialization check of its declaring method.
969 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement
970 = HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
971 ScopedObjectAccess soa(Thread::Current());
972 if (invoke_type == kStatic) {
Vladimir Markofca0b492018-07-23 15:30:52 +0100973 clinit_check =
Vladimir Markoa2c211c2018-11-01 09:50:52 +0000974 ProcessClinitCheckForInvoke(dex_pc, resolved_method, &clinit_check_requirement);
David Brazdildee58d62016-04-07 09:54:26 +0000975 } else if (invoke_type == kSuper) {
976 if (IsSameDexFile(*resolved_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100977 // Update the method index to the one resolved. Note that this may be a no-op if
David Brazdildee58d62016-04-07 09:54:26 +0000978 // we resolved to the method referenced by the instruction.
979 method_idx = resolved_method->GetDexMethodIndex();
David Brazdildee58d62016-04-07 09:54:26 +0000980 }
981 }
982
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +0100983 HInvokeStaticOrDirect::DispatchInfo dispatch_info =
984 HSharpening::SharpenInvokeStaticOrDirect(resolved_method, code_generator_);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100985 MethodReference target_method(resolved_method->GetDexFile(),
986 resolved_method->GetDexMethodIndex());
Vladimir Markoca6fff82017-10-03 14:49:14 +0100987 invoke = new (allocator_) HInvokeStaticOrDirect(allocator_,
988 number_of_arguments,
989 return_type,
990 dex_pc,
991 method_idx,
992 resolved_method,
993 dispatch_info,
994 invoke_type,
995 target_method,
996 clinit_check_requirement);
David Brazdildee58d62016-04-07 09:54:26 +0000997 } else if (invoke_type == kVirtual) {
998 ScopedObjectAccess soa(Thread::Current()); // Needed for the method index
Vladimir Markoca6fff82017-10-03 14:49:14 +0100999 invoke = new (allocator_) HInvokeVirtual(allocator_,
1000 number_of_arguments,
1001 return_type,
1002 dex_pc,
1003 method_idx,
1004 resolved_method,
1005 resolved_method->GetMethodIndex());
David Brazdildee58d62016-04-07 09:54:26 +00001006 } else {
1007 DCHECK_EQ(invoke_type, kInterface);
Nicolas Geoffray29253112020-02-17 09:40:14 +00001008 ScopedObjectAccess soa(Thread::Current()); // Needed for the IMT index and class check below.
1009 if (resolved_method->GetDeclaringClass()->IsObjectClass()) {
1010 // If the resolved method is from j.l.Object, emit a virtual call instead.
1011 // The IMT conflict stub only handles interface methods.
1012 invoke = new (allocator_) HInvokeVirtual(allocator_,
Vladimir Markoca6fff82017-10-03 14:49:14 +01001013 number_of_arguments,
1014 return_type,
1015 dex_pc,
1016 method_idx,
1017 resolved_method,
Nicolas Geoffray29253112020-02-17 09:40:14 +00001018 resolved_method->GetMethodIndex());
1019 } else {
1020 DCHECK(resolved_method->GetDeclaringClass()->IsInterface());
1021 invoke = new (allocator_) HInvokeInterface(allocator_,
1022 number_of_arguments,
1023 return_type,
1024 dex_pc,
1025 method_idx,
1026 resolved_method,
1027 ImTable::GetImtIndex(resolved_method));
1028 }
David Brazdildee58d62016-04-07 09:54:26 +00001029 }
Andreas Gampe3db70682018-12-26 15:12:03 -08001030 return HandleInvoke(invoke, operands, shorty, /* is_unresolved= */ false, clinit_check);
David Brazdildee58d62016-04-07 09:54:26 +00001031}
1032
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001033bool HInstructionBuilder::BuildInvokePolymorphic(uint32_t dex_pc,
Orion Hodsonac141392017-01-13 11:53:47 +00001034 uint32_t method_idx,
Orion Hodson06d10a72018-05-14 08:53:38 +01001035 dex::ProtoIndex proto_idx,
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001036 const InstructionOperands& operands) {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001037 const char* shorty = dex_file_->GetShorty(proto_idx);
1038 DCHECK_EQ(1 + ArtMethod::NumArgRegisters(shorty), operands.GetNumberOfOperands());
1039 DataType::Type return_type = DataType::FromShorty(shorty[0]);
1040 size_t number_of_arguments = strlen(shorty);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001041 HInvoke* invoke = new (allocator_) HInvokePolymorphic(allocator_,
1042 number_of_arguments,
1043 return_type,
1044 dex_pc,
1045 method_idx);
Andreas Gampe3db70682018-12-26 15:12:03 -08001046 return HandleInvoke(invoke, operands, shorty, /* is_unresolved= */ false);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001047}
1048
1049
1050bool HInstructionBuilder::BuildInvokeCustom(uint32_t dex_pc,
1051 uint32_t call_site_idx,
1052 const InstructionOperands& operands) {
1053 dex::ProtoIndex proto_idx = dex_file_->GetProtoIndexForCallSite(call_site_idx);
1054 const char* shorty = dex_file_->GetShorty(proto_idx);
1055 DataType::Type return_type = DataType::FromShorty(shorty[0]);
1056 size_t number_of_arguments = strlen(shorty) - 1;
1057 HInvoke* invoke = new (allocator_) HInvokeCustom(allocator_,
1058 number_of_arguments,
1059 call_site_idx,
1060 return_type,
1061 dex_pc);
Andreas Gampe3db70682018-12-26 15:12:03 -08001062 return HandleInvoke(invoke, operands, shorty, /* is_unresolved= */ false);
Orion Hodsonac141392017-01-13 11:53:47 +00001063}
1064
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001065HNewInstance* HInstructionBuilder::BuildNewInstance(dex::TypeIndex type_index, uint32_t dex_pc) {
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001066 ScopedObjectAccess soa(Thread::Current());
David Brazdildee58d62016-04-07 09:54:26 +00001067
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001068 HLoadClass* load_class = BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001069
David Brazdildee58d62016-04-07 09:54:26 +00001070 HInstruction* cls = load_class;
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001071 Handle<mirror::Class> klass = load_class->GetClass();
1072
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001073 if (!IsInitialized(klass)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001074 cls = new (allocator_) HClinitCheck(load_class, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001075 AppendInstruction(cls);
1076 }
1077
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001078 // Only the access check entrypoint handles the finalizable class case. If we
1079 // need access checks, then we haven't resolved the method and the class may
1080 // again be finalizable.
1081 QuickEntrypointEnum entrypoint = kQuickAllocObjectInitialized;
1082 if (load_class->NeedsAccessCheck() || klass->IsFinalizable() || !klass->IsInstantiable()) {
1083 entrypoint = kQuickAllocObjectWithChecks;
1084 }
Alex Lightd109e302018-06-27 10:25:41 -07001085 // We will always be able to resolve the string class since it is in the BCP.
1086 if (!klass.IsNull() && klass->IsStringClass()) {
1087 entrypoint = kQuickAllocStringObject;
1088 }
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001089
1090 // Consider classes we haven't resolved as potentially finalizable.
Andreas Gampefa4333d2017-02-14 11:10:34 -08001091 bool finalizable = (klass == nullptr) || klass->IsFinalizable();
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001092
Vladimir Markoca6fff82017-10-03 14:49:14 +01001093 HNewInstance* new_instance = new (allocator_) HNewInstance(
David Brazdildee58d62016-04-07 09:54:26 +00001094 cls,
David Brazdildee58d62016-04-07 09:54:26 +00001095 dex_pc,
1096 type_index,
1097 *dex_compilation_unit_->GetDexFile(),
David Brazdildee58d62016-04-07 09:54:26 +00001098 finalizable,
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001099 entrypoint);
1100 AppendInstruction(new_instance);
1101
1102 return new_instance;
1103}
1104
1105void HInstructionBuilder::BuildConstructorFenceForAllocation(HInstruction* allocation) {
1106 DCHECK(allocation != nullptr &&
George Burgess IVf2072992017-05-23 15:36:41 -07001107 (allocation->IsNewInstance() ||
1108 allocation->IsNewArray())); // corresponding to "new" keyword in JLS.
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001109
1110 if (allocation->IsNewInstance()) {
1111 // STRING SPECIAL HANDLING:
1112 // -------------------------------
1113 // Strings have a real HNewInstance node but they end up always having 0 uses.
1114 // All uses of a String HNewInstance are always transformed to replace their input
1115 // of the HNewInstance with an input of the invoke to StringFactory.
1116 //
1117 // Do not emit an HConstructorFence here since it can inhibit some String new-instance
1118 // optimizations (to pass checker tests that rely on those optimizations).
1119 HNewInstance* new_inst = allocation->AsNewInstance();
1120 HLoadClass* load_class = new_inst->GetLoadClass();
1121
1122 Thread* self = Thread::Current();
1123 ScopedObjectAccess soa(self);
1124 StackHandleScope<1> hs(self);
1125 Handle<mirror::Class> klass = load_class->GetClass();
1126 if (klass != nullptr && klass->IsStringClass()) {
1127 return;
1128 // Note: Do not use allocation->IsStringAlloc which requires
1129 // a valid ReferenceTypeInfo, but that doesn't get made until after reference type
1130 // propagation (and instruction builder is too early).
1131 }
1132 // (In terms of correctness, the StringFactory needs to provide its own
1133 // default initialization barrier, see below.)
1134 }
1135
1136 // JLS 17.4.5 "Happens-before Order" describes:
1137 //
1138 // The default initialization of any object happens-before any other actions (other than
1139 // default-writes) of a program.
1140 //
1141 // In our implementation the default initialization of an object to type T means
1142 // setting all of its initial data (object[0..size)) to 0, and setting the
1143 // object's class header (i.e. object.getClass() == T.class).
1144 //
1145 // In practice this fence ensures that the writes to the object header
1146 // are visible to other threads if this object escapes the current thread.
1147 // (and in theory the 0-initializing, but that happens automatically
1148 // when new memory pages are mapped in by the OS).
1149 HConstructorFence* ctor_fence =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001150 new (allocator_) HConstructorFence(allocation, allocation->GetDexPc(), allocator_);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001151 AppendInstruction(ctor_fence);
Igor Murashkin6ef45672017-08-08 13:59:55 -07001152 MaybeRecordStat(
1153 compilation_stats_,
1154 MethodCompilationStat::kConstructorFenceGeneratedNew);
David Brazdildee58d62016-04-07 09:54:26 +00001155}
1156
Vladimir Marko2ab1bdd2018-07-12 09:59:56 +01001157static bool IsInBootImage(ObjPtr<mirror::Class> cls, const CompilerOptions& compiler_options)
1158 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko6a67bea2020-01-20 13:05:55 +00001159 if (Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(cls)) {
1160 return true;
1161 }
1162 if (compiler_options.IsBootImage() || compiler_options.IsBootImageExtension()) {
Vladimir Marko2ab1bdd2018-07-12 09:59:56 +01001163 std::string temp;
1164 const char* descriptor = cls->GetDescriptor(&temp);
1165 return compiler_options.IsImageClass(descriptor);
1166 } else {
Vladimir Marko6a67bea2020-01-20 13:05:55 +00001167 return false;
Vladimir Marko2ab1bdd2018-07-12 09:59:56 +01001168 }
1169}
1170
1171static bool IsSubClass(ObjPtr<mirror::Class> to_test, ObjPtr<mirror::Class> super_class)
1172 REQUIRES_SHARED(Locks::mutator_lock_) {
1173 return to_test != nullptr && !to_test->IsInterface() && to_test->IsSubClass(super_class);
1174}
1175
1176static bool HasTrivialClinit(ObjPtr<mirror::Class> klass, PointerSize pointer_size)
1177 REQUIRES_SHARED(Locks::mutator_lock_) {
1178 // Check if the class has encoded fields that trigger bytecode execution.
1179 // (Encoded fields are just a different representation of <clinit>.)
1180 if (klass->NumStaticFields() != 0u) {
1181 DCHECK(klass->GetClassDef() != nullptr);
1182 EncodedStaticFieldValueIterator it(klass->GetDexFile(), *klass->GetClassDef());
1183 for (; it.HasNext(); it.Next()) {
1184 switch (it.GetValueType()) {
1185 case EncodedArrayValueIterator::ValueType::kBoolean:
1186 case EncodedArrayValueIterator::ValueType::kByte:
1187 case EncodedArrayValueIterator::ValueType::kShort:
1188 case EncodedArrayValueIterator::ValueType::kChar:
1189 case EncodedArrayValueIterator::ValueType::kInt:
1190 case EncodedArrayValueIterator::ValueType::kLong:
1191 case EncodedArrayValueIterator::ValueType::kFloat:
1192 case EncodedArrayValueIterator::ValueType::kDouble:
1193 case EncodedArrayValueIterator::ValueType::kNull:
1194 case EncodedArrayValueIterator::ValueType::kString:
1195 // Primitive, null or j.l.String initialization is permitted.
1196 break;
1197 case EncodedArrayValueIterator::ValueType::kType:
1198 // Type initialization can load classes and execute bytecode through a class loader
1199 // which can execute arbitrary bytecode. We do not optimize for known class loaders;
1200 // kType is rarely used (if ever).
1201 return false;
1202 default:
1203 // Other types in the encoded static field list are rejected by the DexFileVerifier.
1204 LOG(FATAL) << "Unexpected type " << it.GetValueType();
1205 UNREACHABLE();
1206 }
1207 }
1208 }
1209 // Check if the class has <clinit> that executes arbitrary code.
1210 // Initialization of static fields of the class itself with constants is allowed.
1211 ArtMethod* clinit = klass->FindClassInitializer(pointer_size);
1212 if (clinit != nullptr) {
1213 const DexFile& dex_file = *clinit->GetDexFile();
1214 CodeItemInstructionAccessor accessor(dex_file, clinit->GetCodeItem());
1215 for (DexInstructionPcPair it : accessor) {
1216 switch (it->Opcode()) {
1217 case Instruction::CONST_4:
1218 case Instruction::CONST_16:
1219 case Instruction::CONST:
1220 case Instruction::CONST_HIGH16:
1221 case Instruction::CONST_WIDE_16:
1222 case Instruction::CONST_WIDE_32:
1223 case Instruction::CONST_WIDE:
1224 case Instruction::CONST_WIDE_HIGH16:
1225 case Instruction::CONST_STRING:
1226 case Instruction::CONST_STRING_JUMBO:
1227 // Primitive, null or j.l.String initialization is permitted.
1228 break;
1229 case Instruction::RETURN_VOID:
1230 case Instruction::RETURN_VOID_NO_BARRIER:
1231 break;
1232 case Instruction::SPUT:
1233 case Instruction::SPUT_WIDE:
1234 case Instruction::SPUT_OBJECT:
1235 case Instruction::SPUT_BOOLEAN:
1236 case Instruction::SPUT_BYTE:
1237 case Instruction::SPUT_CHAR:
1238 case Instruction::SPUT_SHORT:
1239 // Only initialization of a static field of the same class is permitted.
1240 if (dex_file.GetFieldId(it->VRegB_21c()).class_idx_ != klass->GetDexTypeIndex()) {
1241 return false;
1242 }
1243 break;
1244 case Instruction::NEW_ARRAY:
1245 // Only primitive arrays are permitted.
1246 if (Primitive::GetType(dex_file.GetTypeDescriptor(dex_file.GetTypeId(
1247 dex::TypeIndex(it->VRegC_22c())))[1]) == Primitive::kPrimNot) {
1248 return false;
1249 }
1250 break;
1251 case Instruction::APUT:
1252 case Instruction::APUT_WIDE:
1253 case Instruction::APUT_BOOLEAN:
1254 case Instruction::APUT_BYTE:
1255 case Instruction::APUT_CHAR:
1256 case Instruction::APUT_SHORT:
1257 case Instruction::FILL_ARRAY_DATA:
1258 case Instruction::NOP:
1259 // Allow initialization of primitive arrays (only constants can be stored).
1260 // Note: We expect NOPs used for fill-array-data-payload but accept all NOPs
1261 // (even unreferenced switch payloads if they make it through the verifier).
1262 break;
1263 default:
1264 return false;
1265 }
1266 }
1267 }
1268 return true;
1269}
1270
1271static bool HasTrivialInitialization(ObjPtr<mirror::Class> cls,
1272 const CompilerOptions& compiler_options)
1273 REQUIRES_SHARED(Locks::mutator_lock_) {
1274 Runtime* runtime = Runtime::Current();
1275 PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
1276
1277 // Check the superclass chain.
1278 for (ObjPtr<mirror::Class> klass = cls; klass != nullptr; klass = klass->GetSuperClass()) {
1279 if (klass->IsInitialized() && IsInBootImage(klass, compiler_options)) {
1280 break; // `klass` and its superclasses are already initialized in the boot image.
1281 }
1282 if (!HasTrivialClinit(klass, pointer_size)) {
1283 return false;
1284 }
1285 }
1286
1287 // Also check interfaces with default methods as they need to be initialized as well.
1288 ObjPtr<mirror::IfTable> iftable = cls->GetIfTable();
1289 DCHECK(iftable != nullptr);
1290 for (int32_t i = 0, count = iftable->Count(); i != count; ++i) {
1291 ObjPtr<mirror::Class> iface = iftable->GetInterface(i);
1292 if (!iface->HasDefaultMethods()) {
1293 continue; // Initializing `cls` does not initialize this interface.
1294 }
1295 if (iface->IsInitialized() && IsInBootImage(iface, compiler_options)) {
1296 continue; // This interface is already initialized in the boot image.
1297 }
1298 if (!HasTrivialClinit(iface, pointer_size)) {
1299 return false;
1300 }
1301 }
1302 return true;
1303}
1304
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001305bool HInstructionBuilder::IsInitialized(Handle<mirror::Class> cls) const {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001306 if (cls == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +00001307 return false;
1308 }
1309
Vladimir Marko51b8aaf2017-06-09 15:17:05 +01001310 // Check if the class will be initialized at runtime.
1311 if (cls->IsInitialized()) {
1312 Runtime* runtime = Runtime::Current();
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +00001313 if (runtime->IsAotCompiler()) {
1314 // Assume loaded only if klass is in the boot image. App classes cannot be assumed
1315 // loaded because we don't even know what class loader will be used to load them.
1316 if (IsInBootImage(cls.Get(), code_generator_->GetCompilerOptions())) {
1317 return true;
1318 }
1319 } else {
Vladimir Marko51b8aaf2017-06-09 15:17:05 +01001320 DCHECK(runtime->UseJitCompilation());
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +00001321 if (Runtime::Current()->GetJit()->CanAssumeInitialized(
1322 cls.Get(),
1323 graph_->IsCompilingForSharedJitCode())) {
1324 // For JIT, the class cannot revert to an uninitialized state.
1325 return true;
1326 }
Vladimir Marko51b8aaf2017-06-09 15:17:05 +01001327 }
1328 }
1329
Vladimir Marko7f260d42018-10-30 18:09:55 +00001330 // We can avoid the class initialization check for `cls` in static methods and constructors
1331 // in the very same class; invoking a static method involves a class initialization check
1332 // and so does the instance allocation that must be executed before invoking a constructor.
1333 // Other instance methods of the same class can run on an escaped instance
Vladimir Marko51b8aaf2017-06-09 15:17:05 +01001334 // of an erroneous class. Even a superclass may need to be checked as the subclass
1335 // can be completely initialized while the superclass is initializing and the subclass
1336 // remains initialized when the superclass initializer throws afterwards. b/62478025
1337 // Note: The HClinitCheck+HInvokeStaticOrDirect merging can still apply.
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001338 auto is_static_method_or_constructor_of_cls = [cls](const DexCompilationUnit& compilation_unit)
1339 REQUIRES_SHARED(Locks::mutator_lock_) {
1340 return (compilation_unit.GetAccessFlags() & (kAccStatic | kAccConstructor)) != 0u &&
1341 compilation_unit.GetCompilingClass().Get() == cls.Get();
1342 };
1343 if (is_static_method_or_constructor_of_cls(*outer_compilation_unit_) ||
1344 // Check also the innermost method. Though excessive copies of ClinitCheck can be
1345 // eliminated by GVN, that happens only after the decision whether to inline the
1346 // graph or not and that may depend on the presence of the ClinitCheck.
1347 // TODO: We should walk over the entire inlined method chain, but we don't pass that
1348 // information to the builder.
1349 is_static_method_or_constructor_of_cls(*dex_compilation_unit_)) {
Vladimir Marko2ab1bdd2018-07-12 09:59:56 +01001350 return true;
1351 }
David Brazdildee58d62016-04-07 09:54:26 +00001352
Vladimir Marko2ab1bdd2018-07-12 09:59:56 +01001353 // Otherwise, we may be able to avoid the check if `cls` is a superclass of a method being
1354 // compiled here (anywhere in the inlining chain) as the `cls` must have started initializing
1355 // before calling any `cls` or subclass methods. Static methods require a clinit check and
1356 // instance methods require an instance which cannot be created before doing a clinit check.
1357 // When a subclass of `cls` starts initializing, it starts initializing its superclass
1358 // chain up to `cls` without running any bytecode, i.e. without any opportunity for circular
1359 // initialization weirdness.
1360 //
1361 // If the initialization of `cls` is trivial (`cls` and its superclasses and superinterfaces
1362 // with default methods initialize only their own static fields using constant values), it must
1363 // complete, either successfully or by throwing and marking `cls` erroneous, without allocating
1364 // any instances of `cls` or subclasses (or any other class) and without calling any methods.
1365 // If it completes by throwing, no instances of `cls` shall be created and no subclass method
1366 // bytecode shall execute (see above), therefore the instruction we're building shall be
1367 // unreachable. By reaching the instruction, we know that `cls` was initialized successfully.
1368 //
1369 // TODO: We should walk over the entire inlined methods chain, but we don't pass that
1370 // information to the builder. (We could also check if we're guaranteed a non-null instance
1371 // of `cls` at this location but that's outside the scope of the instruction builder.)
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001372 bool is_subclass = IsSubClass(outer_compilation_unit_->GetCompilingClass().Get(), cls.Get());
1373 if (dex_compilation_unit_ != outer_compilation_unit_) {
1374 is_subclass = is_subclass ||
1375 IsSubClass(dex_compilation_unit_->GetCompilingClass().Get(), cls.Get());
1376 }
Vladimir Marko3b506202018-10-31 14:33:58 +00001377 if (is_subclass && HasTrivialInitialization(cls.Get(), code_generator_->GetCompilerOptions())) {
Vladimir Marko2ab1bdd2018-07-12 09:59:56 +01001378 return true;
1379 }
David Brazdildee58d62016-04-07 09:54:26 +00001380
1381 return false;
1382}
1383
1384HClinitCheck* HInstructionBuilder::ProcessClinitCheckForInvoke(
Vladimir Markofca0b492018-07-23 15:30:52 +01001385 uint32_t dex_pc,
1386 ArtMethod* resolved_method,
1387 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001388 Handle<mirror::Class> klass = handles_->NewHandle(resolved_method->GetDeclaringClass());
David Brazdildee58d62016-04-07 09:54:26 +00001389
1390 HClinitCheck* clinit_check = nullptr;
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001391 if (IsInitialized(klass)) {
David Brazdildee58d62016-04-07 09:54:26 +00001392 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001393 } else {
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001394 HLoadClass* cls = BuildLoadClass(klass->GetDexTypeIndex(),
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001395 klass->GetDexFile(),
1396 klass,
1397 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08001398 /* needs_access_check= */ false);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001399 if (cls != nullptr) {
1400 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
Vladimir Markoca6fff82017-10-03 14:49:14 +01001401 clinit_check = new (allocator_) HClinitCheck(cls, dex_pc);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001402 AppendInstruction(clinit_check);
1403 }
David Brazdildee58d62016-04-07 09:54:26 +00001404 }
1405 return clinit_check;
1406}
1407
1408bool HInstructionBuilder::SetupInvokeArguments(HInvoke* invoke,
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001409 const InstructionOperands& operands,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001410 const char* shorty,
David Brazdildee58d62016-04-07 09:54:26 +00001411 size_t start_index,
1412 size_t* argument_index) {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001413 uint32_t shorty_index = 1; // Skip the return type.
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001414 const size_t number_of_operands = operands.GetNumberOfOperands();
David Brazdildee58d62016-04-07 09:54:26 +00001415 for (size_t i = start_index;
1416 // Make sure we don't go over the expected arguments or over the number of
1417 // dex registers given. If the instruction was seen as dead by the verifier,
1418 // it hasn't been properly checked.
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001419 (i < number_of_operands) && (*argument_index < invoke->GetNumberOfArguments());
David Brazdildee58d62016-04-07 09:54:26 +00001420 i++, (*argument_index)++) {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001421 DataType::Type type = DataType::FromShorty(shorty[shorty_index++]);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001422 bool is_wide = (type == DataType::Type::kInt64) || (type == DataType::Type::kFloat64);
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001423 if (is_wide && ((i + 1 == number_of_operands) ||
1424 (operands.GetOperand(i) + 1 != operands.GetOperand(i + 1)))) {
David Brazdildee58d62016-04-07 09:54:26 +00001425 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
1426 // reject any class where this is violated. However, the verifier only does these checks
1427 // on non trivially dead instructions, so we just bailout the compilation.
1428 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001429 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001430 << " because of non-sequential dex register pair in wide argument";
Igor Murashkin1e065a52017-08-09 13:20:34 -07001431 MaybeRecordStat(compilation_stats_,
1432 MethodCompilationStat::kNotCompiledMalformedOpcode);
David Brazdildee58d62016-04-07 09:54:26 +00001433 return false;
1434 }
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001435 HInstruction* arg = LoadLocal(operands.GetOperand(i), type);
David Brazdildee58d62016-04-07 09:54:26 +00001436 invoke->SetArgumentAt(*argument_index, arg);
1437 if (is_wide) {
1438 i++;
1439 }
1440 }
1441
1442 if (*argument_index != invoke->GetNumberOfArguments()) {
1443 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001444 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001445 << " because of wrong number of arguments in invoke instruction";
Igor Murashkin1e065a52017-08-09 13:20:34 -07001446 MaybeRecordStat(compilation_stats_,
1447 MethodCompilationStat::kNotCompiledMalformedOpcode);
David Brazdildee58d62016-04-07 09:54:26 +00001448 return false;
1449 }
1450
1451 if (invoke->IsInvokeStaticOrDirect() &&
1452 HInvokeStaticOrDirect::NeedsCurrentMethodInput(
1453 invoke->AsInvokeStaticOrDirect()->GetMethodLoadKind())) {
1454 invoke->SetArgumentAt(*argument_index, graph_->GetCurrentMethod());
1455 (*argument_index)++;
1456 }
1457
1458 return true;
1459}
1460
1461bool HInstructionBuilder::HandleInvoke(HInvoke* invoke,
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001462 const InstructionOperands& operands,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001463 const char* shorty,
1464 bool is_unresolved,
1465 HClinitCheck* clinit_check) {
David Brazdildee58d62016-04-07 09:54:26 +00001466 DCHECK(!invoke->IsInvokeStaticOrDirect() || !invoke->AsInvokeStaticOrDirect()->IsStringInit());
1467
1468 size_t start_index = 0;
1469 size_t argument_index = 0;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001470 if (invoke->GetInvokeType() != InvokeType::kStatic) { // Instance call.
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001471 uint32_t obj_reg = operands.GetOperand(0);
Aart Bik296fbb42016-06-07 13:49:12 -07001472 HInstruction* arg = is_unresolved
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001473 ? LoadLocal(obj_reg, DataType::Type::kReference)
Aart Bik296fbb42016-06-07 13:49:12 -07001474 : LoadNullCheckedLocal(obj_reg, invoke->GetDexPc());
David Brazdilc120bbe2016-04-22 16:57:00 +01001475 invoke->SetArgumentAt(0, arg);
David Brazdildee58d62016-04-07 09:54:26 +00001476 start_index = 1;
1477 argument_index = 1;
1478 }
1479
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001480 if (!SetupInvokeArguments(invoke, operands, shorty, start_index, &argument_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00001481 return false;
1482 }
1483
1484 if (clinit_check != nullptr) {
1485 // Add the class initialization check as last input of `invoke`.
1486 DCHECK(invoke->IsInvokeStaticOrDirect());
1487 DCHECK(invoke->AsInvokeStaticOrDirect()->GetClinitCheckRequirement()
1488 == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit);
1489 invoke->SetArgumentAt(argument_index, clinit_check);
1490 argument_index++;
1491 }
1492
1493 AppendInstruction(invoke);
1494 latest_result_ = invoke;
1495
1496 return true;
1497}
1498
1499bool HInstructionBuilder::HandleStringInit(HInvoke* invoke,
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001500 const InstructionOperands& operands,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001501 const char* shorty) {
David Brazdildee58d62016-04-07 09:54:26 +00001502 DCHECK(invoke->IsInvokeStaticOrDirect());
1503 DCHECK(invoke->AsInvokeStaticOrDirect()->IsStringInit());
1504
1505 size_t start_index = 1;
1506 size_t argument_index = 0;
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001507 if (!SetupInvokeArguments(invoke, operands, shorty, start_index, &argument_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00001508 return false;
1509 }
1510
1511 AppendInstruction(invoke);
1512
1513 // This is a StringFactory call, not an actual String constructor. Its result
1514 // replaces the empty String pre-allocated by NewInstance.
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001515 uint32_t orig_this_reg = operands.GetOperand(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001516 HInstruction* arg_this = LoadLocal(orig_this_reg, DataType::Type::kReference);
David Brazdildee58d62016-04-07 09:54:26 +00001517
1518 // Replacing the NewInstance might render it redundant. Keep a list of these
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +01001519 // to be visited once it is clear whether it has remaining uses.
David Brazdildee58d62016-04-07 09:54:26 +00001520 if (arg_this->IsNewInstance()) {
1521 ssa_builder_->AddUninitializedString(arg_this->AsNewInstance());
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +01001522 } else {
1523 DCHECK(arg_this->IsPhi());
1524 // We can get a phi as input of a String.<init> if there is a loop between the
1525 // allocation and the String.<init> call. As we don't know which other phis might alias
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +01001526 // with `arg_this`, we keep a record of those invocations so we can later replace
1527 // the allocation with the invocation.
1528 // Add the actual 'this' input so the analysis knows what is the allocation instruction.
1529 // The input will be removed during the analysis.
1530 invoke->AddInput(arg_this);
1531 ssa_builder_->AddUninitializedStringPhi(invoke);
1532 }
1533 // Walk over all vregs and replace any occurrence of `arg_this` with `invoke`.
1534 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
1535 if ((*current_locals_)[vreg] == arg_this) {
1536 (*current_locals_)[vreg] = invoke;
1537 }
David Brazdildee58d62016-04-07 09:54:26 +00001538 }
David Brazdildee58d62016-04-07 09:54:26 +00001539 return true;
1540}
1541
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001542static DataType::Type GetFieldAccessType(const DexFile& dex_file, uint16_t field_index) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001543 const dex::FieldId& field_id = dex_file.GetFieldId(field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001544 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001545 return DataType::FromShorty(type[0]);
David Brazdildee58d62016-04-07 09:54:26 +00001546}
1547
1548bool HInstructionBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
1549 uint32_t dex_pc,
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001550 bool is_put,
1551 size_t quicken_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001552 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1553 uint32_t obj_reg = instruction.VRegB_22c();
1554 uint16_t field_index;
1555 if (instruction.IsQuickened()) {
1556 if (!CanDecodeQuickenedInfo()) {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00001557 VLOG(compiler) << "Not compiled: Could not decode quickened instruction "
1558 << instruction.Opcode();
David Brazdildee58d62016-04-07 09:54:26 +00001559 return false;
1560 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001561 field_index = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00001562 } else {
1563 field_index = instruction.VRegC_22c();
1564 }
1565
1566 ScopedObjectAccess soa(Thread::Current());
Andreas Gampe3db70682018-12-26 15:12:03 -08001567 ArtField* resolved_field = ResolveField(field_index, /* is_static= */ false, is_put);
David Brazdildee58d62016-04-07 09:54:26 +00001568
Aart Bik14154132016-06-02 17:53:58 -07001569 // Generate an explicit null check on the reference, unless the field access
1570 // is unresolved. In that case, we rely on the runtime to perform various
1571 // checks first, followed by a null check.
1572 HInstruction* object = (resolved_field == nullptr)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001573 ? LoadLocal(obj_reg, DataType::Type::kReference)
Aart Bik14154132016-06-02 17:53:58 -07001574 : LoadNullCheckedLocal(obj_reg, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001575
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001576 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001577 if (is_put) {
1578 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1579 HInstruction* field_set = nullptr;
1580 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001581 MaybeRecordStat(compilation_stats_,
1582 MethodCompilationStat::kUnresolvedField);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001583 field_set = new (allocator_) HUnresolvedInstanceFieldSet(object,
1584 value,
1585 field_type,
1586 field_index,
1587 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001588 } else {
1589 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Vladimir Markoca6fff82017-10-03 14:49:14 +01001590 field_set = new (allocator_) HInstanceFieldSet(object,
1591 value,
1592 resolved_field,
1593 field_type,
1594 resolved_field->GetOffset(),
1595 resolved_field->IsVolatile(),
1596 field_index,
1597 class_def_index,
1598 *dex_file_,
1599 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001600 }
1601 AppendInstruction(field_set);
1602 } else {
1603 HInstruction* field_get = nullptr;
1604 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001605 MaybeRecordStat(compilation_stats_,
1606 MethodCompilationStat::kUnresolvedField);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001607 field_get = new (allocator_) HUnresolvedInstanceFieldGet(object,
1608 field_type,
1609 field_index,
1610 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001611 } else {
1612 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Vladimir Markoca6fff82017-10-03 14:49:14 +01001613 field_get = new (allocator_) HInstanceFieldGet(object,
1614 resolved_field,
1615 field_type,
1616 resolved_field->GetOffset(),
1617 resolved_field->IsVolatile(),
1618 field_index,
1619 class_def_index,
1620 *dex_file_,
1621 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001622 }
1623 AppendInstruction(field_get);
1624 UpdateLocal(source_or_dest_reg, field_get);
1625 }
1626
1627 return true;
1628}
1629
David Brazdildee58d62016-04-07 09:54:26 +00001630void HInstructionBuilder::BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001631 uint32_t dex_pc,
1632 bool is_put,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001633 DataType::Type field_type) {
David Brazdildee58d62016-04-07 09:54:26 +00001634 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1635 uint16_t field_index = instruction.VRegB_21c();
1636
1637 if (is_put) {
1638 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1639 AppendInstruction(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001640 new (allocator_) HUnresolvedStaticFieldSet(value, field_type, field_index, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001641 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001642 AppendInstruction(new (allocator_) HUnresolvedStaticFieldGet(field_type, field_index, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001643 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1644 }
1645}
1646
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001647ArtField* HInstructionBuilder::ResolveField(uint16_t field_idx, bool is_static, bool is_put) {
1648 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001649
1650 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001651 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001652
Vladimir Markoe11dd502017-12-08 14:09:45 +00001653 ArtField* resolved_field = class_linker->ResolveField(field_idx,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001654 dex_compilation_unit_->GetDexCache(),
1655 class_loader,
1656 is_static);
Nicolas Geoffrayf3688822020-03-25 15:04:03 +00001657 DCHECK_EQ(resolved_field == nullptr, soa.Self()->IsExceptionPending())
1658 << "field="
1659 << ((resolved_field == nullptr) ? "null" : resolved_field->PrettyField())
1660 << ", exception="
1661 << (soa.Self()->IsExceptionPending() ? soa.Self()->GetException()->Dump() : "null");
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001662 if (UNLIKELY(resolved_field == nullptr)) {
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001663 // Clean up any exception left by field resolution.
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001664 soa.Self()->ClearException();
1665 return nullptr;
1666 }
1667
1668 // Check static/instance. The class linker has a fast path for looking into the dex cache
1669 // and does not check static/instance if it hits it.
1670 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
1671 return nullptr;
1672 }
1673
1674 // Check access.
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001675 Handle<mirror::Class> compiling_class = dex_compilation_unit_->GetCompilingClass();
Andreas Gampefa4333d2017-02-14 11:10:34 -08001676 if (compiling_class == nullptr) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001677 if (!resolved_field->IsPublic()) {
1678 return nullptr;
1679 }
1680 } else if (!compiling_class->CanAccessResolvedField(resolved_field->GetDeclaringClass(),
1681 resolved_field,
1682 dex_compilation_unit_->GetDexCache().Get(),
1683 field_idx)) {
1684 return nullptr;
1685 }
1686
1687 if (is_put &&
1688 resolved_field->IsFinal() &&
1689 (compiling_class.Get() != resolved_field->GetDeclaringClass())) {
1690 // Final fields can only be updated within their own class.
1691 // TODO: Only allow it in constructors. b/34966607.
1692 return nullptr;
1693 }
1694
1695 return resolved_field;
1696}
1697
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00001698void HInstructionBuilder::BuildStaticFieldAccess(const Instruction& instruction,
David Brazdildee58d62016-04-07 09:54:26 +00001699 uint32_t dex_pc,
1700 bool is_put) {
1701 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1702 uint16_t field_index = instruction.VRegB_21c();
1703
1704 ScopedObjectAccess soa(Thread::Current());
Andreas Gampe3db70682018-12-26 15:12:03 -08001705 ArtField* resolved_field = ResolveField(field_index, /* is_static= */ true, is_put);
David Brazdildee58d62016-04-07 09:54:26 +00001706
1707 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001708 MaybeRecordStat(compilation_stats_,
1709 MethodCompilationStat::kUnresolvedField);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001710 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001711 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00001712 return;
David Brazdildee58d62016-04-07 09:54:26 +00001713 }
1714
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001715 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001716
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001717 Handle<mirror::Class> klass = handles_->NewHandle(resolved_field->GetDeclaringClass());
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001718 HLoadClass* constant = BuildLoadClass(klass->GetDexTypeIndex(),
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001719 klass->GetDexFile(),
1720 klass,
1721 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08001722 /* needs_access_check= */ false);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001723
1724 if (constant == nullptr) {
1725 // The class cannot be referenced from this compiled code. Generate
1726 // an unresolved access.
Igor Murashkin1e065a52017-08-09 13:20:34 -07001727 MaybeRecordStat(compilation_stats_,
1728 MethodCompilationStat::kUnresolvedFieldNotAFastAccess);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001729 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00001730 return;
David Brazdildee58d62016-04-07 09:54:26 +00001731 }
1732
David Brazdildee58d62016-04-07 09:54:26 +00001733 HInstruction* cls = constant;
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001734 if (!IsInitialized(klass)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001735 cls = new (allocator_) HClinitCheck(constant, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001736 AppendInstruction(cls);
1737 }
1738
1739 uint16_t class_def_index = klass->GetDexClassDefIndex();
1740 if (is_put) {
1741 // We need to keep the class alive before loading the value.
1742 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1743 DCHECK_EQ(HPhi::ToPhiType(value->GetType()), HPhi::ToPhiType(field_type));
Vladimir Markoca6fff82017-10-03 14:49:14 +01001744 AppendInstruction(new (allocator_) HStaticFieldSet(cls,
1745 value,
1746 resolved_field,
1747 field_type,
1748 resolved_field->GetOffset(),
1749 resolved_field->IsVolatile(),
1750 field_index,
1751 class_def_index,
1752 *dex_file_,
1753 dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001754 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001755 AppendInstruction(new (allocator_) HStaticFieldGet(cls,
1756 resolved_field,
1757 field_type,
1758 resolved_field->GetOffset(),
1759 resolved_field->IsVolatile(),
1760 field_index,
1761 class_def_index,
1762 *dex_file_,
1763 dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001764 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1765 }
David Brazdildee58d62016-04-07 09:54:26 +00001766}
1767
1768void HInstructionBuilder::BuildCheckedDivRem(uint16_t out_vreg,
Vladimir Markoca6fff82017-10-03 14:49:14 +01001769 uint16_t first_vreg,
1770 int64_t second_vreg_or_constant,
1771 uint32_t dex_pc,
1772 DataType::Type type,
1773 bool second_is_constant,
1774 bool isDiv) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001775 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
David Brazdildee58d62016-04-07 09:54:26 +00001776
1777 HInstruction* first = LoadLocal(first_vreg, type);
1778 HInstruction* second = nullptr;
1779 if (second_is_constant) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001780 if (type == DataType::Type::kInt32) {
David Brazdildee58d62016-04-07 09:54:26 +00001781 second = graph_->GetIntConstant(second_vreg_or_constant, dex_pc);
1782 } else {
1783 second = graph_->GetLongConstant(second_vreg_or_constant, dex_pc);
1784 }
1785 } else {
1786 second = LoadLocal(second_vreg_or_constant, type);
1787 }
1788
1789 if (!second_is_constant
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001790 || (type == DataType::Type::kInt32 && second->AsIntConstant()->GetValue() == 0)
1791 || (type == DataType::Type::kInt64 && second->AsLongConstant()->GetValue() == 0)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001792 second = new (allocator_) HDivZeroCheck(second, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001793 AppendInstruction(second);
1794 }
1795
1796 if (isDiv) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001797 AppendInstruction(new (allocator_) HDiv(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001798 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001799 AppendInstruction(new (allocator_) HRem(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001800 }
1801 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
1802}
1803
1804void HInstructionBuilder::BuildArrayAccess(const Instruction& instruction,
1805 uint32_t dex_pc,
1806 bool is_put,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001807 DataType::Type anticipated_type) {
David Brazdildee58d62016-04-07 09:54:26 +00001808 uint8_t source_or_dest_reg = instruction.VRegA_23x();
1809 uint8_t array_reg = instruction.VRegB_23x();
1810 uint8_t index_reg = instruction.VRegC_23x();
1811
David Brazdilc120bbe2016-04-22 16:57:00 +01001812 HInstruction* object = LoadNullCheckedLocal(array_reg, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001813 HInstruction* length = new (allocator_) HArrayLength(object, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001814 AppendInstruction(length);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001815 HInstruction* index = LoadLocal(index_reg, DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001816 index = new (allocator_) HBoundsCheck(index, length, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001817 AppendInstruction(index);
1818 if (is_put) {
1819 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
1820 // TODO: Insert a type check node if the type is Object.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001821 HArraySet* aset = new (allocator_) HArraySet(object, index, value, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001822 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1823 AppendInstruction(aset);
1824 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001825 HArrayGet* aget = new (allocator_) HArrayGet(object, index, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001826 ssa_builder_->MaybeAddAmbiguousArrayGet(aget);
1827 AppendInstruction(aget);
1828 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1829 }
1830 graph_->SetHasBoundsChecks(true);
1831}
1832
Vladimir Markob5461632018-10-15 14:24:21 +01001833HNewArray* HInstructionBuilder::BuildNewArray(uint32_t dex_pc,
1834 dex::TypeIndex type_index,
1835 HInstruction* length) {
1836 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
1837
1838 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(type_index));
1839 DCHECK_EQ(descriptor[0], '[');
1840 size_t component_type_shift = Primitive::ComponentSizeShift(Primitive::GetType(descriptor[1]));
1841
1842 HNewArray* new_array = new (allocator_) HNewArray(cls, length, dex_pc, component_type_shift);
1843 AppendInstruction(new_array);
1844 return new_array;
1845}
1846
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001847HNewArray* HInstructionBuilder::BuildFilledNewArray(uint32_t dex_pc,
1848 dex::TypeIndex type_index,
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001849 const InstructionOperands& operands) {
1850 const size_t number_of_operands = operands.GetNumberOfOperands();
1851 HInstruction* length = graph_->GetIntConstant(number_of_operands, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001852
Vladimir Markob5461632018-10-15 14:24:21 +01001853 HNewArray* new_array = BuildNewArray(dex_pc, type_index, length);
David Brazdildee58d62016-04-07 09:54:26 +00001854 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1855 DCHECK_EQ(descriptor[0], '[') << descriptor;
1856 char primitive = descriptor[1];
1857 DCHECK(primitive == 'I'
1858 || primitive == 'L'
1859 || primitive == '[') << descriptor;
1860 bool is_reference_array = (primitive == 'L') || (primitive == '[');
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001861 DataType::Type type = is_reference_array ? DataType::Type::kReference : DataType::Type::kInt32;
David Brazdildee58d62016-04-07 09:54:26 +00001862
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001863 for (size_t i = 0; i < number_of_operands; ++i) {
1864 HInstruction* value = LoadLocal(operands.GetOperand(i), type);
David Brazdildee58d62016-04-07 09:54:26 +00001865 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
Vladimir Markob5461632018-10-15 14:24:21 +01001866 HArraySet* aset = new (allocator_) HArraySet(new_array, index, value, type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001867 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1868 AppendInstruction(aset);
1869 }
Vladimir Markob5461632018-10-15 14:24:21 +01001870 latest_result_ = new_array;
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001871
Vladimir Markob5461632018-10-15 14:24:21 +01001872 return new_array;
David Brazdildee58d62016-04-07 09:54:26 +00001873}
1874
1875template <typename T>
1876void HInstructionBuilder::BuildFillArrayData(HInstruction* object,
1877 const T* data,
1878 uint32_t element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001879 DataType::Type anticipated_type,
David Brazdildee58d62016-04-07 09:54:26 +00001880 uint32_t dex_pc) {
1881 for (uint32_t i = 0; i < element_count; ++i) {
1882 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1883 HInstruction* value = graph_->GetIntConstant(data[i], dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001884 HArraySet* aset = new (allocator_) HArraySet(object, index, value, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001885 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1886 AppendInstruction(aset);
1887 }
1888}
1889
1890void HInstructionBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
David Brazdilc120bbe2016-04-22 16:57:00 +01001891 HInstruction* array = LoadNullCheckedLocal(instruction.VRegA_31t(), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001892
1893 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
1894 const Instruction::ArrayDataPayload* payload =
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001895 reinterpret_cast<const Instruction::ArrayDataPayload*>(
1896 code_item_accessor_.Insns() + payload_offset);
David Brazdildee58d62016-04-07 09:54:26 +00001897 const uint8_t* data = payload->data;
1898 uint32_t element_count = payload->element_count;
1899
Vladimir Markoc69fba22016-09-06 16:49:15 +01001900 if (element_count == 0u) {
1901 // For empty payload we emit only the null check above.
1902 return;
1903 }
1904
Vladimir Markoca6fff82017-10-03 14:49:14 +01001905 HInstruction* length = new (allocator_) HArrayLength(array, dex_pc);
Vladimir Markoc69fba22016-09-06 16:49:15 +01001906 AppendInstruction(length);
1907
David Brazdildee58d62016-04-07 09:54:26 +00001908 // Implementation of this DEX instruction seems to be that the bounds check is
1909 // done before doing any stores.
1910 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001911 AppendInstruction(new (allocator_) HBoundsCheck(last_index, length, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001912
1913 switch (payload->element_width) {
1914 case 1:
David Brazdilc120bbe2016-04-22 16:57:00 +01001915 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001916 reinterpret_cast<const int8_t*>(data),
1917 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001918 DataType::Type::kInt8,
David Brazdildee58d62016-04-07 09:54:26 +00001919 dex_pc);
1920 break;
1921 case 2:
David Brazdilc120bbe2016-04-22 16:57:00 +01001922 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001923 reinterpret_cast<const int16_t*>(data),
1924 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001925 DataType::Type::kInt16,
David Brazdildee58d62016-04-07 09:54:26 +00001926 dex_pc);
1927 break;
1928 case 4:
David Brazdilc120bbe2016-04-22 16:57:00 +01001929 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001930 reinterpret_cast<const int32_t*>(data),
1931 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001932 DataType::Type::kInt32,
David Brazdildee58d62016-04-07 09:54:26 +00001933 dex_pc);
1934 break;
1935 case 8:
David Brazdilc120bbe2016-04-22 16:57:00 +01001936 BuildFillWideArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001937 reinterpret_cast<const int64_t*>(data),
1938 element_count,
1939 dex_pc);
1940 break;
1941 default:
1942 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1943 }
1944 graph_->SetHasBoundsChecks(true);
1945}
1946
1947void HInstructionBuilder::BuildFillWideArrayData(HInstruction* object,
1948 const int64_t* data,
1949 uint32_t element_count,
1950 uint32_t dex_pc) {
1951 for (uint32_t i = 0; i < element_count; ++i) {
1952 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1953 HInstruction* value = graph_->GetLongConstant(data[i], dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001954 HArraySet* aset =
1955 new (allocator_) HArraySet(object, index, value, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001956 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1957 AppendInstruction(aset);
1958 }
1959}
1960
Vladimir Marko28e012a2017-12-07 11:22:59 +00001961void HInstructionBuilder::BuildLoadString(dex::StringIndex string_index, uint32_t dex_pc) {
1962 HLoadString* load_string =
1963 new (allocator_) HLoadString(graph_->GetCurrentMethod(), string_index, *dex_file_, dex_pc);
1964 HSharpening::ProcessLoadString(load_string,
1965 code_generator_,
Vladimir Marko28e012a2017-12-07 11:22:59 +00001966 *dex_compilation_unit_,
1967 handles_);
1968 AppendInstruction(load_string);
1969}
1970
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001971HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index, uint32_t dex_pc) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001972 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001973 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
Vladimir Marko175e7862018-03-27 09:03:13 +00001974 Handle<mirror::Class> klass = ResolveClass(soa, type_index);
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001975 bool needs_access_check = LoadClassNeedsAccessCheck(klass);
1976 return BuildLoadClass(type_index, dex_file, klass, dex_pc, needs_access_check);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001977}
1978
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001979HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001980 const DexFile& dex_file,
1981 Handle<mirror::Class> klass,
1982 uint32_t dex_pc,
1983 bool needs_access_check) {
1984 // Try to find a reference in the compiling dex file.
1985 const DexFile* actual_dex_file = &dex_file;
1986 if (!IsSameDexFile(dex_file, *dex_compilation_unit_->GetDexFile())) {
1987 dex::TypeIndex local_type_index =
1988 klass->FindTypeIndexInOtherDexFile(*dex_compilation_unit_->GetDexFile());
1989 if (local_type_index.IsValid()) {
1990 type_index = local_type_index;
1991 actual_dex_file = dex_compilation_unit_->GetDexFile();
1992 }
1993 }
1994
1995 // Note: `klass` must be from `handles_`.
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001996 bool is_referrers_class =
1997 (klass != nullptr) && (outer_compilation_unit_->GetCompilingClass().Get() == klass.Get());
Vladimir Markoca6fff82017-10-03 14:49:14 +01001998 HLoadClass* load_class = new (allocator_) HLoadClass(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001999 graph_->GetCurrentMethod(),
2000 type_index,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002001 *actual_dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00002002 klass,
Vladimir Markofca0b492018-07-23 15:30:52 +01002003 is_referrers_class,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00002004 dex_pc,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002005 needs_access_check);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00002006
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002007 HLoadClass::LoadKind load_kind = HSharpening::ComputeLoadClassKind(load_class,
2008 code_generator_,
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002009 *dex_compilation_unit_);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002010
2011 if (load_kind == HLoadClass::LoadKind::kInvalid) {
2012 // We actually cannot reference this class, we're forced to bail.
2013 return nullptr;
2014 }
Vladimir Marko28e012a2017-12-07 11:22:59 +00002015 // Load kind must be set before inserting the instruction into the graph.
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002016 load_class->SetLoadKind(load_kind);
Vladimir Marko28e012a2017-12-07 11:22:59 +00002017 AppendInstruction(load_class);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00002018 return load_class;
2019}
2020
Vladimir Marko175e7862018-03-27 09:03:13 +00002021Handle<mirror::Class> HInstructionBuilder::ResolveClass(ScopedObjectAccess& soa,
2022 dex::TypeIndex type_index) {
Vladimir Marko3b506202018-10-31 14:33:58 +00002023 auto it = class_cache_.find(type_index);
2024 if (it != class_cache_.end()) {
2025 return it->second;
2026 }
2027
2028 ObjPtr<mirror::Class> klass = dex_compilation_unit_->GetClassLinker()->ResolveType(
2029 type_index, dex_compilation_unit_->GetDexCache(), dex_compilation_unit_->GetClassLoader());
2030 DCHECK_EQ(klass == nullptr, soa.Self()->IsExceptionPending());
2031 soa.Self()->ClearException(); // Clean up the exception left by type resolution if any.
2032
2033 Handle<mirror::Class> h_klass = handles_->NewHandle(klass);
2034 class_cache_.Put(type_index, h_klass);
2035 return h_klass;
Vladimir Marko175e7862018-03-27 09:03:13 +00002036}
2037
Vladimir Markoa2c211c2018-11-01 09:50:52 +00002038bool HInstructionBuilder::LoadClassNeedsAccessCheck(Handle<mirror::Class> klass) {
Vladimir Marko175e7862018-03-27 09:03:13 +00002039 if (klass == nullptr) {
2040 return true;
2041 } else if (klass->IsPublic()) {
2042 return false;
2043 } else {
Vladimir Markoa2c211c2018-11-01 09:50:52 +00002044 ObjPtr<mirror::Class> compiling_class = dex_compilation_unit_->GetCompilingClass().Get();
Vladimir Marko175e7862018-03-27 09:03:13 +00002045 return compiling_class == nullptr || !compiling_class->CanAccess(klass.Get());
2046 }
2047}
2048
Orion Hodson06d10a72018-05-14 08:53:38 +01002049void HInstructionBuilder::BuildLoadMethodHandle(uint16_t method_handle_index, uint32_t dex_pc) {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01002050 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
Orion Hodson06d10a72018-05-14 08:53:38 +01002051 HLoadMethodHandle* load_method_handle = new (allocator_) HLoadMethodHandle(
2052 graph_->GetCurrentMethod(), method_handle_index, dex_file, dex_pc);
Orion Hodsondbaa5c72018-05-10 08:22:46 +01002053 AppendInstruction(load_method_handle);
2054}
2055
Orion Hodson06d10a72018-05-14 08:53:38 +01002056void HInstructionBuilder::BuildLoadMethodType(dex::ProtoIndex proto_index, uint32_t dex_pc) {
Orion Hodson18259d72018-04-12 11:18:23 +01002057 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
2058 HLoadMethodType* load_method_type =
Orion Hodson06d10a72018-05-14 08:53:38 +01002059 new (allocator_) HLoadMethodType(graph_->GetCurrentMethod(), proto_index, dex_file, dex_pc);
Orion Hodson18259d72018-04-12 11:18:23 +01002060 AppendInstruction(load_method_type);
2061}
2062
David Brazdildee58d62016-04-07 09:54:26 +00002063void HInstructionBuilder::BuildTypeCheck(const Instruction& instruction,
2064 uint8_t destination,
2065 uint8_t reference,
Andreas Gampea5b09a62016-11-17 15:21:22 -08002066 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +00002067 uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002068 HInstruction* object = LoadLocal(reference, DataType::Type::kReference);
David Brazdildee58d62016-04-07 09:54:26 +00002069
Nicolas Geoffray5247c082017-01-13 14:17:29 +00002070 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko175e7862018-03-27 09:03:13 +00002071 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
2072 Handle<mirror::Class> klass = ResolveClass(soa, type_index);
Vladimir Markoa2c211c2018-11-01 09:50:52 +00002073 bool needs_access_check = LoadClassNeedsAccessCheck(klass);
Vladimir Marko175e7862018-03-27 09:03:13 +00002074 TypeCheckKind check_kind = HSharpening::ComputeTypeCheckKind(
Vladimir Markodc4bcce2018-06-21 16:15:42 +01002075 klass.Get(), code_generator_, needs_access_check);
Vladimir Marko175e7862018-03-27 09:03:13 +00002076
2077 HInstruction* class_or_null = nullptr;
2078 HIntConstant* bitstring_path_to_root = nullptr;
2079 HIntConstant* bitstring_mask = nullptr;
2080 if (check_kind == TypeCheckKind::kBitstringCheck) {
2081 // TODO: Allow using the bitstring check also if we need an access check.
2082 DCHECK(!needs_access_check);
2083 class_or_null = graph_->GetNullConstant(dex_pc);
2084 MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_);
2085 uint32_t path_to_root =
2086 SubtypeCheck<ObjPtr<mirror::Class>>::GetEncodedPathToRootForTarget(klass.Get());
2087 uint32_t mask = SubtypeCheck<ObjPtr<mirror::Class>>::GetEncodedPathToRootMask(klass.Get());
2088 bitstring_path_to_root = graph_->GetIntConstant(static_cast<int32_t>(path_to_root), dex_pc);
2089 bitstring_mask = graph_->GetIntConstant(static_cast<int32_t>(mask), dex_pc);
2090 } else {
Vladimir Markoa2c211c2018-11-01 09:50:52 +00002091 class_or_null = BuildLoadClass(type_index, dex_file, klass, dex_pc, needs_access_check);
Vladimir Marko175e7862018-03-27 09:03:13 +00002092 }
2093 DCHECK(class_or_null != nullptr);
2094
David Brazdildee58d62016-04-07 09:54:26 +00002095 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
Vladimir Marko175e7862018-03-27 09:03:13 +00002096 AppendInstruction(new (allocator_) HInstanceOf(object,
2097 class_or_null,
2098 check_kind,
2099 klass,
2100 dex_pc,
2101 allocator_,
2102 bitstring_path_to_root,
2103 bitstring_mask));
David Brazdildee58d62016-04-07 09:54:26 +00002104 UpdateLocal(destination, current_block_->GetLastInstruction());
2105 } else {
2106 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
2107 // We emit a CheckCast followed by a BoundType. CheckCast is a statement
2108 // which may throw. If it succeeds BoundType sets the new type of `object`
2109 // for all subsequent uses.
Vladimir Marko175e7862018-03-27 09:03:13 +00002110 AppendInstruction(
2111 new (allocator_) HCheckCast(object,
2112 class_or_null,
2113 check_kind,
2114 klass,
2115 dex_pc,
2116 allocator_,
2117 bitstring_path_to_root,
2118 bitstring_mask));
Vladimir Markoca6fff82017-10-03 14:49:14 +01002119 AppendInstruction(new (allocator_) HBoundType(object, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002120 UpdateLocal(reference, current_block_->GetLastInstruction());
2121 }
2122}
2123
David Brazdildee58d62016-04-07 09:54:26 +00002124bool HInstructionBuilder::CanDecodeQuickenedInfo() const {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002125 return !quicken_info_.IsNull();
David Brazdildee58d62016-04-07 09:54:26 +00002126}
2127
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002128uint16_t HInstructionBuilder::LookupQuickenedInfo(uint32_t quicken_index) {
2129 DCHECK(CanDecodeQuickenedInfo());
2130 return quicken_info_.GetData(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00002131}
2132
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002133bool HInstructionBuilder::ProcessDexInstruction(const Instruction& instruction,
2134 uint32_t dex_pc,
2135 size_t quicken_index) {
David Brazdildee58d62016-04-07 09:54:26 +00002136 switch (instruction.Opcode()) {
2137 case Instruction::CONST_4: {
2138 int32_t register_index = instruction.VRegA();
2139 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n(), dex_pc);
2140 UpdateLocal(register_index, constant);
2141 break;
2142 }
2143
2144 case Instruction::CONST_16: {
2145 int32_t register_index = instruction.VRegA();
2146 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s(), dex_pc);
2147 UpdateLocal(register_index, constant);
2148 break;
2149 }
2150
2151 case Instruction::CONST: {
2152 int32_t register_index = instruction.VRegA();
2153 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i(), dex_pc);
2154 UpdateLocal(register_index, constant);
2155 break;
2156 }
2157
2158 case Instruction::CONST_HIGH16: {
2159 int32_t register_index = instruction.VRegA();
2160 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16, dex_pc);
2161 UpdateLocal(register_index, constant);
2162 break;
2163 }
2164
2165 case Instruction::CONST_WIDE_16: {
2166 int32_t register_index = instruction.VRegA();
2167 // Get 16 bits of constant value, sign extended to 64 bits.
2168 int64_t value = instruction.VRegB_21s();
2169 value <<= 48;
2170 value >>= 48;
2171 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
2172 UpdateLocal(register_index, constant);
2173 break;
2174 }
2175
2176 case Instruction::CONST_WIDE_32: {
2177 int32_t register_index = instruction.VRegA();
2178 // Get 32 bits of constant value, sign extended to 64 bits.
2179 int64_t value = instruction.VRegB_31i();
2180 value <<= 32;
2181 value >>= 32;
2182 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
2183 UpdateLocal(register_index, constant);
2184 break;
2185 }
2186
2187 case Instruction::CONST_WIDE: {
2188 int32_t register_index = instruction.VRegA();
2189 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l(), dex_pc);
2190 UpdateLocal(register_index, constant);
2191 break;
2192 }
2193
2194 case Instruction::CONST_WIDE_HIGH16: {
2195 int32_t register_index = instruction.VRegA();
2196 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
2197 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
2198 UpdateLocal(register_index, constant);
2199 break;
2200 }
2201
2202 // Note that the SSA building will refine the types.
2203 case Instruction::MOVE:
2204 case Instruction::MOVE_FROM16:
2205 case Instruction::MOVE_16: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002206 HInstruction* value = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +00002207 UpdateLocal(instruction.VRegA(), value);
2208 break;
2209 }
2210
2211 // Note that the SSA building will refine the types.
2212 case Instruction::MOVE_WIDE:
2213 case Instruction::MOVE_WIDE_FROM16:
2214 case Instruction::MOVE_WIDE_16: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002215 HInstruction* value = LoadLocal(instruction.VRegB(), DataType::Type::kInt64);
David Brazdildee58d62016-04-07 09:54:26 +00002216 UpdateLocal(instruction.VRegA(), value);
2217 break;
2218 }
2219
2220 case Instruction::MOVE_OBJECT:
2221 case Instruction::MOVE_OBJECT_16:
2222 case Instruction::MOVE_OBJECT_FROM16: {
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01002223 // The verifier has no notion of a null type, so a move-object of constant 0
2224 // will lead to the same constant 0 in the destination register. To mimic
2225 // this behavior, we just pretend we haven't seen a type change (int to reference)
2226 // for the 0 constant and phis. We rely on our type propagation to eventually get the
2227 // types correct.
2228 uint32_t reg_number = instruction.VRegB();
2229 HInstruction* value = (*current_locals_)[reg_number];
2230 if (value->IsIntConstant()) {
2231 DCHECK_EQ(value->AsIntConstant()->GetValue(), 0);
2232 } else if (value->IsPhi()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002233 DCHECK(value->GetType() == DataType::Type::kInt32 ||
2234 value->GetType() == DataType::Type::kReference);
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01002235 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002236 value = LoadLocal(reg_number, DataType::Type::kReference);
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01002237 }
David Brazdildee58d62016-04-07 09:54:26 +00002238 UpdateLocal(instruction.VRegA(), value);
2239 break;
2240 }
2241
2242 case Instruction::RETURN_VOID_NO_BARRIER:
2243 case Instruction::RETURN_VOID: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002244 BuildReturn(instruction, DataType::Type::kVoid, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002245 break;
2246 }
2247
2248#define IF_XX(comparison, cond) \
2249 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
2250 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
2251
2252 IF_XX(HEqual, EQ);
2253 IF_XX(HNotEqual, NE);
2254 IF_XX(HLessThan, LT);
2255 IF_XX(HLessThanOrEqual, LE);
2256 IF_XX(HGreaterThan, GT);
2257 IF_XX(HGreaterThanOrEqual, GE);
2258
2259 case Instruction::GOTO:
2260 case Instruction::GOTO_16:
2261 case Instruction::GOTO_32: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002262 AppendInstruction(new (allocator_) HGoto(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002263 current_block_ = nullptr;
2264 break;
2265 }
2266
2267 case Instruction::RETURN: {
2268 BuildReturn(instruction, return_type_, dex_pc);
2269 break;
2270 }
2271
2272 case Instruction::RETURN_OBJECT: {
2273 BuildReturn(instruction, return_type_, dex_pc);
2274 break;
2275 }
2276
2277 case Instruction::RETURN_WIDE: {
2278 BuildReturn(instruction, return_type_, dex_pc);
2279 break;
2280 }
2281
2282 case Instruction::INVOKE_DIRECT:
2283 case Instruction::INVOKE_INTERFACE:
2284 case Instruction::INVOKE_STATIC:
2285 case Instruction::INVOKE_SUPER:
2286 case Instruction::INVOKE_VIRTUAL:
2287 case Instruction::INVOKE_VIRTUAL_QUICK: {
2288 uint16_t method_idx;
2289 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_QUICK) {
2290 if (!CanDecodeQuickenedInfo()) {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002291 VLOG(compiler) << "Not compiled: Could not decode quickened instruction "
2292 << instruction.Opcode();
David Brazdildee58d62016-04-07 09:54:26 +00002293 return false;
2294 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002295 method_idx = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00002296 } else {
2297 method_idx = instruction.VRegB_35c();
2298 }
David Brazdildee58d62016-04-07 09:54:26 +00002299 uint32_t args[5];
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002300 uint32_t number_of_vreg_arguments = instruction.GetVarArgs(args);
2301 VarArgsInstructionOperands operands(args, number_of_vreg_arguments);
2302 if (!BuildInvoke(instruction, dex_pc, method_idx, operands)) {
David Brazdildee58d62016-04-07 09:54:26 +00002303 return false;
2304 }
2305 break;
2306 }
2307
2308 case Instruction::INVOKE_DIRECT_RANGE:
2309 case Instruction::INVOKE_INTERFACE_RANGE:
2310 case Instruction::INVOKE_STATIC_RANGE:
2311 case Instruction::INVOKE_SUPER_RANGE:
2312 case Instruction::INVOKE_VIRTUAL_RANGE:
2313 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2314 uint16_t method_idx;
2315 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK) {
2316 if (!CanDecodeQuickenedInfo()) {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002317 VLOG(compiler) << "Not compiled: Could not decode quickened instruction "
2318 << instruction.Opcode();
David Brazdildee58d62016-04-07 09:54:26 +00002319 return false;
2320 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002321 method_idx = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00002322 } else {
2323 method_idx = instruction.VRegB_3rc();
2324 }
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002325 RangeInstructionOperands operands(instruction.VRegC(), instruction.VRegA_3rc());
2326 if (!BuildInvoke(instruction, dex_pc, method_idx, operands)) {
David Brazdildee58d62016-04-07 09:54:26 +00002327 return false;
2328 }
2329 break;
2330 }
2331
Orion Hodsonac141392017-01-13 11:53:47 +00002332 case Instruction::INVOKE_POLYMORPHIC: {
2333 uint16_t method_idx = instruction.VRegB_45cc();
Orion Hodson06d10a72018-05-14 08:53:38 +01002334 dex::ProtoIndex proto_idx(instruction.VRegH_45cc());
Orion Hodsonac141392017-01-13 11:53:47 +00002335 uint32_t args[5];
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002336 uint32_t number_of_vreg_arguments = instruction.GetVarArgs(args);
2337 VarArgsInstructionOperands operands(args, number_of_vreg_arguments);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002338 return BuildInvokePolymorphic(dex_pc, method_idx, proto_idx, operands);
Orion Hodsonac141392017-01-13 11:53:47 +00002339 }
2340
2341 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
2342 uint16_t method_idx = instruction.VRegB_4rcc();
Orion Hodson06d10a72018-05-14 08:53:38 +01002343 dex::ProtoIndex proto_idx(instruction.VRegH_4rcc());
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002344 RangeInstructionOperands operands(instruction.VRegC_4rcc(), instruction.VRegA_4rcc());
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002345 return BuildInvokePolymorphic(dex_pc, method_idx, proto_idx, operands);
2346 }
2347
2348 case Instruction::INVOKE_CUSTOM: {
2349 uint16_t call_site_idx = instruction.VRegB_35c();
2350 uint32_t args[5];
2351 uint32_t number_of_vreg_arguments = instruction.GetVarArgs(args);
2352 VarArgsInstructionOperands operands(args, number_of_vreg_arguments);
2353 return BuildInvokeCustom(dex_pc, call_site_idx, operands);
2354 }
2355
2356 case Instruction::INVOKE_CUSTOM_RANGE: {
2357 uint16_t call_site_idx = instruction.VRegB_3rc();
2358 RangeInstructionOperands operands(instruction.VRegC_3rc(), instruction.VRegA_3rc());
2359 return BuildInvokeCustom(dex_pc, call_site_idx, operands);
Orion Hodsonac141392017-01-13 11:53:47 +00002360 }
2361
David Brazdildee58d62016-04-07 09:54:26 +00002362 case Instruction::NEG_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002363 Unop_12x<HNeg>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002364 break;
2365 }
2366
2367 case Instruction::NEG_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002368 Unop_12x<HNeg>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002369 break;
2370 }
2371
2372 case Instruction::NEG_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002373 Unop_12x<HNeg>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002374 break;
2375 }
2376
2377 case Instruction::NEG_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002378 Unop_12x<HNeg>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002379 break;
2380 }
2381
2382 case Instruction::NOT_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002383 Unop_12x<HNot>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002384 break;
2385 }
2386
2387 case Instruction::NOT_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002388 Unop_12x<HNot>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002389 break;
2390 }
2391
2392 case Instruction::INT_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002393 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002394 break;
2395 }
2396
2397 case Instruction::INT_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002398 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002399 break;
2400 }
2401
2402 case Instruction::INT_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002403 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002404 break;
2405 }
2406
2407 case Instruction::LONG_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002408 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002409 break;
2410 }
2411
2412 case Instruction::LONG_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002413 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002414 break;
2415 }
2416
2417 case Instruction::LONG_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002418 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002419 break;
2420 }
2421
2422 case Instruction::FLOAT_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002423 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002424 break;
2425 }
2426
2427 case Instruction::FLOAT_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002428 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002429 break;
2430 }
2431
2432 case Instruction::FLOAT_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002433 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002434 break;
2435 }
2436
2437 case Instruction::DOUBLE_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002438 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002439 break;
2440 }
2441
2442 case Instruction::DOUBLE_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002443 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002444 break;
2445 }
2446
2447 case Instruction::DOUBLE_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002448 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002449 break;
2450 }
2451
2452 case Instruction::INT_TO_BYTE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002453 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt8, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002454 break;
2455 }
2456
2457 case Instruction::INT_TO_SHORT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002458 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt16, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002459 break;
2460 }
2461
2462 case Instruction::INT_TO_CHAR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002463 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kUint16, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002464 break;
2465 }
2466
2467 case Instruction::ADD_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002468 Binop_23x<HAdd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002469 break;
2470 }
2471
2472 case Instruction::ADD_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002473 Binop_23x<HAdd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002474 break;
2475 }
2476
2477 case Instruction::ADD_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002478 Binop_23x<HAdd>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002479 break;
2480 }
2481
2482 case Instruction::ADD_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002483 Binop_23x<HAdd>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002484 break;
2485 }
2486
2487 case Instruction::SUB_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002488 Binop_23x<HSub>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002489 break;
2490 }
2491
2492 case Instruction::SUB_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002493 Binop_23x<HSub>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002494 break;
2495 }
2496
2497 case Instruction::SUB_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002498 Binop_23x<HSub>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002499 break;
2500 }
2501
2502 case Instruction::SUB_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002503 Binop_23x<HSub>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002504 break;
2505 }
2506
2507 case Instruction::ADD_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002508 Binop_12x<HAdd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002509 break;
2510 }
2511
2512 case Instruction::MUL_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002513 Binop_23x<HMul>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002514 break;
2515 }
2516
2517 case Instruction::MUL_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002518 Binop_23x<HMul>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002519 break;
2520 }
2521
2522 case Instruction::MUL_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002523 Binop_23x<HMul>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002524 break;
2525 }
2526
2527 case Instruction::MUL_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002528 Binop_23x<HMul>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002529 break;
2530 }
2531
2532 case Instruction::DIV_INT: {
2533 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002534 dex_pc, DataType::Type::kInt32, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002535 break;
2536 }
2537
2538 case Instruction::DIV_LONG: {
2539 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002540 dex_pc, DataType::Type::kInt64, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002541 break;
2542 }
2543
2544 case Instruction::DIV_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002545 Binop_23x<HDiv>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002546 break;
2547 }
2548
2549 case Instruction::DIV_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002550 Binop_23x<HDiv>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002551 break;
2552 }
2553
2554 case Instruction::REM_INT: {
2555 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002556 dex_pc, DataType::Type::kInt32, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002557 break;
2558 }
2559
2560 case Instruction::REM_LONG: {
2561 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002562 dex_pc, DataType::Type::kInt64, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002563 break;
2564 }
2565
2566 case Instruction::REM_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002567 Binop_23x<HRem>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002568 break;
2569 }
2570
2571 case Instruction::REM_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002572 Binop_23x<HRem>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002573 break;
2574 }
2575
2576 case Instruction::AND_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002577 Binop_23x<HAnd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002578 break;
2579 }
2580
2581 case Instruction::AND_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002582 Binop_23x<HAnd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002583 break;
2584 }
2585
2586 case Instruction::SHL_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002587 Binop_23x_shift<HShl>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002588 break;
2589 }
2590
2591 case Instruction::SHL_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002592 Binop_23x_shift<HShl>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002593 break;
2594 }
2595
2596 case Instruction::SHR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002597 Binop_23x_shift<HShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002598 break;
2599 }
2600
2601 case Instruction::SHR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002602 Binop_23x_shift<HShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002603 break;
2604 }
2605
2606 case Instruction::USHR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002607 Binop_23x_shift<HUShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002608 break;
2609 }
2610
2611 case Instruction::USHR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002612 Binop_23x_shift<HUShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002613 break;
2614 }
2615
2616 case Instruction::OR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002617 Binop_23x<HOr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002618 break;
2619 }
2620
2621 case Instruction::OR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002622 Binop_23x<HOr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002623 break;
2624 }
2625
2626 case Instruction::XOR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002627 Binop_23x<HXor>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002628 break;
2629 }
2630
2631 case Instruction::XOR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002632 Binop_23x<HXor>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002633 break;
2634 }
2635
2636 case Instruction::ADD_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002637 Binop_12x<HAdd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002638 break;
2639 }
2640
2641 case Instruction::ADD_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002642 Binop_12x<HAdd>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002643 break;
2644 }
2645
2646 case Instruction::ADD_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002647 Binop_12x<HAdd>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002648 break;
2649 }
2650
2651 case Instruction::SUB_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002652 Binop_12x<HSub>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002653 break;
2654 }
2655
2656 case Instruction::SUB_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002657 Binop_12x<HSub>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002658 break;
2659 }
2660
2661 case Instruction::SUB_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002662 Binop_12x<HSub>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002663 break;
2664 }
2665
2666 case Instruction::SUB_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002667 Binop_12x<HSub>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002668 break;
2669 }
2670
2671 case Instruction::MUL_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002672 Binop_12x<HMul>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002673 break;
2674 }
2675
2676 case Instruction::MUL_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002677 Binop_12x<HMul>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002678 break;
2679 }
2680
2681 case Instruction::MUL_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002682 Binop_12x<HMul>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002683 break;
2684 }
2685
2686 case Instruction::MUL_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002687 Binop_12x<HMul>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002688 break;
2689 }
2690
2691 case Instruction::DIV_INT_2ADDR: {
2692 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002693 dex_pc, DataType::Type::kInt32, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002694 break;
2695 }
2696
2697 case Instruction::DIV_LONG_2ADDR: {
2698 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002699 dex_pc, DataType::Type::kInt64, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002700 break;
2701 }
2702
2703 case Instruction::REM_INT_2ADDR: {
2704 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002705 dex_pc, DataType::Type::kInt32, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002706 break;
2707 }
2708
2709 case Instruction::REM_LONG_2ADDR: {
2710 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002711 dex_pc, DataType::Type::kInt64, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002712 break;
2713 }
2714
2715 case Instruction::REM_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002716 Binop_12x<HRem>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002717 break;
2718 }
2719
2720 case Instruction::REM_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002721 Binop_12x<HRem>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002722 break;
2723 }
2724
2725 case Instruction::SHL_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002726 Binop_12x_shift<HShl>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002727 break;
2728 }
2729
2730 case Instruction::SHL_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002731 Binop_12x_shift<HShl>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002732 break;
2733 }
2734
2735 case Instruction::SHR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002736 Binop_12x_shift<HShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002737 break;
2738 }
2739
2740 case Instruction::SHR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002741 Binop_12x_shift<HShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002742 break;
2743 }
2744
2745 case Instruction::USHR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002746 Binop_12x_shift<HUShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002747 break;
2748 }
2749
2750 case Instruction::USHR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002751 Binop_12x_shift<HUShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002752 break;
2753 }
2754
2755 case Instruction::DIV_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002756 Binop_12x<HDiv>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002757 break;
2758 }
2759
2760 case Instruction::DIV_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002761 Binop_12x<HDiv>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002762 break;
2763 }
2764
2765 case Instruction::AND_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002766 Binop_12x<HAnd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002767 break;
2768 }
2769
2770 case Instruction::AND_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002771 Binop_12x<HAnd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002772 break;
2773 }
2774
2775 case Instruction::OR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002776 Binop_12x<HOr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002777 break;
2778 }
2779
2780 case Instruction::OR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002781 Binop_12x<HOr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002782 break;
2783 }
2784
2785 case Instruction::XOR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002786 Binop_12x<HXor>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002787 break;
2788 }
2789
2790 case Instruction::XOR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002791 Binop_12x<HXor>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002792 break;
2793 }
2794
2795 case Instruction::ADD_INT_LIT16: {
2796 Binop_22s<HAdd>(instruction, false, dex_pc);
2797 break;
2798 }
2799
2800 case Instruction::AND_INT_LIT16: {
2801 Binop_22s<HAnd>(instruction, false, dex_pc);
2802 break;
2803 }
2804
2805 case Instruction::OR_INT_LIT16: {
2806 Binop_22s<HOr>(instruction, false, dex_pc);
2807 break;
2808 }
2809
2810 case Instruction::XOR_INT_LIT16: {
2811 Binop_22s<HXor>(instruction, false, dex_pc);
2812 break;
2813 }
2814
2815 case Instruction::RSUB_INT: {
2816 Binop_22s<HSub>(instruction, true, dex_pc);
2817 break;
2818 }
2819
2820 case Instruction::MUL_INT_LIT16: {
2821 Binop_22s<HMul>(instruction, false, dex_pc);
2822 break;
2823 }
2824
2825 case Instruction::ADD_INT_LIT8: {
2826 Binop_22b<HAdd>(instruction, false, dex_pc);
2827 break;
2828 }
2829
2830 case Instruction::AND_INT_LIT8: {
2831 Binop_22b<HAnd>(instruction, false, dex_pc);
2832 break;
2833 }
2834
2835 case Instruction::OR_INT_LIT8: {
2836 Binop_22b<HOr>(instruction, false, dex_pc);
2837 break;
2838 }
2839
2840 case Instruction::XOR_INT_LIT8: {
2841 Binop_22b<HXor>(instruction, false, dex_pc);
2842 break;
2843 }
2844
2845 case Instruction::RSUB_INT_LIT8: {
2846 Binop_22b<HSub>(instruction, true, dex_pc);
2847 break;
2848 }
2849
2850 case Instruction::MUL_INT_LIT8: {
2851 Binop_22b<HMul>(instruction, false, dex_pc);
2852 break;
2853 }
2854
2855 case Instruction::DIV_INT_LIT16:
2856 case Instruction::DIV_INT_LIT8: {
2857 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002858 dex_pc, DataType::Type::kInt32, true, true);
David Brazdildee58d62016-04-07 09:54:26 +00002859 break;
2860 }
2861
2862 case Instruction::REM_INT_LIT16:
2863 case Instruction::REM_INT_LIT8: {
2864 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002865 dex_pc, DataType::Type::kInt32, true, false);
David Brazdildee58d62016-04-07 09:54:26 +00002866 break;
2867 }
2868
2869 case Instruction::SHL_INT_LIT8: {
2870 Binop_22b<HShl>(instruction, false, dex_pc);
2871 break;
2872 }
2873
2874 case Instruction::SHR_INT_LIT8: {
2875 Binop_22b<HShr>(instruction, false, dex_pc);
2876 break;
2877 }
2878
2879 case Instruction::USHR_INT_LIT8: {
2880 Binop_22b<HUShr>(instruction, false, dex_pc);
2881 break;
2882 }
2883
2884 case Instruction::NEW_INSTANCE: {
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002885 HNewInstance* new_instance =
2886 BuildNewInstance(dex::TypeIndex(instruction.VRegB_21c()), dex_pc);
2887 DCHECK(new_instance != nullptr);
2888
David Brazdildee58d62016-04-07 09:54:26 +00002889 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002890 BuildConstructorFenceForAllocation(new_instance);
David Brazdildee58d62016-04-07 09:54:26 +00002891 break;
2892 }
2893
2894 case Instruction::NEW_ARRAY: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002895 dex::TypeIndex type_index(instruction.VRegC_22c());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002896 HInstruction* length = LoadLocal(instruction.VRegB_22c(), DataType::Type::kInt32);
Vladimir Markob5461632018-10-15 14:24:21 +01002897 HNewArray* new_array = BuildNewArray(dex_pc, type_index, length);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002898
David Brazdildee58d62016-04-07 09:54:26 +00002899 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002900 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002901 break;
2902 }
2903
2904 case Instruction::FILLED_NEW_ARRAY: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002905 dex::TypeIndex type_index(instruction.VRegB_35c());
David Brazdildee58d62016-04-07 09:54:26 +00002906 uint32_t args[5];
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002907 uint32_t number_of_vreg_arguments = instruction.GetVarArgs(args);
2908 VarArgsInstructionOperands operands(args, number_of_vreg_arguments);
2909 HNewArray* new_array = BuildFilledNewArray(dex_pc, type_index, operands);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002910 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002911 break;
2912 }
2913
2914 case Instruction::FILLED_NEW_ARRAY_RANGE: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002915 dex::TypeIndex type_index(instruction.VRegB_3rc());
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002916 RangeInstructionOperands operands(instruction.VRegC_3rc(), instruction.VRegA_3rc());
2917 HNewArray* new_array = BuildFilledNewArray(dex_pc, type_index, operands);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002918 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002919 break;
2920 }
2921
2922 case Instruction::FILL_ARRAY_DATA: {
2923 BuildFillArrayData(instruction, dex_pc);
2924 break;
2925 }
2926
2927 case Instruction::MOVE_RESULT:
2928 case Instruction::MOVE_RESULT_WIDE:
2929 case Instruction::MOVE_RESULT_OBJECT: {
2930 DCHECK(latest_result_ != nullptr);
2931 UpdateLocal(instruction.VRegA(), latest_result_);
2932 latest_result_ = nullptr;
2933 break;
2934 }
2935
2936 case Instruction::CMP_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002937 Binop_23x_cmp(instruction, DataType::Type::kInt64, ComparisonBias::kNoBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002938 break;
2939 }
2940
2941 case Instruction::CMPG_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002942 Binop_23x_cmp(instruction, DataType::Type::kFloat32, ComparisonBias::kGtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002943 break;
2944 }
2945
2946 case Instruction::CMPG_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002947 Binop_23x_cmp(instruction, DataType::Type::kFloat64, ComparisonBias::kGtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002948 break;
2949 }
2950
2951 case Instruction::CMPL_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002952 Binop_23x_cmp(instruction, DataType::Type::kFloat32, ComparisonBias::kLtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002953 break;
2954 }
2955
2956 case Instruction::CMPL_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002957 Binop_23x_cmp(instruction, DataType::Type::kFloat64, ComparisonBias::kLtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002958 break;
2959 }
2960
2961 case Instruction::NOP:
2962 break;
2963
2964 case Instruction::IGET:
2965 case Instruction::IGET_QUICK:
2966 case Instruction::IGET_WIDE:
2967 case Instruction::IGET_WIDE_QUICK:
2968 case Instruction::IGET_OBJECT:
2969 case Instruction::IGET_OBJECT_QUICK:
2970 case Instruction::IGET_BOOLEAN:
2971 case Instruction::IGET_BOOLEAN_QUICK:
2972 case Instruction::IGET_BYTE:
2973 case Instruction::IGET_BYTE_QUICK:
2974 case Instruction::IGET_CHAR:
2975 case Instruction::IGET_CHAR_QUICK:
2976 case Instruction::IGET_SHORT:
2977 case Instruction::IGET_SHORT_QUICK: {
Andreas Gampe3db70682018-12-26 15:12:03 -08002978 if (!BuildInstanceFieldAccess(instruction, dex_pc, /* is_put= */ false, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00002979 return false;
2980 }
2981 break;
2982 }
2983
2984 case Instruction::IPUT:
2985 case Instruction::IPUT_QUICK:
2986 case Instruction::IPUT_WIDE:
2987 case Instruction::IPUT_WIDE_QUICK:
2988 case Instruction::IPUT_OBJECT:
2989 case Instruction::IPUT_OBJECT_QUICK:
2990 case Instruction::IPUT_BOOLEAN:
2991 case Instruction::IPUT_BOOLEAN_QUICK:
2992 case Instruction::IPUT_BYTE:
2993 case Instruction::IPUT_BYTE_QUICK:
2994 case Instruction::IPUT_CHAR:
2995 case Instruction::IPUT_CHAR_QUICK:
2996 case Instruction::IPUT_SHORT:
2997 case Instruction::IPUT_SHORT_QUICK: {
Andreas Gampe3db70682018-12-26 15:12:03 -08002998 if (!BuildInstanceFieldAccess(instruction, dex_pc, /* is_put= */ true, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00002999 return false;
3000 }
3001 break;
3002 }
3003
3004 case Instruction::SGET:
3005 case Instruction::SGET_WIDE:
3006 case Instruction::SGET_OBJECT:
3007 case Instruction::SGET_BOOLEAN:
3008 case Instruction::SGET_BYTE:
3009 case Instruction::SGET_CHAR:
3010 case Instruction::SGET_SHORT: {
Andreas Gampe3db70682018-12-26 15:12:03 -08003011 BuildStaticFieldAccess(instruction, dex_pc, /* is_put= */ false);
David Brazdildee58d62016-04-07 09:54:26 +00003012 break;
3013 }
3014
3015 case Instruction::SPUT:
3016 case Instruction::SPUT_WIDE:
3017 case Instruction::SPUT_OBJECT:
3018 case Instruction::SPUT_BOOLEAN:
3019 case Instruction::SPUT_BYTE:
3020 case Instruction::SPUT_CHAR:
3021 case Instruction::SPUT_SHORT: {
Andreas Gampe3db70682018-12-26 15:12:03 -08003022 BuildStaticFieldAccess(instruction, dex_pc, /* is_put= */ true);
David Brazdildee58d62016-04-07 09:54:26 +00003023 break;
3024 }
3025
3026#define ARRAY_XX(kind, anticipated_type) \
3027 case Instruction::AGET##kind: { \
3028 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
3029 break; \
3030 } \
3031 case Instruction::APUT##kind: { \
3032 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
3033 break; \
3034 }
3035
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003036 ARRAY_XX(, DataType::Type::kInt32);
3037 ARRAY_XX(_WIDE, DataType::Type::kInt64);
3038 ARRAY_XX(_OBJECT, DataType::Type::kReference);
3039 ARRAY_XX(_BOOLEAN, DataType::Type::kBool);
3040 ARRAY_XX(_BYTE, DataType::Type::kInt8);
3041 ARRAY_XX(_CHAR, DataType::Type::kUint16);
3042 ARRAY_XX(_SHORT, DataType::Type::kInt16);
David Brazdildee58d62016-04-07 09:54:26 +00003043
3044 case Instruction::ARRAY_LENGTH: {
David Brazdilc120bbe2016-04-22 16:57:00 +01003045 HInstruction* object = LoadNullCheckedLocal(instruction.VRegB_12x(), dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01003046 AppendInstruction(new (allocator_) HArrayLength(object, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00003047 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
3048 break;
3049 }
3050
3051 case Instruction::CONST_STRING: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08003052 dex::StringIndex string_index(instruction.VRegB_21c());
Vladimir Marko28e012a2017-12-07 11:22:59 +00003053 BuildLoadString(string_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003054 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
3055 break;
3056 }
3057
3058 case Instruction::CONST_STRING_JUMBO: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08003059 dex::StringIndex string_index(instruction.VRegB_31c());
Vladimir Marko28e012a2017-12-07 11:22:59 +00003060 BuildLoadString(string_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003061 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
3062 break;
3063 }
3064
3065 case Instruction::CONST_CLASS: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003066 dex::TypeIndex type_index(instruction.VRegB_21c());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00003067 BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003068 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
3069 break;
3070 }
3071
Orion Hodsondbaa5c72018-05-10 08:22:46 +01003072 case Instruction::CONST_METHOD_HANDLE: {
3073 uint16_t method_handle_idx = instruction.VRegB_21c();
3074 BuildLoadMethodHandle(method_handle_idx, dex_pc);
3075 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
3076 break;
3077 }
3078
Orion Hodson18259d72018-04-12 11:18:23 +01003079 case Instruction::CONST_METHOD_TYPE: {
Orion Hodson06d10a72018-05-14 08:53:38 +01003080 dex::ProtoIndex proto_idx(instruction.VRegB_21c());
Orion Hodson18259d72018-04-12 11:18:23 +01003081 BuildLoadMethodType(proto_idx, dex_pc);
3082 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
3083 break;
3084 }
3085
David Brazdildee58d62016-04-07 09:54:26 +00003086 case Instruction::MOVE_EXCEPTION: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003087 AppendInstruction(new (allocator_) HLoadException(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00003088 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
Vladimir Markoca6fff82017-10-03 14:49:14 +01003089 AppendInstruction(new (allocator_) HClearException(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00003090 break;
3091 }
3092
3093 case Instruction::THROW: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003094 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01003095 AppendInstruction(new (allocator_) HThrow(exception, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00003096 // We finished building this block. Set the current block to null to avoid
3097 // adding dead instructions to it.
3098 current_block_ = nullptr;
3099 break;
3100 }
3101
3102 case Instruction::INSTANCE_OF: {
3103 uint8_t destination = instruction.VRegA_22c();
3104 uint8_t reference = instruction.VRegB_22c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08003105 dex::TypeIndex type_index(instruction.VRegC_22c());
David Brazdildee58d62016-04-07 09:54:26 +00003106 BuildTypeCheck(instruction, destination, reference, type_index, dex_pc);
3107 break;
3108 }
3109
3110 case Instruction::CHECK_CAST: {
3111 uint8_t reference = instruction.VRegA_21c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08003112 dex::TypeIndex type_index(instruction.VRegB_21c());
David Brazdildee58d62016-04-07 09:54:26 +00003113 BuildTypeCheck(instruction, -1, reference, type_index, dex_pc);
3114 break;
3115 }
3116
3117 case Instruction::MONITOR_ENTER: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003118 AppendInstruction(new (allocator_) HMonitorOperation(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003119 LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference),
David Brazdildee58d62016-04-07 09:54:26 +00003120 HMonitorOperation::OperationKind::kEnter,
3121 dex_pc));
Artem Serov2808be82018-12-20 19:15:11 +00003122 graph_->SetHasMonitorOperations(true);
David Brazdildee58d62016-04-07 09:54:26 +00003123 break;
3124 }
3125
3126 case Instruction::MONITOR_EXIT: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003127 AppendInstruction(new (allocator_) HMonitorOperation(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003128 LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference),
David Brazdildee58d62016-04-07 09:54:26 +00003129 HMonitorOperation::OperationKind::kExit,
3130 dex_pc));
Artem Serov2808be82018-12-20 19:15:11 +00003131 graph_->SetHasMonitorOperations(true);
David Brazdildee58d62016-04-07 09:54:26 +00003132 break;
3133 }
3134
3135 case Instruction::SPARSE_SWITCH:
3136 case Instruction::PACKED_SWITCH: {
3137 BuildSwitch(instruction, dex_pc);
3138 break;
3139 }
3140
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003141 case Instruction::UNUSED_3E:
3142 case Instruction::UNUSED_3F:
3143 case Instruction::UNUSED_40:
3144 case Instruction::UNUSED_41:
3145 case Instruction::UNUSED_42:
3146 case Instruction::UNUSED_43:
3147 case Instruction::UNUSED_79:
3148 case Instruction::UNUSED_7A:
3149 case Instruction::UNUSED_F3:
3150 case Instruction::UNUSED_F4:
3151 case Instruction::UNUSED_F5:
3152 case Instruction::UNUSED_F6:
3153 case Instruction::UNUSED_F7:
3154 case Instruction::UNUSED_F8:
3155 case Instruction::UNUSED_F9: {
David Brazdildee58d62016-04-07 09:54:26 +00003156 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07003157 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00003158 << " because of unhandled instruction "
3159 << instruction.Name();
Igor Murashkin1e065a52017-08-09 13:20:34 -07003160 MaybeRecordStat(compilation_stats_,
3161 MethodCompilationStat::kNotCompiledUnhandledInstruction);
David Brazdildee58d62016-04-07 09:54:26 +00003162 return false;
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003163 }
David Brazdildee58d62016-04-07 09:54:26 +00003164 }
3165 return true;
3166} // NOLINT(readability/fn_size)
3167
Vladimir Marko8d6768d2017-03-14 10:13:21 +00003168ObjPtr<mirror::Class> HInstructionBuilder::LookupResolvedType(
3169 dex::TypeIndex type_index,
3170 const DexCompilationUnit& compilation_unit) const {
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003171 return compilation_unit.GetClassLinker()->LookupResolvedType(
Vladimir Marko8d6768d2017-03-14 10:13:21 +00003172 type_index, compilation_unit.GetDexCache().Get(), compilation_unit.GetClassLoader().Get());
3173}
3174
3175ObjPtr<mirror::Class> HInstructionBuilder::LookupReferrerClass() const {
3176 // TODO: Cache the result in a Handle<mirror::Class>.
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003177 const dex::MethodId& method_id =
Vladimir Marko8d6768d2017-03-14 10:13:21 +00003178 dex_compilation_unit_->GetDexFile()->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
3179 return LookupResolvedType(method_id.class_idx_, *dex_compilation_unit_);
3180}
3181
David Brazdildee58d62016-04-07 09:54:26 +00003182} // namespace art