blob: 3401e651634a498da38b21f3c1f198df3c7da476 [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"
Nicolas Geoffray396198b2020-06-16 12:02:45 +010037#include "reflective_handle_scope-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070038#include "scoped_thread_state_change-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070039#include "sharpening.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010040#include "ssa_builder.h"
Andreas Gampea7c83ac2017-09-11 08:14:23 -070041#include "well_known_classes.h"
David Brazdildee58d62016-04-07 09:54:26 +000042
Vladimir Marko0a516052019-10-14 13:00:44 +000043namespace art {
David Brazdildee58d62016-04-07 09:54:26 +000044
Vladimir Markocfd65802020-08-18 09:29:51 +010045namespace {
46
47class SamePackageCompare {
48 public:
49 explicit SamePackageCompare(const DexCompilationUnit& dex_compilation_unit)
50 : dex_compilation_unit_(dex_compilation_unit) {}
51
52 bool operator()(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) {
53 if (klass->GetClassLoader() != dex_compilation_unit_.GetClassLoader().Get()) {
54 return false;
55 }
56 if (referrers_descriptor_ == nullptr) {
57 const DexFile* dex_file = dex_compilation_unit_.GetDexFile();
58 uint32_t referrers_method_idx = dex_compilation_unit_.GetDexMethodIndex();
59 referrers_descriptor_ =
60 dex_file->StringByTypeIdx(dex_file->GetMethodId(referrers_method_idx).class_idx_);
61 referrers_package_length_ = PackageLength(referrers_descriptor_);
62 }
63 std::string temp;
64 const char* klass_descriptor = klass->GetDescriptor(&temp);
65 size_t klass_package_length = PackageLength(klass_descriptor);
66 return (referrers_package_length_ == klass_package_length) &&
67 memcmp(referrers_descriptor_, klass_descriptor, referrers_package_length_) == 0;
68 };
69
70 private:
71 static size_t PackageLength(const char* descriptor) {
72 const char* slash_pos = strrchr(descriptor, '/');
73 return (slash_pos != nullptr) ? static_cast<size_t>(slash_pos - descriptor) : 0u;
74 }
75
76 const DexCompilationUnit& dex_compilation_unit_;
77 const char* referrers_descriptor_ = nullptr;
78 size_t referrers_package_length_ = 0u;
79};
80
81} // anonymous namespace
82
Mathieu Chartier808c7a52017-12-15 11:19:33 -080083HInstructionBuilder::HInstructionBuilder(HGraph* graph,
84 HBasicBlockBuilder* block_builder,
85 SsaBuilder* ssa_builder,
86 const DexFile* dex_file,
87 const CodeItemDebugInfoAccessor& accessor,
88 DataType::Type return_type,
89 const DexCompilationUnit* dex_compilation_unit,
90 const DexCompilationUnit* outer_compilation_unit,
Mathieu Chartier808c7a52017-12-15 11:19:33 -080091 CodeGenerator* code_generator,
Mathieu Chartier210531f2018-01-12 10:15:51 -080092 ArrayRef<const uint8_t> interpreter_metadata,
Mathieu Chartier808c7a52017-12-15 11:19:33 -080093 OptimizingCompilerStats* compiler_stats,
Mathieu Chartier808c7a52017-12-15 11:19:33 -080094 ScopedArenaAllocator* local_allocator)
95 : allocator_(graph->GetAllocator()),
96 graph_(graph),
Mathieu Chartier808c7a52017-12-15 11:19:33 -080097 dex_file_(dex_file),
98 code_item_accessor_(accessor),
99 return_type_(return_type),
100 block_builder_(block_builder),
101 ssa_builder_(ssa_builder),
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800102 code_generator_(code_generator),
103 dex_compilation_unit_(dex_compilation_unit),
104 outer_compilation_unit_(outer_compilation_unit),
105 quicken_info_(interpreter_metadata),
106 compilation_stats_(compiler_stats),
107 local_allocator_(local_allocator),
108 locals_for_(local_allocator->Adapter(kArenaAllocGraphBuilder)),
109 current_block_(nullptr),
110 current_locals_(nullptr),
111 latest_result_(nullptr),
112 current_this_parameter_(nullptr),
Vladimir Marko3b506202018-10-31 14:33:58 +0000113 loop_headers_(local_allocator->Adapter(kArenaAllocGraphBuilder)),
114 class_cache_(std::less<dex::TypeIndex>(), local_allocator->Adapter(kArenaAllocGraphBuilder)) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800115 loop_headers_.reserve(kDefaultNumberOfLoops);
116}
117
David Brazdildee58d62016-04-07 09:54:26 +0000118HBasicBlock* HInstructionBuilder::FindBlockStartingAt(uint32_t dex_pc) const {
119 return block_builder_->GetBlockAt(dex_pc);
120}
121
Vladimir Marko69d310e2017-10-09 14:12:23 +0100122inline ScopedArenaVector<HInstruction*>* HInstructionBuilder::GetLocalsFor(HBasicBlock* block) {
123 ScopedArenaVector<HInstruction*>* locals = &locals_for_[block->GetBlockId()];
David Brazdildee58d62016-04-07 09:54:26 +0000124 const size_t vregs = graph_->GetNumberOfVRegs();
Mingyao Yang01b47b02017-02-03 12:09:57 -0800125 if (locals->size() == vregs) {
126 return locals;
127 }
128 return GetLocalsForWithAllocation(block, locals, vregs);
129}
David Brazdildee58d62016-04-07 09:54:26 +0000130
Vladimir Marko69d310e2017-10-09 14:12:23 +0100131ScopedArenaVector<HInstruction*>* HInstructionBuilder::GetLocalsForWithAllocation(
Mingyao Yang01b47b02017-02-03 12:09:57 -0800132 HBasicBlock* block,
Vladimir Marko69d310e2017-10-09 14:12:23 +0100133 ScopedArenaVector<HInstruction*>* locals,
Mingyao Yang01b47b02017-02-03 12:09:57 -0800134 const size_t vregs) {
135 DCHECK_NE(locals->size(), vregs);
136 locals->resize(vregs, nullptr);
137 if (block->IsCatchBlock()) {
138 // We record incoming inputs of catch phis at throwing instructions and
139 // must therefore eagerly create the phis. Phis for undefined vregs will
140 // be deleted when the first throwing instruction with the vreg undefined
141 // is encountered. Unused phis will be removed by dead phi analysis.
142 for (size_t i = 0; i < vregs; ++i) {
143 // No point in creating the catch phi if it is already undefined at
144 // the first throwing instruction.
145 HInstruction* current_local_value = (*current_locals_)[i];
146 if (current_local_value != nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100147 HPhi* phi = new (allocator_) HPhi(
148 allocator_,
Mingyao Yang01b47b02017-02-03 12:09:57 -0800149 i,
150 0,
151 current_local_value->GetType());
152 block->AddPhi(phi);
153 (*locals)[i] = phi;
David Brazdildee58d62016-04-07 09:54:26 +0000154 }
155 }
156 }
157 return locals;
158}
159
Mingyao Yang01b47b02017-02-03 12:09:57 -0800160inline HInstruction* HInstructionBuilder::ValueOfLocalAt(HBasicBlock* block, size_t local) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100161 ScopedArenaVector<HInstruction*>* locals = GetLocalsFor(block);
David Brazdildee58d62016-04-07 09:54:26 +0000162 return (*locals)[local];
163}
164
165void HInstructionBuilder::InitializeBlockLocals() {
166 current_locals_ = GetLocalsFor(current_block_);
167
168 if (current_block_->IsCatchBlock()) {
169 // Catch phis were already created and inputs collected from throwing sites.
170 if (kIsDebugBuild) {
171 // Make sure there was at least one throwing instruction which initialized
172 // locals (guaranteed by HGraphBuilder) and that all try blocks have been
173 // visited already (from HTryBoundary scoping and reverse post order).
174 bool catch_block_visited = false;
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100175 for (HBasicBlock* current : graph_->GetReversePostOrder()) {
David Brazdildee58d62016-04-07 09:54:26 +0000176 if (current == current_block_) {
177 catch_block_visited = true;
178 } else if (current->IsTryBlock()) {
179 const HTryBoundary& try_entry = current->GetTryCatchInformation()->GetTryEntry();
180 if (try_entry.HasExceptionHandler(*current_block_)) {
181 DCHECK(!catch_block_visited) << "Catch block visited before its try block.";
182 }
183 }
184 }
185 DCHECK_EQ(current_locals_->size(), graph_->GetNumberOfVRegs())
186 << "No instructions throwing into a live catch block.";
187 }
188 } else if (current_block_->IsLoopHeader()) {
189 // If the block is a loop header, we know we only have visited the pre header
190 // because we are visiting in reverse post order. We create phis for all initialized
191 // locals from the pre header. Their inputs will be populated at the end of
192 // the analysis.
193 for (size_t local = 0; local < current_locals_->size(); ++local) {
194 HInstruction* incoming =
195 ValueOfLocalAt(current_block_->GetLoopInformation()->GetPreHeader(), local);
196 if (incoming != nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100197 HPhi* phi = new (allocator_) HPhi(
198 allocator_,
David Brazdildee58d62016-04-07 09:54:26 +0000199 local,
200 0,
201 incoming->GetType());
202 current_block_->AddPhi(phi);
203 (*current_locals_)[local] = phi;
204 }
205 }
206
207 // Save the loop header so that the last phase of the analysis knows which
208 // blocks need to be updated.
209 loop_headers_.push_back(current_block_);
210 } else if (current_block_->GetPredecessors().size() > 0) {
211 // All predecessors have already been visited because we are visiting in reverse post order.
212 // We merge the values of all locals, creating phis if those values differ.
213 for (size_t local = 0; local < current_locals_->size(); ++local) {
214 bool one_predecessor_has_no_value = false;
215 bool is_different = false;
216 HInstruction* value = ValueOfLocalAt(current_block_->GetPredecessors()[0], local);
217
218 for (HBasicBlock* predecessor : current_block_->GetPredecessors()) {
219 HInstruction* current = ValueOfLocalAt(predecessor, local);
220 if (current == nullptr) {
221 one_predecessor_has_no_value = true;
222 break;
223 } else if (current != value) {
224 is_different = true;
225 }
226 }
227
228 if (one_predecessor_has_no_value) {
229 // If one predecessor has no value for this local, we trust the verifier has
230 // successfully checked that there is a store dominating any read after this block.
231 continue;
232 }
233
234 if (is_different) {
235 HInstruction* first_input = ValueOfLocalAt(current_block_->GetPredecessors()[0], local);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100236 HPhi* phi = new (allocator_) HPhi(
237 allocator_,
David Brazdildee58d62016-04-07 09:54:26 +0000238 local,
239 current_block_->GetPredecessors().size(),
240 first_input->GetType());
241 for (size_t i = 0; i < current_block_->GetPredecessors().size(); i++) {
242 HInstruction* pred_value = ValueOfLocalAt(current_block_->GetPredecessors()[i], local);
243 phi->SetRawInputAt(i, pred_value);
244 }
245 current_block_->AddPhi(phi);
246 value = phi;
247 }
248 (*current_locals_)[local] = value;
249 }
250 }
251}
252
253void HInstructionBuilder::PropagateLocalsToCatchBlocks() {
254 const HTryBoundary& try_entry = current_block_->GetTryCatchInformation()->GetTryEntry();
255 for (HBasicBlock* catch_block : try_entry.GetExceptionHandlers()) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100256 ScopedArenaVector<HInstruction*>* handler_locals = GetLocalsFor(catch_block);
David Brazdildee58d62016-04-07 09:54:26 +0000257 DCHECK_EQ(handler_locals->size(), current_locals_->size());
258 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
259 HInstruction* handler_value = (*handler_locals)[vreg];
260 if (handler_value == nullptr) {
261 // Vreg was undefined at a previously encountered throwing instruction
262 // and the catch phi was deleted. Do not record the local value.
263 continue;
264 }
265 DCHECK(handler_value->IsPhi());
266
267 HInstruction* local_value = (*current_locals_)[vreg];
268 if (local_value == nullptr) {
269 // This is the first instruction throwing into `catch_block` where
270 // `vreg` is undefined. Delete the catch phi.
271 catch_block->RemovePhi(handler_value->AsPhi());
272 (*handler_locals)[vreg] = nullptr;
273 } else {
274 // Vreg has been defined at all instructions throwing into `catch_block`
275 // encountered so far. Record the local value in the catch phi.
276 handler_value->AsPhi()->AddInput(local_value);
277 }
278 }
279 }
280}
281
282void HInstructionBuilder::AppendInstruction(HInstruction* instruction) {
283 current_block_->AddInstruction(instruction);
284 InitializeInstruction(instruction);
285}
286
287void HInstructionBuilder::InsertInstructionAtTop(HInstruction* instruction) {
288 if (current_block_->GetInstructions().IsEmpty()) {
289 current_block_->AddInstruction(instruction);
290 } else {
291 current_block_->InsertInstructionBefore(instruction, current_block_->GetFirstInstruction());
292 }
293 InitializeInstruction(instruction);
294}
295
296void HInstructionBuilder::InitializeInstruction(HInstruction* instruction) {
297 if (instruction->NeedsEnvironment()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100298 HEnvironment* environment = new (allocator_) HEnvironment(
299 allocator_,
David Brazdildee58d62016-04-07 09:54:26 +0000300 current_locals_->size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000301 graph_->GetArtMethod(),
David Brazdildee58d62016-04-07 09:54:26 +0000302 instruction->GetDexPc(),
David Brazdildee58d62016-04-07 09:54:26 +0000303 instruction);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100304 environment->CopyFrom(ArrayRef<HInstruction* const>(*current_locals_));
David Brazdildee58d62016-04-07 09:54:26 +0000305 instruction->SetRawEnvironment(environment);
306 }
307}
308
David Brazdilc120bbe2016-04-22 16:57:00 +0100309HInstruction* HInstructionBuilder::LoadNullCheckedLocal(uint32_t register_index, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100310 HInstruction* ref = LoadLocal(register_index, DataType::Type::kReference);
David Brazdilc120bbe2016-04-22 16:57:00 +0100311 if (!ref->CanBeNull()) {
312 return ref;
313 }
314
Vladimir Markoca6fff82017-10-03 14:49:14 +0100315 HNullCheck* null_check = new (allocator_) HNullCheck(ref, dex_pc);
David Brazdilc120bbe2016-04-22 16:57:00 +0100316 AppendInstruction(null_check);
317 return null_check;
318}
319
David Brazdildee58d62016-04-07 09:54:26 +0000320void HInstructionBuilder::SetLoopHeaderPhiInputs() {
321 for (size_t i = loop_headers_.size(); i > 0; --i) {
322 HBasicBlock* block = loop_headers_[i - 1];
323 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
324 HPhi* phi = it.Current()->AsPhi();
325 size_t vreg = phi->GetRegNumber();
326 for (HBasicBlock* predecessor : block->GetPredecessors()) {
327 HInstruction* value = ValueOfLocalAt(predecessor, vreg);
328 if (value == nullptr) {
329 // Vreg is undefined at this predecessor. Mark it dead and leave with
330 // fewer inputs than predecessors. SsaChecker will fail if not removed.
331 phi->SetDead();
332 break;
333 } else {
334 phi->AddInput(value);
335 }
336 }
337 }
338 }
339}
340
341static bool IsBlockPopulated(HBasicBlock* block) {
342 if (block->IsLoopHeader()) {
343 // Suspend checks were inserted into loop headers during building of dominator tree.
344 DCHECK(block->GetFirstInstruction()->IsSuspendCheck());
345 return block->GetFirstInstruction() != block->GetLastInstruction();
346 } else {
347 return !block->GetInstructions().IsEmpty();
348 }
349}
350
351bool HInstructionBuilder::Build() {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800352 DCHECK(code_item_accessor_.HasCodeItem());
Vladimir Marko69d310e2017-10-09 14:12:23 +0100353 locals_for_.resize(
354 graph_->GetBlocks().size(),
355 ScopedArenaVector<HInstruction*>(local_allocator_->Adapter(kArenaAllocGraphBuilder)));
David Brazdildee58d62016-04-07 09:54:26 +0000356
357 // Find locations where we want to generate extra stackmaps for native debugging.
358 // This allows us to generate the info only at interesting points (for example,
359 // at start of java statement) rather than before every dex instruction.
Vladimir Marko3b506202018-10-31 14:33:58 +0000360 const bool native_debuggable = code_generator_ != nullptr &&
361 code_generator_->GetCompilerOptions().GetNativeDebuggable();
David Brazdildee58d62016-04-07 09:54:26 +0000362 ArenaBitVector* native_debug_info_locations = nullptr;
363 if (native_debuggable) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100364 native_debug_info_locations = FindNativeDebugInfoLocations();
David Brazdildee58d62016-04-07 09:54:26 +0000365 }
366
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100367 for (HBasicBlock* block : graph_->GetReversePostOrder()) {
368 current_block_ = block;
David Brazdildee58d62016-04-07 09:54:26 +0000369 uint32_t block_dex_pc = current_block_->GetDexPc();
370
371 InitializeBlockLocals();
372
373 if (current_block_->IsEntryBlock()) {
374 InitializeParameters();
Vladimir Markoca6fff82017-10-03 14:49:14 +0100375 AppendInstruction(new (allocator_) HSuspendCheck(0u));
376 AppendInstruction(new (allocator_) HGoto(0u));
David Brazdildee58d62016-04-07 09:54:26 +0000377 continue;
378 } else if (current_block_->IsExitBlock()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100379 AppendInstruction(new (allocator_) HExit());
David Brazdildee58d62016-04-07 09:54:26 +0000380 continue;
381 } else if (current_block_->IsLoopHeader()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100382 HSuspendCheck* suspend_check = new (allocator_) HSuspendCheck(current_block_->GetDexPc());
David Brazdildee58d62016-04-07 09:54:26 +0000383 current_block_->GetLoopInformation()->SetSuspendCheck(suspend_check);
384 // This is slightly odd because the loop header might not be empty (TryBoundary).
385 // But we're still creating the environment with locals from the top of the block.
386 InsertInstructionAtTop(suspend_check);
387 }
388
389 if (block_dex_pc == kNoDexPc || current_block_ != block_builder_->GetBlockAt(block_dex_pc)) {
390 // Synthetic block that does not need to be populated.
391 DCHECK(IsBlockPopulated(current_block_));
392 continue;
393 }
394
395 DCHECK(!IsBlockPopulated(current_block_));
396
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700397 uint32_t quicken_index = 0;
398 if (CanDecodeQuickenedInfo()) {
399 quicken_index = block_builder_->GetQuickenIndex(block_dex_pc);
400 }
401
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800402 for (const DexInstructionPcPair& pair : code_item_accessor_.InstructionsFrom(block_dex_pc)) {
David Brazdildee58d62016-04-07 09:54:26 +0000403 if (current_block_ == nullptr) {
404 // The previous instruction ended this block.
405 break;
406 }
407
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800408 const uint32_t dex_pc = pair.DexPc();
David Brazdildee58d62016-04-07 09:54:26 +0000409 if (dex_pc != block_dex_pc && FindBlockStartingAt(dex_pc) != nullptr) {
410 // This dex_pc starts a new basic block.
411 break;
412 }
413
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800414 if (current_block_->IsTryBlock() && IsThrowingDexInstruction(pair.Inst())) {
David Brazdildee58d62016-04-07 09:54:26 +0000415 PropagateLocalsToCatchBlocks();
416 }
417
418 if (native_debuggable && native_debug_info_locations->IsBitSet(dex_pc)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100419 AppendInstruction(new (allocator_) HNativeDebugInfo(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000420 }
421
Vladimir Markof91fc122020-05-13 09:21:00 +0100422 // Note: There may be no Thread for gtests.
423 DCHECK(Thread::Current() == nullptr || !Thread::Current()->IsExceptionPending())
Nicolas Geoffray605c5912020-04-08 15:12:39 +0100424 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
425 << " " << pair.Inst().Name() << "@" << dex_pc;
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800426 if (!ProcessDexInstruction(pair.Inst(), dex_pc, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +0000427 return false;
428 }
Vladimir Markof91fc122020-05-13 09:21:00 +0100429 DCHECK(Thread::Current() == nullptr || !Thread::Current()->IsExceptionPending())
Nicolas Geoffrayfbf53b52020-04-01 15:20:14 +0100430 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
Nicolas Geoffray605c5912020-04-08 15:12:39 +0100431 << " " << pair.Inst().Name() << "@" << dex_pc;
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700432
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800433 if (QuickenInfoTable::NeedsIndexForInstruction(&pair.Inst())) {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700434 ++quicken_index;
435 }
David Brazdildee58d62016-04-07 09:54:26 +0000436 }
437
438 if (current_block_ != nullptr) {
439 // Branching instructions clear current_block, so we know the last
440 // instruction of the current block is not a branching instruction.
441 // We add an unconditional Goto to the next block.
442 DCHECK_EQ(current_block_->GetSuccessors().size(), 1u);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100443 AppendInstruction(new (allocator_) HGoto());
David Brazdildee58d62016-04-07 09:54:26 +0000444 }
445 }
446
447 SetLoopHeaderPhiInputs();
448
449 return true;
450}
451
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000452void HInstructionBuilder::BuildIntrinsic(ArtMethod* method) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800453 DCHECK(!code_item_accessor_.HasCodeItem());
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000454 DCHECK(method->IsIntrinsic());
Vladimir Marko43d57552020-09-07 12:30:17 +0000455 if (kIsDebugBuild) {
456 ScopedObjectAccess soa(Thread::Current());
457 CHECK(!method->IsSignaturePolymorphic());
458 }
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000459
460 locals_for_.resize(
461 graph_->GetBlocks().size(),
462 ScopedArenaVector<HInstruction*>(local_allocator_->Adapter(kArenaAllocGraphBuilder)));
463
464 // Fill the entry block. Do not add suspend check, we do not want a suspend
465 // check in intrinsics; intrinsic methods are supposed to be fast.
466 current_block_ = graph_->GetEntryBlock();
467 InitializeBlockLocals();
468 InitializeParameters();
469 AppendInstruction(new (allocator_) HGoto(0u));
470
471 // Fill the body.
472 current_block_ = current_block_->GetSingleSuccessor();
473 InitializeBlockLocals();
474 DCHECK(!IsBlockPopulated(current_block_));
475
Vladimir Marko5f846072020-04-09 13:20:11 +0100476 // Add the intermediate representation, if available, or invoke instruction.
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000477 size_t in_vregs = graph_->GetNumberOfInVRegs();
478 size_t number_of_arguments =
479 in_vregs - std::count(current_locals_->end() - in_vregs, current_locals_->end(), nullptr);
480 uint32_t method_idx = dex_compilation_unit_->GetDexMethodIndex();
Vladimir Marko5f846072020-04-09 13:20:11 +0100481 const char* shorty = dex_file_->GetMethodShorty(method_idx);
Treehugger Robot2c5827a2018-05-17 22:26:08 +0000482 RangeInstructionOperands operands(graph_->GetNumberOfVRegs() - in_vregs, in_vregs);
Vladimir Marko5f846072020-04-09 13:20:11 +0100483 if (!BuildSimpleIntrinsic(method, kNoDexPc, operands, shorty)) {
484 // Some intrinsics without intermediate representation still yield a leaf method,
485 // so build the invoke. Use HInvokeStaticOrDirect even for methods that would
486 // normally use an HInvokeVirtual (sharpen the call).
487 MethodReference target_method(dex_file_, method_idx);
488 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
489 HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall,
490 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
491 /* method_load_data= */ 0u
492 };
493 InvokeType invoke_type = dex_compilation_unit_->IsStatic() ? kStatic : kDirect;
494 HInvokeStaticOrDirect* invoke = new (allocator_) HInvokeStaticOrDirect(
495 allocator_,
496 number_of_arguments,
497 return_type_,
498 kNoDexPc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +0100499 target_method,
Vladimir Marko5f846072020-04-09 13:20:11 +0100500 method,
501 dispatch_info,
502 invoke_type,
503 target_method,
504 HInvokeStaticOrDirect::ClinitCheckRequirement::kNone);
505 HandleInvoke(invoke, operands, shorty, /* is_unresolved= */ false);
506 }
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000507
508 // Add the return instruction.
509 if (return_type_ == DataType::Type::kVoid) {
510 AppendInstruction(new (allocator_) HReturnVoid());
511 } else {
Vladimir Marko5f846072020-04-09 13:20:11 +0100512 AppendInstruction(new (allocator_) HReturn(latest_result_));
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000513 }
514
515 // Fill the exit block.
516 DCHECK_EQ(current_block_->GetSingleSuccessor(), graph_->GetExitBlock());
517 current_block_ = graph_->GetExitBlock();
518 InitializeBlockLocals();
519 AppendInstruction(new (allocator_) HExit());
520}
521
Vladimir Marko69d310e2017-10-09 14:12:23 +0100522ArenaBitVector* HInstructionBuilder::FindNativeDebugInfoLocations() {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100523 ArenaBitVector* locations = ArenaBitVector::Create(local_allocator_,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800524 code_item_accessor_.InsnsSizeInCodeUnits(),
Andreas Gampe3db70682018-12-26 15:12:03 -0800525 /* expandable= */ false,
Vladimir Marko69d310e2017-10-09 14:12:23 +0100526 kArenaAllocGraphBuilder);
527 locations->ClearAllBits();
Mathieu Chartier3e2e1232018-09-11 12:35:30 -0700528 // The visitor gets called when the line number changes.
529 // In other words, it marks the start of new java statement.
530 code_item_accessor_.DecodeDebugPositionInfo([&](const DexFile::PositionInfo& entry) {
531 locations->SetBit(entry.address_);
532 return false;
533 });
David Brazdildee58d62016-04-07 09:54:26 +0000534 // Instruction-specific tweaks.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800535 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800536 switch (inst->Opcode()) {
David Brazdildee58d62016-04-07 09:54:26 +0000537 case Instruction::MOVE_EXCEPTION: {
538 // Stop in native debugger after the exception has been moved.
539 // The compiler also expects the move at the start of basic block so
540 // we do not want to interfere by inserting native-debug-info before it.
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800541 locations->ClearBit(inst.DexPc());
542 DexInstructionIterator next = std::next(DexInstructionIterator(inst));
543 DCHECK(next.DexPc() != inst.DexPc());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800544 if (next != code_item_accessor_.end()) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -0700545 locations->SetBit(next.DexPc());
David Brazdildee58d62016-04-07 09:54:26 +0000546 }
547 break;
548 }
549 default:
550 break;
551 }
552 }
Vladimir Marko69d310e2017-10-09 14:12:23 +0100553 return locations;
David Brazdildee58d62016-04-07 09:54:26 +0000554}
555
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100556HInstruction* HInstructionBuilder::LoadLocal(uint32_t reg_number, DataType::Type type) const {
David Brazdildee58d62016-04-07 09:54:26 +0000557 HInstruction* value = (*current_locals_)[reg_number];
558 DCHECK(value != nullptr);
559
560 // If the operation requests a specific type, we make sure its input is of that type.
561 if (type != value->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100562 if (DataType::IsFloatingPointType(type)) {
Aart Bik31883642016-06-06 15:02:44 -0700563 value = ssa_builder_->GetFloatOrDoubleEquivalent(value, type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100564 } else if (type == DataType::Type::kReference) {
Aart Bik31883642016-06-06 15:02:44 -0700565 value = ssa_builder_->GetReferenceTypeEquivalent(value);
David Brazdildee58d62016-04-07 09:54:26 +0000566 }
Aart Bik31883642016-06-06 15:02:44 -0700567 DCHECK(value != nullptr);
David Brazdildee58d62016-04-07 09:54:26 +0000568 }
569
570 return value;
571}
572
573void HInstructionBuilder::UpdateLocal(uint32_t reg_number, HInstruction* stored_value) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100574 DataType::Type stored_type = stored_value->GetType();
575 DCHECK_NE(stored_type, DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +0000576
577 // Storing into vreg `reg_number` may implicitly invalidate the surrounding
578 // registers. Consider the following cases:
579 // (1) Storing a wide value must overwrite previous values in both `reg_number`
580 // and `reg_number+1`. We store `nullptr` in `reg_number+1`.
581 // (2) If vreg `reg_number-1` holds a wide value, writing into `reg_number`
582 // must invalidate it. We store `nullptr` in `reg_number-1`.
583 // Consequently, storing a wide value into the high vreg of another wide value
584 // will invalidate both `reg_number-1` and `reg_number+1`.
585
586 if (reg_number != 0) {
587 HInstruction* local_low = (*current_locals_)[reg_number - 1];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100588 if (local_low != nullptr && DataType::Is64BitType(local_low->GetType())) {
David Brazdildee58d62016-04-07 09:54:26 +0000589 // The vreg we are storing into was previously the high vreg of a pair.
590 // We need to invalidate its low vreg.
591 DCHECK((*current_locals_)[reg_number] == nullptr);
592 (*current_locals_)[reg_number - 1] = nullptr;
593 }
594 }
595
596 (*current_locals_)[reg_number] = stored_value;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100597 if (DataType::Is64BitType(stored_type)) {
David Brazdildee58d62016-04-07 09:54:26 +0000598 // We are storing a pair. Invalidate the instruction in the high vreg.
599 (*current_locals_)[reg_number + 1] = nullptr;
600 }
601}
602
603void HInstructionBuilder::InitializeParameters() {
604 DCHECK(current_block_->IsEntryBlock());
605
Vladimir Marko69d310e2017-10-09 14:12:23 +0100606 // outer_compilation_unit_ is null only when unit testing.
607 if (outer_compilation_unit_ == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +0000608 return;
609 }
610
611 const char* shorty = dex_compilation_unit_->GetShorty();
612 uint16_t number_of_parameters = graph_->GetNumberOfInVRegs();
613 uint16_t locals_index = graph_->GetNumberOfLocalVRegs();
614 uint16_t parameter_index = 0;
615
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800616 const dex::MethodId& referrer_method_id =
David Brazdildee58d62016-04-07 09:54:26 +0000617 dex_file_->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
618 if (!dex_compilation_unit_->IsStatic()) {
619 // Add the implicit 'this' argument, not expressed in the signature.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100620 HParameterValue* parameter = new (allocator_) HParameterValue(*dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +0000621 referrer_method_id.class_idx_,
622 parameter_index++,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100623 DataType::Type::kReference,
Andreas Gampe3db70682018-12-26 15:12:03 -0800624 /* is_this= */ true);
David Brazdildee58d62016-04-07 09:54:26 +0000625 AppendInstruction(parameter);
626 UpdateLocal(locals_index++, parameter);
627 number_of_parameters--;
Igor Murashkind01745e2017-04-05 16:40:31 -0700628 current_this_parameter_ = parameter;
629 } else {
630 DCHECK(current_this_parameter_ == nullptr);
David Brazdildee58d62016-04-07 09:54:26 +0000631 }
632
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800633 const dex::ProtoId& proto = dex_file_->GetMethodPrototype(referrer_method_id);
634 const dex::TypeList* arg_types = dex_file_->GetProtoParameters(proto);
David Brazdildee58d62016-04-07 09:54:26 +0000635 for (int i = 0, shorty_pos = 1; i < number_of_parameters; i++) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100636 HParameterValue* parameter = new (allocator_) HParameterValue(
David Brazdildee58d62016-04-07 09:54:26 +0000637 *dex_file_,
638 arg_types->GetTypeItem(shorty_pos - 1).type_idx_,
639 parameter_index++,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100640 DataType::FromShorty(shorty[shorty_pos]),
Andreas Gampe3db70682018-12-26 15:12:03 -0800641 /* is_this= */ false);
David Brazdildee58d62016-04-07 09:54:26 +0000642 ++shorty_pos;
643 AppendInstruction(parameter);
644 // Store the parameter value in the local that the dex code will use
645 // to reference that parameter.
646 UpdateLocal(locals_index++, parameter);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100647 if (DataType::Is64BitType(parameter->GetType())) {
David Brazdildee58d62016-04-07 09:54:26 +0000648 i++;
649 locals_index++;
650 parameter_index++;
651 }
652 }
653}
654
655template<typename T>
656void HInstructionBuilder::If_22t(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100657 HInstruction* first = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
658 HInstruction* second = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100659 T* comparison = new (allocator_) T(first, second, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000660 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100661 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000662 current_block_ = nullptr;
663}
664
665template<typename T>
666void HInstructionBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100667 HInstruction* value = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100668 T* comparison = new (allocator_) T(value, graph_->GetIntConstant(0, dex_pc), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000669 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100670 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000671 current_block_ = nullptr;
672}
673
674template<typename T>
675void HInstructionBuilder::Unop_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100676 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000677 uint32_t dex_pc) {
678 HInstruction* first = LoadLocal(instruction.VRegB(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100679 AppendInstruction(new (allocator_) T(type, first, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000680 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
681}
682
683void HInstructionBuilder::Conversion_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100684 DataType::Type input_type,
685 DataType::Type result_type,
David Brazdildee58d62016-04-07 09:54:26 +0000686 uint32_t dex_pc) {
687 HInstruction* first = LoadLocal(instruction.VRegB(), input_type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100688 AppendInstruction(new (allocator_) HTypeConversion(result_type, first, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000689 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
690}
691
692template<typename T>
693void HInstructionBuilder::Binop_23x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100694 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000695 uint32_t dex_pc) {
696 HInstruction* first = LoadLocal(instruction.VRegB(), type);
697 HInstruction* second = LoadLocal(instruction.VRegC(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100698 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000699 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
700}
701
702template<typename T>
703void HInstructionBuilder::Binop_23x_shift(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100704 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000705 uint32_t dex_pc) {
706 HInstruction* first = LoadLocal(instruction.VRegB(), type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100707 HInstruction* second = LoadLocal(instruction.VRegC(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100708 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000709 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
710}
711
712void HInstructionBuilder::Binop_23x_cmp(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100713 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000714 ComparisonBias bias,
715 uint32_t dex_pc) {
716 HInstruction* first = LoadLocal(instruction.VRegB(), type);
717 HInstruction* second = LoadLocal(instruction.VRegC(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100718 AppendInstruction(new (allocator_) HCompare(type, first, second, bias, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000719 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
720}
721
722template<typename T>
723void HInstructionBuilder::Binop_12x_shift(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100724 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000725 uint32_t dex_pc) {
726 HInstruction* first = LoadLocal(instruction.VRegA(), type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100727 HInstruction* second = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100728 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000729 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
730}
731
732template<typename T>
733void HInstructionBuilder::Binop_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100734 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000735 uint32_t dex_pc) {
736 HInstruction* first = LoadLocal(instruction.VRegA(), type);
737 HInstruction* second = LoadLocal(instruction.VRegB(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100738 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000739 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
740}
741
742template<typename T>
743void HInstructionBuilder::Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100744 HInstruction* first = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000745 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s(), dex_pc);
746 if (reverse) {
747 std::swap(first, second);
748 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100749 AppendInstruction(new (allocator_) T(DataType::Type::kInt32, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000750 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
751}
752
753template<typename T>
754void HInstructionBuilder::Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100755 HInstruction* first = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000756 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b(), dex_pc);
757 if (reverse) {
758 std::swap(first, second);
759 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100760 AppendInstruction(new (allocator_) T(DataType::Type::kInt32, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000761 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
762}
763
Igor Murashkind01745e2017-04-05 16:40:31 -0700764// Does the method being compiled need any constructor barriers being inserted?
765// (Always 'false' for methods that aren't <init>.)
Vladimir Markoc1c34522018-10-31 13:56:49 +0000766static bool RequiresConstructorBarrier(const DexCompilationUnit* cu) {
Igor Murashkin032cacd2017-04-06 14:40:08 -0700767 // Can be null in unit tests only.
768 if (UNLIKELY(cu == nullptr)) {
769 return false;
770 }
771
Vladimir Markoc1c34522018-10-31 13:56:49 +0000772 // Constructor barriers are applicable only for <init> methods.
773 if (LIKELY(!cu->IsConstructor() || cu->IsStatic())) {
774 return false;
775 }
776
777 return cu->RequiresConstructorBarrier();
David Brazdildee58d62016-04-07 09:54:26 +0000778}
779
780// Returns true if `block` has only one successor which starts at the next
781// dex_pc after `instruction` at `dex_pc`.
782static bool IsFallthroughInstruction(const Instruction& instruction,
783 uint32_t dex_pc,
784 HBasicBlock* block) {
785 uint32_t next_dex_pc = dex_pc + instruction.SizeInCodeUnits();
786 return block->GetSingleSuccessor()->GetDexPc() == next_dex_pc;
787}
788
789void HInstructionBuilder::BuildSwitch(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100790 HInstruction* value = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000791 DexSwitchTable table(instruction, dex_pc);
792
793 if (table.GetNumEntries() == 0) {
794 // Empty Switch. Code falls through to the next block.
795 DCHECK(IsFallthroughInstruction(instruction, dex_pc, current_block_));
Vladimir Markoca6fff82017-10-03 14:49:14 +0100796 AppendInstruction(new (allocator_) HGoto(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000797 } else if (table.ShouldBuildDecisionTree()) {
798 for (DexSwitchTableIterator it(table); !it.Done(); it.Advance()) {
799 HInstruction* case_value = graph_->GetIntConstant(it.CurrentKey(), dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100800 HEqual* comparison = new (allocator_) HEqual(value, case_value, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000801 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100802 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000803
804 if (!it.IsLast()) {
805 current_block_ = FindBlockStartingAt(it.GetDexPcForCurrentIndex());
806 }
807 }
808 } else {
809 AppendInstruction(
Vladimir Markoca6fff82017-10-03 14:49:14 +0100810 new (allocator_) HPackedSwitch(table.GetEntryAt(0), table.GetNumEntries(), value, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000811 }
812
813 current_block_ = nullptr;
814}
815
816void HInstructionBuilder::BuildReturn(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100817 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000818 uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100819 if (type == DataType::Type::kVoid) {
Igor Murashkind01745e2017-04-05 16:40:31 -0700820 // Only <init> (which is a return-void) could possibly have a constructor fence.
Igor Murashkin032cacd2017-04-06 14:40:08 -0700821 // This may insert additional redundant constructor fences from the super constructors.
822 // TODO: remove redundant constructor fences (b/36656456).
Vladimir Markoc1c34522018-10-31 13:56:49 +0000823 if (RequiresConstructorBarrier(dex_compilation_unit_)) {
Igor Murashkind01745e2017-04-05 16:40:31 -0700824 // Compiling instance constructor.
Vladimir Markoba118822017-06-12 15:41:56 +0100825 DCHECK_STREQ("<init>", graph_->GetMethodName());
Igor Murashkind01745e2017-04-05 16:40:31 -0700826
827 HInstruction* fence_target = current_this_parameter_;
828 DCHECK(fence_target != nullptr);
829
Vladimir Markoca6fff82017-10-03 14:49:14 +0100830 AppendInstruction(new (allocator_) HConstructorFence(fence_target, dex_pc, allocator_));
Igor Murashkin6ef45672017-08-08 13:59:55 -0700831 MaybeRecordStat(
832 compilation_stats_,
833 MethodCompilationStat::kConstructorFenceGeneratedFinal);
David Brazdildee58d62016-04-07 09:54:26 +0000834 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100835 AppendInstruction(new (allocator_) HReturnVoid(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000836 } else {
Vladimir Markoc1c34522018-10-31 13:56:49 +0000837 DCHECK(!RequiresConstructorBarrier(dex_compilation_unit_));
David Brazdildee58d62016-04-07 09:54:26 +0000838 HInstruction* value = LoadLocal(instruction.VRegA(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100839 AppendInstruction(new (allocator_) HReturn(value, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000840 }
841 current_block_ = nullptr;
842}
843
844static InvokeType GetInvokeTypeFromOpCode(Instruction::Code opcode) {
845 switch (opcode) {
846 case Instruction::INVOKE_STATIC:
847 case Instruction::INVOKE_STATIC_RANGE:
848 return kStatic;
849 case Instruction::INVOKE_DIRECT:
850 case Instruction::INVOKE_DIRECT_RANGE:
851 return kDirect;
852 case Instruction::INVOKE_VIRTUAL:
853 case Instruction::INVOKE_VIRTUAL_QUICK:
854 case Instruction::INVOKE_VIRTUAL_RANGE:
855 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
856 return kVirtual;
857 case Instruction::INVOKE_INTERFACE:
858 case Instruction::INVOKE_INTERFACE_RANGE:
859 return kInterface;
860 case Instruction::INVOKE_SUPER_RANGE:
861 case Instruction::INVOKE_SUPER:
862 return kSuper;
863 default:
864 LOG(FATAL) << "Unexpected invoke opcode: " << opcode;
865 UNREACHABLE();
866 }
867}
868
Vladimir Marko2f40d242020-04-08 12:56:45 +0100869// Try to resolve a method using the class linker. Return null if a method could
870// not be resolved or the resolved method cannot be used for some reason.
871// Also retrieve method data needed for creating the invoke intermediate
872// representation while we hold the mutator lock here.
873static ArtMethod* ResolveMethod(uint16_t method_idx,
874 ArtMethod* referrer,
875 const DexCompilationUnit& dex_compilation_unit,
876 /*inout*/InvokeType* invoke_type,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +0100877 /*out*/MethodReference* method_info,
Vladimir Marko2f40d242020-04-08 12:56:45 +0100878 /*out*/bool* is_string_constructor) {
David Brazdildee58d62016-04-07 09:54:26 +0000879 ScopedObjectAccess soa(Thread::Current());
David Brazdildee58d62016-04-07 09:54:26 +0000880
Vladimir Marko2f40d242020-04-08 12:56:45 +0100881 ClassLinker* class_linker = dex_compilation_unit.GetClassLinker();
882 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit.GetClassLoader();
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100883
Vladimir Markoba118822017-06-12 15:41:56 +0100884 ArtMethod* resolved_method =
885 class_linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
Vladimir Markoba118822017-06-12 15:41:56 +0100886 method_idx,
Vladimir Marko2f40d242020-04-08 12:56:45 +0100887 dex_compilation_unit.GetDexCache(),
Vladimir Markoba118822017-06-12 15:41:56 +0100888 class_loader,
Vladimir Marko2f40d242020-04-08 12:56:45 +0100889 referrer,
890 *invoke_type);
David Brazdildee58d62016-04-07 09:54:26 +0000891
892 if (UNLIKELY(resolved_method == nullptr)) {
893 // Clean up any exception left by type resolution.
894 soa.Self()->ClearException();
895 return nullptr;
896 }
Nicolas Geoffray605c5912020-04-08 15:12:39 +0100897 DCHECK(!soa.Self()->IsExceptionPending());
David Brazdildee58d62016-04-07 09:54:26 +0000898
Vladimir Markoba118822017-06-12 15:41:56 +0100899 // The referrer may be unresolved for AOT if we're compiling a class that cannot be
900 // resolved because, for example, we don't find a superclass in the classpath.
Vladimir Marko2f40d242020-04-08 12:56:45 +0100901 if (referrer == nullptr) {
Vladimir Markoba118822017-06-12 15:41:56 +0100902 // The class linker cannot check access without a referrer, so we have to do it.
Vladimir Markocfd65802020-08-18 09:29:51 +0100903 // Check if the declaring class or referencing class is accessible.
904 SamePackageCompare same_package(dex_compilation_unit);
905 ObjPtr<mirror::Class> declaring_class = resolved_method->GetDeclaringClass();
906 bool declaring_class_accessible = declaring_class->IsPublic() || same_package(declaring_class);
907 if (!declaring_class_accessible) {
908 // It is possible to access members from an inaccessible superclass
909 // by referencing them through an accessible subclass.
910 ObjPtr<mirror::Class> referenced_class = class_linker->LookupResolvedType(
911 dex_compilation_unit.GetDexFile()->GetMethodId(method_idx).class_idx_,
912 dex_compilation_unit.GetDexCache().Get(),
913 class_loader.Get());
914 DCHECK(referenced_class != nullptr); // Must have been resolved when resolving the method.
915 if (!referenced_class->IsPublic() && !same_package(referenced_class)) {
916 return nullptr;
917 }
918 }
919 // Check whether the method itself is accessible.
920 // Since the referrer is unresolved but the method is resolved, it cannot be
921 // inside the same class, so a private method is known to be inaccessible.
922 // And without a resolved referrer, we cannot check for protected member access
923 // in superlass, so we handle only access to public member or within the package.
924 if (resolved_method->IsPrivate() ||
925 (!resolved_method->IsPublic() && !declaring_class_accessible)) {
David Brazdildee58d62016-04-07 09:54:26 +0000926 return nullptr;
927 }
David Brazdildee58d62016-04-07 09:54:26 +0000928 }
929
930 // We have to special case the invoke-super case, as ClassLinker::ResolveMethod does not.
931 // We need to look at the referrer's super class vtable. We need to do this to know if we need to
932 // make this an invoke-unresolved to handle cross-dex invokes or abstract super methods, both of
933 // which require runtime handling.
Vladimir Marko2f40d242020-04-08 12:56:45 +0100934 if (*invoke_type == kSuper) {
935 ObjPtr<mirror::Class> compiling_class = dex_compilation_unit.GetCompilingClass().Get();
Andreas Gampefa4333d2017-02-14 11:10:34 -0800936 if (compiling_class == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +0000937 // We could not determine the method's class we need to wait until runtime.
938 DCHECK(Runtime::Current()->IsAotCompiler());
939 return nullptr;
940 }
Vladimir Markoba118822017-06-12 15:41:56 +0100941 ObjPtr<mirror::Class> referenced_class = class_linker->LookupResolvedType(
Vladimir Marko2f40d242020-04-08 12:56:45 +0100942 dex_compilation_unit.GetDexFile()->GetMethodId(method_idx).class_idx_,
943 dex_compilation_unit.GetDexCache().Get(),
Vladimir Markoba118822017-06-12 15:41:56 +0100944 class_loader.Get());
945 DCHECK(referenced_class != nullptr); // We have already resolved a method from this class.
946 if (!referenced_class->IsAssignableFrom(compiling_class)) {
Aart Bikf663e342016-04-04 17:28:59 -0700947 // We cannot statically determine the target method. The runtime will throw a
948 // NoSuchMethodError on this one.
949 return nullptr;
950 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100951 ArtMethod* actual_method;
Vladimir Markoba118822017-06-12 15:41:56 +0100952 if (referenced_class->IsInterface()) {
953 actual_method = referenced_class->FindVirtualMethodForInterfaceSuper(
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100954 resolved_method, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000955 } else {
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100956 uint16_t vtable_index = resolved_method->GetMethodIndex();
957 actual_method = compiling_class->GetSuperClass()->GetVTableEntry(
958 vtable_index, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000959 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100960 if (actual_method != resolved_method &&
Vladimir Marko2f40d242020-04-08 12:56:45 +0100961 !IsSameDexFile(*actual_method->GetDexFile(), *dex_compilation_unit.GetDexFile())) {
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100962 // The back-end code generator relies on this check in order to ensure that it will not
963 // attempt to read the dex_cache with a dex_method_index that is not from the correct
964 // 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 +0000965 // builder, which means that the code-generator (and sharpening and inliner, maybe)
966 // might invoke an incorrect method.
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100967 // TODO: The actual method could still be referenced in the current dex file, so we
968 // could try locating it.
969 // TODO: Remove the dex_file restriction.
970 return nullptr;
971 }
972 if (!actual_method->IsInvokable()) {
973 // Fail if the actual method cannot be invoked. Otherwise, the runtime resolution stub
974 // could resolve the callee to the wrong method.
975 return nullptr;
976 }
977 resolved_method = actual_method;
David Brazdildee58d62016-04-07 09:54:26 +0000978 }
979
Vladimir Marko2f40d242020-04-08 12:56:45 +0100980 if (*invoke_type == kInterface) {
981 if (resolved_method->GetDeclaringClass()->IsObjectClass()) {
982 // If the resolved method is from j.l.Object, emit a virtual call instead.
983 // The IMT conflict stub only handles interface methods.
984 *invoke_type = kVirtual;
985 } else {
986 DCHECK(resolved_method->GetDeclaringClass()->IsInterface());
987 }
988 }
David Brazdildee58d62016-04-07 09:54:26 +0000989
Vladimir Marko2f40d242020-04-08 12:56:45 +0100990 if (*invoke_type == kDirect || *invoke_type == kStatic || *invoke_type == kSuper) {
991 // Record the target method needed for HInvokeStaticOrDirect.
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +0100992 *method_info =
Vladimir Marko2f40d242020-04-08 12:56:45 +0100993 MethodReference(resolved_method->GetDexFile(), resolved_method->GetDexMethodIndex());
994 } else if (*invoke_type == kVirtual) {
995 // For HInvokeVirtual we need the vtable index.
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +0100996 *method_info = MethodReference(/*file=*/ nullptr, resolved_method->GetVtableIndex());
Andra Danciua0130e82020-07-23 12:34:56 +0000997 } else if (*invoke_type == kInterface) {
Vladimir Marko2f40d242020-04-08 12:56:45 +0100998 // For HInvokeInterface we need the IMT index.
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +0100999 *method_info = MethodReference(/*file=*/ nullptr, ImTable::GetImtIndex(resolved_method));
Andra Danciua0130e82020-07-23 12:34:56 +00001000 } else {
1001 // For HInvokePolymorphic we don't need the target method yet
1002 DCHECK_EQ(*invoke_type, kPolymorphic);
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01001003 DCHECK(method_info == nullptr);
Vladimir Marko2f40d242020-04-08 12:56:45 +01001004 }
1005
1006 *is_string_constructor =
1007 resolved_method->IsConstructor() && resolved_method->GetDeclaringClass()->IsStringClass();
1008
1009 return resolved_method;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01001010}
1011
David Brazdildee58d62016-04-07 09:54:26 +00001012bool HInstructionBuilder::BuildInvoke(const Instruction& instruction,
1013 uint32_t dex_pc,
1014 uint32_t method_idx,
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001015 const InstructionOperands& operands) {
David Brazdildee58d62016-04-07 09:54:26 +00001016 InvokeType invoke_type = GetInvokeTypeFromOpCode(instruction.Opcode());
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001017 const char* shorty = dex_file_->GetMethodShorty(method_idx);
1018 DataType::Type return_type = DataType::FromShorty(shorty[0]);
David Brazdildee58d62016-04-07 09:54:26 +00001019
1020 // Remove the return type from the 'proto'.
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001021 size_t number_of_arguments = strlen(shorty) - 1;
David Brazdildee58d62016-04-07 09:54:26 +00001022 if (invoke_type != kStatic) { // instance call
1023 // One extra argument for 'this'.
1024 number_of_arguments++;
1025 }
1026
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01001027 MethodReference method_info(nullptr, 0u);
Vladimir Marko2f40d242020-04-08 12:56:45 +01001028 bool is_string_constructor = false;
1029 ArtMethod* resolved_method = ResolveMethod(method_idx,
1030 graph_->GetArtMethod(),
1031 *dex_compilation_unit_,
1032 &invoke_type,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01001033 &method_info,
Vladimir Marko2f40d242020-04-08 12:56:45 +01001034 &is_string_constructor);
David Brazdildee58d62016-04-07 09:54:26 +00001035
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01001036 MethodReference method_reference(&graph_->GetDexFile(), method_idx);
David Brazdildee58d62016-04-07 09:54:26 +00001037 if (UNLIKELY(resolved_method == nullptr)) {
Nicolas Geoffray605c5912020-04-08 15:12:39 +01001038 DCHECK(!Thread::Current()->IsExceptionPending());
Igor Murashkin1e065a52017-08-09 13:20:34 -07001039 MaybeRecordStat(compilation_stats_,
1040 MethodCompilationStat::kUnresolvedMethod);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001041 HInvoke* invoke = new (allocator_) HInvokeUnresolved(allocator_,
1042 number_of_arguments,
1043 return_type,
1044 dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01001045 method_reference,
Vladimir Markoca6fff82017-10-03 14:49:14 +01001046 invoke_type);
Andreas Gampe3db70682018-12-26 15:12:03 -08001047 return HandleInvoke(invoke, operands, shorty, /* is_unresolved= */ true);
David Brazdildee58d62016-04-07 09:54:26 +00001048 }
1049
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01001050 // Replace calls to String.<init> with StringFactory.
Vladimir Marko2f40d242020-04-08 12:56:45 +01001051 if (is_string_constructor) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01001052 uint32_t string_init_entry_point = WellKnownClasses::StringInitToEntryPoint(resolved_method);
1053 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
1054 HInvokeStaticOrDirect::MethodLoadKind::kStringInit,
1055 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00001056 dchecked_integral_cast<uint64_t>(string_init_entry_point)
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01001057 };
Nicolas Geoffrayf665f842018-02-15 12:29:06 +00001058 // We pass null for the resolved_method to ensure optimizations
1059 // don't rely on it.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001060 HInvoke* invoke = new (allocator_) HInvokeStaticOrDirect(
1061 allocator_,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01001062 number_of_arguments - 1,
Andreas Gampe3db70682018-12-26 15:12:03 -08001063 /* return_type= */ DataType::Type::kReference,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01001064 dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01001065 method_reference,
Andreas Gampe3db70682018-12-26 15:12:03 -08001066 /* resolved_method= */ nullptr,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01001067 dispatch_info,
1068 invoke_type,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01001069 method_info,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01001070 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001071 return HandleStringInit(invoke, operands, shorty);
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01001072 }
1073
David Brazdildee58d62016-04-07 09:54:26 +00001074 // Potential class initialization check, in the case of a static method call.
Vladimir Marko2f40d242020-04-08 12:56:45 +01001075 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement =
1076 HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
David Brazdildee58d62016-04-07 09:54:26 +00001077 HClinitCheck* clinit_check = nullptr;
Vladimir Marko2f40d242020-04-08 12:56:45 +01001078 if (invoke_type == kStatic) {
1079 clinit_check = ProcessClinitCheckForInvoke(dex_pc, resolved_method, &clinit_check_requirement);
1080 }
1081
Vladimir Marko5f846072020-04-09 13:20:11 +01001082 // Try to build an HIR replacement for the intrinsic.
1083 if (UNLIKELY(resolved_method->IsIntrinsic())) {
1084 // All intrinsics are in the primary boot image, so their class can always be referenced
1085 // and we do not need to rely on the implicit class initialization check. The class should
1086 // be initialized but we do not require that here.
1087 DCHECK_NE(clinit_check_requirement, HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit);
1088 if (BuildSimpleIntrinsic(resolved_method, dex_pc, operands, shorty)) {
1089 return true;
1090 }
1091 }
1092
David Brazdildee58d62016-04-07 09:54:26 +00001093 HInvoke* invoke = nullptr;
1094 if (invoke_type == kDirect || invoke_type == kStatic || invoke_type == kSuper) {
Vladimir Marko2f40d242020-04-08 12:56:45 +01001095 if (invoke_type == kSuper) {
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01001096 if (IsSameDexFile(*method_info.dex_file, *dex_compilation_unit_->GetDexFile())) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001097 // Update the method index to the one resolved. Note that this may be a no-op if
David Brazdildee58d62016-04-07 09:54:26 +00001098 // we resolved to the method referenced by the instruction.
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01001099 method_reference.index = method_info.index;
David Brazdildee58d62016-04-07 09:54:26 +00001100 }
1101 }
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01001102 HInvokeStaticOrDirect::DispatchInfo dispatch_info =
1103 HSharpening::SharpenInvokeStaticOrDirect(resolved_method, code_generator_);
Vladimir Markod3e9c622020-08-05 12:20:28 +01001104 if (dispatch_info.code_ptr_location ==
1105 HInvokeStaticOrDirect::CodePtrLocation::kCallCriticalNative) {
1106 graph_->SetHasDirectCriticalNativeCall(true);
1107 }
Vladimir Markoca6fff82017-10-03 14:49:14 +01001108 invoke = new (allocator_) HInvokeStaticOrDirect(allocator_,
1109 number_of_arguments,
1110 return_type,
1111 dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01001112 method_reference,
Vladimir Markoca6fff82017-10-03 14:49:14 +01001113 resolved_method,
1114 dispatch_info,
1115 invoke_type,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01001116 method_info,
Vladimir Markoca6fff82017-10-03 14:49:14 +01001117 clinit_check_requirement);
Vladimir Marko5f846072020-04-09 13:20:11 +01001118 if (clinit_check != nullptr) {
1119 // Add the class initialization check as last input of `invoke`.
1120 DCHECK_EQ(clinit_check_requirement, HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit);
1121 size_t clinit_check_index = invoke->InputCount() - 1u;
1122 DCHECK(invoke->InputAt(clinit_check_index) == nullptr);
1123 invoke->SetArgumentAt(clinit_check_index, clinit_check);
1124 }
David Brazdildee58d62016-04-07 09:54:26 +00001125 } else if (invoke_type == kVirtual) {
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01001126 DCHECK(method_info.dex_file == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001127 invoke = new (allocator_) HInvokeVirtual(allocator_,
1128 number_of_arguments,
1129 return_type,
1130 dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01001131 method_reference,
Vladimir Markoca6fff82017-10-03 14:49:14 +01001132 resolved_method,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01001133 /*vtable_index=*/ method_info.index);
David Brazdildee58d62016-04-07 09:54:26 +00001134 } else {
1135 DCHECK_EQ(invoke_type, kInterface);
Vladimir Marko2f40d242020-04-08 12:56:45 +01001136 invoke = new (allocator_) HInvokeInterface(allocator_,
Vladimir Markoca6fff82017-10-03 14:49:14 +01001137 number_of_arguments,
1138 return_type,
1139 dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01001140 method_reference,
Vladimir Markoca6fff82017-10-03 14:49:14 +01001141 resolved_method,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01001142 /*imt_index=*/ method_info.index);
David Brazdildee58d62016-04-07 09:54:26 +00001143 }
Vladimir Marko5f846072020-04-09 13:20:11 +01001144 return HandleInvoke(invoke, operands, shorty, /* is_unresolved= */ false);
David Brazdildee58d62016-04-07 09:54:26 +00001145}
1146
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001147bool HInstructionBuilder::BuildInvokePolymorphic(uint32_t dex_pc,
Orion Hodsonac141392017-01-13 11:53:47 +00001148 uint32_t method_idx,
Orion Hodson06d10a72018-05-14 08:53:38 +01001149 dex::ProtoIndex proto_idx,
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001150 const InstructionOperands& operands) {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001151 const char* shorty = dex_file_->GetShorty(proto_idx);
1152 DCHECK_EQ(1 + ArtMethod::NumArgRegisters(shorty), operands.GetNumberOfOperands());
1153 DataType::Type return_type = DataType::FromShorty(shorty[0]);
1154 size_t number_of_arguments = strlen(shorty);
Andra Danciua0130e82020-07-23 12:34:56 +00001155 // We use ResolveMethod which is also used in BuildInvoke in order to
1156 // not duplicate code. As such, we need to provide is_string_constructor
1157 // even if we don't need it afterwards.
1158 InvokeType invoke_type = InvokeType::kPolymorphic;
1159 bool is_string_constructor = false;
1160 ArtMethod* resolved_method = ResolveMethod(method_idx,
1161 graph_->GetArtMethod(),
1162 *dex_compilation_unit_,
1163 &invoke_type,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01001164 /* method_info= */ nullptr,
Andra Danciua0130e82020-07-23 12:34:56 +00001165 &is_string_constructor);
Andra Danciuaa358832020-08-25 15:09:43 +00001166
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01001167 MethodReference method_reference(&graph_->GetDexFile(), method_idx);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001168 HInvoke* invoke = new (allocator_) HInvokePolymorphic(allocator_,
1169 number_of_arguments,
1170 return_type,
1171 dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01001172 method_reference,
Andra Danciu73c31802020-09-01 13:17:05 +00001173 resolved_method,
1174 proto_idx);
Andra Danciu49a19f32020-08-27 12:44:25 +00001175 if (!HandleInvoke(invoke, operands, shorty, /* is_unresolved= */ false)) {
1176 return false;
1177 }
1178
1179 bool needs_ret_type_check =
1180 resolved_method->GetIntrinsic() == static_cast<uint32_t>(Intrinsics::kVarHandleGet) &&
1181 return_type == DataType::Type::kReference &&
Andra Danciu0875b0a2020-08-28 11:49:44 +00001182 // VarHandle.get() is only implemented for fields now.
1183 number_of_arguments < 3u;
Andra Danciu49a19f32020-08-27 12:44:25 +00001184 if (needs_ret_type_check) {
Andra Danciuaa358832020-08-25 15:09:43 +00001185 ScopedObjectAccess soa(Thread::Current());
1186 ArtMethod* referrer = graph_->GetArtMethod();
1187 dex::TypeIndex ret_type_index = referrer->GetDexFile()->GetProtoId(proto_idx).return_type_idx_;
Andra Danciuaa358832020-08-25 15:09:43 +00001188
Andra Danciu49a19f32020-08-27 12:44:25 +00001189 // Type check is needed because intrinsic implementations do not type check the retrieved
1190 // reference.
1191 BuildTypeCheck(/* is_instance_of= */ false, invoke, ret_type_index, dex_pc);
1192 latest_result_ = current_block_->GetLastInstruction();
Andra Danciuaa358832020-08-25 15:09:43 +00001193 }
1194
Andra Danciu49a19f32020-08-27 12:44:25 +00001195 return true;
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001196}
1197
1198
1199bool HInstructionBuilder::BuildInvokeCustom(uint32_t dex_pc,
1200 uint32_t call_site_idx,
1201 const InstructionOperands& operands) {
1202 dex::ProtoIndex proto_idx = dex_file_->GetProtoIndexForCallSite(call_site_idx);
1203 const char* shorty = dex_file_->GetShorty(proto_idx);
1204 DataType::Type return_type = DataType::FromShorty(shorty[0]);
1205 size_t number_of_arguments = strlen(shorty) - 1;
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01001206 // HInvokeCustom takes a DexNoNoIndex method reference.
1207 MethodReference method_reference(&graph_->GetDexFile(), dex::kDexNoIndex);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001208 HInvoke* invoke = new (allocator_) HInvokeCustom(allocator_,
1209 number_of_arguments,
1210 call_site_idx,
1211 return_type,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01001212 dex_pc,
1213 method_reference);
Andreas Gampe3db70682018-12-26 15:12:03 -08001214 return HandleInvoke(invoke, operands, shorty, /* is_unresolved= */ false);
Orion Hodsonac141392017-01-13 11:53:47 +00001215}
1216
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001217HNewInstance* HInstructionBuilder::BuildNewInstance(dex::TypeIndex type_index, uint32_t dex_pc) {
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001218 ScopedObjectAccess soa(Thread::Current());
David Brazdildee58d62016-04-07 09:54:26 +00001219
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001220 HLoadClass* load_class = BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001221
David Brazdildee58d62016-04-07 09:54:26 +00001222 HInstruction* cls = load_class;
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001223 Handle<mirror::Class> klass = load_class->GetClass();
1224
Vladimir Marko2f40d242020-04-08 12:56:45 +01001225 if (!IsInitialized(klass.Get())) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001226 cls = new (allocator_) HClinitCheck(load_class, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001227 AppendInstruction(cls);
1228 }
1229
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001230 // Only the access check entrypoint handles the finalizable class case. If we
1231 // need access checks, then we haven't resolved the method and the class may
1232 // again be finalizable.
1233 QuickEntrypointEnum entrypoint = kQuickAllocObjectInitialized;
1234 if (load_class->NeedsAccessCheck() || klass->IsFinalizable() || !klass->IsInstantiable()) {
1235 entrypoint = kQuickAllocObjectWithChecks;
1236 }
Alex Lightd109e302018-06-27 10:25:41 -07001237 // We will always be able to resolve the string class since it is in the BCP.
1238 if (!klass.IsNull() && klass->IsStringClass()) {
1239 entrypoint = kQuickAllocStringObject;
1240 }
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001241
1242 // Consider classes we haven't resolved as potentially finalizable.
Andreas Gampefa4333d2017-02-14 11:10:34 -08001243 bool finalizable = (klass == nullptr) || klass->IsFinalizable();
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001244
Vladimir Markoca6fff82017-10-03 14:49:14 +01001245 HNewInstance* new_instance = new (allocator_) HNewInstance(
David Brazdildee58d62016-04-07 09:54:26 +00001246 cls,
David Brazdildee58d62016-04-07 09:54:26 +00001247 dex_pc,
1248 type_index,
1249 *dex_compilation_unit_->GetDexFile(),
David Brazdildee58d62016-04-07 09:54:26 +00001250 finalizable,
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001251 entrypoint);
1252 AppendInstruction(new_instance);
1253
1254 return new_instance;
1255}
1256
1257void HInstructionBuilder::BuildConstructorFenceForAllocation(HInstruction* allocation) {
1258 DCHECK(allocation != nullptr &&
George Burgess IVf2072992017-05-23 15:36:41 -07001259 (allocation->IsNewInstance() ||
1260 allocation->IsNewArray())); // corresponding to "new" keyword in JLS.
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001261
1262 if (allocation->IsNewInstance()) {
1263 // STRING SPECIAL HANDLING:
1264 // -------------------------------
1265 // Strings have a real HNewInstance node but they end up always having 0 uses.
1266 // All uses of a String HNewInstance are always transformed to replace their input
1267 // of the HNewInstance with an input of the invoke to StringFactory.
1268 //
1269 // Do not emit an HConstructorFence here since it can inhibit some String new-instance
1270 // optimizations (to pass checker tests that rely on those optimizations).
1271 HNewInstance* new_inst = allocation->AsNewInstance();
1272 HLoadClass* load_class = new_inst->GetLoadClass();
1273
1274 Thread* self = Thread::Current();
1275 ScopedObjectAccess soa(self);
1276 StackHandleScope<1> hs(self);
1277 Handle<mirror::Class> klass = load_class->GetClass();
1278 if (klass != nullptr && klass->IsStringClass()) {
1279 return;
1280 // Note: Do not use allocation->IsStringAlloc which requires
1281 // a valid ReferenceTypeInfo, but that doesn't get made until after reference type
1282 // propagation (and instruction builder is too early).
1283 }
1284 // (In terms of correctness, the StringFactory needs to provide its own
1285 // default initialization barrier, see below.)
1286 }
1287
1288 // JLS 17.4.5 "Happens-before Order" describes:
1289 //
1290 // The default initialization of any object happens-before any other actions (other than
1291 // default-writes) of a program.
1292 //
1293 // In our implementation the default initialization of an object to type T means
1294 // setting all of its initial data (object[0..size)) to 0, and setting the
1295 // object's class header (i.e. object.getClass() == T.class).
1296 //
1297 // In practice this fence ensures that the writes to the object header
1298 // are visible to other threads if this object escapes the current thread.
1299 // (and in theory the 0-initializing, but that happens automatically
1300 // when new memory pages are mapped in by the OS).
1301 HConstructorFence* ctor_fence =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001302 new (allocator_) HConstructorFence(allocation, allocation->GetDexPc(), allocator_);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001303 AppendInstruction(ctor_fence);
Igor Murashkin6ef45672017-08-08 13:59:55 -07001304 MaybeRecordStat(
1305 compilation_stats_,
1306 MethodCompilationStat::kConstructorFenceGeneratedNew);
David Brazdildee58d62016-04-07 09:54:26 +00001307}
1308
Vladimir Marko2ab1bdd2018-07-12 09:59:56 +01001309static bool IsInBootImage(ObjPtr<mirror::Class> cls, const CompilerOptions& compiler_options)
1310 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko6a67bea2020-01-20 13:05:55 +00001311 if (Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(cls)) {
1312 return true;
1313 }
1314 if (compiler_options.IsBootImage() || compiler_options.IsBootImageExtension()) {
Vladimir Marko2ab1bdd2018-07-12 09:59:56 +01001315 std::string temp;
1316 const char* descriptor = cls->GetDescriptor(&temp);
1317 return compiler_options.IsImageClass(descriptor);
1318 } else {
Vladimir Marko6a67bea2020-01-20 13:05:55 +00001319 return false;
Vladimir Marko2ab1bdd2018-07-12 09:59:56 +01001320 }
1321}
1322
1323static bool IsSubClass(ObjPtr<mirror::Class> to_test, ObjPtr<mirror::Class> super_class)
1324 REQUIRES_SHARED(Locks::mutator_lock_) {
1325 return to_test != nullptr && !to_test->IsInterface() && to_test->IsSubClass(super_class);
1326}
1327
1328static bool HasTrivialClinit(ObjPtr<mirror::Class> klass, PointerSize pointer_size)
1329 REQUIRES_SHARED(Locks::mutator_lock_) {
1330 // Check if the class has encoded fields that trigger bytecode execution.
1331 // (Encoded fields are just a different representation of <clinit>.)
1332 if (klass->NumStaticFields() != 0u) {
1333 DCHECK(klass->GetClassDef() != nullptr);
1334 EncodedStaticFieldValueIterator it(klass->GetDexFile(), *klass->GetClassDef());
1335 for (; it.HasNext(); it.Next()) {
1336 switch (it.GetValueType()) {
1337 case EncodedArrayValueIterator::ValueType::kBoolean:
1338 case EncodedArrayValueIterator::ValueType::kByte:
1339 case EncodedArrayValueIterator::ValueType::kShort:
1340 case EncodedArrayValueIterator::ValueType::kChar:
1341 case EncodedArrayValueIterator::ValueType::kInt:
1342 case EncodedArrayValueIterator::ValueType::kLong:
1343 case EncodedArrayValueIterator::ValueType::kFloat:
1344 case EncodedArrayValueIterator::ValueType::kDouble:
1345 case EncodedArrayValueIterator::ValueType::kNull:
1346 case EncodedArrayValueIterator::ValueType::kString:
1347 // Primitive, null or j.l.String initialization is permitted.
1348 break;
1349 case EncodedArrayValueIterator::ValueType::kType:
1350 // Type initialization can load classes and execute bytecode through a class loader
1351 // which can execute arbitrary bytecode. We do not optimize for known class loaders;
1352 // kType is rarely used (if ever).
1353 return false;
1354 default:
1355 // Other types in the encoded static field list are rejected by the DexFileVerifier.
1356 LOG(FATAL) << "Unexpected type " << it.GetValueType();
1357 UNREACHABLE();
1358 }
1359 }
1360 }
1361 // Check if the class has <clinit> that executes arbitrary code.
1362 // Initialization of static fields of the class itself with constants is allowed.
1363 ArtMethod* clinit = klass->FindClassInitializer(pointer_size);
1364 if (clinit != nullptr) {
1365 const DexFile& dex_file = *clinit->GetDexFile();
1366 CodeItemInstructionAccessor accessor(dex_file, clinit->GetCodeItem());
1367 for (DexInstructionPcPair it : accessor) {
1368 switch (it->Opcode()) {
1369 case Instruction::CONST_4:
1370 case Instruction::CONST_16:
1371 case Instruction::CONST:
1372 case Instruction::CONST_HIGH16:
1373 case Instruction::CONST_WIDE_16:
1374 case Instruction::CONST_WIDE_32:
1375 case Instruction::CONST_WIDE:
1376 case Instruction::CONST_WIDE_HIGH16:
1377 case Instruction::CONST_STRING:
1378 case Instruction::CONST_STRING_JUMBO:
1379 // Primitive, null or j.l.String initialization is permitted.
1380 break;
1381 case Instruction::RETURN_VOID:
1382 case Instruction::RETURN_VOID_NO_BARRIER:
1383 break;
1384 case Instruction::SPUT:
1385 case Instruction::SPUT_WIDE:
1386 case Instruction::SPUT_OBJECT:
1387 case Instruction::SPUT_BOOLEAN:
1388 case Instruction::SPUT_BYTE:
1389 case Instruction::SPUT_CHAR:
1390 case Instruction::SPUT_SHORT:
1391 // Only initialization of a static field of the same class is permitted.
1392 if (dex_file.GetFieldId(it->VRegB_21c()).class_idx_ != klass->GetDexTypeIndex()) {
1393 return false;
1394 }
1395 break;
1396 case Instruction::NEW_ARRAY:
1397 // Only primitive arrays are permitted.
1398 if (Primitive::GetType(dex_file.GetTypeDescriptor(dex_file.GetTypeId(
1399 dex::TypeIndex(it->VRegC_22c())))[1]) == Primitive::kPrimNot) {
1400 return false;
1401 }
1402 break;
1403 case Instruction::APUT:
1404 case Instruction::APUT_WIDE:
1405 case Instruction::APUT_BOOLEAN:
1406 case Instruction::APUT_BYTE:
1407 case Instruction::APUT_CHAR:
1408 case Instruction::APUT_SHORT:
1409 case Instruction::FILL_ARRAY_DATA:
1410 case Instruction::NOP:
1411 // Allow initialization of primitive arrays (only constants can be stored).
1412 // Note: We expect NOPs used for fill-array-data-payload but accept all NOPs
1413 // (even unreferenced switch payloads if they make it through the verifier).
1414 break;
1415 default:
1416 return false;
1417 }
1418 }
1419 }
1420 return true;
1421}
1422
1423static bool HasTrivialInitialization(ObjPtr<mirror::Class> cls,
1424 const CompilerOptions& compiler_options)
1425 REQUIRES_SHARED(Locks::mutator_lock_) {
1426 Runtime* runtime = Runtime::Current();
1427 PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
1428
1429 // Check the superclass chain.
1430 for (ObjPtr<mirror::Class> klass = cls; klass != nullptr; klass = klass->GetSuperClass()) {
1431 if (klass->IsInitialized() && IsInBootImage(klass, compiler_options)) {
1432 break; // `klass` and its superclasses are already initialized in the boot image.
1433 }
1434 if (!HasTrivialClinit(klass, pointer_size)) {
1435 return false;
1436 }
1437 }
1438
1439 // Also check interfaces with default methods as they need to be initialized as well.
1440 ObjPtr<mirror::IfTable> iftable = cls->GetIfTable();
1441 DCHECK(iftable != nullptr);
1442 for (int32_t i = 0, count = iftable->Count(); i != count; ++i) {
1443 ObjPtr<mirror::Class> iface = iftable->GetInterface(i);
1444 if (!iface->HasDefaultMethods()) {
1445 continue; // Initializing `cls` does not initialize this interface.
1446 }
1447 if (iface->IsInitialized() && IsInBootImage(iface, compiler_options)) {
1448 continue; // This interface is already initialized in the boot image.
1449 }
1450 if (!HasTrivialClinit(iface, pointer_size)) {
1451 return false;
1452 }
1453 }
1454 return true;
1455}
1456
Vladimir Marko2f40d242020-04-08 12:56:45 +01001457bool HInstructionBuilder::IsInitialized(ObjPtr<mirror::Class> cls) const {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001458 if (cls == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +00001459 return false;
1460 }
1461
Vladimir Marko51b8aaf2017-06-09 15:17:05 +01001462 // Check if the class will be initialized at runtime.
1463 if (cls->IsInitialized()) {
Vladimir Marko695348f2020-05-19 14:42:02 +01001464 const CompilerOptions& compiler_options = code_generator_->GetCompilerOptions();
1465 if (compiler_options.IsAotCompiler()) {
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +00001466 // Assume loaded only if klass is in the boot image. App classes cannot be assumed
1467 // loaded because we don't even know what class loader will be used to load them.
Vladimir Marko695348f2020-05-19 14:42:02 +01001468 if (IsInBootImage(cls, compiler_options)) {
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +00001469 return true;
1470 }
1471 } else {
Vladimir Marko695348f2020-05-19 14:42:02 +01001472 DCHECK(compiler_options.IsJitCompiler());
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +00001473 if (Runtime::Current()->GetJit()->CanAssumeInitialized(
Vladimir Marko2f40d242020-04-08 12:56:45 +01001474 cls,
Vladimir Marko695348f2020-05-19 14:42:02 +01001475 compiler_options.IsJitCompilerForSharedCode())) {
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +00001476 // For JIT, the class cannot revert to an uninitialized state.
1477 return true;
1478 }
Vladimir Marko51b8aaf2017-06-09 15:17:05 +01001479 }
1480 }
1481
Vladimir Marko7f260d42018-10-30 18:09:55 +00001482 // We can avoid the class initialization check for `cls` in static methods and constructors
1483 // in the very same class; invoking a static method involves a class initialization check
1484 // and so does the instance allocation that must be executed before invoking a constructor.
1485 // Other instance methods of the same class can run on an escaped instance
Vladimir Marko51b8aaf2017-06-09 15:17:05 +01001486 // of an erroneous class. Even a superclass may need to be checked as the subclass
1487 // can be completely initialized while the superclass is initializing and the subclass
1488 // remains initialized when the superclass initializer throws afterwards. b/62478025
1489 // Note: The HClinitCheck+HInvokeStaticOrDirect merging can still apply.
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001490 auto is_static_method_or_constructor_of_cls = [cls](const DexCompilationUnit& compilation_unit)
1491 REQUIRES_SHARED(Locks::mutator_lock_) {
1492 return (compilation_unit.GetAccessFlags() & (kAccStatic | kAccConstructor)) != 0u &&
Vladimir Marko2f40d242020-04-08 12:56:45 +01001493 compilation_unit.GetCompilingClass().Get() == cls;
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001494 };
1495 if (is_static_method_or_constructor_of_cls(*outer_compilation_unit_) ||
1496 // Check also the innermost method. Though excessive copies of ClinitCheck can be
1497 // eliminated by GVN, that happens only after the decision whether to inline the
1498 // graph or not and that may depend on the presence of the ClinitCheck.
1499 // TODO: We should walk over the entire inlined method chain, but we don't pass that
1500 // information to the builder.
1501 is_static_method_or_constructor_of_cls(*dex_compilation_unit_)) {
Vladimir Marko2ab1bdd2018-07-12 09:59:56 +01001502 return true;
1503 }
David Brazdildee58d62016-04-07 09:54:26 +00001504
Vladimir Marko2ab1bdd2018-07-12 09:59:56 +01001505 // Otherwise, we may be able to avoid the check if `cls` is a superclass of a method being
1506 // compiled here (anywhere in the inlining chain) as the `cls` must have started initializing
1507 // before calling any `cls` or subclass methods. Static methods require a clinit check and
1508 // instance methods require an instance which cannot be created before doing a clinit check.
1509 // When a subclass of `cls` starts initializing, it starts initializing its superclass
1510 // chain up to `cls` without running any bytecode, i.e. without any opportunity for circular
1511 // initialization weirdness.
1512 //
1513 // If the initialization of `cls` is trivial (`cls` and its superclasses and superinterfaces
1514 // with default methods initialize only their own static fields using constant values), it must
1515 // complete, either successfully or by throwing and marking `cls` erroneous, without allocating
1516 // any instances of `cls` or subclasses (or any other class) and without calling any methods.
1517 // If it completes by throwing, no instances of `cls` shall be created and no subclass method
1518 // bytecode shall execute (see above), therefore the instruction we're building shall be
1519 // unreachable. By reaching the instruction, we know that `cls` was initialized successfully.
1520 //
1521 // TODO: We should walk over the entire inlined methods chain, but we don't pass that
1522 // information to the builder. (We could also check if we're guaranteed a non-null instance
1523 // of `cls` at this location but that's outside the scope of the instruction builder.)
Vladimir Marko2f40d242020-04-08 12:56:45 +01001524 bool is_subclass = IsSubClass(outer_compilation_unit_->GetCompilingClass().Get(), cls);
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001525 if (dex_compilation_unit_ != outer_compilation_unit_) {
1526 is_subclass = is_subclass ||
Vladimir Marko2f40d242020-04-08 12:56:45 +01001527 IsSubClass(dex_compilation_unit_->GetCompilingClass().Get(), cls);
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001528 }
Vladimir Marko2f40d242020-04-08 12:56:45 +01001529 if (is_subclass && HasTrivialInitialization(cls, code_generator_->GetCompilerOptions())) {
Vladimir Marko2ab1bdd2018-07-12 09:59:56 +01001530 return true;
1531 }
David Brazdildee58d62016-04-07 09:54:26 +00001532
1533 return false;
1534}
1535
1536HClinitCheck* HInstructionBuilder::ProcessClinitCheckForInvoke(
Vladimir Markofca0b492018-07-23 15:30:52 +01001537 uint32_t dex_pc,
1538 ArtMethod* resolved_method,
1539 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
Vladimir Marko2f40d242020-04-08 12:56:45 +01001540 ScopedObjectAccess soa(Thread::Current());
1541 ObjPtr<mirror::Class> klass = resolved_method->GetDeclaringClass();
David Brazdildee58d62016-04-07 09:54:26 +00001542
1543 HClinitCheck* clinit_check = nullptr;
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001544 if (IsInitialized(klass)) {
David Brazdildee58d62016-04-07 09:54:26 +00001545 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001546 } else {
Vladimir Marko02ca05a2020-05-12 13:58:51 +01001547 Handle<mirror::Class> h_klass = graph_->GetHandleCache()->NewHandle(klass);
Vladimir Marko2f40d242020-04-08 12:56:45 +01001548 HLoadClass* cls = BuildLoadClass(h_klass->GetDexTypeIndex(),
1549 h_klass->GetDexFile(),
1550 h_klass,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001551 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08001552 /* needs_access_check= */ false);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001553 if (cls != nullptr) {
1554 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
Vladimir Markoca6fff82017-10-03 14:49:14 +01001555 clinit_check = new (allocator_) HClinitCheck(cls, dex_pc);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001556 AppendInstruction(clinit_check);
Vladimir Marko2f40d242020-04-08 12:56:45 +01001557 } else {
1558 // Let the invoke handle this with an implicit class initialization check.
1559 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001560 }
David Brazdildee58d62016-04-07 09:54:26 +00001561 }
1562 return clinit_check;
1563}
1564
Vladimir Marko5f846072020-04-09 13:20:11 +01001565bool HInstructionBuilder::SetupInvokeArguments(HInstruction* invoke,
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001566 const InstructionOperands& operands,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001567 const char* shorty,
Vladimir Marko5f846072020-04-09 13:20:11 +01001568 ReceiverArg receiver_arg) {
1569 // Note: The `invoke` can be an intrinsic replacement, so not necessaritly HInvoke.
1570 // In that case, do not log errors, they shall be reported when we try to build the HInvoke.
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001571 uint32_t shorty_index = 1; // Skip the return type.
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001572 const size_t number_of_operands = operands.GetNumberOfOperands();
Vladimir Marko5f846072020-04-09 13:20:11 +01001573 bool argument_length_error = false;
1574
1575 size_t start_index = 0u;
1576 size_t argument_index = 0u;
1577 if (receiver_arg != ReceiverArg::kNone) {
1578 if (number_of_operands == 0u) {
1579 argument_length_error = true;
1580 } else {
1581 start_index = 1u;
1582 if (receiver_arg != ReceiverArg::kIgnored) {
1583 uint32_t obj_reg = operands.GetOperand(0u);
1584 HInstruction* arg = (receiver_arg == ReceiverArg::kPlainArg)
1585 ? LoadLocal(obj_reg, DataType::Type::kReference)
1586 : LoadNullCheckedLocal(obj_reg, invoke->GetDexPc());
1587 if (receiver_arg != ReceiverArg::kNullCheckedOnly) {
1588 invoke->SetRawInputAt(0u, arg);
1589 argument_index = 1u;
1590 }
1591 }
1592 }
1593 }
1594
1595 for (size_t i = start_index; i < number_of_operands; ++i, ++argument_index) {
1596 // Make sure we don't go over the expected arguments or over the number of
1597 // dex registers given. If the instruction was seen as dead by the verifier,
1598 // it hasn't been properly checked.
1599 if (UNLIKELY(shorty[shorty_index] == 0)) {
1600 argument_length_error = true;
1601 break;
1602 }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001603 DataType::Type type = DataType::FromShorty(shorty[shorty_index++]);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001604 bool is_wide = (type == DataType::Type::kInt64) || (type == DataType::Type::kFloat64);
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001605 if (is_wide && ((i + 1 == number_of_operands) ||
1606 (operands.GetOperand(i) + 1 != operands.GetOperand(i + 1)))) {
Vladimir Marko5f846072020-04-09 13:20:11 +01001607 if (invoke->IsInvoke()) {
1608 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
1609 // reject any class where this is violated. However, the verifier only does these checks
1610 // on non trivially dead instructions, so we just bailout the compilation.
1611 VLOG(compiler) << "Did not compile "
1612 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
1613 << " because of non-sequential dex register pair in wide argument";
1614 MaybeRecordStat(compilation_stats_,
1615 MethodCompilationStat::kNotCompiledMalformedOpcode);
1616 }
David Brazdildee58d62016-04-07 09:54:26 +00001617 return false;
1618 }
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001619 HInstruction* arg = LoadLocal(operands.GetOperand(i), type);
Vladimir Marko5f846072020-04-09 13:20:11 +01001620 DCHECK(invoke->InputAt(argument_index) == nullptr);
1621 invoke->SetRawInputAt(argument_index, arg);
David Brazdildee58d62016-04-07 09:54:26 +00001622 if (is_wide) {
Vladimir Marko5f846072020-04-09 13:20:11 +01001623 ++i;
David Brazdildee58d62016-04-07 09:54:26 +00001624 }
1625 }
1626
Vladimir Marko5f846072020-04-09 13:20:11 +01001627 argument_length_error = argument_length_error || shorty[shorty_index] != 0;
1628 if (argument_length_error) {
1629 if (invoke->IsInvoke()) {
1630 VLOG(compiler) << "Did not compile "
1631 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
1632 << " because of wrong number of arguments in invoke instruction";
1633 MaybeRecordStat(compilation_stats_,
1634 MethodCompilationStat::kNotCompiledMalformedOpcode);
1635 }
David Brazdildee58d62016-04-07 09:54:26 +00001636 return false;
1637 }
1638
1639 if (invoke->IsInvokeStaticOrDirect() &&
1640 HInvokeStaticOrDirect::NeedsCurrentMethodInput(
Vladimir Marko86c87522020-05-11 16:55:55 +01001641 invoke->AsInvokeStaticOrDirect()->GetDispatchInfo())) {
1642 DCHECK_EQ(argument_index, invoke->AsInvokeStaticOrDirect()->GetCurrentMethodIndex());
Vladimir Marko5f846072020-04-09 13:20:11 +01001643 DCHECK(invoke->InputAt(argument_index) == nullptr);
1644 invoke->SetRawInputAt(argument_index, graph_->GetCurrentMethod());
David Brazdildee58d62016-04-07 09:54:26 +00001645 }
1646
1647 return true;
1648}
1649
1650bool HInstructionBuilder::HandleInvoke(HInvoke* invoke,
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001651 const InstructionOperands& operands,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001652 const char* shorty,
Vladimir Marko5f846072020-04-09 13:20:11 +01001653 bool is_unresolved) {
David Brazdildee58d62016-04-07 09:54:26 +00001654 DCHECK(!invoke->IsInvokeStaticOrDirect() || !invoke->AsInvokeStaticOrDirect()->IsStringInit());
1655
Vladimir Marko5f846072020-04-09 13:20:11 +01001656 ReceiverArg receiver_arg = (invoke->GetInvokeType() == InvokeType::kStatic)
1657 ? ReceiverArg::kNone
1658 : (is_unresolved ? ReceiverArg::kPlainArg : ReceiverArg::kNullCheckedArg);
1659 if (!SetupInvokeArguments(invoke, operands, shorty, receiver_arg)) {
David Brazdildee58d62016-04-07 09:54:26 +00001660 return false;
1661 }
1662
David Brazdildee58d62016-04-07 09:54:26 +00001663 AppendInstruction(invoke);
1664 latest_result_ = invoke;
1665
1666 return true;
1667}
1668
Vladimir Marko5f846072020-04-09 13:20:11 +01001669bool HInstructionBuilder::BuildSimpleIntrinsic(ArtMethod* method,
1670 uint32_t dex_pc,
1671 const InstructionOperands& operands,
1672 const char* shorty) {
1673 Intrinsics intrinsic = static_cast<Intrinsics>(method->GetIntrinsic());
1674 DCHECK_NE(intrinsic, Intrinsics::kNone);
1675 constexpr DataType::Type kInt32 = DataType::Type::kInt32;
1676 constexpr DataType::Type kInt64 = DataType::Type::kInt64;
1677 constexpr DataType::Type kFloat32 = DataType::Type::kFloat32;
1678 constexpr DataType::Type kFloat64 = DataType::Type::kFloat64;
1679 ReceiverArg receiver_arg = method->IsStatic() ? ReceiverArg::kNone : ReceiverArg::kNullCheckedArg;
1680 HInstruction* instruction = nullptr;
1681 switch (intrinsic) {
1682 case Intrinsics::kIntegerRotateRight:
1683 case Intrinsics::kIntegerRotateLeft:
1684 // For rotate left, we negate the distance below.
1685 instruction = new (allocator_) HRor(kInt32, /*value=*/ nullptr, /*distance=*/ nullptr);
1686 break;
1687 case Intrinsics::kLongRotateRight:
1688 case Intrinsics::kLongRotateLeft:
1689 // For rotate left, we negate the distance below.
1690 instruction = new (allocator_) HRor(kInt64, /*value=*/ nullptr, /*distance=*/ nullptr);
1691 break;
1692 case Intrinsics::kIntegerCompare:
1693 instruction = new (allocator_) HCompare(
1694 kInt32, /*first=*/ nullptr, /*second=*/ nullptr, ComparisonBias::kNoBias, dex_pc);
1695 break;
1696 case Intrinsics::kLongCompare:
1697 instruction = new (allocator_) HCompare(
1698 kInt64, /*first=*/ nullptr, /*second=*/ nullptr, ComparisonBias::kNoBias, dex_pc);
1699 break;
1700 case Intrinsics::kIntegerSignum:
1701 instruction = new (allocator_) HCompare(
1702 kInt32, /*first=*/ nullptr, graph_->GetIntConstant(0), ComparisonBias::kNoBias, dex_pc);
1703 break;
1704 case Intrinsics::kLongSignum:
1705 instruction = new (allocator_) HCompare(
1706 kInt64, /*first=*/ nullptr, graph_->GetLongConstant(0), ComparisonBias::kNoBias, dex_pc);
1707 break;
1708 case Intrinsics::kFloatIsNaN:
1709 case Intrinsics::kDoubleIsNaN: {
1710 // IsNaN(x) is the same as x != x.
1711 instruction = new (allocator_) HNotEqual(/*first=*/ nullptr, /*second=*/ nullptr, dex_pc);
1712 instruction->AsCondition()->SetBias(ComparisonBias::kLtBias);
1713 break;
1714 }
1715 case Intrinsics::kStringCharAt:
1716 // We treat String as an array to allow DCE and BCE to seamlessly work on strings.
1717 instruction = new (allocator_) HArrayGet(/*array=*/ nullptr,
1718 /*index=*/ nullptr,
1719 DataType::Type::kUint16,
1720 SideEffects::None(), // Strings are immutable.
1721 dex_pc,
1722 /*is_string_char_at=*/ true);
1723 break;
1724 case Intrinsics::kStringIsEmpty:
1725 case Intrinsics::kStringLength:
1726 // We treat String as an array to allow DCE and BCE to seamlessly work on strings.
1727 // For String.isEmpty(), we add a comparison with 0 below.
1728 instruction =
1729 new (allocator_) HArrayLength(/*array=*/ nullptr, dex_pc, /* is_string_length= */ true);
1730 break;
1731 case Intrinsics::kUnsafeLoadFence:
1732 receiver_arg = ReceiverArg::kNullCheckedOnly;
1733 instruction = new (allocator_) HMemoryBarrier(MemBarrierKind::kLoadAny, dex_pc);
1734 break;
1735 case Intrinsics::kUnsafeStoreFence:
1736 receiver_arg = ReceiverArg::kNullCheckedOnly;
1737 instruction = new (allocator_) HMemoryBarrier(MemBarrierKind::kAnyStore, dex_pc);
1738 break;
1739 case Intrinsics::kUnsafeFullFence:
1740 receiver_arg = ReceiverArg::kNullCheckedOnly;
1741 instruction = new (allocator_) HMemoryBarrier(MemBarrierKind::kAnyAny, dex_pc);
1742 break;
1743 case Intrinsics::kVarHandleFullFence:
1744 instruction = new (allocator_) HMemoryBarrier(MemBarrierKind::kAnyAny, dex_pc);
1745 break;
1746 case Intrinsics::kVarHandleAcquireFence:
1747 instruction = new (allocator_) HMemoryBarrier(MemBarrierKind::kLoadAny, dex_pc);
1748 break;
1749 case Intrinsics::kVarHandleReleaseFence:
1750 instruction = new (allocator_) HMemoryBarrier(MemBarrierKind::kAnyStore, dex_pc);
1751 break;
1752 case Intrinsics::kVarHandleLoadLoadFence:
1753 instruction = new (allocator_) HMemoryBarrier(MemBarrierKind::kLoadAny, dex_pc);
1754 break;
1755 case Intrinsics::kVarHandleStoreStoreFence:
1756 instruction = new (allocator_) HMemoryBarrier(MemBarrierKind::kStoreStore, dex_pc);
1757 break;
1758 case Intrinsics::kMathMinIntInt:
1759 instruction = new (allocator_) HMin(kInt32, /*left=*/ nullptr, /*right=*/ nullptr, dex_pc);
1760 break;
1761 case Intrinsics::kMathMinLongLong:
1762 instruction = new (allocator_) HMin(kInt64, /*left=*/ nullptr, /*right=*/ nullptr, dex_pc);
1763 break;
1764 case Intrinsics::kMathMinFloatFloat:
1765 instruction = new (allocator_) HMin(kFloat32, /*left=*/ nullptr, /*right=*/ nullptr, dex_pc);
1766 break;
1767 case Intrinsics::kMathMinDoubleDouble:
1768 instruction = new (allocator_) HMin(kFloat64, /*left=*/ nullptr, /*right=*/ nullptr, dex_pc);
1769 break;
1770 case Intrinsics::kMathMaxIntInt:
1771 instruction = new (allocator_) HMax(kInt32, /*left=*/ nullptr, /*right=*/ nullptr, dex_pc);
1772 break;
1773 case Intrinsics::kMathMaxLongLong:
1774 instruction = new (allocator_) HMax(kInt64, /*left=*/ nullptr, /*right=*/ nullptr, dex_pc);
1775 break;
1776 case Intrinsics::kMathMaxFloatFloat:
1777 instruction = new (allocator_) HMax(kFloat32, /*left=*/ nullptr, /*right=*/ nullptr, dex_pc);
1778 break;
1779 case Intrinsics::kMathMaxDoubleDouble:
1780 instruction = new (allocator_) HMax(kFloat64, /*left=*/ nullptr, /*right=*/ nullptr, dex_pc);
1781 break;
1782 case Intrinsics::kMathAbsInt:
1783 instruction = new (allocator_) HAbs(kInt32, /*input=*/ nullptr, dex_pc);
1784 break;
1785 case Intrinsics::kMathAbsLong:
1786 instruction = new (allocator_) HAbs(kInt64, /*input=*/ nullptr, dex_pc);
1787 break;
1788 case Intrinsics::kMathAbsFloat:
1789 instruction = new (allocator_) HAbs(kFloat32, /*input=*/ nullptr, dex_pc);
1790 break;
1791 case Intrinsics::kMathAbsDouble:
1792 instruction = new (allocator_) HAbs(kFloat64, /*input=*/ nullptr, dex_pc);
1793 break;
1794 default:
1795 // We do not have intermediate representation for other intrinsics.
1796 return false;
1797 }
1798 DCHECK(instruction != nullptr);
1799 if (!SetupInvokeArguments(instruction, operands, shorty, receiver_arg)) {
1800 return false;
1801 }
1802
1803 switch (intrinsic) {
1804 case Intrinsics::kIntegerRotateLeft:
1805 case Intrinsics::kLongRotateLeft: {
1806 // Negate the distance value for rotate left.
1807 DCHECK(instruction->IsRor());
1808 HNeg* neg = new (allocator_) HNeg(kInt32, instruction->InputAt(1u));
1809 AppendInstruction(neg);
1810 instruction->SetRawInputAt(1u, neg);
1811 break;
1812 }
1813 case Intrinsics::kFloatIsNaN:
1814 case Intrinsics::kDoubleIsNaN:
1815 // Set the second input to be the same as first.
1816 DCHECK(instruction->IsNotEqual());
1817 DCHECK(instruction->InputAt(1u) == nullptr);
1818 instruction->SetRawInputAt(1u, instruction->InputAt(0u));
1819 break;
1820 case Intrinsics::kStringCharAt: {
1821 // Add bounds check.
1822 HInstruction* array = instruction->InputAt(0u);
1823 HInstruction* index = instruction->InputAt(1u);
1824 HInstruction* length =
1825 new (allocator_) HArrayLength(array, dex_pc, /*is_string_length=*/ true);
1826 AppendInstruction(length);
1827 HBoundsCheck* bounds_check =
1828 new (allocator_) HBoundsCheck(index, length, dex_pc, /*is_string_char_at=*/ true);
1829 AppendInstruction(bounds_check);
1830 graph_->SetHasBoundsChecks(true);
1831 instruction->SetRawInputAt(1u, bounds_check);
1832 break;
1833 }
1834 case Intrinsics::kStringIsEmpty: {
1835 // Compare the length with 0.
1836 DCHECK(instruction->IsArrayLength());
1837 AppendInstruction(instruction);
1838 HEqual* equal = new (allocator_) HEqual(instruction, graph_->GetIntConstant(0), dex_pc);
1839 instruction = equal;
1840 break;
1841 }
1842 default:
1843 break;
1844 }
1845
1846 AppendInstruction(instruction);
1847 latest_result_ = instruction;
1848
1849 return true;
1850}
1851
David Brazdildee58d62016-04-07 09:54:26 +00001852bool HInstructionBuilder::HandleStringInit(HInvoke* invoke,
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001853 const InstructionOperands& operands,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001854 const char* shorty) {
David Brazdildee58d62016-04-07 09:54:26 +00001855 DCHECK(invoke->IsInvokeStaticOrDirect());
1856 DCHECK(invoke->AsInvokeStaticOrDirect()->IsStringInit());
1857
Vladimir Marko5f846072020-04-09 13:20:11 +01001858 if (!SetupInvokeArguments(invoke, operands, shorty, ReceiverArg::kIgnored)) {
David Brazdildee58d62016-04-07 09:54:26 +00001859 return false;
1860 }
1861
1862 AppendInstruction(invoke);
1863
1864 // This is a StringFactory call, not an actual String constructor. Its result
1865 // replaces the empty String pre-allocated by NewInstance.
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001866 uint32_t orig_this_reg = operands.GetOperand(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001867 HInstruction* arg_this = LoadLocal(orig_this_reg, DataType::Type::kReference);
David Brazdildee58d62016-04-07 09:54:26 +00001868
1869 // Replacing the NewInstance might render it redundant. Keep a list of these
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +01001870 // to be visited once it is clear whether it has remaining uses.
David Brazdildee58d62016-04-07 09:54:26 +00001871 if (arg_this->IsNewInstance()) {
1872 ssa_builder_->AddUninitializedString(arg_this->AsNewInstance());
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +01001873 } else {
1874 DCHECK(arg_this->IsPhi());
1875 // We can get a phi as input of a String.<init> if there is a loop between the
1876 // allocation and the String.<init> call. As we don't know which other phis might alias
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +01001877 // with `arg_this`, we keep a record of those invocations so we can later replace
1878 // the allocation with the invocation.
1879 // Add the actual 'this' input so the analysis knows what is the allocation instruction.
1880 // The input will be removed during the analysis.
1881 invoke->AddInput(arg_this);
1882 ssa_builder_->AddUninitializedStringPhi(invoke);
1883 }
1884 // Walk over all vregs and replace any occurrence of `arg_this` with `invoke`.
1885 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
1886 if ((*current_locals_)[vreg] == arg_this) {
1887 (*current_locals_)[vreg] = invoke;
1888 }
David Brazdildee58d62016-04-07 09:54:26 +00001889 }
David Brazdildee58d62016-04-07 09:54:26 +00001890 return true;
1891}
1892
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001893static DataType::Type GetFieldAccessType(const DexFile& dex_file, uint16_t field_index) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001894 const dex::FieldId& field_id = dex_file.GetFieldId(field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001895 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001896 return DataType::FromShorty(type[0]);
David Brazdildee58d62016-04-07 09:54:26 +00001897}
1898
1899bool HInstructionBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
1900 uint32_t dex_pc,
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001901 bool is_put,
1902 size_t quicken_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001903 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1904 uint32_t obj_reg = instruction.VRegB_22c();
1905 uint16_t field_index;
1906 if (instruction.IsQuickened()) {
1907 if (!CanDecodeQuickenedInfo()) {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00001908 VLOG(compiler) << "Not compiled: Could not decode quickened instruction "
1909 << instruction.Opcode();
David Brazdildee58d62016-04-07 09:54:26 +00001910 return false;
1911 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001912 field_index = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00001913 } else {
1914 field_index = instruction.VRegC_22c();
1915 }
1916
1917 ScopedObjectAccess soa(Thread::Current());
Andreas Gampe3db70682018-12-26 15:12:03 -08001918 ArtField* resolved_field = ResolveField(field_index, /* is_static= */ false, is_put);
David Brazdildee58d62016-04-07 09:54:26 +00001919
Aart Bik14154132016-06-02 17:53:58 -07001920 // Generate an explicit null check on the reference, unless the field access
1921 // is unresolved. In that case, we rely on the runtime to perform various
1922 // checks first, followed by a null check.
1923 HInstruction* object = (resolved_field == nullptr)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001924 ? LoadLocal(obj_reg, DataType::Type::kReference)
Aart Bik14154132016-06-02 17:53:58 -07001925 : LoadNullCheckedLocal(obj_reg, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001926
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001927 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001928 if (is_put) {
1929 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1930 HInstruction* field_set = nullptr;
1931 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001932 MaybeRecordStat(compilation_stats_,
1933 MethodCompilationStat::kUnresolvedField);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001934 field_set = new (allocator_) HUnresolvedInstanceFieldSet(object,
1935 value,
1936 field_type,
1937 field_index,
1938 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001939 } else {
1940 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Vladimir Markoca6fff82017-10-03 14:49:14 +01001941 field_set = new (allocator_) HInstanceFieldSet(object,
1942 value,
1943 resolved_field,
1944 field_type,
1945 resolved_field->GetOffset(),
1946 resolved_field->IsVolatile(),
1947 field_index,
1948 class_def_index,
1949 *dex_file_,
1950 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001951 }
1952 AppendInstruction(field_set);
1953 } else {
1954 HInstruction* field_get = nullptr;
1955 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001956 MaybeRecordStat(compilation_stats_,
1957 MethodCompilationStat::kUnresolvedField);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001958 field_get = new (allocator_) HUnresolvedInstanceFieldGet(object,
1959 field_type,
1960 field_index,
1961 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001962 } else {
1963 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Vladimir Markoca6fff82017-10-03 14:49:14 +01001964 field_get = new (allocator_) HInstanceFieldGet(object,
1965 resolved_field,
1966 field_type,
1967 resolved_field->GetOffset(),
1968 resolved_field->IsVolatile(),
1969 field_index,
1970 class_def_index,
1971 *dex_file_,
1972 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001973 }
1974 AppendInstruction(field_get);
1975 UpdateLocal(source_or_dest_reg, field_get);
1976 }
1977
1978 return true;
1979}
1980
David Brazdildee58d62016-04-07 09:54:26 +00001981void HInstructionBuilder::BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001982 uint32_t dex_pc,
1983 bool is_put,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001984 DataType::Type field_type) {
David Brazdildee58d62016-04-07 09:54:26 +00001985 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1986 uint16_t field_index = instruction.VRegB_21c();
1987
1988 if (is_put) {
1989 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1990 AppendInstruction(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001991 new (allocator_) HUnresolvedStaticFieldSet(value, field_type, field_index, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001992 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001993 AppendInstruction(new (allocator_) HUnresolvedStaticFieldGet(field_type, field_index, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001994 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1995 }
1996}
1997
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001998ArtField* HInstructionBuilder::ResolveField(uint16_t field_idx, bool is_static, bool is_put) {
1999 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002000
2001 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002002 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002003
Vladimir Markoe11dd502017-12-08 14:09:45 +00002004 ArtField* resolved_field = class_linker->ResolveField(field_idx,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002005 dex_compilation_unit_->GetDexCache(),
2006 class_loader,
2007 is_static);
Nicolas Geoffrayf3688822020-03-25 15:04:03 +00002008 DCHECK_EQ(resolved_field == nullptr, soa.Self()->IsExceptionPending())
2009 << "field="
2010 << ((resolved_field == nullptr) ? "null" : resolved_field->PrettyField())
2011 << ", exception="
2012 << (soa.Self()->IsExceptionPending() ? soa.Self()->GetException()->Dump() : "null");
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002013 if (UNLIKELY(resolved_field == nullptr)) {
Vladimir Markoa2c211c2018-11-01 09:50:52 +00002014 // Clean up any exception left by field resolution.
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002015 soa.Self()->ClearException();
2016 return nullptr;
2017 }
2018
2019 // Check static/instance. The class linker has a fast path for looking into the dex cache
2020 // and does not check static/instance if it hits it.
2021 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
2022 return nullptr;
2023 }
2024
2025 // Check access.
Vladimir Markoa2c211c2018-11-01 09:50:52 +00002026 Handle<mirror::Class> compiling_class = dex_compilation_unit_->GetCompilingClass();
Andreas Gampefa4333d2017-02-14 11:10:34 -08002027 if (compiling_class == nullptr) {
Vladimir Marko4100e5e2020-08-26 16:44:01 +01002028 // Check if the declaring class or referencing class is accessible.
2029 SamePackageCompare same_package(*dex_compilation_unit_);
2030 ObjPtr<mirror::Class> declaring_class = resolved_field->GetDeclaringClass();
2031 bool declaring_class_accessible = declaring_class->IsPublic() || same_package(declaring_class);
2032 if (!declaring_class_accessible) {
2033 // It is possible to access members from an inaccessible superclass
2034 // by referencing them through an accessible subclass.
2035 ObjPtr<mirror::Class> referenced_class = class_linker->LookupResolvedType(
2036 dex_compilation_unit_->GetDexFile()->GetFieldId(field_idx).class_idx_,
2037 dex_compilation_unit_->GetDexCache().Get(),
2038 class_loader.Get());
2039 DCHECK(referenced_class != nullptr); // Must have been resolved when resolving the field.
2040 if (!referenced_class->IsPublic() && !same_package(referenced_class)) {
2041 return nullptr;
2042 }
2043 }
2044 // Check whether the field itself is accessible.
2045 // Since the referrer is unresolved but the field is resolved, it cannot be
2046 // inside the same class, so a private field is known to be inaccessible.
2047 // And without a resolved referrer, we cannot check for protected member access
2048 // in superlass, so we handle only access to public member or within the package.
2049 if (resolved_field->IsPrivate() ||
2050 (!resolved_field->IsPublic() && !declaring_class_accessible)) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002051 return nullptr;
2052 }
2053 } else if (!compiling_class->CanAccessResolvedField(resolved_field->GetDeclaringClass(),
2054 resolved_field,
2055 dex_compilation_unit_->GetDexCache().Get(),
2056 field_idx)) {
2057 return nullptr;
2058 }
2059
2060 if (is_put &&
2061 resolved_field->IsFinal() &&
2062 (compiling_class.Get() != resolved_field->GetDeclaringClass())) {
2063 // Final fields can only be updated within their own class.
2064 // TODO: Only allow it in constructors. b/34966607.
2065 return nullptr;
2066 }
2067
Nicolas Geoffray396198b2020-06-16 12:02:45 +01002068 StackArtFieldHandleScope<1> rhs(soa.Self());
2069 ReflectiveHandle<ArtField> resolved_field_handle(rhs.NewHandle(resolved_field));
2070 if (resolved_field->ResolveType().IsNull()) {
2071 // ArtField::ResolveType() may fail as evidenced with a dexing bug (b/78788577).
2072 soa.Self()->ClearException();
2073 return nullptr; // Failure
2074 }
2075 return resolved_field_handle.Get();
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002076}
2077
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002078void HInstructionBuilder::BuildStaticFieldAccess(const Instruction& instruction,
David Brazdildee58d62016-04-07 09:54:26 +00002079 uint32_t dex_pc,
2080 bool is_put) {
2081 uint32_t source_or_dest_reg = instruction.VRegA_21c();
2082 uint16_t field_index = instruction.VRegB_21c();
2083
2084 ScopedObjectAccess soa(Thread::Current());
Andreas Gampe3db70682018-12-26 15:12:03 -08002085 ArtField* resolved_field = ResolveField(field_index, /* is_static= */ true, is_put);
David Brazdildee58d62016-04-07 09:54:26 +00002086
2087 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07002088 MaybeRecordStat(compilation_stats_,
2089 MethodCompilationStat::kUnresolvedField);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002090 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00002091 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002092 return;
David Brazdildee58d62016-04-07 09:54:26 +00002093 }
2094
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002095 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00002096
Vladimir Marko02ca05a2020-05-12 13:58:51 +01002097 Handle<mirror::Class> klass =
2098 graph_->GetHandleCache()->NewHandle(resolved_field->GetDeclaringClass());
Vladimir Markoa2c211c2018-11-01 09:50:52 +00002099 HLoadClass* constant = BuildLoadClass(klass->GetDexTypeIndex(),
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002100 klass->GetDexFile(),
2101 klass,
2102 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08002103 /* needs_access_check= */ false);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002104
2105 if (constant == nullptr) {
2106 // The class cannot be referenced from this compiled code. Generate
2107 // an unresolved access.
Igor Murashkin1e065a52017-08-09 13:20:34 -07002108 MaybeRecordStat(compilation_stats_,
2109 MethodCompilationStat::kUnresolvedFieldNotAFastAccess);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002110 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002111 return;
David Brazdildee58d62016-04-07 09:54:26 +00002112 }
2113
David Brazdildee58d62016-04-07 09:54:26 +00002114 HInstruction* cls = constant;
Vladimir Marko2f40d242020-04-08 12:56:45 +01002115 if (!IsInitialized(klass.Get())) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002116 cls = new (allocator_) HClinitCheck(constant, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002117 AppendInstruction(cls);
2118 }
2119
2120 uint16_t class_def_index = klass->GetDexClassDefIndex();
2121 if (is_put) {
2122 // We need to keep the class alive before loading the value.
2123 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
2124 DCHECK_EQ(HPhi::ToPhiType(value->GetType()), HPhi::ToPhiType(field_type));
Vladimir Markoca6fff82017-10-03 14:49:14 +01002125 AppendInstruction(new (allocator_) HStaticFieldSet(cls,
2126 value,
2127 resolved_field,
2128 field_type,
2129 resolved_field->GetOffset(),
2130 resolved_field->IsVolatile(),
2131 field_index,
2132 class_def_index,
2133 *dex_file_,
2134 dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002135 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002136 AppendInstruction(new (allocator_) HStaticFieldGet(cls,
2137 resolved_field,
2138 field_type,
2139 resolved_field->GetOffset(),
2140 resolved_field->IsVolatile(),
2141 field_index,
2142 class_def_index,
2143 *dex_file_,
2144 dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002145 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
2146 }
David Brazdildee58d62016-04-07 09:54:26 +00002147}
2148
2149void HInstructionBuilder::BuildCheckedDivRem(uint16_t out_vreg,
Vladimir Markoca6fff82017-10-03 14:49:14 +01002150 uint16_t first_vreg,
2151 int64_t second_vreg_or_constant,
2152 uint32_t dex_pc,
2153 DataType::Type type,
2154 bool second_is_constant,
2155 bool isDiv) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002156 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
David Brazdildee58d62016-04-07 09:54:26 +00002157
2158 HInstruction* first = LoadLocal(first_vreg, type);
2159 HInstruction* second = nullptr;
2160 if (second_is_constant) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002161 if (type == DataType::Type::kInt32) {
David Brazdildee58d62016-04-07 09:54:26 +00002162 second = graph_->GetIntConstant(second_vreg_or_constant, dex_pc);
2163 } else {
2164 second = graph_->GetLongConstant(second_vreg_or_constant, dex_pc);
2165 }
2166 } else {
2167 second = LoadLocal(second_vreg_or_constant, type);
2168 }
2169
2170 if (!second_is_constant
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002171 || (type == DataType::Type::kInt32 && second->AsIntConstant()->GetValue() == 0)
2172 || (type == DataType::Type::kInt64 && second->AsLongConstant()->GetValue() == 0)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002173 second = new (allocator_) HDivZeroCheck(second, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002174 AppendInstruction(second);
2175 }
2176
2177 if (isDiv) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002178 AppendInstruction(new (allocator_) HDiv(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002179 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002180 AppendInstruction(new (allocator_) HRem(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002181 }
2182 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
2183}
2184
2185void HInstructionBuilder::BuildArrayAccess(const Instruction& instruction,
2186 uint32_t dex_pc,
2187 bool is_put,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002188 DataType::Type anticipated_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002189 uint8_t source_or_dest_reg = instruction.VRegA_23x();
2190 uint8_t array_reg = instruction.VRegB_23x();
2191 uint8_t index_reg = instruction.VRegC_23x();
2192
David Brazdilc120bbe2016-04-22 16:57:00 +01002193 HInstruction* object = LoadNullCheckedLocal(array_reg, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002194 HInstruction* length = new (allocator_) HArrayLength(object, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002195 AppendInstruction(length);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002196 HInstruction* index = LoadLocal(index_reg, DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002197 index = new (allocator_) HBoundsCheck(index, length, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002198 AppendInstruction(index);
2199 if (is_put) {
2200 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
2201 // TODO: Insert a type check node if the type is Object.
Vladimir Markoca6fff82017-10-03 14:49:14 +01002202 HArraySet* aset = new (allocator_) HArraySet(object, index, value, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002203 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
2204 AppendInstruction(aset);
2205 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002206 HArrayGet* aget = new (allocator_) HArrayGet(object, index, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002207 ssa_builder_->MaybeAddAmbiguousArrayGet(aget);
2208 AppendInstruction(aget);
2209 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
2210 }
2211 graph_->SetHasBoundsChecks(true);
2212}
2213
Vladimir Markob5461632018-10-15 14:24:21 +01002214HNewArray* HInstructionBuilder::BuildNewArray(uint32_t dex_pc,
2215 dex::TypeIndex type_index,
2216 HInstruction* length) {
2217 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
2218
2219 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(type_index));
2220 DCHECK_EQ(descriptor[0], '[');
2221 size_t component_type_shift = Primitive::ComponentSizeShift(Primitive::GetType(descriptor[1]));
2222
2223 HNewArray* new_array = new (allocator_) HNewArray(cls, length, dex_pc, component_type_shift);
2224 AppendInstruction(new_array);
2225 return new_array;
2226}
2227
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002228HNewArray* HInstructionBuilder::BuildFilledNewArray(uint32_t dex_pc,
2229 dex::TypeIndex type_index,
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002230 const InstructionOperands& operands) {
2231 const size_t number_of_operands = operands.GetNumberOfOperands();
2232 HInstruction* length = graph_->GetIntConstant(number_of_operands, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002233
Vladimir Markob5461632018-10-15 14:24:21 +01002234 HNewArray* new_array = BuildNewArray(dex_pc, type_index, length);
David Brazdildee58d62016-04-07 09:54:26 +00002235 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
2236 DCHECK_EQ(descriptor[0], '[') << descriptor;
2237 char primitive = descriptor[1];
2238 DCHECK(primitive == 'I'
2239 || primitive == 'L'
2240 || primitive == '[') << descriptor;
2241 bool is_reference_array = (primitive == 'L') || (primitive == '[');
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002242 DataType::Type type = is_reference_array ? DataType::Type::kReference : DataType::Type::kInt32;
David Brazdildee58d62016-04-07 09:54:26 +00002243
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002244 for (size_t i = 0; i < number_of_operands; ++i) {
2245 HInstruction* value = LoadLocal(operands.GetOperand(i), type);
David Brazdildee58d62016-04-07 09:54:26 +00002246 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
Vladimir Markob5461632018-10-15 14:24:21 +01002247 HArraySet* aset = new (allocator_) HArraySet(new_array, index, value, type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002248 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
2249 AppendInstruction(aset);
2250 }
Vladimir Markob5461632018-10-15 14:24:21 +01002251 latest_result_ = new_array;
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002252
Vladimir Markob5461632018-10-15 14:24:21 +01002253 return new_array;
David Brazdildee58d62016-04-07 09:54:26 +00002254}
2255
2256template <typename T>
2257void HInstructionBuilder::BuildFillArrayData(HInstruction* object,
2258 const T* data,
2259 uint32_t element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002260 DataType::Type anticipated_type,
David Brazdildee58d62016-04-07 09:54:26 +00002261 uint32_t dex_pc) {
2262 for (uint32_t i = 0; i < element_count; ++i) {
2263 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
2264 HInstruction* value = graph_->GetIntConstant(data[i], dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002265 HArraySet* aset = new (allocator_) HArraySet(object, index, value, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002266 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
2267 AppendInstruction(aset);
2268 }
2269}
2270
2271void HInstructionBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
David Brazdilc120bbe2016-04-22 16:57:00 +01002272 HInstruction* array = LoadNullCheckedLocal(instruction.VRegA_31t(), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002273
2274 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
2275 const Instruction::ArrayDataPayload* payload =
Mathieu Chartier808c7a52017-12-15 11:19:33 -08002276 reinterpret_cast<const Instruction::ArrayDataPayload*>(
2277 code_item_accessor_.Insns() + payload_offset);
David Brazdildee58d62016-04-07 09:54:26 +00002278 const uint8_t* data = payload->data;
2279 uint32_t element_count = payload->element_count;
2280
Vladimir Markoc69fba22016-09-06 16:49:15 +01002281 if (element_count == 0u) {
2282 // For empty payload we emit only the null check above.
2283 return;
2284 }
2285
Vladimir Markoca6fff82017-10-03 14:49:14 +01002286 HInstruction* length = new (allocator_) HArrayLength(array, dex_pc);
Vladimir Markoc69fba22016-09-06 16:49:15 +01002287 AppendInstruction(length);
2288
David Brazdildee58d62016-04-07 09:54:26 +00002289 // Implementation of this DEX instruction seems to be that the bounds check is
2290 // done before doing any stores.
2291 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002292 AppendInstruction(new (allocator_) HBoundsCheck(last_index, length, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002293
2294 switch (payload->element_width) {
2295 case 1:
David Brazdilc120bbe2016-04-22 16:57:00 +01002296 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00002297 reinterpret_cast<const int8_t*>(data),
2298 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002299 DataType::Type::kInt8,
David Brazdildee58d62016-04-07 09:54:26 +00002300 dex_pc);
2301 break;
2302 case 2:
David Brazdilc120bbe2016-04-22 16:57:00 +01002303 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00002304 reinterpret_cast<const int16_t*>(data),
2305 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002306 DataType::Type::kInt16,
David Brazdildee58d62016-04-07 09:54:26 +00002307 dex_pc);
2308 break;
2309 case 4:
David Brazdilc120bbe2016-04-22 16:57:00 +01002310 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00002311 reinterpret_cast<const int32_t*>(data),
2312 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002313 DataType::Type::kInt32,
David Brazdildee58d62016-04-07 09:54:26 +00002314 dex_pc);
2315 break;
2316 case 8:
David Brazdilc120bbe2016-04-22 16:57:00 +01002317 BuildFillWideArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00002318 reinterpret_cast<const int64_t*>(data),
2319 element_count,
2320 dex_pc);
2321 break;
2322 default:
2323 LOG(FATAL) << "Unknown element width for " << payload->element_width;
2324 }
2325 graph_->SetHasBoundsChecks(true);
2326}
2327
2328void HInstructionBuilder::BuildFillWideArrayData(HInstruction* object,
2329 const int64_t* data,
2330 uint32_t element_count,
2331 uint32_t dex_pc) {
2332 for (uint32_t i = 0; i < element_count; ++i) {
2333 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
2334 HInstruction* value = graph_->GetLongConstant(data[i], dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002335 HArraySet* aset =
2336 new (allocator_) HArraySet(object, index, value, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002337 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
2338 AppendInstruction(aset);
2339 }
2340}
2341
Vladimir Marko28e012a2017-12-07 11:22:59 +00002342void HInstructionBuilder::BuildLoadString(dex::StringIndex string_index, uint32_t dex_pc) {
2343 HLoadString* load_string =
2344 new (allocator_) HLoadString(graph_->GetCurrentMethod(), string_index, *dex_file_, dex_pc);
2345 HSharpening::ProcessLoadString(load_string,
2346 code_generator_,
Vladimir Marko28e012a2017-12-07 11:22:59 +00002347 *dex_compilation_unit_,
Vladimir Marko02ca05a2020-05-12 13:58:51 +01002348 graph_->GetHandleCache()->GetHandles());
Vladimir Marko28e012a2017-12-07 11:22:59 +00002349 AppendInstruction(load_string);
2350}
2351
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002352HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index, uint32_t dex_pc) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +00002353 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002354 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
Vladimir Marko175e7862018-03-27 09:03:13 +00002355 Handle<mirror::Class> klass = ResolveClass(soa, type_index);
Vladimir Marko2f40d242020-04-08 12:56:45 +01002356 bool needs_access_check = LoadClassNeedsAccessCheck(klass.Get());
Vladimir Markoa2c211c2018-11-01 09:50:52 +00002357 return BuildLoadClass(type_index, dex_file, klass, dex_pc, needs_access_check);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002358}
2359
Vladimir Markoa2c211c2018-11-01 09:50:52 +00002360HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002361 const DexFile& dex_file,
2362 Handle<mirror::Class> klass,
2363 uint32_t dex_pc,
2364 bool needs_access_check) {
2365 // Try to find a reference in the compiling dex file.
2366 const DexFile* actual_dex_file = &dex_file;
2367 if (!IsSameDexFile(dex_file, *dex_compilation_unit_->GetDexFile())) {
2368 dex::TypeIndex local_type_index =
2369 klass->FindTypeIndexInOtherDexFile(*dex_compilation_unit_->GetDexFile());
2370 if (local_type_index.IsValid()) {
2371 type_index = local_type_index;
2372 actual_dex_file = dex_compilation_unit_->GetDexFile();
2373 }
2374 }
2375
Vladimir Marko02ca05a2020-05-12 13:58:51 +01002376 // Note: `klass` must be from `graph_->GetHandleCache()`.
Vladimir Markoa2c211c2018-11-01 09:50:52 +00002377 bool is_referrers_class =
2378 (klass != nullptr) && (outer_compilation_unit_->GetCompilingClass().Get() == klass.Get());
Vladimir Markoca6fff82017-10-03 14:49:14 +01002379 HLoadClass* load_class = new (allocator_) HLoadClass(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00002380 graph_->GetCurrentMethod(),
2381 type_index,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002382 *actual_dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00002383 klass,
Vladimir Markofca0b492018-07-23 15:30:52 +01002384 is_referrers_class,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00002385 dex_pc,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002386 needs_access_check);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00002387
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002388 HLoadClass::LoadKind load_kind = HSharpening::ComputeLoadClassKind(load_class,
2389 code_generator_,
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002390 *dex_compilation_unit_);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002391
2392 if (load_kind == HLoadClass::LoadKind::kInvalid) {
2393 // We actually cannot reference this class, we're forced to bail.
2394 return nullptr;
2395 }
Vladimir Marko28e012a2017-12-07 11:22:59 +00002396 // Load kind must be set before inserting the instruction into the graph.
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002397 load_class->SetLoadKind(load_kind);
Vladimir Marko28e012a2017-12-07 11:22:59 +00002398 AppendInstruction(load_class);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00002399 return load_class;
2400}
2401
Vladimir Marko175e7862018-03-27 09:03:13 +00002402Handle<mirror::Class> HInstructionBuilder::ResolveClass(ScopedObjectAccess& soa,
2403 dex::TypeIndex type_index) {
Vladimir Marko3b506202018-10-31 14:33:58 +00002404 auto it = class_cache_.find(type_index);
2405 if (it != class_cache_.end()) {
2406 return it->second;
2407 }
2408
2409 ObjPtr<mirror::Class> klass = dex_compilation_unit_->GetClassLinker()->ResolveType(
2410 type_index, dex_compilation_unit_->GetDexCache(), dex_compilation_unit_->GetClassLoader());
2411 DCHECK_EQ(klass == nullptr, soa.Self()->IsExceptionPending());
2412 soa.Self()->ClearException(); // Clean up the exception left by type resolution if any.
2413
Vladimir Marko02ca05a2020-05-12 13:58:51 +01002414 Handle<mirror::Class> h_klass = graph_->GetHandleCache()->NewHandle(klass);
Vladimir Marko3b506202018-10-31 14:33:58 +00002415 class_cache_.Put(type_index, h_klass);
2416 return h_klass;
Vladimir Marko175e7862018-03-27 09:03:13 +00002417}
2418
Vladimir Marko2f40d242020-04-08 12:56:45 +01002419bool HInstructionBuilder::LoadClassNeedsAccessCheck(ObjPtr<mirror::Class> klass) {
Vladimir Marko175e7862018-03-27 09:03:13 +00002420 if (klass == nullptr) {
2421 return true;
2422 } else if (klass->IsPublic()) {
2423 return false;
Vladimir Marko0280e5d2020-08-27 17:36:24 +01002424 } else if (dex_compilation_unit_->GetCompilingClass() != nullptr) {
2425 return !dex_compilation_unit_->GetCompilingClass()->CanAccess(klass);
Vladimir Marko175e7862018-03-27 09:03:13 +00002426 } else {
Vladimir Marko0280e5d2020-08-27 17:36:24 +01002427 SamePackageCompare same_package(*dex_compilation_unit_);
2428 return !same_package(klass);
Vladimir Marko175e7862018-03-27 09:03:13 +00002429 }
2430}
2431
Orion Hodson06d10a72018-05-14 08:53:38 +01002432void HInstructionBuilder::BuildLoadMethodHandle(uint16_t method_handle_index, uint32_t dex_pc) {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01002433 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
Orion Hodson06d10a72018-05-14 08:53:38 +01002434 HLoadMethodHandle* load_method_handle = new (allocator_) HLoadMethodHandle(
2435 graph_->GetCurrentMethod(), method_handle_index, dex_file, dex_pc);
Orion Hodsondbaa5c72018-05-10 08:22:46 +01002436 AppendInstruction(load_method_handle);
2437}
2438
Orion Hodson06d10a72018-05-14 08:53:38 +01002439void HInstructionBuilder::BuildLoadMethodType(dex::ProtoIndex proto_index, uint32_t dex_pc) {
Orion Hodson18259d72018-04-12 11:18:23 +01002440 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
2441 HLoadMethodType* load_method_type =
Orion Hodson06d10a72018-05-14 08:53:38 +01002442 new (allocator_) HLoadMethodType(graph_->GetCurrentMethod(), proto_index, dex_file, dex_pc);
Orion Hodson18259d72018-04-12 11:18:23 +01002443 AppendInstruction(load_method_type);
2444}
2445
Andra Danciu49a19f32020-08-27 12:44:25 +00002446void HInstructionBuilder::BuildTypeCheck(bool is_instance_of,
2447 HInstruction* object,
Andreas Gampea5b09a62016-11-17 15:21:22 -08002448 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +00002449 uint32_t dex_pc) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +00002450 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko175e7862018-03-27 09:03:13 +00002451 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
2452 Handle<mirror::Class> klass = ResolveClass(soa, type_index);
Vladimir Marko2f40d242020-04-08 12:56:45 +01002453 bool needs_access_check = LoadClassNeedsAccessCheck(klass.Get());
Vladimir Marko175e7862018-03-27 09:03:13 +00002454 TypeCheckKind check_kind = HSharpening::ComputeTypeCheckKind(
Vladimir Markodc4bcce2018-06-21 16:15:42 +01002455 klass.Get(), code_generator_, needs_access_check);
Vladimir Marko175e7862018-03-27 09:03:13 +00002456
2457 HInstruction* class_or_null = nullptr;
2458 HIntConstant* bitstring_path_to_root = nullptr;
2459 HIntConstant* bitstring_mask = nullptr;
2460 if (check_kind == TypeCheckKind::kBitstringCheck) {
2461 // TODO: Allow using the bitstring check also if we need an access check.
2462 DCHECK(!needs_access_check);
2463 class_or_null = graph_->GetNullConstant(dex_pc);
2464 MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_);
2465 uint32_t path_to_root =
2466 SubtypeCheck<ObjPtr<mirror::Class>>::GetEncodedPathToRootForTarget(klass.Get());
2467 uint32_t mask = SubtypeCheck<ObjPtr<mirror::Class>>::GetEncodedPathToRootMask(klass.Get());
2468 bitstring_path_to_root = graph_->GetIntConstant(static_cast<int32_t>(path_to_root), dex_pc);
2469 bitstring_mask = graph_->GetIntConstant(static_cast<int32_t>(mask), dex_pc);
2470 } else {
Vladimir Markoa2c211c2018-11-01 09:50:52 +00002471 class_or_null = BuildLoadClass(type_index, dex_file, klass, dex_pc, needs_access_check);
Vladimir Marko175e7862018-03-27 09:03:13 +00002472 }
2473 DCHECK(class_or_null != nullptr);
2474
Andra Danciu49a19f32020-08-27 12:44:25 +00002475 if (is_instance_of) {
Vladimir Marko175e7862018-03-27 09:03:13 +00002476 AppendInstruction(new (allocator_) HInstanceOf(object,
2477 class_or_null,
2478 check_kind,
2479 klass,
2480 dex_pc,
2481 allocator_,
2482 bitstring_path_to_root,
2483 bitstring_mask));
David Brazdildee58d62016-04-07 09:54:26 +00002484 } else {
David Brazdildee58d62016-04-07 09:54:26 +00002485 // We emit a CheckCast followed by a BoundType. CheckCast is a statement
2486 // which may throw. If it succeeds BoundType sets the new type of `object`
2487 // for all subsequent uses.
Vladimir Marko175e7862018-03-27 09:03:13 +00002488 AppendInstruction(
2489 new (allocator_) HCheckCast(object,
2490 class_or_null,
2491 check_kind,
2492 klass,
2493 dex_pc,
2494 allocator_,
2495 bitstring_path_to_root,
2496 bitstring_mask));
Vladimir Markoca6fff82017-10-03 14:49:14 +01002497 AppendInstruction(new (allocator_) HBoundType(object, dex_pc));
Andra Danciu49a19f32020-08-27 12:44:25 +00002498 }
2499}
2500
2501void HInstructionBuilder::BuildTypeCheck(const Instruction& instruction,
2502 uint8_t destination,
2503 uint8_t reference,
2504 dex::TypeIndex type_index,
2505 uint32_t dex_pc) {
2506 HInstruction* object = LoadLocal(reference, DataType::Type::kReference);
2507 bool is_instance_of = instruction.Opcode() == Instruction::INSTANCE_OF;
2508
2509 BuildTypeCheck(is_instance_of, object, type_index, dex_pc);
2510
2511 if (is_instance_of) {
2512 UpdateLocal(destination, current_block_->GetLastInstruction());
2513 } else {
2514 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
David Brazdildee58d62016-04-07 09:54:26 +00002515 UpdateLocal(reference, current_block_->GetLastInstruction());
2516 }
2517}
2518
David Brazdildee58d62016-04-07 09:54:26 +00002519bool HInstructionBuilder::CanDecodeQuickenedInfo() const {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002520 return !quicken_info_.IsNull();
David Brazdildee58d62016-04-07 09:54:26 +00002521}
2522
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002523uint16_t HInstructionBuilder::LookupQuickenedInfo(uint32_t quicken_index) {
2524 DCHECK(CanDecodeQuickenedInfo());
2525 return quicken_info_.GetData(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00002526}
2527
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002528bool HInstructionBuilder::ProcessDexInstruction(const Instruction& instruction,
2529 uint32_t dex_pc,
2530 size_t quicken_index) {
David Brazdildee58d62016-04-07 09:54:26 +00002531 switch (instruction.Opcode()) {
2532 case Instruction::CONST_4: {
2533 int32_t register_index = instruction.VRegA();
2534 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n(), dex_pc);
2535 UpdateLocal(register_index, constant);
2536 break;
2537 }
2538
2539 case Instruction::CONST_16: {
2540 int32_t register_index = instruction.VRegA();
2541 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s(), dex_pc);
2542 UpdateLocal(register_index, constant);
2543 break;
2544 }
2545
2546 case Instruction::CONST: {
2547 int32_t register_index = instruction.VRegA();
2548 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i(), dex_pc);
2549 UpdateLocal(register_index, constant);
2550 break;
2551 }
2552
2553 case Instruction::CONST_HIGH16: {
2554 int32_t register_index = instruction.VRegA();
2555 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16, dex_pc);
2556 UpdateLocal(register_index, constant);
2557 break;
2558 }
2559
2560 case Instruction::CONST_WIDE_16: {
2561 int32_t register_index = instruction.VRegA();
2562 // Get 16 bits of constant value, sign extended to 64 bits.
2563 int64_t value = instruction.VRegB_21s();
2564 value <<= 48;
2565 value >>= 48;
2566 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
2567 UpdateLocal(register_index, constant);
2568 break;
2569 }
2570
2571 case Instruction::CONST_WIDE_32: {
2572 int32_t register_index = instruction.VRegA();
2573 // Get 32 bits of constant value, sign extended to 64 bits.
2574 int64_t value = instruction.VRegB_31i();
2575 value <<= 32;
2576 value >>= 32;
2577 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
2578 UpdateLocal(register_index, constant);
2579 break;
2580 }
2581
2582 case Instruction::CONST_WIDE: {
2583 int32_t register_index = instruction.VRegA();
2584 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l(), dex_pc);
2585 UpdateLocal(register_index, constant);
2586 break;
2587 }
2588
2589 case Instruction::CONST_WIDE_HIGH16: {
2590 int32_t register_index = instruction.VRegA();
2591 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
2592 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
2593 UpdateLocal(register_index, constant);
2594 break;
2595 }
2596
2597 // Note that the SSA building will refine the types.
2598 case Instruction::MOVE:
2599 case Instruction::MOVE_FROM16:
2600 case Instruction::MOVE_16: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002601 HInstruction* value = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +00002602 UpdateLocal(instruction.VRegA(), value);
2603 break;
2604 }
2605
2606 // Note that the SSA building will refine the types.
2607 case Instruction::MOVE_WIDE:
2608 case Instruction::MOVE_WIDE_FROM16:
2609 case Instruction::MOVE_WIDE_16: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002610 HInstruction* value = LoadLocal(instruction.VRegB(), DataType::Type::kInt64);
David Brazdildee58d62016-04-07 09:54:26 +00002611 UpdateLocal(instruction.VRegA(), value);
2612 break;
2613 }
2614
2615 case Instruction::MOVE_OBJECT:
2616 case Instruction::MOVE_OBJECT_16:
2617 case Instruction::MOVE_OBJECT_FROM16: {
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01002618 // The verifier has no notion of a null type, so a move-object of constant 0
2619 // will lead to the same constant 0 in the destination register. To mimic
2620 // this behavior, we just pretend we haven't seen a type change (int to reference)
2621 // for the 0 constant and phis. We rely on our type propagation to eventually get the
2622 // types correct.
2623 uint32_t reg_number = instruction.VRegB();
2624 HInstruction* value = (*current_locals_)[reg_number];
2625 if (value->IsIntConstant()) {
2626 DCHECK_EQ(value->AsIntConstant()->GetValue(), 0);
2627 } else if (value->IsPhi()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002628 DCHECK(value->GetType() == DataType::Type::kInt32 ||
2629 value->GetType() == DataType::Type::kReference);
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01002630 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002631 value = LoadLocal(reg_number, DataType::Type::kReference);
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01002632 }
David Brazdildee58d62016-04-07 09:54:26 +00002633 UpdateLocal(instruction.VRegA(), value);
2634 break;
2635 }
2636
2637 case Instruction::RETURN_VOID_NO_BARRIER:
2638 case Instruction::RETURN_VOID: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002639 BuildReturn(instruction, DataType::Type::kVoid, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002640 break;
2641 }
2642
2643#define IF_XX(comparison, cond) \
2644 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
2645 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
2646
2647 IF_XX(HEqual, EQ);
2648 IF_XX(HNotEqual, NE);
2649 IF_XX(HLessThan, LT);
2650 IF_XX(HLessThanOrEqual, LE);
2651 IF_XX(HGreaterThan, GT);
2652 IF_XX(HGreaterThanOrEqual, GE);
2653
2654 case Instruction::GOTO:
2655 case Instruction::GOTO_16:
2656 case Instruction::GOTO_32: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002657 AppendInstruction(new (allocator_) HGoto(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002658 current_block_ = nullptr;
2659 break;
2660 }
2661
2662 case Instruction::RETURN: {
2663 BuildReturn(instruction, return_type_, dex_pc);
2664 break;
2665 }
2666
2667 case Instruction::RETURN_OBJECT: {
2668 BuildReturn(instruction, return_type_, dex_pc);
2669 break;
2670 }
2671
2672 case Instruction::RETURN_WIDE: {
2673 BuildReturn(instruction, return_type_, dex_pc);
2674 break;
2675 }
2676
2677 case Instruction::INVOKE_DIRECT:
2678 case Instruction::INVOKE_INTERFACE:
2679 case Instruction::INVOKE_STATIC:
2680 case Instruction::INVOKE_SUPER:
2681 case Instruction::INVOKE_VIRTUAL:
2682 case Instruction::INVOKE_VIRTUAL_QUICK: {
2683 uint16_t method_idx;
2684 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_QUICK) {
2685 if (!CanDecodeQuickenedInfo()) {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002686 VLOG(compiler) << "Not compiled: Could not decode quickened instruction "
2687 << instruction.Opcode();
David Brazdildee58d62016-04-07 09:54:26 +00002688 return false;
2689 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002690 method_idx = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00002691 } else {
2692 method_idx = instruction.VRegB_35c();
2693 }
David Brazdildee58d62016-04-07 09:54:26 +00002694 uint32_t args[5];
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002695 uint32_t number_of_vreg_arguments = instruction.GetVarArgs(args);
2696 VarArgsInstructionOperands operands(args, number_of_vreg_arguments);
2697 if (!BuildInvoke(instruction, dex_pc, method_idx, operands)) {
David Brazdildee58d62016-04-07 09:54:26 +00002698 return false;
2699 }
2700 break;
2701 }
2702
2703 case Instruction::INVOKE_DIRECT_RANGE:
2704 case Instruction::INVOKE_INTERFACE_RANGE:
2705 case Instruction::INVOKE_STATIC_RANGE:
2706 case Instruction::INVOKE_SUPER_RANGE:
2707 case Instruction::INVOKE_VIRTUAL_RANGE:
2708 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2709 uint16_t method_idx;
2710 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK) {
2711 if (!CanDecodeQuickenedInfo()) {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002712 VLOG(compiler) << "Not compiled: Could not decode quickened instruction "
2713 << instruction.Opcode();
David Brazdildee58d62016-04-07 09:54:26 +00002714 return false;
2715 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002716 method_idx = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00002717 } else {
2718 method_idx = instruction.VRegB_3rc();
2719 }
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002720 RangeInstructionOperands operands(instruction.VRegC(), instruction.VRegA_3rc());
2721 if (!BuildInvoke(instruction, dex_pc, method_idx, operands)) {
David Brazdildee58d62016-04-07 09:54:26 +00002722 return false;
2723 }
2724 break;
2725 }
2726
Orion Hodsonac141392017-01-13 11:53:47 +00002727 case Instruction::INVOKE_POLYMORPHIC: {
2728 uint16_t method_idx = instruction.VRegB_45cc();
Orion Hodson06d10a72018-05-14 08:53:38 +01002729 dex::ProtoIndex proto_idx(instruction.VRegH_45cc());
Orion Hodsonac141392017-01-13 11:53:47 +00002730 uint32_t args[5];
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002731 uint32_t number_of_vreg_arguments = instruction.GetVarArgs(args);
2732 VarArgsInstructionOperands operands(args, number_of_vreg_arguments);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002733 return BuildInvokePolymorphic(dex_pc, method_idx, proto_idx, operands);
Orion Hodsonac141392017-01-13 11:53:47 +00002734 }
2735
2736 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
2737 uint16_t method_idx = instruction.VRegB_4rcc();
Orion Hodson06d10a72018-05-14 08:53:38 +01002738 dex::ProtoIndex proto_idx(instruction.VRegH_4rcc());
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002739 RangeInstructionOperands operands(instruction.VRegC_4rcc(), instruction.VRegA_4rcc());
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002740 return BuildInvokePolymorphic(dex_pc, method_idx, proto_idx, operands);
2741 }
2742
2743 case Instruction::INVOKE_CUSTOM: {
2744 uint16_t call_site_idx = instruction.VRegB_35c();
2745 uint32_t args[5];
2746 uint32_t number_of_vreg_arguments = instruction.GetVarArgs(args);
2747 VarArgsInstructionOperands operands(args, number_of_vreg_arguments);
2748 return BuildInvokeCustom(dex_pc, call_site_idx, operands);
2749 }
2750
2751 case Instruction::INVOKE_CUSTOM_RANGE: {
2752 uint16_t call_site_idx = instruction.VRegB_3rc();
2753 RangeInstructionOperands operands(instruction.VRegC_3rc(), instruction.VRegA_3rc());
2754 return BuildInvokeCustom(dex_pc, call_site_idx, operands);
Orion Hodsonac141392017-01-13 11:53:47 +00002755 }
2756
David Brazdildee58d62016-04-07 09:54:26 +00002757 case Instruction::NEG_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002758 Unop_12x<HNeg>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002759 break;
2760 }
2761
2762 case Instruction::NEG_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002763 Unop_12x<HNeg>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002764 break;
2765 }
2766
2767 case Instruction::NEG_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002768 Unop_12x<HNeg>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002769 break;
2770 }
2771
2772 case Instruction::NEG_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002773 Unop_12x<HNeg>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002774 break;
2775 }
2776
2777 case Instruction::NOT_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002778 Unop_12x<HNot>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002779 break;
2780 }
2781
2782 case Instruction::NOT_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002783 Unop_12x<HNot>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002784 break;
2785 }
2786
2787 case Instruction::INT_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002788 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002789 break;
2790 }
2791
2792 case Instruction::INT_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002793 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002794 break;
2795 }
2796
2797 case Instruction::INT_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002798 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002799 break;
2800 }
2801
2802 case Instruction::LONG_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002803 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002804 break;
2805 }
2806
2807 case Instruction::LONG_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002808 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002809 break;
2810 }
2811
2812 case Instruction::LONG_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002813 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002814 break;
2815 }
2816
2817 case Instruction::FLOAT_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002818 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002819 break;
2820 }
2821
2822 case Instruction::FLOAT_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002823 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002824 break;
2825 }
2826
2827 case Instruction::FLOAT_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002828 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002829 break;
2830 }
2831
2832 case Instruction::DOUBLE_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002833 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002834 break;
2835 }
2836
2837 case Instruction::DOUBLE_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002838 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002839 break;
2840 }
2841
2842 case Instruction::DOUBLE_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002843 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002844 break;
2845 }
2846
2847 case Instruction::INT_TO_BYTE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002848 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt8, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002849 break;
2850 }
2851
2852 case Instruction::INT_TO_SHORT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002853 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt16, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002854 break;
2855 }
2856
2857 case Instruction::INT_TO_CHAR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002858 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kUint16, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002859 break;
2860 }
2861
2862 case Instruction::ADD_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002863 Binop_23x<HAdd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002864 break;
2865 }
2866
2867 case Instruction::ADD_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002868 Binop_23x<HAdd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002869 break;
2870 }
2871
2872 case Instruction::ADD_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002873 Binop_23x<HAdd>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002874 break;
2875 }
2876
2877 case Instruction::ADD_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002878 Binop_23x<HAdd>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002879 break;
2880 }
2881
2882 case Instruction::SUB_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002883 Binop_23x<HSub>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002884 break;
2885 }
2886
2887 case Instruction::SUB_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002888 Binop_23x<HSub>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002889 break;
2890 }
2891
2892 case Instruction::SUB_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002893 Binop_23x<HSub>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002894 break;
2895 }
2896
2897 case Instruction::SUB_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002898 Binop_23x<HSub>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002899 break;
2900 }
2901
2902 case Instruction::ADD_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002903 Binop_12x<HAdd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002904 break;
2905 }
2906
2907 case Instruction::MUL_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002908 Binop_23x<HMul>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002909 break;
2910 }
2911
2912 case Instruction::MUL_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002913 Binop_23x<HMul>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002914 break;
2915 }
2916
2917 case Instruction::MUL_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002918 Binop_23x<HMul>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002919 break;
2920 }
2921
2922 case Instruction::MUL_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002923 Binop_23x<HMul>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002924 break;
2925 }
2926
2927 case Instruction::DIV_INT: {
2928 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002929 dex_pc, DataType::Type::kInt32, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002930 break;
2931 }
2932
2933 case Instruction::DIV_LONG: {
2934 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002935 dex_pc, DataType::Type::kInt64, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002936 break;
2937 }
2938
2939 case Instruction::DIV_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002940 Binop_23x<HDiv>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002941 break;
2942 }
2943
2944 case Instruction::DIV_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002945 Binop_23x<HDiv>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002946 break;
2947 }
2948
2949 case Instruction::REM_INT: {
2950 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002951 dex_pc, DataType::Type::kInt32, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002952 break;
2953 }
2954
2955 case Instruction::REM_LONG: {
2956 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002957 dex_pc, DataType::Type::kInt64, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002958 break;
2959 }
2960
2961 case Instruction::REM_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002962 Binop_23x<HRem>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002963 break;
2964 }
2965
2966 case Instruction::REM_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002967 Binop_23x<HRem>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002968 break;
2969 }
2970
2971 case Instruction::AND_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002972 Binop_23x<HAnd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002973 break;
2974 }
2975
2976 case Instruction::AND_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002977 Binop_23x<HAnd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002978 break;
2979 }
2980
2981 case Instruction::SHL_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002982 Binop_23x_shift<HShl>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002983 break;
2984 }
2985
2986 case Instruction::SHL_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002987 Binop_23x_shift<HShl>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002988 break;
2989 }
2990
2991 case Instruction::SHR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002992 Binop_23x_shift<HShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002993 break;
2994 }
2995
2996 case Instruction::SHR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002997 Binop_23x_shift<HShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002998 break;
2999 }
3000
3001 case Instruction::USHR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003002 Binop_23x_shift<HUShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003003 break;
3004 }
3005
3006 case Instruction::USHR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003007 Binop_23x_shift<HUShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003008 break;
3009 }
3010
3011 case Instruction::OR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003012 Binop_23x<HOr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003013 break;
3014 }
3015
3016 case Instruction::OR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003017 Binop_23x<HOr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003018 break;
3019 }
3020
3021 case Instruction::XOR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003022 Binop_23x<HXor>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003023 break;
3024 }
3025
3026 case Instruction::XOR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003027 Binop_23x<HXor>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003028 break;
3029 }
3030
3031 case Instruction::ADD_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003032 Binop_12x<HAdd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003033 break;
3034 }
3035
3036 case Instruction::ADD_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003037 Binop_12x<HAdd>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003038 break;
3039 }
3040
3041 case Instruction::ADD_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003042 Binop_12x<HAdd>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003043 break;
3044 }
3045
3046 case Instruction::SUB_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003047 Binop_12x<HSub>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003048 break;
3049 }
3050
3051 case Instruction::SUB_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003052 Binop_12x<HSub>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003053 break;
3054 }
3055
3056 case Instruction::SUB_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003057 Binop_12x<HSub>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003058 break;
3059 }
3060
3061 case Instruction::SUB_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003062 Binop_12x<HSub>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003063 break;
3064 }
3065
3066 case Instruction::MUL_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003067 Binop_12x<HMul>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003068 break;
3069 }
3070
3071 case Instruction::MUL_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003072 Binop_12x<HMul>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003073 break;
3074 }
3075
3076 case Instruction::MUL_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003077 Binop_12x<HMul>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003078 break;
3079 }
3080
3081 case Instruction::MUL_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003082 Binop_12x<HMul>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003083 break;
3084 }
3085
3086 case Instruction::DIV_INT_2ADDR: {
3087 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003088 dex_pc, DataType::Type::kInt32, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00003089 break;
3090 }
3091
3092 case Instruction::DIV_LONG_2ADDR: {
3093 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003094 dex_pc, DataType::Type::kInt64, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00003095 break;
3096 }
3097
3098 case Instruction::REM_INT_2ADDR: {
3099 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003100 dex_pc, DataType::Type::kInt32, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00003101 break;
3102 }
3103
3104 case Instruction::REM_LONG_2ADDR: {
3105 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003106 dex_pc, DataType::Type::kInt64, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00003107 break;
3108 }
3109
3110 case Instruction::REM_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003111 Binop_12x<HRem>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003112 break;
3113 }
3114
3115 case Instruction::REM_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003116 Binop_12x<HRem>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003117 break;
3118 }
3119
3120 case Instruction::SHL_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003121 Binop_12x_shift<HShl>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003122 break;
3123 }
3124
3125 case Instruction::SHL_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003126 Binop_12x_shift<HShl>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003127 break;
3128 }
3129
3130 case Instruction::SHR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003131 Binop_12x_shift<HShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003132 break;
3133 }
3134
3135 case Instruction::SHR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003136 Binop_12x_shift<HShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003137 break;
3138 }
3139
3140 case Instruction::USHR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003141 Binop_12x_shift<HUShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003142 break;
3143 }
3144
3145 case Instruction::USHR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003146 Binop_12x_shift<HUShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003147 break;
3148 }
3149
3150 case Instruction::DIV_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003151 Binop_12x<HDiv>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003152 break;
3153 }
3154
3155 case Instruction::DIV_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003156 Binop_12x<HDiv>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003157 break;
3158 }
3159
3160 case Instruction::AND_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003161 Binop_12x<HAnd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003162 break;
3163 }
3164
3165 case Instruction::AND_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003166 Binop_12x<HAnd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003167 break;
3168 }
3169
3170 case Instruction::OR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003171 Binop_12x<HOr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003172 break;
3173 }
3174
3175 case Instruction::OR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003176 Binop_12x<HOr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003177 break;
3178 }
3179
3180 case Instruction::XOR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003181 Binop_12x<HXor>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003182 break;
3183 }
3184
3185 case Instruction::XOR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003186 Binop_12x<HXor>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003187 break;
3188 }
3189
3190 case Instruction::ADD_INT_LIT16: {
3191 Binop_22s<HAdd>(instruction, false, dex_pc);
3192 break;
3193 }
3194
3195 case Instruction::AND_INT_LIT16: {
3196 Binop_22s<HAnd>(instruction, false, dex_pc);
3197 break;
3198 }
3199
3200 case Instruction::OR_INT_LIT16: {
3201 Binop_22s<HOr>(instruction, false, dex_pc);
3202 break;
3203 }
3204
3205 case Instruction::XOR_INT_LIT16: {
3206 Binop_22s<HXor>(instruction, false, dex_pc);
3207 break;
3208 }
3209
3210 case Instruction::RSUB_INT: {
3211 Binop_22s<HSub>(instruction, true, dex_pc);
3212 break;
3213 }
3214
3215 case Instruction::MUL_INT_LIT16: {
3216 Binop_22s<HMul>(instruction, false, dex_pc);
3217 break;
3218 }
3219
3220 case Instruction::ADD_INT_LIT8: {
3221 Binop_22b<HAdd>(instruction, false, dex_pc);
3222 break;
3223 }
3224
3225 case Instruction::AND_INT_LIT8: {
3226 Binop_22b<HAnd>(instruction, false, dex_pc);
3227 break;
3228 }
3229
3230 case Instruction::OR_INT_LIT8: {
3231 Binop_22b<HOr>(instruction, false, dex_pc);
3232 break;
3233 }
3234
3235 case Instruction::XOR_INT_LIT8: {
3236 Binop_22b<HXor>(instruction, false, dex_pc);
3237 break;
3238 }
3239
3240 case Instruction::RSUB_INT_LIT8: {
3241 Binop_22b<HSub>(instruction, true, dex_pc);
3242 break;
3243 }
3244
3245 case Instruction::MUL_INT_LIT8: {
3246 Binop_22b<HMul>(instruction, false, dex_pc);
3247 break;
3248 }
3249
3250 case Instruction::DIV_INT_LIT16:
3251 case Instruction::DIV_INT_LIT8: {
3252 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003253 dex_pc, DataType::Type::kInt32, true, true);
David Brazdildee58d62016-04-07 09:54:26 +00003254 break;
3255 }
3256
3257 case Instruction::REM_INT_LIT16:
3258 case Instruction::REM_INT_LIT8: {
3259 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003260 dex_pc, DataType::Type::kInt32, true, false);
David Brazdildee58d62016-04-07 09:54:26 +00003261 break;
3262 }
3263
3264 case Instruction::SHL_INT_LIT8: {
3265 Binop_22b<HShl>(instruction, false, dex_pc);
3266 break;
3267 }
3268
3269 case Instruction::SHR_INT_LIT8: {
3270 Binop_22b<HShr>(instruction, false, dex_pc);
3271 break;
3272 }
3273
3274 case Instruction::USHR_INT_LIT8: {
3275 Binop_22b<HUShr>(instruction, false, dex_pc);
3276 break;
3277 }
3278
3279 case Instruction::NEW_INSTANCE: {
Igor Murashkin79d8fa72017-04-18 09:37:23 -07003280 HNewInstance* new_instance =
3281 BuildNewInstance(dex::TypeIndex(instruction.VRegB_21c()), dex_pc);
3282 DCHECK(new_instance != nullptr);
3283
David Brazdildee58d62016-04-07 09:54:26 +00003284 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
Igor Murashkin79d8fa72017-04-18 09:37:23 -07003285 BuildConstructorFenceForAllocation(new_instance);
David Brazdildee58d62016-04-07 09:54:26 +00003286 break;
3287 }
3288
3289 case Instruction::NEW_ARRAY: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003290 dex::TypeIndex type_index(instruction.VRegC_22c());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003291 HInstruction* length = LoadLocal(instruction.VRegB_22c(), DataType::Type::kInt32);
Vladimir Markob5461632018-10-15 14:24:21 +01003292 HNewArray* new_array = BuildNewArray(dex_pc, type_index, length);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07003293
David Brazdildee58d62016-04-07 09:54:26 +00003294 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
Igor Murashkin79d8fa72017-04-18 09:37:23 -07003295 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00003296 break;
3297 }
3298
3299 case Instruction::FILLED_NEW_ARRAY: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003300 dex::TypeIndex type_index(instruction.VRegB_35c());
David Brazdildee58d62016-04-07 09:54:26 +00003301 uint32_t args[5];
Treehugger Robot2c5827a2018-05-17 22:26:08 +00003302 uint32_t number_of_vreg_arguments = instruction.GetVarArgs(args);
3303 VarArgsInstructionOperands operands(args, number_of_vreg_arguments);
3304 HNewArray* new_array = BuildFilledNewArray(dex_pc, type_index, operands);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07003305 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00003306 break;
3307 }
3308
3309 case Instruction::FILLED_NEW_ARRAY_RANGE: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003310 dex::TypeIndex type_index(instruction.VRegB_3rc());
Treehugger Robot2c5827a2018-05-17 22:26:08 +00003311 RangeInstructionOperands operands(instruction.VRegC_3rc(), instruction.VRegA_3rc());
3312 HNewArray* new_array = BuildFilledNewArray(dex_pc, type_index, operands);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07003313 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00003314 break;
3315 }
3316
3317 case Instruction::FILL_ARRAY_DATA: {
3318 BuildFillArrayData(instruction, dex_pc);
3319 break;
3320 }
3321
3322 case Instruction::MOVE_RESULT:
3323 case Instruction::MOVE_RESULT_WIDE:
3324 case Instruction::MOVE_RESULT_OBJECT: {
3325 DCHECK(latest_result_ != nullptr);
3326 UpdateLocal(instruction.VRegA(), latest_result_);
3327 latest_result_ = nullptr;
3328 break;
3329 }
3330
3331 case Instruction::CMP_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003332 Binop_23x_cmp(instruction, DataType::Type::kInt64, ComparisonBias::kNoBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003333 break;
3334 }
3335
3336 case Instruction::CMPG_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003337 Binop_23x_cmp(instruction, DataType::Type::kFloat32, ComparisonBias::kGtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003338 break;
3339 }
3340
3341 case Instruction::CMPG_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003342 Binop_23x_cmp(instruction, DataType::Type::kFloat64, ComparisonBias::kGtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003343 break;
3344 }
3345
3346 case Instruction::CMPL_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003347 Binop_23x_cmp(instruction, DataType::Type::kFloat32, ComparisonBias::kLtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003348 break;
3349 }
3350
3351 case Instruction::CMPL_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003352 Binop_23x_cmp(instruction, DataType::Type::kFloat64, ComparisonBias::kLtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003353 break;
3354 }
3355
3356 case Instruction::NOP:
3357 break;
3358
3359 case Instruction::IGET:
3360 case Instruction::IGET_QUICK:
3361 case Instruction::IGET_WIDE:
3362 case Instruction::IGET_WIDE_QUICK:
3363 case Instruction::IGET_OBJECT:
3364 case Instruction::IGET_OBJECT_QUICK:
3365 case Instruction::IGET_BOOLEAN:
3366 case Instruction::IGET_BOOLEAN_QUICK:
3367 case Instruction::IGET_BYTE:
3368 case Instruction::IGET_BYTE_QUICK:
3369 case Instruction::IGET_CHAR:
3370 case Instruction::IGET_CHAR_QUICK:
3371 case Instruction::IGET_SHORT:
3372 case Instruction::IGET_SHORT_QUICK: {
Andreas Gampe3db70682018-12-26 15:12:03 -08003373 if (!BuildInstanceFieldAccess(instruction, dex_pc, /* is_put= */ false, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00003374 return false;
3375 }
3376 break;
3377 }
3378
3379 case Instruction::IPUT:
3380 case Instruction::IPUT_QUICK:
3381 case Instruction::IPUT_WIDE:
3382 case Instruction::IPUT_WIDE_QUICK:
3383 case Instruction::IPUT_OBJECT:
3384 case Instruction::IPUT_OBJECT_QUICK:
3385 case Instruction::IPUT_BOOLEAN:
3386 case Instruction::IPUT_BOOLEAN_QUICK:
3387 case Instruction::IPUT_BYTE:
3388 case Instruction::IPUT_BYTE_QUICK:
3389 case Instruction::IPUT_CHAR:
3390 case Instruction::IPUT_CHAR_QUICK:
3391 case Instruction::IPUT_SHORT:
3392 case Instruction::IPUT_SHORT_QUICK: {
Andreas Gampe3db70682018-12-26 15:12:03 -08003393 if (!BuildInstanceFieldAccess(instruction, dex_pc, /* is_put= */ true, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00003394 return false;
3395 }
3396 break;
3397 }
3398
3399 case Instruction::SGET:
3400 case Instruction::SGET_WIDE:
3401 case Instruction::SGET_OBJECT:
3402 case Instruction::SGET_BOOLEAN:
3403 case Instruction::SGET_BYTE:
3404 case Instruction::SGET_CHAR:
3405 case Instruction::SGET_SHORT: {
Andreas Gampe3db70682018-12-26 15:12:03 -08003406 BuildStaticFieldAccess(instruction, dex_pc, /* is_put= */ false);
David Brazdildee58d62016-04-07 09:54:26 +00003407 break;
3408 }
3409
3410 case Instruction::SPUT:
3411 case Instruction::SPUT_WIDE:
3412 case Instruction::SPUT_OBJECT:
3413 case Instruction::SPUT_BOOLEAN:
3414 case Instruction::SPUT_BYTE:
3415 case Instruction::SPUT_CHAR:
3416 case Instruction::SPUT_SHORT: {
Andreas Gampe3db70682018-12-26 15:12:03 -08003417 BuildStaticFieldAccess(instruction, dex_pc, /* is_put= */ true);
David Brazdildee58d62016-04-07 09:54:26 +00003418 break;
3419 }
3420
3421#define ARRAY_XX(kind, anticipated_type) \
3422 case Instruction::AGET##kind: { \
3423 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
3424 break; \
3425 } \
3426 case Instruction::APUT##kind: { \
3427 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
3428 break; \
3429 }
3430
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003431 ARRAY_XX(, DataType::Type::kInt32);
3432 ARRAY_XX(_WIDE, DataType::Type::kInt64);
3433 ARRAY_XX(_OBJECT, DataType::Type::kReference);
3434 ARRAY_XX(_BOOLEAN, DataType::Type::kBool);
3435 ARRAY_XX(_BYTE, DataType::Type::kInt8);
3436 ARRAY_XX(_CHAR, DataType::Type::kUint16);
3437 ARRAY_XX(_SHORT, DataType::Type::kInt16);
David Brazdildee58d62016-04-07 09:54:26 +00003438
3439 case Instruction::ARRAY_LENGTH: {
David Brazdilc120bbe2016-04-22 16:57:00 +01003440 HInstruction* object = LoadNullCheckedLocal(instruction.VRegB_12x(), dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01003441 AppendInstruction(new (allocator_) HArrayLength(object, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00003442 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
3443 break;
3444 }
3445
3446 case Instruction::CONST_STRING: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08003447 dex::StringIndex string_index(instruction.VRegB_21c());
Vladimir Marko28e012a2017-12-07 11:22:59 +00003448 BuildLoadString(string_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003449 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
3450 break;
3451 }
3452
3453 case Instruction::CONST_STRING_JUMBO: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08003454 dex::StringIndex string_index(instruction.VRegB_31c());
Vladimir Marko28e012a2017-12-07 11:22:59 +00003455 BuildLoadString(string_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003456 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
3457 break;
3458 }
3459
3460 case Instruction::CONST_CLASS: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003461 dex::TypeIndex type_index(instruction.VRegB_21c());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00003462 BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003463 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
3464 break;
3465 }
3466
Orion Hodsondbaa5c72018-05-10 08:22:46 +01003467 case Instruction::CONST_METHOD_HANDLE: {
3468 uint16_t method_handle_idx = instruction.VRegB_21c();
3469 BuildLoadMethodHandle(method_handle_idx, dex_pc);
3470 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
3471 break;
3472 }
3473
Orion Hodson18259d72018-04-12 11:18:23 +01003474 case Instruction::CONST_METHOD_TYPE: {
Orion Hodson06d10a72018-05-14 08:53:38 +01003475 dex::ProtoIndex proto_idx(instruction.VRegB_21c());
Orion Hodson18259d72018-04-12 11:18:23 +01003476 BuildLoadMethodType(proto_idx, dex_pc);
3477 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
3478 break;
3479 }
3480
David Brazdildee58d62016-04-07 09:54:26 +00003481 case Instruction::MOVE_EXCEPTION: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003482 AppendInstruction(new (allocator_) HLoadException(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00003483 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
Vladimir Markoca6fff82017-10-03 14:49:14 +01003484 AppendInstruction(new (allocator_) HClearException(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00003485 break;
3486 }
3487
3488 case Instruction::THROW: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003489 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01003490 AppendInstruction(new (allocator_) HThrow(exception, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00003491 // We finished building this block. Set the current block to null to avoid
3492 // adding dead instructions to it.
3493 current_block_ = nullptr;
3494 break;
3495 }
3496
3497 case Instruction::INSTANCE_OF: {
3498 uint8_t destination = instruction.VRegA_22c();
3499 uint8_t reference = instruction.VRegB_22c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08003500 dex::TypeIndex type_index(instruction.VRegC_22c());
David Brazdildee58d62016-04-07 09:54:26 +00003501 BuildTypeCheck(instruction, destination, reference, type_index, dex_pc);
3502 break;
3503 }
3504
3505 case Instruction::CHECK_CAST: {
3506 uint8_t reference = instruction.VRegA_21c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08003507 dex::TypeIndex type_index(instruction.VRegB_21c());
David Brazdildee58d62016-04-07 09:54:26 +00003508 BuildTypeCheck(instruction, -1, reference, type_index, dex_pc);
3509 break;
3510 }
3511
3512 case Instruction::MONITOR_ENTER: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003513 AppendInstruction(new (allocator_) HMonitorOperation(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003514 LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference),
David Brazdildee58d62016-04-07 09:54:26 +00003515 HMonitorOperation::OperationKind::kEnter,
3516 dex_pc));
Artem Serov2808be82018-12-20 19:15:11 +00003517 graph_->SetHasMonitorOperations(true);
David Brazdildee58d62016-04-07 09:54:26 +00003518 break;
3519 }
3520
3521 case Instruction::MONITOR_EXIT: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003522 AppendInstruction(new (allocator_) HMonitorOperation(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003523 LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference),
David Brazdildee58d62016-04-07 09:54:26 +00003524 HMonitorOperation::OperationKind::kExit,
3525 dex_pc));
Artem Serov2808be82018-12-20 19:15:11 +00003526 graph_->SetHasMonitorOperations(true);
David Brazdildee58d62016-04-07 09:54:26 +00003527 break;
3528 }
3529
3530 case Instruction::SPARSE_SWITCH:
3531 case Instruction::PACKED_SWITCH: {
3532 BuildSwitch(instruction, dex_pc);
3533 break;
3534 }
3535
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003536 case Instruction::UNUSED_3E:
3537 case Instruction::UNUSED_3F:
3538 case Instruction::UNUSED_40:
3539 case Instruction::UNUSED_41:
3540 case Instruction::UNUSED_42:
3541 case Instruction::UNUSED_43:
3542 case Instruction::UNUSED_79:
3543 case Instruction::UNUSED_7A:
3544 case Instruction::UNUSED_F3:
3545 case Instruction::UNUSED_F4:
3546 case Instruction::UNUSED_F5:
3547 case Instruction::UNUSED_F6:
3548 case Instruction::UNUSED_F7:
3549 case Instruction::UNUSED_F8:
3550 case Instruction::UNUSED_F9: {
David Brazdildee58d62016-04-07 09:54:26 +00003551 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07003552 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00003553 << " because of unhandled instruction "
3554 << instruction.Name();
Igor Murashkin1e065a52017-08-09 13:20:34 -07003555 MaybeRecordStat(compilation_stats_,
3556 MethodCompilationStat::kNotCompiledUnhandledInstruction);
David Brazdildee58d62016-04-07 09:54:26 +00003557 return false;
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003558 }
David Brazdildee58d62016-04-07 09:54:26 +00003559 }
3560 return true;
3561} // NOLINT(readability/fn_size)
3562
Vladimir Marko8d6768d2017-03-14 10:13:21 +00003563ObjPtr<mirror::Class> HInstructionBuilder::LookupResolvedType(
3564 dex::TypeIndex type_index,
3565 const DexCompilationUnit& compilation_unit) const {
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003566 return compilation_unit.GetClassLinker()->LookupResolvedType(
Vladimir Marko8d6768d2017-03-14 10:13:21 +00003567 type_index, compilation_unit.GetDexCache().Get(), compilation_unit.GetClassLoader().Get());
3568}
3569
3570ObjPtr<mirror::Class> HInstructionBuilder::LookupReferrerClass() const {
3571 // TODO: Cache the result in a Handle<mirror::Class>.
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003572 const dex::MethodId& method_id =
Vladimir Marko8d6768d2017-03-14 10:13:21 +00003573 dex_compilation_unit_->GetDexFile()->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
3574 return LookupResolvedType(method_id.class_idx_, *dex_compilation_unit_);
3575}
3576
David Brazdildee58d62016-04-07 09:54:26 +00003577} // namespace art