blob: 4364d39263aebec992c9afdaf33caa4207da9ca9 [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"
Andra Danciud0f71f22020-09-17 09:00:15 +000032#include "intrinsics.h"
33#include "intrinsics_utils.h"
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +000034#include "jit/jit.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010035#include "mirror/dex_cache.h"
Nicolas Geoffray58cc1cb2017-11-20 13:27:29 +000036#include "oat_file.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010037#include "optimizing_compiler_stats.h"
Nicolas Geoffray396198b2020-06-16 12:02:45 +010038#include "reflective_handle_scope-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070039#include "scoped_thread_state_change-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070040#include "sharpening.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010041#include "ssa_builder.h"
Andreas Gampea7c83ac2017-09-11 08:14:23 -070042#include "well_known_classes.h"
David Brazdildee58d62016-04-07 09:54:26 +000043
Vladimir Marko0a516052019-10-14 13:00:44 +000044namespace art {
David Brazdildee58d62016-04-07 09:54:26 +000045
Vladimir Markocfd65802020-08-18 09:29:51 +010046namespace {
47
48class SamePackageCompare {
49 public:
50 explicit SamePackageCompare(const DexCompilationUnit& dex_compilation_unit)
51 : dex_compilation_unit_(dex_compilation_unit) {}
52
53 bool operator()(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) {
54 if (klass->GetClassLoader() != dex_compilation_unit_.GetClassLoader().Get()) {
55 return false;
56 }
57 if (referrers_descriptor_ == nullptr) {
58 const DexFile* dex_file = dex_compilation_unit_.GetDexFile();
59 uint32_t referrers_method_idx = dex_compilation_unit_.GetDexMethodIndex();
60 referrers_descriptor_ =
61 dex_file->StringByTypeIdx(dex_file->GetMethodId(referrers_method_idx).class_idx_);
62 referrers_package_length_ = PackageLength(referrers_descriptor_);
63 }
64 std::string temp;
65 const char* klass_descriptor = klass->GetDescriptor(&temp);
66 size_t klass_package_length = PackageLength(klass_descriptor);
67 return (referrers_package_length_ == klass_package_length) &&
68 memcmp(referrers_descriptor_, klass_descriptor, referrers_package_length_) == 0;
69 };
70
71 private:
72 static size_t PackageLength(const char* descriptor) {
73 const char* slash_pos = strrchr(descriptor, '/');
74 return (slash_pos != nullptr) ? static_cast<size_t>(slash_pos - descriptor) : 0u;
75 }
76
77 const DexCompilationUnit& dex_compilation_unit_;
78 const char* referrers_descriptor_ = nullptr;
79 size_t referrers_package_length_ = 0u;
80};
81
82} // anonymous namespace
83
Mathieu Chartier808c7a52017-12-15 11:19:33 -080084HInstructionBuilder::HInstructionBuilder(HGraph* graph,
85 HBasicBlockBuilder* block_builder,
86 SsaBuilder* ssa_builder,
87 const DexFile* dex_file,
88 const CodeItemDebugInfoAccessor& accessor,
89 DataType::Type return_type,
90 const DexCompilationUnit* dex_compilation_unit,
91 const DexCompilationUnit* outer_compilation_unit,
Mathieu Chartier808c7a52017-12-15 11:19:33 -080092 CodeGenerator* code_generator,
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),
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800105 compilation_stats_(compiler_stats),
106 local_allocator_(local_allocator),
107 locals_for_(local_allocator->Adapter(kArenaAllocGraphBuilder)),
108 current_block_(nullptr),
109 current_locals_(nullptr),
110 latest_result_(nullptr),
111 current_this_parameter_(nullptr),
Vladimir Marko3b506202018-10-31 14:33:58 +0000112 loop_headers_(local_allocator->Adapter(kArenaAllocGraphBuilder)),
113 class_cache_(std::less<dex::TypeIndex>(), local_allocator->Adapter(kArenaAllocGraphBuilder)) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800114 loop_headers_.reserve(kDefaultNumberOfLoops);
115}
116
David Brazdildee58d62016-04-07 09:54:26 +0000117HBasicBlock* HInstructionBuilder::FindBlockStartingAt(uint32_t dex_pc) const {
118 return block_builder_->GetBlockAt(dex_pc);
119}
120
Vladimir Marko69d310e2017-10-09 14:12:23 +0100121inline ScopedArenaVector<HInstruction*>* HInstructionBuilder::GetLocalsFor(HBasicBlock* block) {
122 ScopedArenaVector<HInstruction*>* locals = &locals_for_[block->GetBlockId()];
David Brazdildee58d62016-04-07 09:54:26 +0000123 const size_t vregs = graph_->GetNumberOfVRegs();
Mingyao Yang01b47b02017-02-03 12:09:57 -0800124 if (locals->size() == vregs) {
125 return locals;
126 }
127 return GetLocalsForWithAllocation(block, locals, vregs);
128}
David Brazdildee58d62016-04-07 09:54:26 +0000129
Vladimir Marko69d310e2017-10-09 14:12:23 +0100130ScopedArenaVector<HInstruction*>* HInstructionBuilder::GetLocalsForWithAllocation(
Mingyao Yang01b47b02017-02-03 12:09:57 -0800131 HBasicBlock* block,
Vladimir Marko69d310e2017-10-09 14:12:23 +0100132 ScopedArenaVector<HInstruction*>* locals,
Mingyao Yang01b47b02017-02-03 12:09:57 -0800133 const size_t vregs) {
134 DCHECK_NE(locals->size(), vregs);
135 locals->resize(vregs, nullptr);
136 if (block->IsCatchBlock()) {
137 // We record incoming inputs of catch phis at throwing instructions and
138 // must therefore eagerly create the phis. Phis for undefined vregs will
139 // be deleted when the first throwing instruction with the vreg undefined
140 // is encountered. Unused phis will be removed by dead phi analysis.
141 for (size_t i = 0; i < vregs; ++i) {
142 // No point in creating the catch phi if it is already undefined at
143 // the first throwing instruction.
144 HInstruction* current_local_value = (*current_locals_)[i];
145 if (current_local_value != nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100146 HPhi* phi = new (allocator_) HPhi(
147 allocator_,
Mingyao Yang01b47b02017-02-03 12:09:57 -0800148 i,
149 0,
150 current_local_value->GetType());
151 block->AddPhi(phi);
152 (*locals)[i] = phi;
David Brazdildee58d62016-04-07 09:54:26 +0000153 }
154 }
155 }
156 return locals;
157}
158
Mingyao Yang01b47b02017-02-03 12:09:57 -0800159inline HInstruction* HInstructionBuilder::ValueOfLocalAt(HBasicBlock* block, size_t local) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100160 ScopedArenaVector<HInstruction*>* locals = GetLocalsFor(block);
David Brazdildee58d62016-04-07 09:54:26 +0000161 return (*locals)[local];
162}
163
164void HInstructionBuilder::InitializeBlockLocals() {
165 current_locals_ = GetLocalsFor(current_block_);
166
167 if (current_block_->IsCatchBlock()) {
168 // Catch phis were already created and inputs collected from throwing sites.
169 if (kIsDebugBuild) {
170 // Make sure there was at least one throwing instruction which initialized
171 // locals (guaranteed by HGraphBuilder) and that all try blocks have been
172 // visited already (from HTryBoundary scoping and reverse post order).
173 bool catch_block_visited = false;
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100174 for (HBasicBlock* current : graph_->GetReversePostOrder()) {
David Brazdildee58d62016-04-07 09:54:26 +0000175 if (current == current_block_) {
176 catch_block_visited = true;
177 } else if (current->IsTryBlock()) {
178 const HTryBoundary& try_entry = current->GetTryCatchInformation()->GetTryEntry();
179 if (try_entry.HasExceptionHandler(*current_block_)) {
180 DCHECK(!catch_block_visited) << "Catch block visited before its try block.";
181 }
182 }
183 }
184 DCHECK_EQ(current_locals_->size(), graph_->GetNumberOfVRegs())
185 << "No instructions throwing into a live catch block.";
186 }
187 } else if (current_block_->IsLoopHeader()) {
188 // If the block is a loop header, we know we only have visited the pre header
189 // because we are visiting in reverse post order. We create phis for all initialized
190 // locals from the pre header. Their inputs will be populated at the end of
191 // the analysis.
192 for (size_t local = 0; local < current_locals_->size(); ++local) {
193 HInstruction* incoming =
194 ValueOfLocalAt(current_block_->GetLoopInformation()->GetPreHeader(), local);
195 if (incoming != nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100196 HPhi* phi = new (allocator_) HPhi(
197 allocator_,
David Brazdildee58d62016-04-07 09:54:26 +0000198 local,
199 0,
200 incoming->GetType());
201 current_block_->AddPhi(phi);
202 (*current_locals_)[local] = phi;
203 }
204 }
205
206 // Save the loop header so that the last phase of the analysis knows which
207 // blocks need to be updated.
208 loop_headers_.push_back(current_block_);
209 } else if (current_block_->GetPredecessors().size() > 0) {
210 // All predecessors have already been visited because we are visiting in reverse post order.
211 // We merge the values of all locals, creating phis if those values differ.
212 for (size_t local = 0; local < current_locals_->size(); ++local) {
213 bool one_predecessor_has_no_value = false;
214 bool is_different = false;
215 HInstruction* value = ValueOfLocalAt(current_block_->GetPredecessors()[0], local);
216
217 for (HBasicBlock* predecessor : current_block_->GetPredecessors()) {
218 HInstruction* current = ValueOfLocalAt(predecessor, local);
219 if (current == nullptr) {
220 one_predecessor_has_no_value = true;
221 break;
222 } else if (current != value) {
223 is_different = true;
224 }
225 }
226
227 if (one_predecessor_has_no_value) {
228 // If one predecessor has no value for this local, we trust the verifier has
229 // successfully checked that there is a store dominating any read after this block.
230 continue;
231 }
232
233 if (is_different) {
234 HInstruction* first_input = ValueOfLocalAt(current_block_->GetPredecessors()[0], local);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100235 HPhi* phi = new (allocator_) HPhi(
236 allocator_,
David Brazdildee58d62016-04-07 09:54:26 +0000237 local,
238 current_block_->GetPredecessors().size(),
239 first_input->GetType());
240 for (size_t i = 0; i < current_block_->GetPredecessors().size(); i++) {
241 HInstruction* pred_value = ValueOfLocalAt(current_block_->GetPredecessors()[i], local);
242 phi->SetRawInputAt(i, pred_value);
243 }
244 current_block_->AddPhi(phi);
245 value = phi;
246 }
247 (*current_locals_)[local] = value;
248 }
249 }
250}
251
252void HInstructionBuilder::PropagateLocalsToCatchBlocks() {
253 const HTryBoundary& try_entry = current_block_->GetTryCatchInformation()->GetTryEntry();
254 for (HBasicBlock* catch_block : try_entry.GetExceptionHandlers()) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100255 ScopedArenaVector<HInstruction*>* handler_locals = GetLocalsFor(catch_block);
David Brazdildee58d62016-04-07 09:54:26 +0000256 DCHECK_EQ(handler_locals->size(), current_locals_->size());
257 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
258 HInstruction* handler_value = (*handler_locals)[vreg];
259 if (handler_value == nullptr) {
260 // Vreg was undefined at a previously encountered throwing instruction
261 // and the catch phi was deleted. Do not record the local value.
262 continue;
263 }
264 DCHECK(handler_value->IsPhi());
265
266 HInstruction* local_value = (*current_locals_)[vreg];
267 if (local_value == nullptr) {
268 // This is the first instruction throwing into `catch_block` where
269 // `vreg` is undefined. Delete the catch phi.
270 catch_block->RemovePhi(handler_value->AsPhi());
271 (*handler_locals)[vreg] = nullptr;
272 } else {
273 // Vreg has been defined at all instructions throwing into `catch_block`
274 // encountered so far. Record the local value in the catch phi.
275 handler_value->AsPhi()->AddInput(local_value);
276 }
277 }
278 }
279}
280
281void HInstructionBuilder::AppendInstruction(HInstruction* instruction) {
282 current_block_->AddInstruction(instruction);
283 InitializeInstruction(instruction);
284}
285
286void HInstructionBuilder::InsertInstructionAtTop(HInstruction* instruction) {
287 if (current_block_->GetInstructions().IsEmpty()) {
288 current_block_->AddInstruction(instruction);
289 } else {
290 current_block_->InsertInstructionBefore(instruction, current_block_->GetFirstInstruction());
291 }
292 InitializeInstruction(instruction);
293}
294
295void HInstructionBuilder::InitializeInstruction(HInstruction* instruction) {
296 if (instruction->NeedsEnvironment()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100297 HEnvironment* environment = new (allocator_) HEnvironment(
298 allocator_,
David Brazdildee58d62016-04-07 09:54:26 +0000299 current_locals_->size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000300 graph_->GetArtMethod(),
David Brazdildee58d62016-04-07 09:54:26 +0000301 instruction->GetDexPc(),
David Brazdildee58d62016-04-07 09:54:26 +0000302 instruction);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100303 environment->CopyFrom(ArrayRef<HInstruction* const>(*current_locals_));
David Brazdildee58d62016-04-07 09:54:26 +0000304 instruction->SetRawEnvironment(environment);
305 }
306}
307
David Brazdilc120bbe2016-04-22 16:57:00 +0100308HInstruction* HInstructionBuilder::LoadNullCheckedLocal(uint32_t register_index, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100309 HInstruction* ref = LoadLocal(register_index, DataType::Type::kReference);
David Brazdilc120bbe2016-04-22 16:57:00 +0100310 if (!ref->CanBeNull()) {
311 return ref;
312 }
313
Vladimir Markoca6fff82017-10-03 14:49:14 +0100314 HNullCheck* null_check = new (allocator_) HNullCheck(ref, dex_pc);
David Brazdilc120bbe2016-04-22 16:57:00 +0100315 AppendInstruction(null_check);
316 return null_check;
317}
318
David Brazdildee58d62016-04-07 09:54:26 +0000319void HInstructionBuilder::SetLoopHeaderPhiInputs() {
320 for (size_t i = loop_headers_.size(); i > 0; --i) {
321 HBasicBlock* block = loop_headers_[i - 1];
322 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
323 HPhi* phi = it.Current()->AsPhi();
324 size_t vreg = phi->GetRegNumber();
325 for (HBasicBlock* predecessor : block->GetPredecessors()) {
326 HInstruction* value = ValueOfLocalAt(predecessor, vreg);
327 if (value == nullptr) {
328 // Vreg is undefined at this predecessor. Mark it dead and leave with
329 // fewer inputs than predecessors. SsaChecker will fail if not removed.
330 phi->SetDead();
331 break;
332 } else {
333 phi->AddInput(value);
334 }
335 }
336 }
337 }
338}
339
340static bool IsBlockPopulated(HBasicBlock* block) {
341 if (block->IsLoopHeader()) {
342 // Suspend checks were inserted into loop headers during building of dominator tree.
343 DCHECK(block->GetFirstInstruction()->IsSuspendCheck());
344 return block->GetFirstInstruction() != block->GetLastInstruction();
345 } else {
346 return !block->GetInstructions().IsEmpty();
347 }
348}
349
350bool HInstructionBuilder::Build() {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800351 DCHECK(code_item_accessor_.HasCodeItem());
Vladimir Marko69d310e2017-10-09 14:12:23 +0100352 locals_for_.resize(
353 graph_->GetBlocks().size(),
354 ScopedArenaVector<HInstruction*>(local_allocator_->Adapter(kArenaAllocGraphBuilder)));
David Brazdildee58d62016-04-07 09:54:26 +0000355
356 // Find locations where we want to generate extra stackmaps for native debugging.
357 // This allows us to generate the info only at interesting points (for example,
358 // at start of java statement) rather than before every dex instruction.
Vladimir Marko3b506202018-10-31 14:33:58 +0000359 const bool native_debuggable = code_generator_ != nullptr &&
360 code_generator_->GetCompilerOptions().GetNativeDebuggable();
David Brazdildee58d62016-04-07 09:54:26 +0000361 ArenaBitVector* native_debug_info_locations = nullptr;
362 if (native_debuggable) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100363 native_debug_info_locations = FindNativeDebugInfoLocations();
David Brazdildee58d62016-04-07 09:54:26 +0000364 }
365
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100366 for (HBasicBlock* block : graph_->GetReversePostOrder()) {
367 current_block_ = block;
David Brazdildee58d62016-04-07 09:54:26 +0000368 uint32_t block_dex_pc = current_block_->GetDexPc();
369
370 InitializeBlockLocals();
371
372 if (current_block_->IsEntryBlock()) {
373 InitializeParameters();
Vladimir Markoca6fff82017-10-03 14:49:14 +0100374 AppendInstruction(new (allocator_) HSuspendCheck(0u));
375 AppendInstruction(new (allocator_) HGoto(0u));
David Brazdildee58d62016-04-07 09:54:26 +0000376 continue;
377 } else if (current_block_->IsExitBlock()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100378 AppendInstruction(new (allocator_) HExit());
David Brazdildee58d62016-04-07 09:54:26 +0000379 continue;
380 } else if (current_block_->IsLoopHeader()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100381 HSuspendCheck* suspend_check = new (allocator_) HSuspendCheck(current_block_->GetDexPc());
David Brazdildee58d62016-04-07 09:54:26 +0000382 current_block_->GetLoopInformation()->SetSuspendCheck(suspend_check);
383 // This is slightly odd because the loop header might not be empty (TryBoundary).
384 // But we're still creating the environment with locals from the top of the block.
385 InsertInstructionAtTop(suspend_check);
386 }
387
388 if (block_dex_pc == kNoDexPc || current_block_ != block_builder_->GetBlockAt(block_dex_pc)) {
389 // Synthetic block that does not need to be populated.
390 DCHECK(IsBlockPopulated(current_block_));
391 continue;
392 }
393
394 DCHECK(!IsBlockPopulated(current_block_));
395
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800396 for (const DexInstructionPcPair& pair : code_item_accessor_.InstructionsFrom(block_dex_pc)) {
David Brazdildee58d62016-04-07 09:54:26 +0000397 if (current_block_ == nullptr) {
398 // The previous instruction ended this block.
399 break;
400 }
401
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800402 const uint32_t dex_pc = pair.DexPc();
David Brazdildee58d62016-04-07 09:54:26 +0000403 if (dex_pc != block_dex_pc && FindBlockStartingAt(dex_pc) != nullptr) {
404 // This dex_pc starts a new basic block.
405 break;
406 }
407
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800408 if (current_block_->IsTryBlock() && IsThrowingDexInstruction(pair.Inst())) {
David Brazdildee58d62016-04-07 09:54:26 +0000409 PropagateLocalsToCatchBlocks();
410 }
411
412 if (native_debuggable && native_debug_info_locations->IsBitSet(dex_pc)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100413 AppendInstruction(new (allocator_) HNativeDebugInfo(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000414 }
415
Vladimir Markof91fc122020-05-13 09:21:00 +0100416 // Note: There may be no Thread for gtests.
417 DCHECK(Thread::Current() == nullptr || !Thread::Current()->IsExceptionPending())
Nicolas Geoffray605c5912020-04-08 15:12:39 +0100418 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
419 << " " << pair.Inst().Name() << "@" << dex_pc;
Nicolas Geoffray4924ea92021-03-23 08:25:31 +0000420 if (!ProcessDexInstruction(pair.Inst(), dex_pc)) {
David Brazdildee58d62016-04-07 09:54:26 +0000421 return false;
422 }
Vladimir Markof91fc122020-05-13 09:21:00 +0100423 DCHECK(Thread::Current() == nullptr || !Thread::Current()->IsExceptionPending())
Nicolas Geoffrayfbf53b52020-04-01 15:20:14 +0100424 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
Nicolas Geoffray605c5912020-04-08 15:12:39 +0100425 << " " << pair.Inst().Name() << "@" << dex_pc;
David Brazdildee58d62016-04-07 09:54:26 +0000426 }
427
428 if (current_block_ != nullptr) {
429 // Branching instructions clear current_block, so we know the last
430 // instruction of the current block is not a branching instruction.
431 // We add an unconditional Goto to the next block.
432 DCHECK_EQ(current_block_->GetSuccessors().size(), 1u);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100433 AppendInstruction(new (allocator_) HGoto());
David Brazdildee58d62016-04-07 09:54:26 +0000434 }
435 }
436
437 SetLoopHeaderPhiInputs();
438
439 return true;
440}
441
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000442void HInstructionBuilder::BuildIntrinsic(ArtMethod* method) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800443 DCHECK(!code_item_accessor_.HasCodeItem());
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000444 DCHECK(method->IsIntrinsic());
Vladimir Marko43d57552020-09-07 12:30:17 +0000445 if (kIsDebugBuild) {
446 ScopedObjectAccess soa(Thread::Current());
447 CHECK(!method->IsSignaturePolymorphic());
448 }
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000449
450 locals_for_.resize(
451 graph_->GetBlocks().size(),
452 ScopedArenaVector<HInstruction*>(local_allocator_->Adapter(kArenaAllocGraphBuilder)));
453
454 // Fill the entry block. Do not add suspend check, we do not want a suspend
455 // check in intrinsics; intrinsic methods are supposed to be fast.
456 current_block_ = graph_->GetEntryBlock();
457 InitializeBlockLocals();
458 InitializeParameters();
459 AppendInstruction(new (allocator_) HGoto(0u));
460
461 // Fill the body.
462 current_block_ = current_block_->GetSingleSuccessor();
463 InitializeBlockLocals();
464 DCHECK(!IsBlockPopulated(current_block_));
465
Vladimir Marko5f846072020-04-09 13:20:11 +0100466 // Add the intermediate representation, if available, or invoke instruction.
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000467 size_t in_vregs = graph_->GetNumberOfInVRegs();
468 size_t number_of_arguments =
469 in_vregs - std::count(current_locals_->end() - in_vregs, current_locals_->end(), nullptr);
470 uint32_t method_idx = dex_compilation_unit_->GetDexMethodIndex();
Vladimir Marko5f846072020-04-09 13:20:11 +0100471 const char* shorty = dex_file_->GetMethodShorty(method_idx);
Treehugger Robot2c5827a2018-05-17 22:26:08 +0000472 RangeInstructionOperands operands(graph_->GetNumberOfVRegs() - in_vregs, in_vregs);
Vladimir Marko5f846072020-04-09 13:20:11 +0100473 if (!BuildSimpleIntrinsic(method, kNoDexPc, operands, shorty)) {
474 // Some intrinsics without intermediate representation still yield a leaf method,
475 // so build the invoke. Use HInvokeStaticOrDirect even for methods that would
476 // normally use an HInvokeVirtual (sharpen the call).
477 MethodReference target_method(dex_file_, method_idx);
478 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +0100479 MethodLoadKind::kRuntimeCall,
480 CodePtrLocation::kCallArtMethod,
Vladimir Marko5f846072020-04-09 13:20:11 +0100481 /* method_load_data= */ 0u
482 };
483 InvokeType invoke_type = dex_compilation_unit_->IsStatic() ? kStatic : kDirect;
484 HInvokeStaticOrDirect* invoke = new (allocator_) HInvokeStaticOrDirect(
485 allocator_,
486 number_of_arguments,
487 return_type_,
488 kNoDexPc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +0100489 target_method,
Vladimir Marko5f846072020-04-09 13:20:11 +0100490 method,
491 dispatch_info,
492 invoke_type,
493 target_method,
494 HInvokeStaticOrDirect::ClinitCheckRequirement::kNone);
495 HandleInvoke(invoke, operands, shorty, /* is_unresolved= */ false);
496 }
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000497
498 // Add the return instruction.
499 if (return_type_ == DataType::Type::kVoid) {
500 AppendInstruction(new (allocator_) HReturnVoid());
501 } else {
Vladimir Marko5f846072020-04-09 13:20:11 +0100502 AppendInstruction(new (allocator_) HReturn(latest_result_));
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000503 }
504
505 // Fill the exit block.
506 DCHECK_EQ(current_block_->GetSingleSuccessor(), graph_->GetExitBlock());
507 current_block_ = graph_->GetExitBlock();
508 InitializeBlockLocals();
509 AppendInstruction(new (allocator_) HExit());
510}
511
Vladimir Marko69d310e2017-10-09 14:12:23 +0100512ArenaBitVector* HInstructionBuilder::FindNativeDebugInfoLocations() {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100513 ArenaBitVector* locations = ArenaBitVector::Create(local_allocator_,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800514 code_item_accessor_.InsnsSizeInCodeUnits(),
Andreas Gampe3db70682018-12-26 15:12:03 -0800515 /* expandable= */ false,
Vladimir Marko69d310e2017-10-09 14:12:23 +0100516 kArenaAllocGraphBuilder);
517 locations->ClearAllBits();
Mathieu Chartier3e2e1232018-09-11 12:35:30 -0700518 // The visitor gets called when the line number changes.
519 // In other words, it marks the start of new java statement.
520 code_item_accessor_.DecodeDebugPositionInfo([&](const DexFile::PositionInfo& entry) {
521 locations->SetBit(entry.address_);
522 return false;
523 });
David Brazdildee58d62016-04-07 09:54:26 +0000524 // Instruction-specific tweaks.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800525 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800526 switch (inst->Opcode()) {
David Brazdildee58d62016-04-07 09:54:26 +0000527 case Instruction::MOVE_EXCEPTION: {
528 // Stop in native debugger after the exception has been moved.
529 // The compiler also expects the move at the start of basic block so
530 // we do not want to interfere by inserting native-debug-info before it.
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800531 locations->ClearBit(inst.DexPc());
532 DexInstructionIterator next = std::next(DexInstructionIterator(inst));
533 DCHECK(next.DexPc() != inst.DexPc());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800534 if (next != code_item_accessor_.end()) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -0700535 locations->SetBit(next.DexPc());
David Brazdildee58d62016-04-07 09:54:26 +0000536 }
537 break;
538 }
539 default:
540 break;
541 }
542 }
Vladimir Marko69d310e2017-10-09 14:12:23 +0100543 return locations;
David Brazdildee58d62016-04-07 09:54:26 +0000544}
545
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100546HInstruction* HInstructionBuilder::LoadLocal(uint32_t reg_number, DataType::Type type) const {
David Brazdildee58d62016-04-07 09:54:26 +0000547 HInstruction* value = (*current_locals_)[reg_number];
548 DCHECK(value != nullptr);
549
550 // If the operation requests a specific type, we make sure its input is of that type.
551 if (type != value->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100552 if (DataType::IsFloatingPointType(type)) {
Aart Bik31883642016-06-06 15:02:44 -0700553 value = ssa_builder_->GetFloatOrDoubleEquivalent(value, type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100554 } else if (type == DataType::Type::kReference) {
Aart Bik31883642016-06-06 15:02:44 -0700555 value = ssa_builder_->GetReferenceTypeEquivalent(value);
David Brazdildee58d62016-04-07 09:54:26 +0000556 }
Aart Bik31883642016-06-06 15:02:44 -0700557 DCHECK(value != nullptr);
David Brazdildee58d62016-04-07 09:54:26 +0000558 }
559
560 return value;
561}
562
563void HInstructionBuilder::UpdateLocal(uint32_t reg_number, HInstruction* stored_value) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100564 DataType::Type stored_type = stored_value->GetType();
565 DCHECK_NE(stored_type, DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +0000566
567 // Storing into vreg `reg_number` may implicitly invalidate the surrounding
568 // registers. Consider the following cases:
569 // (1) Storing a wide value must overwrite previous values in both `reg_number`
570 // and `reg_number+1`. We store `nullptr` in `reg_number+1`.
571 // (2) If vreg `reg_number-1` holds a wide value, writing into `reg_number`
572 // must invalidate it. We store `nullptr` in `reg_number-1`.
573 // Consequently, storing a wide value into the high vreg of another wide value
574 // will invalidate both `reg_number-1` and `reg_number+1`.
575
576 if (reg_number != 0) {
577 HInstruction* local_low = (*current_locals_)[reg_number - 1];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100578 if (local_low != nullptr && DataType::Is64BitType(local_low->GetType())) {
David Brazdildee58d62016-04-07 09:54:26 +0000579 // The vreg we are storing into was previously the high vreg of a pair.
580 // We need to invalidate its low vreg.
581 DCHECK((*current_locals_)[reg_number] == nullptr);
582 (*current_locals_)[reg_number - 1] = nullptr;
583 }
584 }
585
586 (*current_locals_)[reg_number] = stored_value;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100587 if (DataType::Is64BitType(stored_type)) {
David Brazdildee58d62016-04-07 09:54:26 +0000588 // We are storing a pair. Invalidate the instruction in the high vreg.
589 (*current_locals_)[reg_number + 1] = nullptr;
590 }
591}
592
593void HInstructionBuilder::InitializeParameters() {
594 DCHECK(current_block_->IsEntryBlock());
595
Vladimir Marko69d310e2017-10-09 14:12:23 +0100596 // outer_compilation_unit_ is null only when unit testing.
597 if (outer_compilation_unit_ == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +0000598 return;
599 }
600
601 const char* shorty = dex_compilation_unit_->GetShorty();
602 uint16_t number_of_parameters = graph_->GetNumberOfInVRegs();
603 uint16_t locals_index = graph_->GetNumberOfLocalVRegs();
604 uint16_t parameter_index = 0;
605
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800606 const dex::MethodId& referrer_method_id =
David Brazdildee58d62016-04-07 09:54:26 +0000607 dex_file_->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
608 if (!dex_compilation_unit_->IsStatic()) {
609 // Add the implicit 'this' argument, not expressed in the signature.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100610 HParameterValue* parameter = new (allocator_) HParameterValue(*dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +0000611 referrer_method_id.class_idx_,
612 parameter_index++,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100613 DataType::Type::kReference,
Andreas Gampe3db70682018-12-26 15:12:03 -0800614 /* is_this= */ true);
David Brazdildee58d62016-04-07 09:54:26 +0000615 AppendInstruction(parameter);
616 UpdateLocal(locals_index++, parameter);
617 number_of_parameters--;
Igor Murashkind01745e2017-04-05 16:40:31 -0700618 current_this_parameter_ = parameter;
619 } else {
620 DCHECK(current_this_parameter_ == nullptr);
David Brazdildee58d62016-04-07 09:54:26 +0000621 }
622
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800623 const dex::ProtoId& proto = dex_file_->GetMethodPrototype(referrer_method_id);
624 const dex::TypeList* arg_types = dex_file_->GetProtoParameters(proto);
David Brazdildee58d62016-04-07 09:54:26 +0000625 for (int i = 0, shorty_pos = 1; i < number_of_parameters; i++) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100626 HParameterValue* parameter = new (allocator_) HParameterValue(
David Brazdildee58d62016-04-07 09:54:26 +0000627 *dex_file_,
628 arg_types->GetTypeItem(shorty_pos - 1).type_idx_,
629 parameter_index++,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100630 DataType::FromShorty(shorty[shorty_pos]),
Andreas Gampe3db70682018-12-26 15:12:03 -0800631 /* is_this= */ false);
David Brazdildee58d62016-04-07 09:54:26 +0000632 ++shorty_pos;
633 AppendInstruction(parameter);
634 // Store the parameter value in the local that the dex code will use
635 // to reference that parameter.
636 UpdateLocal(locals_index++, parameter);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100637 if (DataType::Is64BitType(parameter->GetType())) {
David Brazdildee58d62016-04-07 09:54:26 +0000638 i++;
639 locals_index++;
640 parameter_index++;
641 }
642 }
643}
644
645template<typename T>
646void HInstructionBuilder::If_22t(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100647 HInstruction* first = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
648 HInstruction* second = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100649 T* comparison = new (allocator_) T(first, second, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000650 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100651 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000652 current_block_ = nullptr;
653}
654
655template<typename T>
656void HInstructionBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100657 HInstruction* value = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100658 T* comparison = new (allocator_) T(value, graph_->GetIntConstant(0, dex_pc), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000659 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100660 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000661 current_block_ = nullptr;
662}
663
664template<typename T>
665void HInstructionBuilder::Unop_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100666 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000667 uint32_t dex_pc) {
668 HInstruction* first = LoadLocal(instruction.VRegB(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100669 AppendInstruction(new (allocator_) T(type, first, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000670 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
671}
672
673void HInstructionBuilder::Conversion_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100674 DataType::Type input_type,
675 DataType::Type result_type,
David Brazdildee58d62016-04-07 09:54:26 +0000676 uint32_t dex_pc) {
677 HInstruction* first = LoadLocal(instruction.VRegB(), input_type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100678 AppendInstruction(new (allocator_) HTypeConversion(result_type, first, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000679 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
680}
681
682template<typename T>
683void HInstructionBuilder::Binop_23x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100684 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000685 uint32_t dex_pc) {
686 HInstruction* first = LoadLocal(instruction.VRegB(), type);
687 HInstruction* second = LoadLocal(instruction.VRegC(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100688 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000689 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
690}
691
692template<typename T>
693void HInstructionBuilder::Binop_23x_shift(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);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100697 HInstruction* second = LoadLocal(instruction.VRegC(), DataType::Type::kInt32);
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
702void HInstructionBuilder::Binop_23x_cmp(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100703 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000704 ComparisonBias bias,
705 uint32_t dex_pc) {
706 HInstruction* first = LoadLocal(instruction.VRegB(), type);
707 HInstruction* second = LoadLocal(instruction.VRegC(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100708 AppendInstruction(new (allocator_) HCompare(type, first, second, bias, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000709 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
710}
711
712template<typename T>
713void HInstructionBuilder::Binop_12x_shift(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100714 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000715 uint32_t dex_pc) {
716 HInstruction* first = LoadLocal(instruction.VRegA(), type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100717 HInstruction* second = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100718 AppendInstruction(new (allocator_) T(type, first, second, 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(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);
727 HInstruction* second = LoadLocal(instruction.VRegB(), type);
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_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100734 HInstruction* first = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000735 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s(), dex_pc);
736 if (reverse) {
737 std::swap(first, second);
738 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100739 AppendInstruction(new (allocator_) T(DataType::Type::kInt32, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000740 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
741}
742
743template<typename T>
744void HInstructionBuilder::Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100745 HInstruction* first = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000746 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b(), dex_pc);
747 if (reverse) {
748 std::swap(first, second);
749 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100750 AppendInstruction(new (allocator_) T(DataType::Type::kInt32, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000751 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
752}
753
Igor Murashkind01745e2017-04-05 16:40:31 -0700754// Does the method being compiled need any constructor barriers being inserted?
755// (Always 'false' for methods that aren't <init>.)
Vladimir Markoc1c34522018-10-31 13:56:49 +0000756static bool RequiresConstructorBarrier(const DexCompilationUnit* cu) {
Igor Murashkin032cacd2017-04-06 14:40:08 -0700757 // Can be null in unit tests only.
758 if (UNLIKELY(cu == nullptr)) {
759 return false;
760 }
761
Vladimir Markoc1c34522018-10-31 13:56:49 +0000762 // Constructor barriers are applicable only for <init> methods.
763 if (LIKELY(!cu->IsConstructor() || cu->IsStatic())) {
764 return false;
765 }
766
767 return cu->RequiresConstructorBarrier();
David Brazdildee58d62016-04-07 09:54:26 +0000768}
769
770// Returns true if `block` has only one successor which starts at the next
771// dex_pc after `instruction` at `dex_pc`.
772static bool IsFallthroughInstruction(const Instruction& instruction,
773 uint32_t dex_pc,
774 HBasicBlock* block) {
775 uint32_t next_dex_pc = dex_pc + instruction.SizeInCodeUnits();
776 return block->GetSingleSuccessor()->GetDexPc() == next_dex_pc;
777}
778
779void HInstructionBuilder::BuildSwitch(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100780 HInstruction* value = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000781 DexSwitchTable table(instruction, dex_pc);
782
783 if (table.GetNumEntries() == 0) {
784 // Empty Switch. Code falls through to the next block.
785 DCHECK(IsFallthroughInstruction(instruction, dex_pc, current_block_));
Vladimir Markoca6fff82017-10-03 14:49:14 +0100786 AppendInstruction(new (allocator_) HGoto(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000787 } else if (table.ShouldBuildDecisionTree()) {
788 for (DexSwitchTableIterator it(table); !it.Done(); it.Advance()) {
789 HInstruction* case_value = graph_->GetIntConstant(it.CurrentKey(), dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100790 HEqual* comparison = new (allocator_) HEqual(value, case_value, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000791 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100792 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000793
794 if (!it.IsLast()) {
795 current_block_ = FindBlockStartingAt(it.GetDexPcForCurrentIndex());
796 }
797 }
798 } else {
799 AppendInstruction(
Vladimir Markoca6fff82017-10-03 14:49:14 +0100800 new (allocator_) HPackedSwitch(table.GetEntryAt(0), table.GetNumEntries(), value, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000801 }
802
803 current_block_ = nullptr;
804}
805
806void HInstructionBuilder::BuildReturn(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100807 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000808 uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100809 if (type == DataType::Type::kVoid) {
Igor Murashkind01745e2017-04-05 16:40:31 -0700810 // Only <init> (which is a return-void) could possibly have a constructor fence.
Igor Murashkin032cacd2017-04-06 14:40:08 -0700811 // This may insert additional redundant constructor fences from the super constructors.
812 // TODO: remove redundant constructor fences (b/36656456).
Vladimir Markoc1c34522018-10-31 13:56:49 +0000813 if (RequiresConstructorBarrier(dex_compilation_unit_)) {
Igor Murashkind01745e2017-04-05 16:40:31 -0700814 // Compiling instance constructor.
Vladimir Markoba118822017-06-12 15:41:56 +0100815 DCHECK_STREQ("<init>", graph_->GetMethodName());
Igor Murashkind01745e2017-04-05 16:40:31 -0700816
817 HInstruction* fence_target = current_this_parameter_;
818 DCHECK(fence_target != nullptr);
819
Vladimir Markoca6fff82017-10-03 14:49:14 +0100820 AppendInstruction(new (allocator_) HConstructorFence(fence_target, dex_pc, allocator_));
Igor Murashkin6ef45672017-08-08 13:59:55 -0700821 MaybeRecordStat(
822 compilation_stats_,
823 MethodCompilationStat::kConstructorFenceGeneratedFinal);
David Brazdildee58d62016-04-07 09:54:26 +0000824 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100825 AppendInstruction(new (allocator_) HReturnVoid(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000826 } else {
Vladimir Markoc1c34522018-10-31 13:56:49 +0000827 DCHECK(!RequiresConstructorBarrier(dex_compilation_unit_));
David Brazdildee58d62016-04-07 09:54:26 +0000828 HInstruction* value = LoadLocal(instruction.VRegA(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100829 AppendInstruction(new (allocator_) HReturn(value, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000830 }
831 current_block_ = nullptr;
832}
833
834static InvokeType GetInvokeTypeFromOpCode(Instruction::Code opcode) {
835 switch (opcode) {
836 case Instruction::INVOKE_STATIC:
837 case Instruction::INVOKE_STATIC_RANGE:
838 return kStatic;
839 case Instruction::INVOKE_DIRECT:
840 case Instruction::INVOKE_DIRECT_RANGE:
841 return kDirect;
842 case Instruction::INVOKE_VIRTUAL:
David Brazdildee58d62016-04-07 09:54:26 +0000843 case Instruction::INVOKE_VIRTUAL_RANGE:
David Brazdildee58d62016-04-07 09:54:26 +0000844 return kVirtual;
845 case Instruction::INVOKE_INTERFACE:
846 case Instruction::INVOKE_INTERFACE_RANGE:
847 return kInterface;
848 case Instruction::INVOKE_SUPER_RANGE:
849 case Instruction::INVOKE_SUPER:
850 return kSuper;
851 default:
852 LOG(FATAL) << "Unexpected invoke opcode: " << opcode;
853 UNREACHABLE();
854 }
855}
856
Vladimir Marko2f40d242020-04-08 12:56:45 +0100857// Try to resolve a method using the class linker. Return null if a method could
858// not be resolved or the resolved method cannot be used for some reason.
859// Also retrieve method data needed for creating the invoke intermediate
860// representation while we hold the mutator lock here.
861static ArtMethod* ResolveMethod(uint16_t method_idx,
862 ArtMethod* referrer,
863 const DexCompilationUnit& dex_compilation_unit,
864 /*inout*/InvokeType* invoke_type,
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +0100865 /*out*/MethodReference* resolved_method_info,
866 /*out*/uint16_t* imt_or_vtable_index,
Vladimir Marko2f40d242020-04-08 12:56:45 +0100867 /*out*/bool* is_string_constructor) {
David Brazdildee58d62016-04-07 09:54:26 +0000868 ScopedObjectAccess soa(Thread::Current());
David Brazdildee58d62016-04-07 09:54:26 +0000869
Vladimir Marko2f40d242020-04-08 12:56:45 +0100870 ClassLinker* class_linker = dex_compilation_unit.GetClassLinker();
871 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit.GetClassLoader();
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100872
Vladimir Markoba118822017-06-12 15:41:56 +0100873 ArtMethod* resolved_method =
874 class_linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
Vladimir Markoba118822017-06-12 15:41:56 +0100875 method_idx,
Vladimir Marko2f40d242020-04-08 12:56:45 +0100876 dex_compilation_unit.GetDexCache(),
Vladimir Markoba118822017-06-12 15:41:56 +0100877 class_loader,
Vladimir Marko2f40d242020-04-08 12:56:45 +0100878 referrer,
879 *invoke_type);
David Brazdildee58d62016-04-07 09:54:26 +0000880
881 if (UNLIKELY(resolved_method == nullptr)) {
882 // Clean up any exception left by type resolution.
883 soa.Self()->ClearException();
884 return nullptr;
885 }
Nicolas Geoffray605c5912020-04-08 15:12:39 +0100886 DCHECK(!soa.Self()->IsExceptionPending());
David Brazdildee58d62016-04-07 09:54:26 +0000887
Vladimir Markoba118822017-06-12 15:41:56 +0100888 // The referrer may be unresolved for AOT if we're compiling a class that cannot be
889 // resolved because, for example, we don't find a superclass in the classpath.
Vladimir Marko2f40d242020-04-08 12:56:45 +0100890 if (referrer == nullptr) {
Vladimir Markoba118822017-06-12 15:41:56 +0100891 // The class linker cannot check access without a referrer, so we have to do it.
Vladimir Markocfd65802020-08-18 09:29:51 +0100892 // Check if the declaring class or referencing class is accessible.
893 SamePackageCompare same_package(dex_compilation_unit);
894 ObjPtr<mirror::Class> declaring_class = resolved_method->GetDeclaringClass();
895 bool declaring_class_accessible = declaring_class->IsPublic() || same_package(declaring_class);
896 if (!declaring_class_accessible) {
897 // It is possible to access members from an inaccessible superclass
898 // by referencing them through an accessible subclass.
899 ObjPtr<mirror::Class> referenced_class = class_linker->LookupResolvedType(
900 dex_compilation_unit.GetDexFile()->GetMethodId(method_idx).class_idx_,
901 dex_compilation_unit.GetDexCache().Get(),
902 class_loader.Get());
903 DCHECK(referenced_class != nullptr); // Must have been resolved when resolving the method.
904 if (!referenced_class->IsPublic() && !same_package(referenced_class)) {
905 return nullptr;
906 }
907 }
908 // Check whether the method itself is accessible.
909 // Since the referrer is unresolved but the method is resolved, it cannot be
910 // inside the same class, so a private method is known to be inaccessible.
911 // And without a resolved referrer, we cannot check for protected member access
912 // in superlass, so we handle only access to public member or within the package.
913 if (resolved_method->IsPrivate() ||
914 (!resolved_method->IsPublic() && !declaring_class_accessible)) {
David Brazdildee58d62016-04-07 09:54:26 +0000915 return nullptr;
916 }
David Brazdildee58d62016-04-07 09:54:26 +0000917 }
918
919 // We have to special case the invoke-super case, as ClassLinker::ResolveMethod does not.
920 // We need to look at the referrer's super class vtable. We need to do this to know if we need to
921 // make this an invoke-unresolved to handle cross-dex invokes or abstract super methods, both of
922 // which require runtime handling.
Vladimir Marko2f40d242020-04-08 12:56:45 +0100923 if (*invoke_type == kSuper) {
924 ObjPtr<mirror::Class> compiling_class = dex_compilation_unit.GetCompilingClass().Get();
Andreas Gampefa4333d2017-02-14 11:10:34 -0800925 if (compiling_class == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +0000926 // We could not determine the method's class we need to wait until runtime.
927 DCHECK(Runtime::Current()->IsAotCompiler());
928 return nullptr;
929 }
Vladimir Markoba118822017-06-12 15:41:56 +0100930 ObjPtr<mirror::Class> referenced_class = class_linker->LookupResolvedType(
Vladimir Marko2f40d242020-04-08 12:56:45 +0100931 dex_compilation_unit.GetDexFile()->GetMethodId(method_idx).class_idx_,
932 dex_compilation_unit.GetDexCache().Get(),
Vladimir Markoba118822017-06-12 15:41:56 +0100933 class_loader.Get());
934 DCHECK(referenced_class != nullptr); // We have already resolved a method from this class.
935 if (!referenced_class->IsAssignableFrom(compiling_class)) {
Aart Bikf663e342016-04-04 17:28:59 -0700936 // We cannot statically determine the target method. The runtime will throw a
937 // NoSuchMethodError on this one.
938 return nullptr;
939 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100940 ArtMethod* actual_method;
Vladimir Markoba118822017-06-12 15:41:56 +0100941 if (referenced_class->IsInterface()) {
942 actual_method = referenced_class->FindVirtualMethodForInterfaceSuper(
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100943 resolved_method, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000944 } else {
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100945 uint16_t vtable_index = resolved_method->GetMethodIndex();
Nicolas Geoffraybd570592020-12-23 16:37:44 +0000946 if (vtable_index >= static_cast<uint32_t>(
947 compiling_class->GetSuperClass()->GetVTableLength())) {
948 // No super method. The runtime will throw a NoSuchMethodError.
949 return nullptr;
950 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100951 actual_method = compiling_class->GetSuperClass()->GetVTableEntry(
952 vtable_index, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000953 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100954 if (!actual_method->IsInvokable()) {
955 // Fail if the actual method cannot be invoked. Otherwise, the runtime resolution stub
956 // could resolve the callee to the wrong method.
957 return nullptr;
958 }
Nicolas Geoffray25b9c7d2020-09-17 17:34:34 +0100959 // Call GetCanonicalMethod in case the resolved method is a copy: for super calls, the encoding
960 // of ArtMethod in BSS relies on not having copies there.
961 resolved_method = actual_method->GetCanonicalMethod(class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000962 }
963
Vladimir Marko2f40d242020-04-08 12:56:45 +0100964 if (*invoke_type == kInterface) {
965 if (resolved_method->GetDeclaringClass()->IsObjectClass()) {
966 // If the resolved method is from j.l.Object, emit a virtual call instead.
967 // The IMT conflict stub only handles interface methods.
968 *invoke_type = kVirtual;
969 } else {
970 DCHECK(resolved_method->GetDeclaringClass()->IsInterface());
971 }
972 }
David Brazdildee58d62016-04-07 09:54:26 +0000973
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +0100974 *resolved_method_info =
Vladimir Marko2f40d242020-04-08 12:56:45 +0100975 MethodReference(resolved_method->GetDexFile(), resolved_method->GetDexMethodIndex());
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +0100976 if (*invoke_type == kVirtual) {
Vladimir Marko2f40d242020-04-08 12:56:45 +0100977 // For HInvokeVirtual we need the vtable index.
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +0100978 *imt_or_vtable_index = resolved_method->GetVtableIndex();
Andra Danciua0130e82020-07-23 12:34:56 +0000979 } else if (*invoke_type == kInterface) {
Vladimir Marko2f40d242020-04-08 12:56:45 +0100980 // For HInvokeInterface we need the IMT index.
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +0100981 *imt_or_vtable_index = ImTable::GetImtIndex(resolved_method);
Vladimir Marko2f40d242020-04-08 12:56:45 +0100982 }
983
984 *is_string_constructor =
985 resolved_method->IsConstructor() && resolved_method->GetDeclaringClass()->IsStringClass();
986
987 return resolved_method;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100988}
989
David Brazdildee58d62016-04-07 09:54:26 +0000990bool HInstructionBuilder::BuildInvoke(const Instruction& instruction,
991 uint32_t dex_pc,
992 uint32_t method_idx,
Treehugger Robot2c5827a2018-05-17 22:26:08 +0000993 const InstructionOperands& operands) {
David Brazdildee58d62016-04-07 09:54:26 +0000994 InvokeType invoke_type = GetInvokeTypeFromOpCode(instruction.Opcode());
Orion Hodson4c8e12e2018-05-18 08:33:20 +0100995 const char* shorty = dex_file_->GetMethodShorty(method_idx);
996 DataType::Type return_type = DataType::FromShorty(shorty[0]);
David Brazdildee58d62016-04-07 09:54:26 +0000997
998 // Remove the return type from the 'proto'.
Orion Hodson4c8e12e2018-05-18 08:33:20 +0100999 size_t number_of_arguments = strlen(shorty) - 1;
David Brazdildee58d62016-04-07 09:54:26 +00001000 if (invoke_type != kStatic) { // instance call
1001 // One extra argument for 'this'.
1002 number_of_arguments++;
1003 }
1004
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01001005 MethodReference resolved_method_reference(nullptr, 0u);
Vladimir Marko2f40d242020-04-08 12:56:45 +01001006 bool is_string_constructor = false;
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01001007 uint16_t imt_or_vtable_index = DexFile::kDexNoIndex16;
Vladimir Marko2f40d242020-04-08 12:56:45 +01001008 ArtMethod* resolved_method = ResolveMethod(method_idx,
1009 graph_->GetArtMethod(),
1010 *dex_compilation_unit_,
1011 &invoke_type,
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01001012 &resolved_method_reference,
1013 &imt_or_vtable_index,
Vladimir Marko2f40d242020-04-08 12:56:45 +01001014 &is_string_constructor);
David Brazdildee58d62016-04-07 09:54:26 +00001015
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01001016 MethodReference method_reference(&graph_->GetDexFile(), method_idx);
David Brazdildee58d62016-04-07 09:54:26 +00001017 if (UNLIKELY(resolved_method == nullptr)) {
Nicolas Geoffray605c5912020-04-08 15:12:39 +01001018 DCHECK(!Thread::Current()->IsExceptionPending());
Igor Murashkin1e065a52017-08-09 13:20:34 -07001019 MaybeRecordStat(compilation_stats_,
1020 MethodCompilationStat::kUnresolvedMethod);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001021 HInvoke* invoke = new (allocator_) HInvokeUnresolved(allocator_,
1022 number_of_arguments,
1023 return_type,
1024 dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01001025 method_reference,
Vladimir Markoca6fff82017-10-03 14:49:14 +01001026 invoke_type);
Andreas Gampe3db70682018-12-26 15:12:03 -08001027 return HandleInvoke(invoke, operands, shorty, /* is_unresolved= */ true);
David Brazdildee58d62016-04-07 09:54:26 +00001028 }
1029
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01001030 // Replace calls to String.<init> with StringFactory.
Vladimir Marko2f40d242020-04-08 12:56:45 +01001031 if (is_string_constructor) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01001032 uint32_t string_init_entry_point = WellKnownClasses::StringInitToEntryPoint(resolved_method);
1033 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01001034 MethodLoadKind::kStringInit,
1035 CodePtrLocation::kCallArtMethod,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00001036 dchecked_integral_cast<uint64_t>(string_init_entry_point)
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01001037 };
Nicolas Geoffrayf665f842018-02-15 12:29:06 +00001038 // We pass null for the resolved_method to ensure optimizations
1039 // don't rely on it.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001040 HInvoke* invoke = new (allocator_) HInvokeStaticOrDirect(
1041 allocator_,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01001042 number_of_arguments - 1,
Andreas Gampe3db70682018-12-26 15:12:03 -08001043 /* return_type= */ DataType::Type::kReference,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01001044 dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01001045 method_reference,
Andreas Gampe3db70682018-12-26 15:12:03 -08001046 /* resolved_method= */ nullptr,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01001047 dispatch_info,
1048 invoke_type,
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01001049 resolved_method_reference,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01001050 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001051 return HandleStringInit(invoke, operands, shorty);
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01001052 }
1053
David Brazdildee58d62016-04-07 09:54:26 +00001054 // Potential class initialization check, in the case of a static method call.
Vladimir Marko2f40d242020-04-08 12:56:45 +01001055 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement =
1056 HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
David Brazdildee58d62016-04-07 09:54:26 +00001057 HClinitCheck* clinit_check = nullptr;
Vladimir Marko2f40d242020-04-08 12:56:45 +01001058 if (invoke_type == kStatic) {
1059 clinit_check = ProcessClinitCheckForInvoke(dex_pc, resolved_method, &clinit_check_requirement);
1060 }
1061
Vladimir Marko5f846072020-04-09 13:20:11 +01001062 // Try to build an HIR replacement for the intrinsic.
1063 if (UNLIKELY(resolved_method->IsIntrinsic())) {
1064 // All intrinsics are in the primary boot image, so their class can always be referenced
1065 // and we do not need to rely on the implicit class initialization check. The class should
1066 // be initialized but we do not require that here.
1067 DCHECK_NE(clinit_check_requirement, HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit);
1068 if (BuildSimpleIntrinsic(resolved_method, dex_pc, operands, shorty)) {
1069 return true;
1070 }
1071 }
1072
David Brazdildee58d62016-04-07 09:54:26 +00001073 HInvoke* invoke = nullptr;
1074 if (invoke_type == kDirect || invoke_type == kStatic || invoke_type == kSuper) {
Nicolas Geoffray1fef8772020-09-09 13:57:17 +01001075 // For sharpening, we create another MethodReference, to account for the
1076 // kSuper case below where we cannot find a dex method index.
1077 bool has_method_id = true;
Vladimir Marko2f40d242020-04-08 12:56:45 +01001078 if (invoke_type == kSuper) {
Nicolas Geoffray1fef8772020-09-09 13:57:17 +01001079 uint32_t dex_method_index = method_reference.index;
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01001080 if (IsSameDexFile(*resolved_method_reference.dex_file,
1081 *dex_compilation_unit_->GetDexFile())) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001082 // Update the method index to the one resolved. Note that this may be a no-op if
David Brazdildee58d62016-04-07 09:54:26 +00001083 // we resolved to the method referenced by the instruction.
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01001084 dex_method_index = resolved_method_reference.index;
Nicolas Geoffray1fef8772020-09-09 13:57:17 +01001085 } else {
1086 // Try to find a dex method index in this caller's dex file.
1087 ScopedObjectAccess soa(Thread::Current());
1088 dex_method_index = resolved_method->FindDexMethodIndexInOtherDexFile(
1089 *dex_compilation_unit_->GetDexFile(), method_idx);
1090 }
1091 if (dex_method_index == dex::kDexNoIndex) {
1092 has_method_id = false;
1093 } else {
1094 method_reference.index = dex_method_index;
David Brazdildee58d62016-04-07 09:54:26 +00001095 }
1096 }
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01001097 HInvokeStaticOrDirect::DispatchInfo dispatch_info =
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01001098 HSharpening::SharpenLoadMethod(resolved_method,
1099 has_method_id,
Nicolas Geoffrayd5a86952021-01-19 10:35:54 +00001100 /* for_interface_call= */ false,
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01001101 code_generator_);
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01001102 if (dispatch_info.code_ptr_location == CodePtrLocation::kCallCriticalNative) {
Vladimir Markod3e9c622020-08-05 12:20:28 +01001103 graph_->SetHasDirectCriticalNativeCall(true);
1104 }
Vladimir Markoca6fff82017-10-03 14:49:14 +01001105 invoke = new (allocator_) HInvokeStaticOrDirect(allocator_,
1106 number_of_arguments,
1107 return_type,
1108 dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01001109 method_reference,
Vladimir Markoca6fff82017-10-03 14:49:14 +01001110 resolved_method,
1111 dispatch_info,
1112 invoke_type,
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01001113 resolved_method_reference,
Vladimir Markoca6fff82017-10-03 14:49:14 +01001114 clinit_check_requirement);
Vladimir Marko5f846072020-04-09 13:20:11 +01001115 if (clinit_check != nullptr) {
1116 // Add the class initialization check as last input of `invoke`.
1117 DCHECK_EQ(clinit_check_requirement, HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit);
1118 size_t clinit_check_index = invoke->InputCount() - 1u;
1119 DCHECK(invoke->InputAt(clinit_check_index) == nullptr);
1120 invoke->SetArgumentAt(clinit_check_index, clinit_check);
1121 }
David Brazdildee58d62016-04-07 09:54:26 +00001122 } else if (invoke_type == kVirtual) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001123 invoke = new (allocator_) HInvokeVirtual(allocator_,
1124 number_of_arguments,
1125 return_type,
1126 dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01001127 method_reference,
Vladimir Markoca6fff82017-10-03 14:49:14 +01001128 resolved_method,
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01001129 resolved_method_reference,
1130 /*vtable_index=*/ imt_or_vtable_index);
David Brazdildee58d62016-04-07 09:54:26 +00001131 } else {
1132 DCHECK_EQ(invoke_type, kInterface);
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01001133 if (kIsDebugBuild) {
1134 ScopedObjectAccess soa(Thread::Current());
1135 DCHECK(resolved_method->GetDeclaringClass()->IsInterface());
1136 }
Nicolas Geoffrayd5a86952021-01-19 10:35:54 +00001137 MethodLoadKind load_kind = HSharpening::SharpenLoadMethod(
1138 resolved_method,
1139 /* has_method_id= */ true,
1140 /* for_interface_call= */ true,
1141 code_generator_)
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01001142 .method_load_kind;
Vladimir Marko2f40d242020-04-08 12:56:45 +01001143 invoke = new (allocator_) HInvokeInterface(allocator_,
Vladimir Markoca6fff82017-10-03 14:49:14 +01001144 number_of_arguments,
1145 return_type,
1146 dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01001147 method_reference,
Vladimir Markoca6fff82017-10-03 14:49:14 +01001148 resolved_method,
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01001149 resolved_method_reference,
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01001150 /*imt_index=*/ imt_or_vtable_index,
1151 load_kind);
David Brazdildee58d62016-04-07 09:54:26 +00001152 }
Vladimir Marko5f846072020-04-09 13:20:11 +01001153 return HandleInvoke(invoke, operands, shorty, /* is_unresolved= */ false);
David Brazdildee58d62016-04-07 09:54:26 +00001154}
1155
Andra Danciud0f71f22020-09-17 09:00:15 +00001156static bool VarHandleAccessorNeedsReturnTypeCheck(HInvoke* invoke, DataType::Type return_type) {
1157 mirror::VarHandle::AccessModeTemplate access_mode_template =
1158 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(invoke->GetIntrinsic());
1159
1160 switch (access_mode_template) {
1161 case mirror::VarHandle::AccessModeTemplate::kGet:
1162 case mirror::VarHandle::AccessModeTemplate::kGetAndUpdate:
1163 case mirror::VarHandle::AccessModeTemplate::kCompareAndExchange:
1164 return return_type == DataType::Type::kReference;
1165 case mirror::VarHandle::AccessModeTemplate::kSet:
1166 case mirror::VarHandle::AccessModeTemplate::kCompareAndSet:
1167 return false;
1168 }
1169}
1170
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001171bool HInstructionBuilder::BuildInvokePolymorphic(uint32_t dex_pc,
Orion Hodsonac141392017-01-13 11:53:47 +00001172 uint32_t method_idx,
Orion Hodson06d10a72018-05-14 08:53:38 +01001173 dex::ProtoIndex proto_idx,
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001174 const InstructionOperands& operands) {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001175 const char* shorty = dex_file_->GetShorty(proto_idx);
1176 DCHECK_EQ(1 + ArtMethod::NumArgRegisters(shorty), operands.GetNumberOfOperands());
1177 DataType::Type return_type = DataType::FromShorty(shorty[0]);
1178 size_t number_of_arguments = strlen(shorty);
Andra Danciua0130e82020-07-23 12:34:56 +00001179 // We use ResolveMethod which is also used in BuildInvoke in order to
1180 // not duplicate code. As such, we need to provide is_string_constructor
1181 // even if we don't need it afterwards.
1182 InvokeType invoke_type = InvokeType::kPolymorphic;
1183 bool is_string_constructor = false;
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01001184 uint16_t imt_or_vtable_index = DexFile::kDexNoIndex16;
1185 MethodReference resolved_method_reference(nullptr, 0u);
Andra Danciua0130e82020-07-23 12:34:56 +00001186 ArtMethod* resolved_method = ResolveMethod(method_idx,
1187 graph_->GetArtMethod(),
1188 *dex_compilation_unit_,
1189 &invoke_type,
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01001190 &resolved_method_reference,
1191 &imt_or_vtable_index,
Andra Danciua0130e82020-07-23 12:34:56 +00001192 &is_string_constructor);
Andra Danciuaa358832020-08-25 15:09:43 +00001193
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01001194 MethodReference method_reference(&graph_->GetDexFile(), method_idx);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001195 HInvoke* invoke = new (allocator_) HInvokePolymorphic(allocator_,
1196 number_of_arguments,
1197 return_type,
1198 dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01001199 method_reference,
Andra Danciu73c31802020-09-01 13:17:05 +00001200 resolved_method,
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01001201 resolved_method_reference,
Andra Danciu73c31802020-09-01 13:17:05 +00001202 proto_idx);
Andra Danciu49a19f32020-08-27 12:44:25 +00001203 if (!HandleInvoke(invoke, operands, shorty, /* is_unresolved= */ false)) {
1204 return false;
1205 }
1206
Andra Danciud0f71f22020-09-17 09:00:15 +00001207 if (invoke->GetIntrinsic() != Intrinsics::kMethodHandleInvoke &&
1208 invoke->GetIntrinsic() != Intrinsics::kMethodHandleInvokeExact &&
1209 VarHandleAccessorNeedsReturnTypeCheck(invoke, return_type)) {
1210 // Type check is needed because VarHandle intrinsics do not type check the retrieved reference.
Andra Danciuaa358832020-08-25 15:09:43 +00001211 ScopedObjectAccess soa(Thread::Current());
1212 ArtMethod* referrer = graph_->GetArtMethod();
Andra Danciud0f71f22020-09-17 09:00:15 +00001213 dex::TypeIndex return_type_index =
1214 referrer->GetDexFile()->GetProtoId(proto_idx).return_type_idx_;
Andra Danciuaa358832020-08-25 15:09:43 +00001215
Andra Danciud0f71f22020-09-17 09:00:15 +00001216 BuildTypeCheck(/* is_instance_of= */ false, invoke, return_type_index, dex_pc);
Andra Danciu49a19f32020-08-27 12:44:25 +00001217 latest_result_ = current_block_->GetLastInstruction();
Andra Danciuaa358832020-08-25 15:09:43 +00001218 }
1219
Andra Danciu49a19f32020-08-27 12:44:25 +00001220 return true;
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001221}
1222
1223
1224bool HInstructionBuilder::BuildInvokeCustom(uint32_t dex_pc,
1225 uint32_t call_site_idx,
1226 const InstructionOperands& operands) {
1227 dex::ProtoIndex proto_idx = dex_file_->GetProtoIndexForCallSite(call_site_idx);
1228 const char* shorty = dex_file_->GetShorty(proto_idx);
1229 DataType::Type return_type = DataType::FromShorty(shorty[0]);
1230 size_t number_of_arguments = strlen(shorty) - 1;
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01001231 // HInvokeCustom takes a DexNoNoIndex method reference.
1232 MethodReference method_reference(&graph_->GetDexFile(), dex::kDexNoIndex);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001233 HInvoke* invoke = new (allocator_) HInvokeCustom(allocator_,
1234 number_of_arguments,
1235 call_site_idx,
1236 return_type,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01001237 dex_pc,
1238 method_reference);
Andreas Gampe3db70682018-12-26 15:12:03 -08001239 return HandleInvoke(invoke, operands, shorty, /* is_unresolved= */ false);
Orion Hodsonac141392017-01-13 11:53:47 +00001240}
1241
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001242HNewInstance* HInstructionBuilder::BuildNewInstance(dex::TypeIndex type_index, uint32_t dex_pc) {
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001243 ScopedObjectAccess soa(Thread::Current());
David Brazdildee58d62016-04-07 09:54:26 +00001244
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001245 HLoadClass* load_class = BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001246
David Brazdildee58d62016-04-07 09:54:26 +00001247 HInstruction* cls = load_class;
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001248 Handle<mirror::Class> klass = load_class->GetClass();
1249
Vladimir Marko2f40d242020-04-08 12:56:45 +01001250 if (!IsInitialized(klass.Get())) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001251 cls = new (allocator_) HClinitCheck(load_class, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001252 AppendInstruction(cls);
1253 }
1254
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001255 // Only the access check entrypoint handles the finalizable class case. If we
1256 // need access checks, then we haven't resolved the method and the class may
1257 // again be finalizable.
1258 QuickEntrypointEnum entrypoint = kQuickAllocObjectInitialized;
Vladimir Marko270e10a2020-09-24 11:48:47 +01001259 if (load_class->NeedsAccessCheck() ||
1260 klass == nullptr || // Finalizable/instantiable is unknown.
1261 klass->IsFinalizable() ||
Nicolas Geoffray54ed0152020-12-23 17:29:32 +00001262 klass.Get() == klass->GetClass() || // Classes cannot be allocated in code
Vladimir Marko270e10a2020-09-24 11:48:47 +01001263 !klass->IsInstantiable()) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001264 entrypoint = kQuickAllocObjectWithChecks;
1265 }
Alex Lightd109e302018-06-27 10:25:41 -07001266 // We will always be able to resolve the string class since it is in the BCP.
1267 if (!klass.IsNull() && klass->IsStringClass()) {
1268 entrypoint = kQuickAllocStringObject;
1269 }
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001270
1271 // Consider classes we haven't resolved as potentially finalizable.
Andreas Gampefa4333d2017-02-14 11:10:34 -08001272 bool finalizable = (klass == nullptr) || klass->IsFinalizable();
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001273
Vladimir Markoca6fff82017-10-03 14:49:14 +01001274 HNewInstance* new_instance = new (allocator_) HNewInstance(
David Brazdildee58d62016-04-07 09:54:26 +00001275 cls,
David Brazdildee58d62016-04-07 09:54:26 +00001276 dex_pc,
1277 type_index,
1278 *dex_compilation_unit_->GetDexFile(),
David Brazdildee58d62016-04-07 09:54:26 +00001279 finalizable,
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001280 entrypoint);
1281 AppendInstruction(new_instance);
1282
1283 return new_instance;
1284}
1285
1286void HInstructionBuilder::BuildConstructorFenceForAllocation(HInstruction* allocation) {
1287 DCHECK(allocation != nullptr &&
George Burgess IVf2072992017-05-23 15:36:41 -07001288 (allocation->IsNewInstance() ||
1289 allocation->IsNewArray())); // corresponding to "new" keyword in JLS.
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001290
1291 if (allocation->IsNewInstance()) {
1292 // STRING SPECIAL HANDLING:
1293 // -------------------------------
1294 // Strings have a real HNewInstance node but they end up always having 0 uses.
1295 // All uses of a String HNewInstance are always transformed to replace their input
1296 // of the HNewInstance with an input of the invoke to StringFactory.
1297 //
1298 // Do not emit an HConstructorFence here since it can inhibit some String new-instance
1299 // optimizations (to pass checker tests that rely on those optimizations).
1300 HNewInstance* new_inst = allocation->AsNewInstance();
1301 HLoadClass* load_class = new_inst->GetLoadClass();
1302
1303 Thread* self = Thread::Current();
1304 ScopedObjectAccess soa(self);
1305 StackHandleScope<1> hs(self);
1306 Handle<mirror::Class> klass = load_class->GetClass();
1307 if (klass != nullptr && klass->IsStringClass()) {
1308 return;
1309 // Note: Do not use allocation->IsStringAlloc which requires
1310 // a valid ReferenceTypeInfo, but that doesn't get made until after reference type
1311 // propagation (and instruction builder is too early).
1312 }
1313 // (In terms of correctness, the StringFactory needs to provide its own
1314 // default initialization barrier, see below.)
1315 }
1316
1317 // JLS 17.4.5 "Happens-before Order" describes:
1318 //
1319 // The default initialization of any object happens-before any other actions (other than
1320 // default-writes) of a program.
1321 //
1322 // In our implementation the default initialization of an object to type T means
1323 // setting all of its initial data (object[0..size)) to 0, and setting the
1324 // object's class header (i.e. object.getClass() == T.class).
1325 //
1326 // In practice this fence ensures that the writes to the object header
1327 // are visible to other threads if this object escapes the current thread.
1328 // (and in theory the 0-initializing, but that happens automatically
1329 // when new memory pages are mapped in by the OS).
1330 HConstructorFence* ctor_fence =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001331 new (allocator_) HConstructorFence(allocation, allocation->GetDexPc(), allocator_);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001332 AppendInstruction(ctor_fence);
Igor Murashkin6ef45672017-08-08 13:59:55 -07001333 MaybeRecordStat(
1334 compilation_stats_,
1335 MethodCompilationStat::kConstructorFenceGeneratedNew);
David Brazdildee58d62016-04-07 09:54:26 +00001336}
1337
Vladimir Marko2ab1bdd2018-07-12 09:59:56 +01001338static bool IsInBootImage(ObjPtr<mirror::Class> cls, const CompilerOptions& compiler_options)
1339 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko6a67bea2020-01-20 13:05:55 +00001340 if (Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(cls)) {
1341 return true;
1342 }
1343 if (compiler_options.IsBootImage() || compiler_options.IsBootImageExtension()) {
Vladimir Marko2ab1bdd2018-07-12 09:59:56 +01001344 std::string temp;
1345 const char* descriptor = cls->GetDescriptor(&temp);
1346 return compiler_options.IsImageClass(descriptor);
1347 } else {
Vladimir Marko6a67bea2020-01-20 13:05:55 +00001348 return false;
Vladimir Marko2ab1bdd2018-07-12 09:59:56 +01001349 }
1350}
1351
1352static bool IsSubClass(ObjPtr<mirror::Class> to_test, ObjPtr<mirror::Class> super_class)
1353 REQUIRES_SHARED(Locks::mutator_lock_) {
1354 return to_test != nullptr && !to_test->IsInterface() && to_test->IsSubClass(super_class);
1355}
1356
1357static bool HasTrivialClinit(ObjPtr<mirror::Class> klass, PointerSize pointer_size)
1358 REQUIRES_SHARED(Locks::mutator_lock_) {
1359 // Check if the class has encoded fields that trigger bytecode execution.
1360 // (Encoded fields are just a different representation of <clinit>.)
1361 if (klass->NumStaticFields() != 0u) {
1362 DCHECK(klass->GetClassDef() != nullptr);
1363 EncodedStaticFieldValueIterator it(klass->GetDexFile(), *klass->GetClassDef());
1364 for (; it.HasNext(); it.Next()) {
1365 switch (it.GetValueType()) {
1366 case EncodedArrayValueIterator::ValueType::kBoolean:
1367 case EncodedArrayValueIterator::ValueType::kByte:
1368 case EncodedArrayValueIterator::ValueType::kShort:
1369 case EncodedArrayValueIterator::ValueType::kChar:
1370 case EncodedArrayValueIterator::ValueType::kInt:
1371 case EncodedArrayValueIterator::ValueType::kLong:
1372 case EncodedArrayValueIterator::ValueType::kFloat:
1373 case EncodedArrayValueIterator::ValueType::kDouble:
1374 case EncodedArrayValueIterator::ValueType::kNull:
1375 case EncodedArrayValueIterator::ValueType::kString:
1376 // Primitive, null or j.l.String initialization is permitted.
1377 break;
1378 case EncodedArrayValueIterator::ValueType::kType:
1379 // Type initialization can load classes and execute bytecode through a class loader
1380 // which can execute arbitrary bytecode. We do not optimize for known class loaders;
1381 // kType is rarely used (if ever).
1382 return false;
1383 default:
1384 // Other types in the encoded static field list are rejected by the DexFileVerifier.
1385 LOG(FATAL) << "Unexpected type " << it.GetValueType();
1386 UNREACHABLE();
1387 }
1388 }
1389 }
1390 // Check if the class has <clinit> that executes arbitrary code.
1391 // Initialization of static fields of the class itself with constants is allowed.
1392 ArtMethod* clinit = klass->FindClassInitializer(pointer_size);
1393 if (clinit != nullptr) {
1394 const DexFile& dex_file = *clinit->GetDexFile();
1395 CodeItemInstructionAccessor accessor(dex_file, clinit->GetCodeItem());
1396 for (DexInstructionPcPair it : accessor) {
1397 switch (it->Opcode()) {
1398 case Instruction::CONST_4:
1399 case Instruction::CONST_16:
1400 case Instruction::CONST:
1401 case Instruction::CONST_HIGH16:
1402 case Instruction::CONST_WIDE_16:
1403 case Instruction::CONST_WIDE_32:
1404 case Instruction::CONST_WIDE:
1405 case Instruction::CONST_WIDE_HIGH16:
1406 case Instruction::CONST_STRING:
1407 case Instruction::CONST_STRING_JUMBO:
1408 // Primitive, null or j.l.String initialization is permitted.
1409 break;
1410 case Instruction::RETURN_VOID:
Vladimir Marko2ab1bdd2018-07-12 09:59:56 +01001411 break;
1412 case Instruction::SPUT:
1413 case Instruction::SPUT_WIDE:
1414 case Instruction::SPUT_OBJECT:
1415 case Instruction::SPUT_BOOLEAN:
1416 case Instruction::SPUT_BYTE:
1417 case Instruction::SPUT_CHAR:
1418 case Instruction::SPUT_SHORT:
1419 // Only initialization of a static field of the same class is permitted.
1420 if (dex_file.GetFieldId(it->VRegB_21c()).class_idx_ != klass->GetDexTypeIndex()) {
1421 return false;
1422 }
1423 break;
1424 case Instruction::NEW_ARRAY:
1425 // Only primitive arrays are permitted.
1426 if (Primitive::GetType(dex_file.GetTypeDescriptor(dex_file.GetTypeId(
1427 dex::TypeIndex(it->VRegC_22c())))[1]) == Primitive::kPrimNot) {
1428 return false;
1429 }
1430 break;
1431 case Instruction::APUT:
1432 case Instruction::APUT_WIDE:
1433 case Instruction::APUT_BOOLEAN:
1434 case Instruction::APUT_BYTE:
1435 case Instruction::APUT_CHAR:
1436 case Instruction::APUT_SHORT:
1437 case Instruction::FILL_ARRAY_DATA:
1438 case Instruction::NOP:
1439 // Allow initialization of primitive arrays (only constants can be stored).
1440 // Note: We expect NOPs used for fill-array-data-payload but accept all NOPs
1441 // (even unreferenced switch payloads if they make it through the verifier).
1442 break;
1443 default:
1444 return false;
1445 }
1446 }
1447 }
1448 return true;
1449}
1450
1451static bool HasTrivialInitialization(ObjPtr<mirror::Class> cls,
1452 const CompilerOptions& compiler_options)
1453 REQUIRES_SHARED(Locks::mutator_lock_) {
1454 Runtime* runtime = Runtime::Current();
1455 PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
1456
1457 // Check the superclass chain.
1458 for (ObjPtr<mirror::Class> klass = cls; klass != nullptr; klass = klass->GetSuperClass()) {
1459 if (klass->IsInitialized() && IsInBootImage(klass, compiler_options)) {
1460 break; // `klass` and its superclasses are already initialized in the boot image.
1461 }
1462 if (!HasTrivialClinit(klass, pointer_size)) {
1463 return false;
1464 }
1465 }
1466
1467 // Also check interfaces with default methods as they need to be initialized as well.
1468 ObjPtr<mirror::IfTable> iftable = cls->GetIfTable();
1469 DCHECK(iftable != nullptr);
1470 for (int32_t i = 0, count = iftable->Count(); i != count; ++i) {
1471 ObjPtr<mirror::Class> iface = iftable->GetInterface(i);
1472 if (!iface->HasDefaultMethods()) {
1473 continue; // Initializing `cls` does not initialize this interface.
1474 }
1475 if (iface->IsInitialized() && IsInBootImage(iface, compiler_options)) {
1476 continue; // This interface is already initialized in the boot image.
1477 }
1478 if (!HasTrivialClinit(iface, pointer_size)) {
1479 return false;
1480 }
1481 }
1482 return true;
1483}
1484
Vladimir Marko2f40d242020-04-08 12:56:45 +01001485bool HInstructionBuilder::IsInitialized(ObjPtr<mirror::Class> cls) const {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001486 if (cls == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +00001487 return false;
1488 }
1489
Vladimir Marko51b8aaf2017-06-09 15:17:05 +01001490 // Check if the class will be initialized at runtime.
1491 if (cls->IsInitialized()) {
Vladimir Marko695348f2020-05-19 14:42:02 +01001492 const CompilerOptions& compiler_options = code_generator_->GetCompilerOptions();
1493 if (compiler_options.IsAotCompiler()) {
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +00001494 // Assume loaded only if klass is in the boot image. App classes cannot be assumed
1495 // loaded because we don't even know what class loader will be used to load them.
Vladimir Marko695348f2020-05-19 14:42:02 +01001496 if (IsInBootImage(cls, compiler_options)) {
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +00001497 return true;
1498 }
1499 } else {
Vladimir Marko695348f2020-05-19 14:42:02 +01001500 DCHECK(compiler_options.IsJitCompiler());
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +00001501 if (Runtime::Current()->GetJit()->CanAssumeInitialized(
Vladimir Marko2f40d242020-04-08 12:56:45 +01001502 cls,
Vladimir Marko695348f2020-05-19 14:42:02 +01001503 compiler_options.IsJitCompilerForSharedCode())) {
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +00001504 // For JIT, the class cannot revert to an uninitialized state.
1505 return true;
1506 }
Vladimir Marko51b8aaf2017-06-09 15:17:05 +01001507 }
1508 }
1509
Vladimir Marko7f260d42018-10-30 18:09:55 +00001510 // We can avoid the class initialization check for `cls` in static methods and constructors
1511 // in the very same class; invoking a static method involves a class initialization check
1512 // and so does the instance allocation that must be executed before invoking a constructor.
1513 // Other instance methods of the same class can run on an escaped instance
Vladimir Marko51b8aaf2017-06-09 15:17:05 +01001514 // of an erroneous class. Even a superclass may need to be checked as the subclass
1515 // can be completely initialized while the superclass is initializing and the subclass
1516 // remains initialized when the superclass initializer throws afterwards. b/62478025
1517 // Note: The HClinitCheck+HInvokeStaticOrDirect merging can still apply.
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001518 auto is_static_method_or_constructor_of_cls = [cls](const DexCompilationUnit& compilation_unit)
1519 REQUIRES_SHARED(Locks::mutator_lock_) {
1520 return (compilation_unit.GetAccessFlags() & (kAccStatic | kAccConstructor)) != 0u &&
Vladimir Marko2f40d242020-04-08 12:56:45 +01001521 compilation_unit.GetCompilingClass().Get() == cls;
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001522 };
1523 if (is_static_method_or_constructor_of_cls(*outer_compilation_unit_) ||
1524 // Check also the innermost method. Though excessive copies of ClinitCheck can be
1525 // eliminated by GVN, that happens only after the decision whether to inline the
1526 // graph or not and that may depend on the presence of the ClinitCheck.
1527 // TODO: We should walk over the entire inlined method chain, but we don't pass that
1528 // information to the builder.
1529 is_static_method_or_constructor_of_cls(*dex_compilation_unit_)) {
Vladimir Marko2ab1bdd2018-07-12 09:59:56 +01001530 return true;
1531 }
David Brazdildee58d62016-04-07 09:54:26 +00001532
Vladimir Marko2ab1bdd2018-07-12 09:59:56 +01001533 // Otherwise, we may be able to avoid the check if `cls` is a superclass of a method being
1534 // compiled here (anywhere in the inlining chain) as the `cls` must have started initializing
1535 // before calling any `cls` or subclass methods. Static methods require a clinit check and
1536 // instance methods require an instance which cannot be created before doing a clinit check.
1537 // When a subclass of `cls` starts initializing, it starts initializing its superclass
1538 // chain up to `cls` without running any bytecode, i.e. without any opportunity for circular
1539 // initialization weirdness.
1540 //
1541 // If the initialization of `cls` is trivial (`cls` and its superclasses and superinterfaces
1542 // with default methods initialize only their own static fields using constant values), it must
1543 // complete, either successfully or by throwing and marking `cls` erroneous, without allocating
1544 // any instances of `cls` or subclasses (or any other class) and without calling any methods.
1545 // If it completes by throwing, no instances of `cls` shall be created and no subclass method
1546 // bytecode shall execute (see above), therefore the instruction we're building shall be
1547 // unreachable. By reaching the instruction, we know that `cls` was initialized successfully.
1548 //
1549 // TODO: We should walk over the entire inlined methods chain, but we don't pass that
1550 // information to the builder. (We could also check if we're guaranteed a non-null instance
1551 // of `cls` at this location but that's outside the scope of the instruction builder.)
Vladimir Marko2f40d242020-04-08 12:56:45 +01001552 bool is_subclass = IsSubClass(outer_compilation_unit_->GetCompilingClass().Get(), cls);
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001553 if (dex_compilation_unit_ != outer_compilation_unit_) {
1554 is_subclass = is_subclass ||
Vladimir Marko2f40d242020-04-08 12:56:45 +01001555 IsSubClass(dex_compilation_unit_->GetCompilingClass().Get(), cls);
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001556 }
Vladimir Marko2f40d242020-04-08 12:56:45 +01001557 if (is_subclass && HasTrivialInitialization(cls, code_generator_->GetCompilerOptions())) {
Vladimir Marko2ab1bdd2018-07-12 09:59:56 +01001558 return true;
1559 }
David Brazdildee58d62016-04-07 09:54:26 +00001560
1561 return false;
1562}
1563
1564HClinitCheck* HInstructionBuilder::ProcessClinitCheckForInvoke(
Vladimir Markofca0b492018-07-23 15:30:52 +01001565 uint32_t dex_pc,
1566 ArtMethod* resolved_method,
1567 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
Vladimir Marko2f40d242020-04-08 12:56:45 +01001568 ScopedObjectAccess soa(Thread::Current());
1569 ObjPtr<mirror::Class> klass = resolved_method->GetDeclaringClass();
David Brazdildee58d62016-04-07 09:54:26 +00001570
1571 HClinitCheck* clinit_check = nullptr;
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001572 if (IsInitialized(klass)) {
David Brazdildee58d62016-04-07 09:54:26 +00001573 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001574 } else {
Vladimir Marko02ca05a2020-05-12 13:58:51 +01001575 Handle<mirror::Class> h_klass = graph_->GetHandleCache()->NewHandle(klass);
Vladimir Marko2f40d242020-04-08 12:56:45 +01001576 HLoadClass* cls = BuildLoadClass(h_klass->GetDexTypeIndex(),
1577 h_klass->GetDexFile(),
1578 h_klass,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001579 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08001580 /* needs_access_check= */ false);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001581 if (cls != nullptr) {
1582 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
Vladimir Markoca6fff82017-10-03 14:49:14 +01001583 clinit_check = new (allocator_) HClinitCheck(cls, dex_pc);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001584 AppendInstruction(clinit_check);
Vladimir Marko2f40d242020-04-08 12:56:45 +01001585 } else {
1586 // Let the invoke handle this with an implicit class initialization check.
1587 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001588 }
David Brazdildee58d62016-04-07 09:54:26 +00001589 }
1590 return clinit_check;
1591}
1592
Vladimir Marko5f846072020-04-09 13:20:11 +01001593bool HInstructionBuilder::SetupInvokeArguments(HInstruction* invoke,
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001594 const InstructionOperands& operands,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001595 const char* shorty,
Vladimir Marko5f846072020-04-09 13:20:11 +01001596 ReceiverArg receiver_arg) {
1597 // Note: The `invoke` can be an intrinsic replacement, so not necessaritly HInvoke.
1598 // 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 +01001599 uint32_t shorty_index = 1; // Skip the return type.
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001600 const size_t number_of_operands = operands.GetNumberOfOperands();
Vladimir Marko5f846072020-04-09 13:20:11 +01001601 bool argument_length_error = false;
1602
1603 size_t start_index = 0u;
1604 size_t argument_index = 0u;
1605 if (receiver_arg != ReceiverArg::kNone) {
1606 if (number_of_operands == 0u) {
1607 argument_length_error = true;
1608 } else {
1609 start_index = 1u;
1610 if (receiver_arg != ReceiverArg::kIgnored) {
1611 uint32_t obj_reg = operands.GetOperand(0u);
1612 HInstruction* arg = (receiver_arg == ReceiverArg::kPlainArg)
1613 ? LoadLocal(obj_reg, DataType::Type::kReference)
1614 : LoadNullCheckedLocal(obj_reg, invoke->GetDexPc());
1615 if (receiver_arg != ReceiverArg::kNullCheckedOnly) {
1616 invoke->SetRawInputAt(0u, arg);
1617 argument_index = 1u;
1618 }
1619 }
1620 }
1621 }
1622
1623 for (size_t i = start_index; i < number_of_operands; ++i, ++argument_index) {
1624 // Make sure we don't go over the expected arguments or over the number of
1625 // dex registers given. If the instruction was seen as dead by the verifier,
1626 // it hasn't been properly checked.
1627 if (UNLIKELY(shorty[shorty_index] == 0)) {
1628 argument_length_error = true;
1629 break;
1630 }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001631 DataType::Type type = DataType::FromShorty(shorty[shorty_index++]);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001632 bool is_wide = (type == DataType::Type::kInt64) || (type == DataType::Type::kFloat64);
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001633 if (is_wide && ((i + 1 == number_of_operands) ||
1634 (operands.GetOperand(i) + 1 != operands.GetOperand(i + 1)))) {
Vladimir Marko5f846072020-04-09 13:20:11 +01001635 if (invoke->IsInvoke()) {
1636 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
1637 // reject any class where this is violated. However, the verifier only does these checks
1638 // on non trivially dead instructions, so we just bailout the compilation.
1639 VLOG(compiler) << "Did not compile "
1640 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
1641 << " because of non-sequential dex register pair in wide argument";
1642 MaybeRecordStat(compilation_stats_,
1643 MethodCompilationStat::kNotCompiledMalformedOpcode);
1644 }
David Brazdildee58d62016-04-07 09:54:26 +00001645 return false;
1646 }
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001647 HInstruction* arg = LoadLocal(operands.GetOperand(i), type);
Vladimir Marko5f846072020-04-09 13:20:11 +01001648 DCHECK(invoke->InputAt(argument_index) == nullptr);
1649 invoke->SetRawInputAt(argument_index, arg);
David Brazdildee58d62016-04-07 09:54:26 +00001650 if (is_wide) {
Vladimir Marko5f846072020-04-09 13:20:11 +01001651 ++i;
David Brazdildee58d62016-04-07 09:54:26 +00001652 }
1653 }
1654
Vladimir Marko5f846072020-04-09 13:20:11 +01001655 argument_length_error = argument_length_error || shorty[shorty_index] != 0;
1656 if (argument_length_error) {
1657 if (invoke->IsInvoke()) {
1658 VLOG(compiler) << "Did not compile "
1659 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
1660 << " because of wrong number of arguments in invoke instruction";
1661 MaybeRecordStat(compilation_stats_,
1662 MethodCompilationStat::kNotCompiledMalformedOpcode);
1663 }
David Brazdildee58d62016-04-07 09:54:26 +00001664 return false;
1665 }
1666
1667 if (invoke->IsInvokeStaticOrDirect() &&
1668 HInvokeStaticOrDirect::NeedsCurrentMethodInput(
Vladimir Marko86c87522020-05-11 16:55:55 +01001669 invoke->AsInvokeStaticOrDirect()->GetDispatchInfo())) {
1670 DCHECK_EQ(argument_index, invoke->AsInvokeStaticOrDirect()->GetCurrentMethodIndex());
Vladimir Marko5f846072020-04-09 13:20:11 +01001671 DCHECK(invoke->InputAt(argument_index) == nullptr);
1672 invoke->SetRawInputAt(argument_index, graph_->GetCurrentMethod());
David Brazdildee58d62016-04-07 09:54:26 +00001673 }
1674
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01001675 if (invoke->IsInvokeInterface() &&
1676 (invoke->AsInvokeInterface()->GetHiddenArgumentLoadKind() == MethodLoadKind::kRecursive)) {
1677 invoke->SetRawInputAt(invoke->AsInvokeInterface()->GetNumberOfArguments() - 1,
1678 graph_->GetCurrentMethod());
1679 }
1680
David Brazdildee58d62016-04-07 09:54:26 +00001681 return true;
1682}
1683
1684bool HInstructionBuilder::HandleInvoke(HInvoke* invoke,
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001685 const InstructionOperands& operands,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001686 const char* shorty,
Vladimir Marko5f846072020-04-09 13:20:11 +01001687 bool is_unresolved) {
David Brazdildee58d62016-04-07 09:54:26 +00001688 DCHECK(!invoke->IsInvokeStaticOrDirect() || !invoke->AsInvokeStaticOrDirect()->IsStringInit());
1689
Vladimir Marko5f846072020-04-09 13:20:11 +01001690 ReceiverArg receiver_arg = (invoke->GetInvokeType() == InvokeType::kStatic)
1691 ? ReceiverArg::kNone
1692 : (is_unresolved ? ReceiverArg::kPlainArg : ReceiverArg::kNullCheckedArg);
1693 if (!SetupInvokeArguments(invoke, operands, shorty, receiver_arg)) {
David Brazdildee58d62016-04-07 09:54:26 +00001694 return false;
1695 }
1696
David Brazdildee58d62016-04-07 09:54:26 +00001697 AppendInstruction(invoke);
1698 latest_result_ = invoke;
1699
1700 return true;
1701}
1702
Vladimir Marko5f846072020-04-09 13:20:11 +01001703bool HInstructionBuilder::BuildSimpleIntrinsic(ArtMethod* method,
1704 uint32_t dex_pc,
1705 const InstructionOperands& operands,
1706 const char* shorty) {
1707 Intrinsics intrinsic = static_cast<Intrinsics>(method->GetIntrinsic());
1708 DCHECK_NE(intrinsic, Intrinsics::kNone);
1709 constexpr DataType::Type kInt32 = DataType::Type::kInt32;
1710 constexpr DataType::Type kInt64 = DataType::Type::kInt64;
1711 constexpr DataType::Type kFloat32 = DataType::Type::kFloat32;
1712 constexpr DataType::Type kFloat64 = DataType::Type::kFloat64;
1713 ReceiverArg receiver_arg = method->IsStatic() ? ReceiverArg::kNone : ReceiverArg::kNullCheckedArg;
1714 HInstruction* instruction = nullptr;
1715 switch (intrinsic) {
1716 case Intrinsics::kIntegerRotateRight:
1717 case Intrinsics::kIntegerRotateLeft:
1718 // For rotate left, we negate the distance below.
1719 instruction = new (allocator_) HRor(kInt32, /*value=*/ nullptr, /*distance=*/ nullptr);
1720 break;
1721 case Intrinsics::kLongRotateRight:
1722 case Intrinsics::kLongRotateLeft:
1723 // For rotate left, we negate the distance below.
1724 instruction = new (allocator_) HRor(kInt64, /*value=*/ nullptr, /*distance=*/ nullptr);
1725 break;
1726 case Intrinsics::kIntegerCompare:
1727 instruction = new (allocator_) HCompare(
1728 kInt32, /*first=*/ nullptr, /*second=*/ nullptr, ComparisonBias::kNoBias, dex_pc);
1729 break;
1730 case Intrinsics::kLongCompare:
1731 instruction = new (allocator_) HCompare(
1732 kInt64, /*first=*/ nullptr, /*second=*/ nullptr, ComparisonBias::kNoBias, dex_pc);
1733 break;
1734 case Intrinsics::kIntegerSignum:
1735 instruction = new (allocator_) HCompare(
1736 kInt32, /*first=*/ nullptr, graph_->GetIntConstant(0), ComparisonBias::kNoBias, dex_pc);
1737 break;
1738 case Intrinsics::kLongSignum:
1739 instruction = new (allocator_) HCompare(
1740 kInt64, /*first=*/ nullptr, graph_->GetLongConstant(0), ComparisonBias::kNoBias, dex_pc);
1741 break;
1742 case Intrinsics::kFloatIsNaN:
1743 case Intrinsics::kDoubleIsNaN: {
1744 // IsNaN(x) is the same as x != x.
1745 instruction = new (allocator_) HNotEqual(/*first=*/ nullptr, /*second=*/ nullptr, dex_pc);
1746 instruction->AsCondition()->SetBias(ComparisonBias::kLtBias);
1747 break;
1748 }
1749 case Intrinsics::kStringCharAt:
1750 // We treat String as an array to allow DCE and BCE to seamlessly work on strings.
1751 instruction = new (allocator_) HArrayGet(/*array=*/ nullptr,
1752 /*index=*/ nullptr,
1753 DataType::Type::kUint16,
1754 SideEffects::None(), // Strings are immutable.
1755 dex_pc,
1756 /*is_string_char_at=*/ true);
1757 break;
1758 case Intrinsics::kStringIsEmpty:
1759 case Intrinsics::kStringLength:
1760 // We treat String as an array to allow DCE and BCE to seamlessly work on strings.
1761 // For String.isEmpty(), we add a comparison with 0 below.
1762 instruction =
1763 new (allocator_) HArrayLength(/*array=*/ nullptr, dex_pc, /* is_string_length= */ true);
1764 break;
1765 case Intrinsics::kUnsafeLoadFence:
1766 receiver_arg = ReceiverArg::kNullCheckedOnly;
1767 instruction = new (allocator_) HMemoryBarrier(MemBarrierKind::kLoadAny, dex_pc);
1768 break;
1769 case Intrinsics::kUnsafeStoreFence:
1770 receiver_arg = ReceiverArg::kNullCheckedOnly;
1771 instruction = new (allocator_) HMemoryBarrier(MemBarrierKind::kAnyStore, dex_pc);
1772 break;
1773 case Intrinsics::kUnsafeFullFence:
1774 receiver_arg = ReceiverArg::kNullCheckedOnly;
1775 instruction = new (allocator_) HMemoryBarrier(MemBarrierKind::kAnyAny, dex_pc);
1776 break;
1777 case Intrinsics::kVarHandleFullFence:
1778 instruction = new (allocator_) HMemoryBarrier(MemBarrierKind::kAnyAny, dex_pc);
1779 break;
1780 case Intrinsics::kVarHandleAcquireFence:
1781 instruction = new (allocator_) HMemoryBarrier(MemBarrierKind::kLoadAny, dex_pc);
1782 break;
1783 case Intrinsics::kVarHandleReleaseFence:
1784 instruction = new (allocator_) HMemoryBarrier(MemBarrierKind::kAnyStore, dex_pc);
1785 break;
1786 case Intrinsics::kVarHandleLoadLoadFence:
1787 instruction = new (allocator_) HMemoryBarrier(MemBarrierKind::kLoadAny, dex_pc);
1788 break;
1789 case Intrinsics::kVarHandleStoreStoreFence:
1790 instruction = new (allocator_) HMemoryBarrier(MemBarrierKind::kStoreStore, dex_pc);
1791 break;
1792 case Intrinsics::kMathMinIntInt:
1793 instruction = new (allocator_) HMin(kInt32, /*left=*/ nullptr, /*right=*/ nullptr, dex_pc);
1794 break;
1795 case Intrinsics::kMathMinLongLong:
1796 instruction = new (allocator_) HMin(kInt64, /*left=*/ nullptr, /*right=*/ nullptr, dex_pc);
1797 break;
1798 case Intrinsics::kMathMinFloatFloat:
1799 instruction = new (allocator_) HMin(kFloat32, /*left=*/ nullptr, /*right=*/ nullptr, dex_pc);
1800 break;
1801 case Intrinsics::kMathMinDoubleDouble:
1802 instruction = new (allocator_) HMin(kFloat64, /*left=*/ nullptr, /*right=*/ nullptr, dex_pc);
1803 break;
1804 case Intrinsics::kMathMaxIntInt:
1805 instruction = new (allocator_) HMax(kInt32, /*left=*/ nullptr, /*right=*/ nullptr, dex_pc);
1806 break;
1807 case Intrinsics::kMathMaxLongLong:
1808 instruction = new (allocator_) HMax(kInt64, /*left=*/ nullptr, /*right=*/ nullptr, dex_pc);
1809 break;
1810 case Intrinsics::kMathMaxFloatFloat:
1811 instruction = new (allocator_) HMax(kFloat32, /*left=*/ nullptr, /*right=*/ nullptr, dex_pc);
1812 break;
1813 case Intrinsics::kMathMaxDoubleDouble:
1814 instruction = new (allocator_) HMax(kFloat64, /*left=*/ nullptr, /*right=*/ nullptr, dex_pc);
1815 break;
1816 case Intrinsics::kMathAbsInt:
1817 instruction = new (allocator_) HAbs(kInt32, /*input=*/ nullptr, dex_pc);
1818 break;
1819 case Intrinsics::kMathAbsLong:
1820 instruction = new (allocator_) HAbs(kInt64, /*input=*/ nullptr, dex_pc);
1821 break;
1822 case Intrinsics::kMathAbsFloat:
1823 instruction = new (allocator_) HAbs(kFloat32, /*input=*/ nullptr, dex_pc);
1824 break;
1825 case Intrinsics::kMathAbsDouble:
1826 instruction = new (allocator_) HAbs(kFloat64, /*input=*/ nullptr, dex_pc);
1827 break;
1828 default:
1829 // We do not have intermediate representation for other intrinsics.
1830 return false;
1831 }
1832 DCHECK(instruction != nullptr);
1833 if (!SetupInvokeArguments(instruction, operands, shorty, receiver_arg)) {
1834 return false;
1835 }
1836
1837 switch (intrinsic) {
1838 case Intrinsics::kIntegerRotateLeft:
1839 case Intrinsics::kLongRotateLeft: {
1840 // Negate the distance value for rotate left.
1841 DCHECK(instruction->IsRor());
1842 HNeg* neg = new (allocator_) HNeg(kInt32, instruction->InputAt(1u));
1843 AppendInstruction(neg);
1844 instruction->SetRawInputAt(1u, neg);
1845 break;
1846 }
1847 case Intrinsics::kFloatIsNaN:
1848 case Intrinsics::kDoubleIsNaN:
1849 // Set the second input to be the same as first.
1850 DCHECK(instruction->IsNotEqual());
1851 DCHECK(instruction->InputAt(1u) == nullptr);
1852 instruction->SetRawInputAt(1u, instruction->InputAt(0u));
1853 break;
1854 case Intrinsics::kStringCharAt: {
1855 // Add bounds check.
1856 HInstruction* array = instruction->InputAt(0u);
1857 HInstruction* index = instruction->InputAt(1u);
1858 HInstruction* length =
1859 new (allocator_) HArrayLength(array, dex_pc, /*is_string_length=*/ true);
1860 AppendInstruction(length);
1861 HBoundsCheck* bounds_check =
1862 new (allocator_) HBoundsCheck(index, length, dex_pc, /*is_string_char_at=*/ true);
1863 AppendInstruction(bounds_check);
1864 graph_->SetHasBoundsChecks(true);
1865 instruction->SetRawInputAt(1u, bounds_check);
1866 break;
1867 }
1868 case Intrinsics::kStringIsEmpty: {
1869 // Compare the length with 0.
1870 DCHECK(instruction->IsArrayLength());
1871 AppendInstruction(instruction);
1872 HEqual* equal = new (allocator_) HEqual(instruction, graph_->GetIntConstant(0), dex_pc);
1873 instruction = equal;
1874 break;
1875 }
1876 default:
1877 break;
1878 }
1879
1880 AppendInstruction(instruction);
1881 latest_result_ = instruction;
1882
1883 return true;
1884}
1885
David Brazdildee58d62016-04-07 09:54:26 +00001886bool HInstructionBuilder::HandleStringInit(HInvoke* invoke,
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001887 const InstructionOperands& operands,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001888 const char* shorty) {
David Brazdildee58d62016-04-07 09:54:26 +00001889 DCHECK(invoke->IsInvokeStaticOrDirect());
1890 DCHECK(invoke->AsInvokeStaticOrDirect()->IsStringInit());
1891
Vladimir Marko5f846072020-04-09 13:20:11 +01001892 if (!SetupInvokeArguments(invoke, operands, shorty, ReceiverArg::kIgnored)) {
David Brazdildee58d62016-04-07 09:54:26 +00001893 return false;
1894 }
1895
1896 AppendInstruction(invoke);
1897
1898 // This is a StringFactory call, not an actual String constructor. Its result
1899 // replaces the empty String pre-allocated by NewInstance.
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001900 uint32_t orig_this_reg = operands.GetOperand(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001901 HInstruction* arg_this = LoadLocal(orig_this_reg, DataType::Type::kReference);
David Brazdildee58d62016-04-07 09:54:26 +00001902
1903 // Replacing the NewInstance might render it redundant. Keep a list of these
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +01001904 // to be visited once it is clear whether it has remaining uses.
David Brazdildee58d62016-04-07 09:54:26 +00001905 if (arg_this->IsNewInstance()) {
1906 ssa_builder_->AddUninitializedString(arg_this->AsNewInstance());
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +01001907 } else {
1908 DCHECK(arg_this->IsPhi());
1909 // We can get a phi as input of a String.<init> if there is a loop between the
1910 // allocation and the String.<init> call. As we don't know which other phis might alias
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +01001911 // with `arg_this`, we keep a record of those invocations so we can later replace
1912 // the allocation with the invocation.
1913 // Add the actual 'this' input so the analysis knows what is the allocation instruction.
1914 // The input will be removed during the analysis.
1915 invoke->AddInput(arg_this);
1916 ssa_builder_->AddUninitializedStringPhi(invoke);
1917 }
1918 // Walk over all vregs and replace any occurrence of `arg_this` with `invoke`.
1919 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
1920 if ((*current_locals_)[vreg] == arg_this) {
1921 (*current_locals_)[vreg] = invoke;
1922 }
David Brazdildee58d62016-04-07 09:54:26 +00001923 }
David Brazdildee58d62016-04-07 09:54:26 +00001924 return true;
1925}
1926
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001927static DataType::Type GetFieldAccessType(const DexFile& dex_file, uint16_t field_index) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001928 const dex::FieldId& field_id = dex_file.GetFieldId(field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001929 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001930 return DataType::FromShorty(type[0]);
David Brazdildee58d62016-04-07 09:54:26 +00001931}
1932
1933bool HInstructionBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
1934 uint32_t dex_pc,
Nicolas Geoffray4924ea92021-03-23 08:25:31 +00001935 bool is_put) {
David Brazdildee58d62016-04-07 09:54:26 +00001936 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1937 uint32_t obj_reg = instruction.VRegB_22c();
Nicolas Geoffray4924ea92021-03-23 08:25:31 +00001938 uint16_t field_index = instruction.VRegC_22c();
David Brazdildee58d62016-04-07 09:54:26 +00001939
1940 ScopedObjectAccess soa(Thread::Current());
Andreas Gampe3db70682018-12-26 15:12:03 -08001941 ArtField* resolved_field = ResolveField(field_index, /* is_static= */ false, is_put);
David Brazdildee58d62016-04-07 09:54:26 +00001942
Aart Bik14154132016-06-02 17:53:58 -07001943 // Generate an explicit null check on the reference, unless the field access
1944 // is unresolved. In that case, we rely on the runtime to perform various
1945 // checks first, followed by a null check.
1946 HInstruction* object = (resolved_field == nullptr)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001947 ? LoadLocal(obj_reg, DataType::Type::kReference)
Aart Bik14154132016-06-02 17:53:58 -07001948 : LoadNullCheckedLocal(obj_reg, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001949
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001950 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001951 if (is_put) {
1952 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1953 HInstruction* field_set = nullptr;
1954 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001955 MaybeRecordStat(compilation_stats_,
1956 MethodCompilationStat::kUnresolvedField);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001957 field_set = new (allocator_) HUnresolvedInstanceFieldSet(object,
1958 value,
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_set = new (allocator_) HInstanceFieldSet(object,
1965 value,
1966 resolved_field,
1967 field_type,
1968 resolved_field->GetOffset(),
1969 resolved_field->IsVolatile(),
1970 field_index,
1971 class_def_index,
1972 *dex_file_,
1973 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001974 }
1975 AppendInstruction(field_set);
1976 } else {
1977 HInstruction* field_get = nullptr;
1978 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001979 MaybeRecordStat(compilation_stats_,
1980 MethodCompilationStat::kUnresolvedField);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001981 field_get = new (allocator_) HUnresolvedInstanceFieldGet(object,
1982 field_type,
1983 field_index,
1984 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001985 } else {
1986 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Vladimir Markoca6fff82017-10-03 14:49:14 +01001987 field_get = new (allocator_) HInstanceFieldGet(object,
1988 resolved_field,
1989 field_type,
1990 resolved_field->GetOffset(),
1991 resolved_field->IsVolatile(),
1992 field_index,
1993 class_def_index,
1994 *dex_file_,
1995 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001996 }
1997 AppendInstruction(field_get);
1998 UpdateLocal(source_or_dest_reg, field_get);
1999 }
2000
2001 return true;
2002}
2003
David Brazdildee58d62016-04-07 09:54:26 +00002004void HInstructionBuilder::BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002005 uint32_t dex_pc,
2006 bool is_put,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002007 DataType::Type field_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002008 uint32_t source_or_dest_reg = instruction.VRegA_21c();
2009 uint16_t field_index = instruction.VRegB_21c();
2010
2011 if (is_put) {
2012 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
2013 AppendInstruction(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002014 new (allocator_) HUnresolvedStaticFieldSet(value, field_type, field_index, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002015 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002016 AppendInstruction(new (allocator_) HUnresolvedStaticFieldGet(field_type, field_index, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002017 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
2018 }
2019}
2020
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002021ArtField* HInstructionBuilder::ResolveField(uint16_t field_idx, bool is_static, bool is_put) {
2022 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002023
2024 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002025 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002026
Nicolas Geoffraycb5f97d2021-01-05 15:30:41 +00002027 ArtField* resolved_field = class_linker->ResolveFieldJLS(field_idx,
2028 dex_compilation_unit_->GetDexCache(),
2029 class_loader);
Nicolas Geoffrayf3688822020-03-25 15:04:03 +00002030 DCHECK_EQ(resolved_field == nullptr, soa.Self()->IsExceptionPending())
2031 << "field="
2032 << ((resolved_field == nullptr) ? "null" : resolved_field->PrettyField())
2033 << ", exception="
2034 << (soa.Self()->IsExceptionPending() ? soa.Self()->GetException()->Dump() : "null");
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002035 if (UNLIKELY(resolved_field == nullptr)) {
Vladimir Markoa2c211c2018-11-01 09:50:52 +00002036 // Clean up any exception left by field resolution.
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002037 soa.Self()->ClearException();
2038 return nullptr;
2039 }
2040
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002041 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
2042 return nullptr;
2043 }
2044
2045 // Check access.
Vladimir Markoa2c211c2018-11-01 09:50:52 +00002046 Handle<mirror::Class> compiling_class = dex_compilation_unit_->GetCompilingClass();
Andreas Gampefa4333d2017-02-14 11:10:34 -08002047 if (compiling_class == nullptr) {
Vladimir Marko4100e5e2020-08-26 16:44:01 +01002048 // Check if the declaring class or referencing class is accessible.
2049 SamePackageCompare same_package(*dex_compilation_unit_);
2050 ObjPtr<mirror::Class> declaring_class = resolved_field->GetDeclaringClass();
2051 bool declaring_class_accessible = declaring_class->IsPublic() || same_package(declaring_class);
2052 if (!declaring_class_accessible) {
2053 // It is possible to access members from an inaccessible superclass
2054 // by referencing them through an accessible subclass.
2055 ObjPtr<mirror::Class> referenced_class = class_linker->LookupResolvedType(
2056 dex_compilation_unit_->GetDexFile()->GetFieldId(field_idx).class_idx_,
2057 dex_compilation_unit_->GetDexCache().Get(),
2058 class_loader.Get());
2059 DCHECK(referenced_class != nullptr); // Must have been resolved when resolving the field.
2060 if (!referenced_class->IsPublic() && !same_package(referenced_class)) {
2061 return nullptr;
2062 }
2063 }
2064 // Check whether the field itself is accessible.
2065 // Since the referrer is unresolved but the field is resolved, it cannot be
2066 // inside the same class, so a private field is known to be inaccessible.
2067 // And without a resolved referrer, we cannot check for protected member access
2068 // in superlass, so we handle only access to public member or within the package.
2069 if (resolved_field->IsPrivate() ||
2070 (!resolved_field->IsPublic() && !declaring_class_accessible)) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002071 return nullptr;
2072 }
2073 } else if (!compiling_class->CanAccessResolvedField(resolved_field->GetDeclaringClass(),
2074 resolved_field,
2075 dex_compilation_unit_->GetDexCache().Get(),
2076 field_idx)) {
2077 return nullptr;
2078 }
2079
2080 if (is_put &&
2081 resolved_field->IsFinal() &&
2082 (compiling_class.Get() != resolved_field->GetDeclaringClass())) {
2083 // Final fields can only be updated within their own class.
2084 // TODO: Only allow it in constructors. b/34966607.
2085 return nullptr;
2086 }
2087
Nicolas Geoffray396198b2020-06-16 12:02:45 +01002088 StackArtFieldHandleScope<1> rhs(soa.Self());
2089 ReflectiveHandle<ArtField> resolved_field_handle(rhs.NewHandle(resolved_field));
2090 if (resolved_field->ResolveType().IsNull()) {
2091 // ArtField::ResolveType() may fail as evidenced with a dexing bug (b/78788577).
2092 soa.Self()->ClearException();
2093 return nullptr; // Failure
2094 }
2095 return resolved_field_handle.Get();
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002096}
2097
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002098void HInstructionBuilder::BuildStaticFieldAccess(const Instruction& instruction,
David Brazdildee58d62016-04-07 09:54:26 +00002099 uint32_t dex_pc,
2100 bool is_put) {
2101 uint32_t source_or_dest_reg = instruction.VRegA_21c();
2102 uint16_t field_index = instruction.VRegB_21c();
2103
2104 ScopedObjectAccess soa(Thread::Current());
Andreas Gampe3db70682018-12-26 15:12:03 -08002105 ArtField* resolved_field = ResolveField(field_index, /* is_static= */ true, is_put);
David Brazdildee58d62016-04-07 09:54:26 +00002106
2107 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07002108 MaybeRecordStat(compilation_stats_,
2109 MethodCompilationStat::kUnresolvedField);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002110 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00002111 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002112 return;
David Brazdildee58d62016-04-07 09:54:26 +00002113 }
2114
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002115 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00002116
Vladimir Marko02ca05a2020-05-12 13:58:51 +01002117 Handle<mirror::Class> klass =
2118 graph_->GetHandleCache()->NewHandle(resolved_field->GetDeclaringClass());
Vladimir Markoa2c211c2018-11-01 09:50:52 +00002119 HLoadClass* constant = BuildLoadClass(klass->GetDexTypeIndex(),
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002120 klass->GetDexFile(),
2121 klass,
2122 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08002123 /* needs_access_check= */ false);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002124
2125 if (constant == nullptr) {
2126 // The class cannot be referenced from this compiled code. Generate
2127 // an unresolved access.
Igor Murashkin1e065a52017-08-09 13:20:34 -07002128 MaybeRecordStat(compilation_stats_,
2129 MethodCompilationStat::kUnresolvedFieldNotAFastAccess);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002130 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002131 return;
David Brazdildee58d62016-04-07 09:54:26 +00002132 }
2133
David Brazdildee58d62016-04-07 09:54:26 +00002134 HInstruction* cls = constant;
Vladimir Marko2f40d242020-04-08 12:56:45 +01002135 if (!IsInitialized(klass.Get())) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002136 cls = new (allocator_) HClinitCheck(constant, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002137 AppendInstruction(cls);
2138 }
2139
2140 uint16_t class_def_index = klass->GetDexClassDefIndex();
2141 if (is_put) {
2142 // We need to keep the class alive before loading the value.
2143 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
2144 DCHECK_EQ(HPhi::ToPhiType(value->GetType()), HPhi::ToPhiType(field_type));
Vladimir Markoca6fff82017-10-03 14:49:14 +01002145 AppendInstruction(new (allocator_) HStaticFieldSet(cls,
2146 value,
2147 resolved_field,
2148 field_type,
2149 resolved_field->GetOffset(),
2150 resolved_field->IsVolatile(),
2151 field_index,
2152 class_def_index,
2153 *dex_file_,
2154 dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002155 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002156 AppendInstruction(new (allocator_) HStaticFieldGet(cls,
2157 resolved_field,
2158 field_type,
2159 resolved_field->GetOffset(),
2160 resolved_field->IsVolatile(),
2161 field_index,
2162 class_def_index,
2163 *dex_file_,
2164 dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002165 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
2166 }
David Brazdildee58d62016-04-07 09:54:26 +00002167}
2168
2169void HInstructionBuilder::BuildCheckedDivRem(uint16_t out_vreg,
Vladimir Markoca6fff82017-10-03 14:49:14 +01002170 uint16_t first_vreg,
2171 int64_t second_vreg_or_constant,
2172 uint32_t dex_pc,
2173 DataType::Type type,
2174 bool second_is_constant,
2175 bool isDiv) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002176 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
David Brazdildee58d62016-04-07 09:54:26 +00002177
2178 HInstruction* first = LoadLocal(first_vreg, type);
2179 HInstruction* second = nullptr;
2180 if (second_is_constant) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002181 if (type == DataType::Type::kInt32) {
David Brazdildee58d62016-04-07 09:54:26 +00002182 second = graph_->GetIntConstant(second_vreg_or_constant, dex_pc);
2183 } else {
2184 second = graph_->GetLongConstant(second_vreg_or_constant, dex_pc);
2185 }
2186 } else {
2187 second = LoadLocal(second_vreg_or_constant, type);
2188 }
2189
2190 if (!second_is_constant
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002191 || (type == DataType::Type::kInt32 && second->AsIntConstant()->GetValue() == 0)
2192 || (type == DataType::Type::kInt64 && second->AsLongConstant()->GetValue() == 0)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002193 second = new (allocator_) HDivZeroCheck(second, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002194 AppendInstruction(second);
2195 }
2196
2197 if (isDiv) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002198 AppendInstruction(new (allocator_) HDiv(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002199 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002200 AppendInstruction(new (allocator_) HRem(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002201 }
2202 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
2203}
2204
2205void HInstructionBuilder::BuildArrayAccess(const Instruction& instruction,
2206 uint32_t dex_pc,
2207 bool is_put,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002208 DataType::Type anticipated_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002209 uint8_t source_or_dest_reg = instruction.VRegA_23x();
2210 uint8_t array_reg = instruction.VRegB_23x();
2211 uint8_t index_reg = instruction.VRegC_23x();
2212
David Brazdilc120bbe2016-04-22 16:57:00 +01002213 HInstruction* object = LoadNullCheckedLocal(array_reg, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002214 HInstruction* length = new (allocator_) HArrayLength(object, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002215 AppendInstruction(length);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002216 HInstruction* index = LoadLocal(index_reg, DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002217 index = new (allocator_) HBoundsCheck(index, length, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002218 AppendInstruction(index);
2219 if (is_put) {
2220 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
2221 // TODO: Insert a type check node if the type is Object.
Vladimir Markoca6fff82017-10-03 14:49:14 +01002222 HArraySet* aset = new (allocator_) HArraySet(object, index, value, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002223 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
2224 AppendInstruction(aset);
2225 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002226 HArrayGet* aget = new (allocator_) HArrayGet(object, index, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002227 ssa_builder_->MaybeAddAmbiguousArrayGet(aget);
2228 AppendInstruction(aget);
2229 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
2230 }
2231 graph_->SetHasBoundsChecks(true);
2232}
2233
Vladimir Markob5461632018-10-15 14:24:21 +01002234HNewArray* HInstructionBuilder::BuildNewArray(uint32_t dex_pc,
2235 dex::TypeIndex type_index,
2236 HInstruction* length) {
2237 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
2238
2239 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(type_index));
2240 DCHECK_EQ(descriptor[0], '[');
2241 size_t component_type_shift = Primitive::ComponentSizeShift(Primitive::GetType(descriptor[1]));
2242
2243 HNewArray* new_array = new (allocator_) HNewArray(cls, length, dex_pc, component_type_shift);
2244 AppendInstruction(new_array);
2245 return new_array;
2246}
2247
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002248HNewArray* HInstructionBuilder::BuildFilledNewArray(uint32_t dex_pc,
2249 dex::TypeIndex type_index,
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002250 const InstructionOperands& operands) {
2251 const size_t number_of_operands = operands.GetNumberOfOperands();
2252 HInstruction* length = graph_->GetIntConstant(number_of_operands, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002253
Vladimir Markob5461632018-10-15 14:24:21 +01002254 HNewArray* new_array = BuildNewArray(dex_pc, type_index, length);
David Brazdildee58d62016-04-07 09:54:26 +00002255 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
2256 DCHECK_EQ(descriptor[0], '[') << descriptor;
2257 char primitive = descriptor[1];
2258 DCHECK(primitive == 'I'
2259 || primitive == 'L'
2260 || primitive == '[') << descriptor;
2261 bool is_reference_array = (primitive == 'L') || (primitive == '[');
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002262 DataType::Type type = is_reference_array ? DataType::Type::kReference : DataType::Type::kInt32;
David Brazdildee58d62016-04-07 09:54:26 +00002263
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002264 for (size_t i = 0; i < number_of_operands; ++i) {
2265 HInstruction* value = LoadLocal(operands.GetOperand(i), type);
David Brazdildee58d62016-04-07 09:54:26 +00002266 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
Vladimir Markob5461632018-10-15 14:24:21 +01002267 HArraySet* aset = new (allocator_) HArraySet(new_array, index, value, type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002268 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
2269 AppendInstruction(aset);
2270 }
Vladimir Markob5461632018-10-15 14:24:21 +01002271 latest_result_ = new_array;
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002272
Vladimir Markob5461632018-10-15 14:24:21 +01002273 return new_array;
David Brazdildee58d62016-04-07 09:54:26 +00002274}
2275
2276template <typename T>
2277void HInstructionBuilder::BuildFillArrayData(HInstruction* object,
2278 const T* data,
2279 uint32_t element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002280 DataType::Type anticipated_type,
David Brazdildee58d62016-04-07 09:54:26 +00002281 uint32_t dex_pc) {
2282 for (uint32_t i = 0; i < element_count; ++i) {
2283 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
2284 HInstruction* value = graph_->GetIntConstant(data[i], dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002285 HArraySet* aset = new (allocator_) HArraySet(object, index, value, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002286 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
2287 AppendInstruction(aset);
2288 }
2289}
2290
2291void HInstructionBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
David Brazdilc120bbe2016-04-22 16:57:00 +01002292 HInstruction* array = LoadNullCheckedLocal(instruction.VRegA_31t(), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002293
2294 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
2295 const Instruction::ArrayDataPayload* payload =
Mathieu Chartier808c7a52017-12-15 11:19:33 -08002296 reinterpret_cast<const Instruction::ArrayDataPayload*>(
2297 code_item_accessor_.Insns() + payload_offset);
David Brazdildee58d62016-04-07 09:54:26 +00002298 const uint8_t* data = payload->data;
2299 uint32_t element_count = payload->element_count;
2300
Vladimir Markoc69fba22016-09-06 16:49:15 +01002301 if (element_count == 0u) {
2302 // For empty payload we emit only the null check above.
2303 return;
2304 }
2305
Vladimir Markoca6fff82017-10-03 14:49:14 +01002306 HInstruction* length = new (allocator_) HArrayLength(array, dex_pc);
Vladimir Markoc69fba22016-09-06 16:49:15 +01002307 AppendInstruction(length);
2308
David Brazdildee58d62016-04-07 09:54:26 +00002309 // Implementation of this DEX instruction seems to be that the bounds check is
2310 // done before doing any stores.
2311 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002312 AppendInstruction(new (allocator_) HBoundsCheck(last_index, length, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002313
2314 switch (payload->element_width) {
2315 case 1:
David Brazdilc120bbe2016-04-22 16:57:00 +01002316 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00002317 reinterpret_cast<const int8_t*>(data),
2318 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002319 DataType::Type::kInt8,
David Brazdildee58d62016-04-07 09:54:26 +00002320 dex_pc);
2321 break;
2322 case 2:
David Brazdilc120bbe2016-04-22 16:57:00 +01002323 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00002324 reinterpret_cast<const int16_t*>(data),
2325 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002326 DataType::Type::kInt16,
David Brazdildee58d62016-04-07 09:54:26 +00002327 dex_pc);
2328 break;
2329 case 4:
David Brazdilc120bbe2016-04-22 16:57:00 +01002330 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00002331 reinterpret_cast<const int32_t*>(data),
2332 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002333 DataType::Type::kInt32,
David Brazdildee58d62016-04-07 09:54:26 +00002334 dex_pc);
2335 break;
2336 case 8:
David Brazdilc120bbe2016-04-22 16:57:00 +01002337 BuildFillWideArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00002338 reinterpret_cast<const int64_t*>(data),
2339 element_count,
2340 dex_pc);
2341 break;
2342 default:
2343 LOG(FATAL) << "Unknown element width for " << payload->element_width;
2344 }
2345 graph_->SetHasBoundsChecks(true);
2346}
2347
2348void HInstructionBuilder::BuildFillWideArrayData(HInstruction* object,
2349 const int64_t* data,
2350 uint32_t element_count,
2351 uint32_t dex_pc) {
2352 for (uint32_t i = 0; i < element_count; ++i) {
2353 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
2354 HInstruction* value = graph_->GetLongConstant(data[i], dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002355 HArraySet* aset =
2356 new (allocator_) HArraySet(object, index, value, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002357 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
2358 AppendInstruction(aset);
2359 }
2360}
2361
Vladimir Marko28e012a2017-12-07 11:22:59 +00002362void HInstructionBuilder::BuildLoadString(dex::StringIndex string_index, uint32_t dex_pc) {
2363 HLoadString* load_string =
2364 new (allocator_) HLoadString(graph_->GetCurrentMethod(), string_index, *dex_file_, dex_pc);
2365 HSharpening::ProcessLoadString(load_string,
2366 code_generator_,
Vladimir Marko28e012a2017-12-07 11:22:59 +00002367 *dex_compilation_unit_,
Vladimir Marko02ca05a2020-05-12 13:58:51 +01002368 graph_->GetHandleCache()->GetHandles());
Vladimir Marko28e012a2017-12-07 11:22:59 +00002369 AppendInstruction(load_string);
2370}
2371
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002372HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index, uint32_t dex_pc) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +00002373 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002374 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
Vladimir Marko175e7862018-03-27 09:03:13 +00002375 Handle<mirror::Class> klass = ResolveClass(soa, type_index);
Vladimir Marko270e10a2020-09-24 11:48:47 +01002376 bool needs_access_check = LoadClassNeedsAccessCheck(type_index, klass.Get());
Vladimir Markoa2c211c2018-11-01 09:50:52 +00002377 return BuildLoadClass(type_index, dex_file, klass, dex_pc, needs_access_check);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002378}
2379
Vladimir Markoa2c211c2018-11-01 09:50:52 +00002380HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002381 const DexFile& dex_file,
2382 Handle<mirror::Class> klass,
2383 uint32_t dex_pc,
2384 bool needs_access_check) {
2385 // Try to find a reference in the compiling dex file.
2386 const DexFile* actual_dex_file = &dex_file;
2387 if (!IsSameDexFile(dex_file, *dex_compilation_unit_->GetDexFile())) {
2388 dex::TypeIndex local_type_index =
2389 klass->FindTypeIndexInOtherDexFile(*dex_compilation_unit_->GetDexFile());
2390 if (local_type_index.IsValid()) {
2391 type_index = local_type_index;
2392 actual_dex_file = dex_compilation_unit_->GetDexFile();
2393 }
2394 }
2395
Vladimir Marko270e10a2020-09-24 11:48:47 +01002396 // We cannot use the referrer's class load kind if we need to do an access check.
2397 // If the `klass` is unresolved, we need access check with the exception of the referrer's
2398 // class, see LoadClassNeedsAccessCheck(), so the `!needs_access_check` check is enough.
2399 // Otherwise, also check if the `klass` is the same as the compiling class, which also
2400 // conveniently rejects the case of unresolved compiling class.
Vladimir Markoa2c211c2018-11-01 09:50:52 +00002401 bool is_referrers_class =
Vladimir Marko270e10a2020-09-24 11:48:47 +01002402 !needs_access_check &&
2403 (klass == nullptr || outer_compilation_unit_->GetCompilingClass().Get() == klass.Get());
2404 // Note: `klass` must be from `graph_->GetHandleCache()`.
Vladimir Markoca6fff82017-10-03 14:49:14 +01002405 HLoadClass* load_class = new (allocator_) HLoadClass(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00002406 graph_->GetCurrentMethod(),
2407 type_index,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002408 *actual_dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00002409 klass,
Vladimir Markofca0b492018-07-23 15:30:52 +01002410 is_referrers_class,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00002411 dex_pc,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002412 needs_access_check);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00002413
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002414 HLoadClass::LoadKind load_kind = HSharpening::ComputeLoadClassKind(load_class,
2415 code_generator_,
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002416 *dex_compilation_unit_);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002417
2418 if (load_kind == HLoadClass::LoadKind::kInvalid) {
2419 // We actually cannot reference this class, we're forced to bail.
2420 return nullptr;
2421 }
Vladimir Marko28e012a2017-12-07 11:22:59 +00002422 // Load kind must be set before inserting the instruction into the graph.
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002423 load_class->SetLoadKind(load_kind);
Vladimir Marko28e012a2017-12-07 11:22:59 +00002424 AppendInstruction(load_class);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00002425 return load_class;
2426}
2427
Vladimir Marko175e7862018-03-27 09:03:13 +00002428Handle<mirror::Class> HInstructionBuilder::ResolveClass(ScopedObjectAccess& soa,
2429 dex::TypeIndex type_index) {
Vladimir Marko3b506202018-10-31 14:33:58 +00002430 auto it = class_cache_.find(type_index);
2431 if (it != class_cache_.end()) {
2432 return it->second;
2433 }
2434
2435 ObjPtr<mirror::Class> klass = dex_compilation_unit_->GetClassLinker()->ResolveType(
2436 type_index, dex_compilation_unit_->GetDexCache(), dex_compilation_unit_->GetClassLoader());
2437 DCHECK_EQ(klass == nullptr, soa.Self()->IsExceptionPending());
2438 soa.Self()->ClearException(); // Clean up the exception left by type resolution if any.
2439
Vladimir Marko02ca05a2020-05-12 13:58:51 +01002440 Handle<mirror::Class> h_klass = graph_->GetHandleCache()->NewHandle(klass);
Vladimir Marko3b506202018-10-31 14:33:58 +00002441 class_cache_.Put(type_index, h_klass);
2442 return h_klass;
Vladimir Marko175e7862018-03-27 09:03:13 +00002443}
2444
Vladimir Marko270e10a2020-09-24 11:48:47 +01002445bool HInstructionBuilder::LoadClassNeedsAccessCheck(dex::TypeIndex type_index,
2446 ObjPtr<mirror::Class> klass) {
Vladimir Marko175e7862018-03-27 09:03:13 +00002447 if (klass == nullptr) {
Vladimir Marko270e10a2020-09-24 11:48:47 +01002448 // If the class is unresolved, we can avoid access checks only for references to
2449 // the compiling class as determined by checking the descriptor and ClassLoader.
2450 if (outer_compilation_unit_->GetCompilingClass() != nullptr) {
2451 // Compiling class is resolved, so different from the unresolved class.
2452 return true;
2453 }
2454 if (dex_compilation_unit_->GetClassLoader().Get() !=
2455 outer_compilation_unit_->GetClassLoader().Get()) {
2456 // Resolving the same descriptor in a different ClassLoader than the
2457 // defining loader of the compiling class shall either fail to find
2458 // the class definition, or find a different one.
2459 // (Assuming no custom ClassLoader hierarchy with circular delegation.)
2460 return true;
2461 }
2462 // Check if the class is the outer method's class.
2463 // For the same dex file compare type indexes, otherwise descriptors.
2464 const DexFile* outer_dex_file = outer_compilation_unit_->GetDexFile();
2465 const DexFile* inner_dex_file = dex_compilation_unit_->GetDexFile();
2466 const dex::ClassDef& outer_class_def =
2467 outer_dex_file->GetClassDef(outer_compilation_unit_->GetClassDefIndex());
2468 if (IsSameDexFile(*inner_dex_file, *outer_dex_file)) {
2469 if (type_index != outer_class_def.class_idx_) {
2470 return true;
2471 }
2472 } else {
2473 uint32_t outer_utf16_length;
2474 const char* outer_descriptor =
2475 outer_dex_file->StringByTypeIdx(outer_class_def.class_idx_, &outer_utf16_length);
2476 uint32_t target_utf16_length;
2477 const char* target_descriptor =
2478 inner_dex_file->StringByTypeIdx(type_index, &target_utf16_length);
2479 if (outer_utf16_length != target_utf16_length ||
2480 strcmp(outer_descriptor, target_descriptor) != 0) {
2481 return true;
2482 }
2483 }
2484 // For inlined methods we also need to check if the compiling class
2485 // is public or in the same package as the inlined method's class.
2486 if (dex_compilation_unit_ != outer_compilation_unit_ &&
2487 (outer_class_def.access_flags_ & kAccPublic) == 0) {
2488 DCHECK(dex_compilation_unit_->GetCompilingClass() != nullptr);
2489 SamePackageCompare same_package(*outer_compilation_unit_);
2490 if (!same_package(dex_compilation_unit_->GetCompilingClass().Get())) {
2491 return true;
2492 }
2493 }
2494 return false;
Vladimir Marko175e7862018-03-27 09:03:13 +00002495 } else if (klass->IsPublic()) {
2496 return false;
Vladimir Marko0280e5d2020-08-27 17:36:24 +01002497 } else if (dex_compilation_unit_->GetCompilingClass() != nullptr) {
2498 return !dex_compilation_unit_->GetCompilingClass()->CanAccess(klass);
Vladimir Marko175e7862018-03-27 09:03:13 +00002499 } else {
Vladimir Marko0280e5d2020-08-27 17:36:24 +01002500 SamePackageCompare same_package(*dex_compilation_unit_);
2501 return !same_package(klass);
Vladimir Marko175e7862018-03-27 09:03:13 +00002502 }
2503}
2504
Orion Hodson06d10a72018-05-14 08:53:38 +01002505void HInstructionBuilder::BuildLoadMethodHandle(uint16_t method_handle_index, uint32_t dex_pc) {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01002506 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
Orion Hodson06d10a72018-05-14 08:53:38 +01002507 HLoadMethodHandle* load_method_handle = new (allocator_) HLoadMethodHandle(
2508 graph_->GetCurrentMethod(), method_handle_index, dex_file, dex_pc);
Orion Hodsondbaa5c72018-05-10 08:22:46 +01002509 AppendInstruction(load_method_handle);
2510}
2511
Orion Hodson06d10a72018-05-14 08:53:38 +01002512void HInstructionBuilder::BuildLoadMethodType(dex::ProtoIndex proto_index, uint32_t dex_pc) {
Orion Hodson18259d72018-04-12 11:18:23 +01002513 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
2514 HLoadMethodType* load_method_type =
Orion Hodson06d10a72018-05-14 08:53:38 +01002515 new (allocator_) HLoadMethodType(graph_->GetCurrentMethod(), proto_index, dex_file, dex_pc);
Orion Hodson18259d72018-04-12 11:18:23 +01002516 AppendInstruction(load_method_type);
2517}
2518
Andra Danciu49a19f32020-08-27 12:44:25 +00002519void HInstructionBuilder::BuildTypeCheck(bool is_instance_of,
2520 HInstruction* object,
Andreas Gampea5b09a62016-11-17 15:21:22 -08002521 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +00002522 uint32_t dex_pc) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +00002523 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko175e7862018-03-27 09:03:13 +00002524 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
2525 Handle<mirror::Class> klass = ResolveClass(soa, type_index);
Vladimir Marko270e10a2020-09-24 11:48:47 +01002526 bool needs_access_check = LoadClassNeedsAccessCheck(type_index, klass.Get());
Vladimir Marko175e7862018-03-27 09:03:13 +00002527 TypeCheckKind check_kind = HSharpening::ComputeTypeCheckKind(
Vladimir Markodc4bcce2018-06-21 16:15:42 +01002528 klass.Get(), code_generator_, needs_access_check);
Vladimir Marko175e7862018-03-27 09:03:13 +00002529
2530 HInstruction* class_or_null = nullptr;
2531 HIntConstant* bitstring_path_to_root = nullptr;
2532 HIntConstant* bitstring_mask = nullptr;
2533 if (check_kind == TypeCheckKind::kBitstringCheck) {
2534 // TODO: Allow using the bitstring check also if we need an access check.
2535 DCHECK(!needs_access_check);
2536 class_or_null = graph_->GetNullConstant(dex_pc);
2537 MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_);
2538 uint32_t path_to_root =
2539 SubtypeCheck<ObjPtr<mirror::Class>>::GetEncodedPathToRootForTarget(klass.Get());
2540 uint32_t mask = SubtypeCheck<ObjPtr<mirror::Class>>::GetEncodedPathToRootMask(klass.Get());
2541 bitstring_path_to_root = graph_->GetIntConstant(static_cast<int32_t>(path_to_root), dex_pc);
2542 bitstring_mask = graph_->GetIntConstant(static_cast<int32_t>(mask), dex_pc);
2543 } else {
Vladimir Markoa2c211c2018-11-01 09:50:52 +00002544 class_or_null = BuildLoadClass(type_index, dex_file, klass, dex_pc, needs_access_check);
Vladimir Marko175e7862018-03-27 09:03:13 +00002545 }
2546 DCHECK(class_or_null != nullptr);
2547
Andra Danciu49a19f32020-08-27 12:44:25 +00002548 if (is_instance_of) {
Vladimir Marko175e7862018-03-27 09:03:13 +00002549 AppendInstruction(new (allocator_) HInstanceOf(object,
2550 class_or_null,
2551 check_kind,
2552 klass,
2553 dex_pc,
2554 allocator_,
2555 bitstring_path_to_root,
2556 bitstring_mask));
David Brazdildee58d62016-04-07 09:54:26 +00002557 } else {
David Brazdildee58d62016-04-07 09:54:26 +00002558 // We emit a CheckCast followed by a BoundType. CheckCast is a statement
2559 // which may throw. If it succeeds BoundType sets the new type of `object`
2560 // for all subsequent uses.
Vladimir Marko175e7862018-03-27 09:03:13 +00002561 AppendInstruction(
2562 new (allocator_) HCheckCast(object,
2563 class_or_null,
2564 check_kind,
2565 klass,
2566 dex_pc,
2567 allocator_,
2568 bitstring_path_to_root,
2569 bitstring_mask));
Vladimir Markoca6fff82017-10-03 14:49:14 +01002570 AppendInstruction(new (allocator_) HBoundType(object, dex_pc));
Andra Danciu49a19f32020-08-27 12:44:25 +00002571 }
2572}
2573
2574void HInstructionBuilder::BuildTypeCheck(const Instruction& instruction,
2575 uint8_t destination,
2576 uint8_t reference,
2577 dex::TypeIndex type_index,
2578 uint32_t dex_pc) {
2579 HInstruction* object = LoadLocal(reference, DataType::Type::kReference);
2580 bool is_instance_of = instruction.Opcode() == Instruction::INSTANCE_OF;
2581
2582 BuildTypeCheck(is_instance_of, object, type_index, dex_pc);
2583
2584 if (is_instance_of) {
2585 UpdateLocal(destination, current_block_->GetLastInstruction());
2586 } else {
2587 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
David Brazdildee58d62016-04-07 09:54:26 +00002588 UpdateLocal(reference, current_block_->GetLastInstruction());
2589 }
2590}
2591
Nicolas Geoffray4924ea92021-03-23 08:25:31 +00002592bool HInstructionBuilder::ProcessDexInstruction(const Instruction& instruction, uint32_t dex_pc) {
David Brazdildee58d62016-04-07 09:54:26 +00002593 switch (instruction.Opcode()) {
2594 case Instruction::CONST_4: {
2595 int32_t register_index = instruction.VRegA();
2596 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n(), dex_pc);
2597 UpdateLocal(register_index, constant);
2598 break;
2599 }
2600
2601 case Instruction::CONST_16: {
2602 int32_t register_index = instruction.VRegA();
2603 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s(), dex_pc);
2604 UpdateLocal(register_index, constant);
2605 break;
2606 }
2607
2608 case Instruction::CONST: {
2609 int32_t register_index = instruction.VRegA();
2610 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i(), dex_pc);
2611 UpdateLocal(register_index, constant);
2612 break;
2613 }
2614
2615 case Instruction::CONST_HIGH16: {
2616 int32_t register_index = instruction.VRegA();
2617 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16, dex_pc);
2618 UpdateLocal(register_index, constant);
2619 break;
2620 }
2621
2622 case Instruction::CONST_WIDE_16: {
2623 int32_t register_index = instruction.VRegA();
2624 // Get 16 bits of constant value, sign extended to 64 bits.
2625 int64_t value = instruction.VRegB_21s();
2626 value <<= 48;
2627 value >>= 48;
2628 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
2629 UpdateLocal(register_index, constant);
2630 break;
2631 }
2632
2633 case Instruction::CONST_WIDE_32: {
2634 int32_t register_index = instruction.VRegA();
2635 // Get 32 bits of constant value, sign extended to 64 bits.
2636 int64_t value = instruction.VRegB_31i();
2637 value <<= 32;
2638 value >>= 32;
2639 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
2640 UpdateLocal(register_index, constant);
2641 break;
2642 }
2643
2644 case Instruction::CONST_WIDE: {
2645 int32_t register_index = instruction.VRegA();
2646 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l(), dex_pc);
2647 UpdateLocal(register_index, constant);
2648 break;
2649 }
2650
2651 case Instruction::CONST_WIDE_HIGH16: {
2652 int32_t register_index = instruction.VRegA();
2653 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
2654 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
2655 UpdateLocal(register_index, constant);
2656 break;
2657 }
2658
2659 // Note that the SSA building will refine the types.
2660 case Instruction::MOVE:
2661 case Instruction::MOVE_FROM16:
2662 case Instruction::MOVE_16: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002663 HInstruction* value = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +00002664 UpdateLocal(instruction.VRegA(), value);
2665 break;
2666 }
2667
2668 // Note that the SSA building will refine the types.
2669 case Instruction::MOVE_WIDE:
2670 case Instruction::MOVE_WIDE_FROM16:
2671 case Instruction::MOVE_WIDE_16: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002672 HInstruction* value = LoadLocal(instruction.VRegB(), DataType::Type::kInt64);
David Brazdildee58d62016-04-07 09:54:26 +00002673 UpdateLocal(instruction.VRegA(), value);
2674 break;
2675 }
2676
2677 case Instruction::MOVE_OBJECT:
2678 case Instruction::MOVE_OBJECT_16:
2679 case Instruction::MOVE_OBJECT_FROM16: {
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01002680 // The verifier has no notion of a null type, so a move-object of constant 0
2681 // will lead to the same constant 0 in the destination register. To mimic
2682 // this behavior, we just pretend we haven't seen a type change (int to reference)
2683 // for the 0 constant and phis. We rely on our type propagation to eventually get the
2684 // types correct.
2685 uint32_t reg_number = instruction.VRegB();
2686 HInstruction* value = (*current_locals_)[reg_number];
2687 if (value->IsIntConstant()) {
2688 DCHECK_EQ(value->AsIntConstant()->GetValue(), 0);
2689 } else if (value->IsPhi()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002690 DCHECK(value->GetType() == DataType::Type::kInt32 ||
2691 value->GetType() == DataType::Type::kReference);
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01002692 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002693 value = LoadLocal(reg_number, DataType::Type::kReference);
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01002694 }
David Brazdildee58d62016-04-07 09:54:26 +00002695 UpdateLocal(instruction.VRegA(), value);
2696 break;
2697 }
2698
David Brazdildee58d62016-04-07 09:54:26 +00002699 case Instruction::RETURN_VOID: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002700 BuildReturn(instruction, DataType::Type::kVoid, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002701 break;
2702 }
2703
2704#define IF_XX(comparison, cond) \
2705 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
2706 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
2707
2708 IF_XX(HEqual, EQ);
2709 IF_XX(HNotEqual, NE);
2710 IF_XX(HLessThan, LT);
2711 IF_XX(HLessThanOrEqual, LE);
2712 IF_XX(HGreaterThan, GT);
2713 IF_XX(HGreaterThanOrEqual, GE);
2714
2715 case Instruction::GOTO:
2716 case Instruction::GOTO_16:
2717 case Instruction::GOTO_32: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002718 AppendInstruction(new (allocator_) HGoto(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002719 current_block_ = nullptr;
2720 break;
2721 }
2722
2723 case Instruction::RETURN: {
2724 BuildReturn(instruction, return_type_, dex_pc);
2725 break;
2726 }
2727
2728 case Instruction::RETURN_OBJECT: {
2729 BuildReturn(instruction, return_type_, dex_pc);
2730 break;
2731 }
2732
2733 case Instruction::RETURN_WIDE: {
2734 BuildReturn(instruction, return_type_, dex_pc);
2735 break;
2736 }
2737
2738 case Instruction::INVOKE_DIRECT:
2739 case Instruction::INVOKE_INTERFACE:
2740 case Instruction::INVOKE_STATIC:
2741 case Instruction::INVOKE_SUPER:
Nicolas Geoffray4924ea92021-03-23 08:25:31 +00002742 case Instruction::INVOKE_VIRTUAL: {
2743 uint16_t method_idx = instruction.VRegB_35c();
David Brazdildee58d62016-04-07 09:54:26 +00002744 uint32_t args[5];
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002745 uint32_t number_of_vreg_arguments = instruction.GetVarArgs(args);
2746 VarArgsInstructionOperands operands(args, number_of_vreg_arguments);
2747 if (!BuildInvoke(instruction, dex_pc, method_idx, operands)) {
David Brazdildee58d62016-04-07 09:54:26 +00002748 return false;
2749 }
2750 break;
2751 }
2752
2753 case Instruction::INVOKE_DIRECT_RANGE:
2754 case Instruction::INVOKE_INTERFACE_RANGE:
2755 case Instruction::INVOKE_STATIC_RANGE:
2756 case Instruction::INVOKE_SUPER_RANGE:
Nicolas Geoffray4924ea92021-03-23 08:25:31 +00002757 case Instruction::INVOKE_VIRTUAL_RANGE: {
2758 uint16_t method_idx = instruction.VRegB_3rc();
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002759 RangeInstructionOperands operands(instruction.VRegC(), instruction.VRegA_3rc());
2760 if (!BuildInvoke(instruction, dex_pc, method_idx, operands)) {
David Brazdildee58d62016-04-07 09:54:26 +00002761 return false;
2762 }
2763 break;
2764 }
2765
Orion Hodsonac141392017-01-13 11:53:47 +00002766 case Instruction::INVOKE_POLYMORPHIC: {
2767 uint16_t method_idx = instruction.VRegB_45cc();
Orion Hodson06d10a72018-05-14 08:53:38 +01002768 dex::ProtoIndex proto_idx(instruction.VRegH_45cc());
Orion Hodsonac141392017-01-13 11:53:47 +00002769 uint32_t args[5];
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002770 uint32_t number_of_vreg_arguments = instruction.GetVarArgs(args);
2771 VarArgsInstructionOperands operands(args, number_of_vreg_arguments);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002772 return BuildInvokePolymorphic(dex_pc, method_idx, proto_idx, operands);
Orion Hodsonac141392017-01-13 11:53:47 +00002773 }
2774
2775 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
2776 uint16_t method_idx = instruction.VRegB_4rcc();
Orion Hodson06d10a72018-05-14 08:53:38 +01002777 dex::ProtoIndex proto_idx(instruction.VRegH_4rcc());
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002778 RangeInstructionOperands operands(instruction.VRegC_4rcc(), instruction.VRegA_4rcc());
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002779 return BuildInvokePolymorphic(dex_pc, method_idx, proto_idx, operands);
2780 }
2781
2782 case Instruction::INVOKE_CUSTOM: {
2783 uint16_t call_site_idx = instruction.VRegB_35c();
2784 uint32_t args[5];
2785 uint32_t number_of_vreg_arguments = instruction.GetVarArgs(args);
2786 VarArgsInstructionOperands operands(args, number_of_vreg_arguments);
2787 return BuildInvokeCustom(dex_pc, call_site_idx, operands);
2788 }
2789
2790 case Instruction::INVOKE_CUSTOM_RANGE: {
2791 uint16_t call_site_idx = instruction.VRegB_3rc();
2792 RangeInstructionOperands operands(instruction.VRegC_3rc(), instruction.VRegA_3rc());
2793 return BuildInvokeCustom(dex_pc, call_site_idx, operands);
Orion Hodsonac141392017-01-13 11:53:47 +00002794 }
2795
David Brazdildee58d62016-04-07 09:54:26 +00002796 case Instruction::NEG_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002797 Unop_12x<HNeg>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002798 break;
2799 }
2800
2801 case Instruction::NEG_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002802 Unop_12x<HNeg>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002803 break;
2804 }
2805
2806 case Instruction::NEG_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002807 Unop_12x<HNeg>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002808 break;
2809 }
2810
2811 case Instruction::NEG_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002812 Unop_12x<HNeg>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002813 break;
2814 }
2815
2816 case Instruction::NOT_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002817 Unop_12x<HNot>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002818 break;
2819 }
2820
2821 case Instruction::NOT_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002822 Unop_12x<HNot>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002823 break;
2824 }
2825
2826 case Instruction::INT_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002827 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002828 break;
2829 }
2830
2831 case Instruction::INT_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002832 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002833 break;
2834 }
2835
2836 case Instruction::INT_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002837 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002838 break;
2839 }
2840
2841 case Instruction::LONG_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002842 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002843 break;
2844 }
2845
2846 case Instruction::LONG_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002847 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002848 break;
2849 }
2850
2851 case Instruction::LONG_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002852 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002853 break;
2854 }
2855
2856 case Instruction::FLOAT_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002857 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002858 break;
2859 }
2860
2861 case Instruction::FLOAT_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002862 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002863 break;
2864 }
2865
2866 case Instruction::FLOAT_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002867 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002868 break;
2869 }
2870
2871 case Instruction::DOUBLE_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002872 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002873 break;
2874 }
2875
2876 case Instruction::DOUBLE_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002877 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002878 break;
2879 }
2880
2881 case Instruction::DOUBLE_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002882 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002883 break;
2884 }
2885
2886 case Instruction::INT_TO_BYTE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002887 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt8, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002888 break;
2889 }
2890
2891 case Instruction::INT_TO_SHORT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002892 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt16, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002893 break;
2894 }
2895
2896 case Instruction::INT_TO_CHAR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002897 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kUint16, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002898 break;
2899 }
2900
2901 case Instruction::ADD_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002902 Binop_23x<HAdd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002903 break;
2904 }
2905
2906 case Instruction::ADD_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002907 Binop_23x<HAdd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002908 break;
2909 }
2910
2911 case Instruction::ADD_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002912 Binop_23x<HAdd>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002913 break;
2914 }
2915
2916 case Instruction::ADD_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002917 Binop_23x<HAdd>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002918 break;
2919 }
2920
2921 case Instruction::SUB_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002922 Binop_23x<HSub>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002923 break;
2924 }
2925
2926 case Instruction::SUB_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002927 Binop_23x<HSub>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002928 break;
2929 }
2930
2931 case Instruction::SUB_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002932 Binop_23x<HSub>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002933 break;
2934 }
2935
2936 case Instruction::SUB_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002937 Binop_23x<HSub>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002938 break;
2939 }
2940
2941 case Instruction::ADD_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002942 Binop_12x<HAdd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002943 break;
2944 }
2945
2946 case Instruction::MUL_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002947 Binop_23x<HMul>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002948 break;
2949 }
2950
2951 case Instruction::MUL_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002952 Binop_23x<HMul>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002953 break;
2954 }
2955
2956 case Instruction::MUL_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002957 Binop_23x<HMul>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002958 break;
2959 }
2960
2961 case Instruction::MUL_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002962 Binop_23x<HMul>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002963 break;
2964 }
2965
2966 case Instruction::DIV_INT: {
2967 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002968 dex_pc, DataType::Type::kInt32, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002969 break;
2970 }
2971
2972 case Instruction::DIV_LONG: {
2973 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002974 dex_pc, DataType::Type::kInt64, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002975 break;
2976 }
2977
2978 case Instruction::DIV_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002979 Binop_23x<HDiv>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002980 break;
2981 }
2982
2983 case Instruction::DIV_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002984 Binop_23x<HDiv>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002985 break;
2986 }
2987
2988 case Instruction::REM_INT: {
2989 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002990 dex_pc, DataType::Type::kInt32, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002991 break;
2992 }
2993
2994 case Instruction::REM_LONG: {
2995 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002996 dex_pc, DataType::Type::kInt64, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002997 break;
2998 }
2999
3000 case Instruction::REM_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003001 Binop_23x<HRem>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003002 break;
3003 }
3004
3005 case Instruction::REM_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003006 Binop_23x<HRem>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003007 break;
3008 }
3009
3010 case Instruction::AND_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003011 Binop_23x<HAnd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003012 break;
3013 }
3014
3015 case Instruction::AND_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003016 Binop_23x<HAnd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003017 break;
3018 }
3019
3020 case Instruction::SHL_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003021 Binop_23x_shift<HShl>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003022 break;
3023 }
3024
3025 case Instruction::SHL_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003026 Binop_23x_shift<HShl>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003027 break;
3028 }
3029
3030 case Instruction::SHR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003031 Binop_23x_shift<HShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003032 break;
3033 }
3034
3035 case Instruction::SHR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003036 Binop_23x_shift<HShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003037 break;
3038 }
3039
3040 case Instruction::USHR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003041 Binop_23x_shift<HUShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003042 break;
3043 }
3044
3045 case Instruction::USHR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003046 Binop_23x_shift<HUShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003047 break;
3048 }
3049
3050 case Instruction::OR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003051 Binop_23x<HOr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003052 break;
3053 }
3054
3055 case Instruction::OR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003056 Binop_23x<HOr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003057 break;
3058 }
3059
3060 case Instruction::XOR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003061 Binop_23x<HXor>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003062 break;
3063 }
3064
3065 case Instruction::XOR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003066 Binop_23x<HXor>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003067 break;
3068 }
3069
3070 case Instruction::ADD_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003071 Binop_12x<HAdd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003072 break;
3073 }
3074
3075 case Instruction::ADD_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003076 Binop_12x<HAdd>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003077 break;
3078 }
3079
3080 case Instruction::ADD_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003081 Binop_12x<HAdd>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003082 break;
3083 }
3084
3085 case Instruction::SUB_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003086 Binop_12x<HSub>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003087 break;
3088 }
3089
3090 case Instruction::SUB_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003091 Binop_12x<HSub>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003092 break;
3093 }
3094
3095 case Instruction::SUB_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003096 Binop_12x<HSub>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003097 break;
3098 }
3099
3100 case Instruction::SUB_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003101 Binop_12x<HSub>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003102 break;
3103 }
3104
3105 case Instruction::MUL_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003106 Binop_12x<HMul>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003107 break;
3108 }
3109
3110 case Instruction::MUL_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003111 Binop_12x<HMul>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003112 break;
3113 }
3114
3115 case Instruction::MUL_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003116 Binop_12x<HMul>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003117 break;
3118 }
3119
3120 case Instruction::MUL_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003121 Binop_12x<HMul>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003122 break;
3123 }
3124
3125 case Instruction::DIV_INT_2ADDR: {
3126 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003127 dex_pc, DataType::Type::kInt32, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00003128 break;
3129 }
3130
3131 case Instruction::DIV_LONG_2ADDR: {
3132 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003133 dex_pc, DataType::Type::kInt64, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00003134 break;
3135 }
3136
3137 case Instruction::REM_INT_2ADDR: {
3138 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003139 dex_pc, DataType::Type::kInt32, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00003140 break;
3141 }
3142
3143 case Instruction::REM_LONG_2ADDR: {
3144 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003145 dex_pc, DataType::Type::kInt64, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00003146 break;
3147 }
3148
3149 case Instruction::REM_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003150 Binop_12x<HRem>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003151 break;
3152 }
3153
3154 case Instruction::REM_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003155 Binop_12x<HRem>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003156 break;
3157 }
3158
3159 case Instruction::SHL_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003160 Binop_12x_shift<HShl>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003161 break;
3162 }
3163
3164 case Instruction::SHL_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003165 Binop_12x_shift<HShl>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003166 break;
3167 }
3168
3169 case Instruction::SHR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003170 Binop_12x_shift<HShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003171 break;
3172 }
3173
3174 case Instruction::SHR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003175 Binop_12x_shift<HShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003176 break;
3177 }
3178
3179 case Instruction::USHR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003180 Binop_12x_shift<HUShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003181 break;
3182 }
3183
3184 case Instruction::USHR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003185 Binop_12x_shift<HUShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003186 break;
3187 }
3188
3189 case Instruction::DIV_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003190 Binop_12x<HDiv>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003191 break;
3192 }
3193
3194 case Instruction::DIV_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003195 Binop_12x<HDiv>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003196 break;
3197 }
3198
3199 case Instruction::AND_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003200 Binop_12x<HAnd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003201 break;
3202 }
3203
3204 case Instruction::AND_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003205 Binop_12x<HAnd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003206 break;
3207 }
3208
3209 case Instruction::OR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003210 Binop_12x<HOr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003211 break;
3212 }
3213
3214 case Instruction::OR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003215 Binop_12x<HOr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003216 break;
3217 }
3218
3219 case Instruction::XOR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003220 Binop_12x<HXor>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003221 break;
3222 }
3223
3224 case Instruction::XOR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003225 Binop_12x<HXor>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003226 break;
3227 }
3228
3229 case Instruction::ADD_INT_LIT16: {
3230 Binop_22s<HAdd>(instruction, false, dex_pc);
3231 break;
3232 }
3233
3234 case Instruction::AND_INT_LIT16: {
3235 Binop_22s<HAnd>(instruction, false, dex_pc);
3236 break;
3237 }
3238
3239 case Instruction::OR_INT_LIT16: {
3240 Binop_22s<HOr>(instruction, false, dex_pc);
3241 break;
3242 }
3243
3244 case Instruction::XOR_INT_LIT16: {
3245 Binop_22s<HXor>(instruction, false, dex_pc);
3246 break;
3247 }
3248
3249 case Instruction::RSUB_INT: {
3250 Binop_22s<HSub>(instruction, true, dex_pc);
3251 break;
3252 }
3253
3254 case Instruction::MUL_INT_LIT16: {
3255 Binop_22s<HMul>(instruction, false, dex_pc);
3256 break;
3257 }
3258
3259 case Instruction::ADD_INT_LIT8: {
3260 Binop_22b<HAdd>(instruction, false, dex_pc);
3261 break;
3262 }
3263
3264 case Instruction::AND_INT_LIT8: {
3265 Binop_22b<HAnd>(instruction, false, dex_pc);
3266 break;
3267 }
3268
3269 case Instruction::OR_INT_LIT8: {
3270 Binop_22b<HOr>(instruction, false, dex_pc);
3271 break;
3272 }
3273
3274 case Instruction::XOR_INT_LIT8: {
3275 Binop_22b<HXor>(instruction, false, dex_pc);
3276 break;
3277 }
3278
3279 case Instruction::RSUB_INT_LIT8: {
3280 Binop_22b<HSub>(instruction, true, dex_pc);
3281 break;
3282 }
3283
3284 case Instruction::MUL_INT_LIT8: {
3285 Binop_22b<HMul>(instruction, false, dex_pc);
3286 break;
3287 }
3288
3289 case Instruction::DIV_INT_LIT16:
3290 case Instruction::DIV_INT_LIT8: {
3291 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003292 dex_pc, DataType::Type::kInt32, true, true);
David Brazdildee58d62016-04-07 09:54:26 +00003293 break;
3294 }
3295
3296 case Instruction::REM_INT_LIT16:
3297 case Instruction::REM_INT_LIT8: {
3298 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003299 dex_pc, DataType::Type::kInt32, true, false);
David Brazdildee58d62016-04-07 09:54:26 +00003300 break;
3301 }
3302
3303 case Instruction::SHL_INT_LIT8: {
3304 Binop_22b<HShl>(instruction, false, dex_pc);
3305 break;
3306 }
3307
3308 case Instruction::SHR_INT_LIT8: {
3309 Binop_22b<HShr>(instruction, false, dex_pc);
3310 break;
3311 }
3312
3313 case Instruction::USHR_INT_LIT8: {
3314 Binop_22b<HUShr>(instruction, false, dex_pc);
3315 break;
3316 }
3317
3318 case Instruction::NEW_INSTANCE: {
Igor Murashkin79d8fa72017-04-18 09:37:23 -07003319 HNewInstance* new_instance =
3320 BuildNewInstance(dex::TypeIndex(instruction.VRegB_21c()), dex_pc);
3321 DCHECK(new_instance != nullptr);
3322
David Brazdildee58d62016-04-07 09:54:26 +00003323 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
Igor Murashkin79d8fa72017-04-18 09:37:23 -07003324 BuildConstructorFenceForAllocation(new_instance);
David Brazdildee58d62016-04-07 09:54:26 +00003325 break;
3326 }
3327
3328 case Instruction::NEW_ARRAY: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003329 dex::TypeIndex type_index(instruction.VRegC_22c());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003330 HInstruction* length = LoadLocal(instruction.VRegB_22c(), DataType::Type::kInt32);
Vladimir Markob5461632018-10-15 14:24:21 +01003331 HNewArray* new_array = BuildNewArray(dex_pc, type_index, length);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07003332
David Brazdildee58d62016-04-07 09:54:26 +00003333 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
Igor Murashkin79d8fa72017-04-18 09:37:23 -07003334 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00003335 break;
3336 }
3337
3338 case Instruction::FILLED_NEW_ARRAY: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003339 dex::TypeIndex type_index(instruction.VRegB_35c());
David Brazdildee58d62016-04-07 09:54:26 +00003340 uint32_t args[5];
Treehugger Robot2c5827a2018-05-17 22:26:08 +00003341 uint32_t number_of_vreg_arguments = instruction.GetVarArgs(args);
3342 VarArgsInstructionOperands operands(args, number_of_vreg_arguments);
3343 HNewArray* new_array = BuildFilledNewArray(dex_pc, type_index, operands);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07003344 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00003345 break;
3346 }
3347
3348 case Instruction::FILLED_NEW_ARRAY_RANGE: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003349 dex::TypeIndex type_index(instruction.VRegB_3rc());
Treehugger Robot2c5827a2018-05-17 22:26:08 +00003350 RangeInstructionOperands operands(instruction.VRegC_3rc(), instruction.VRegA_3rc());
3351 HNewArray* new_array = BuildFilledNewArray(dex_pc, type_index, operands);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07003352 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00003353 break;
3354 }
3355
3356 case Instruction::FILL_ARRAY_DATA: {
3357 BuildFillArrayData(instruction, dex_pc);
3358 break;
3359 }
3360
3361 case Instruction::MOVE_RESULT:
3362 case Instruction::MOVE_RESULT_WIDE:
3363 case Instruction::MOVE_RESULT_OBJECT: {
3364 DCHECK(latest_result_ != nullptr);
3365 UpdateLocal(instruction.VRegA(), latest_result_);
3366 latest_result_ = nullptr;
3367 break;
3368 }
3369
3370 case Instruction::CMP_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003371 Binop_23x_cmp(instruction, DataType::Type::kInt64, ComparisonBias::kNoBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003372 break;
3373 }
3374
3375 case Instruction::CMPG_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003376 Binop_23x_cmp(instruction, DataType::Type::kFloat32, ComparisonBias::kGtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003377 break;
3378 }
3379
3380 case Instruction::CMPG_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003381 Binop_23x_cmp(instruction, DataType::Type::kFloat64, ComparisonBias::kGtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003382 break;
3383 }
3384
3385 case Instruction::CMPL_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003386 Binop_23x_cmp(instruction, DataType::Type::kFloat32, ComparisonBias::kLtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003387 break;
3388 }
3389
3390 case Instruction::CMPL_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003391 Binop_23x_cmp(instruction, DataType::Type::kFloat64, ComparisonBias::kLtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003392 break;
3393 }
3394
3395 case Instruction::NOP:
3396 break;
3397
3398 case Instruction::IGET:
David Brazdildee58d62016-04-07 09:54:26 +00003399 case Instruction::IGET_WIDE:
David Brazdildee58d62016-04-07 09:54:26 +00003400 case Instruction::IGET_OBJECT:
David Brazdildee58d62016-04-07 09:54:26 +00003401 case Instruction::IGET_BOOLEAN:
David Brazdildee58d62016-04-07 09:54:26 +00003402 case Instruction::IGET_BYTE:
David Brazdildee58d62016-04-07 09:54:26 +00003403 case Instruction::IGET_CHAR:
Nicolas Geoffray4924ea92021-03-23 08:25:31 +00003404 case Instruction::IGET_SHORT: {
3405 if (!BuildInstanceFieldAccess(instruction, dex_pc, /* is_put= */ false)) {
David Brazdildee58d62016-04-07 09:54:26 +00003406 return false;
3407 }
3408 break;
3409 }
3410
3411 case Instruction::IPUT:
David Brazdildee58d62016-04-07 09:54:26 +00003412 case Instruction::IPUT_WIDE:
David Brazdildee58d62016-04-07 09:54:26 +00003413 case Instruction::IPUT_OBJECT:
David Brazdildee58d62016-04-07 09:54:26 +00003414 case Instruction::IPUT_BOOLEAN:
David Brazdildee58d62016-04-07 09:54:26 +00003415 case Instruction::IPUT_BYTE:
David Brazdildee58d62016-04-07 09:54:26 +00003416 case Instruction::IPUT_CHAR:
Nicolas Geoffray4924ea92021-03-23 08:25:31 +00003417 case Instruction::IPUT_SHORT: {
3418 if (!BuildInstanceFieldAccess(instruction, dex_pc, /* is_put= */ true)) {
David Brazdildee58d62016-04-07 09:54:26 +00003419 return false;
3420 }
3421 break;
3422 }
3423
3424 case Instruction::SGET:
3425 case Instruction::SGET_WIDE:
3426 case Instruction::SGET_OBJECT:
3427 case Instruction::SGET_BOOLEAN:
3428 case Instruction::SGET_BYTE:
3429 case Instruction::SGET_CHAR:
3430 case Instruction::SGET_SHORT: {
Andreas Gampe3db70682018-12-26 15:12:03 -08003431 BuildStaticFieldAccess(instruction, dex_pc, /* is_put= */ false);
David Brazdildee58d62016-04-07 09:54:26 +00003432 break;
3433 }
3434
3435 case Instruction::SPUT:
3436 case Instruction::SPUT_WIDE:
3437 case Instruction::SPUT_OBJECT:
3438 case Instruction::SPUT_BOOLEAN:
3439 case Instruction::SPUT_BYTE:
3440 case Instruction::SPUT_CHAR:
3441 case Instruction::SPUT_SHORT: {
Andreas Gampe3db70682018-12-26 15:12:03 -08003442 BuildStaticFieldAccess(instruction, dex_pc, /* is_put= */ true);
David Brazdildee58d62016-04-07 09:54:26 +00003443 break;
3444 }
3445
3446#define ARRAY_XX(kind, anticipated_type) \
3447 case Instruction::AGET##kind: { \
3448 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
3449 break; \
3450 } \
3451 case Instruction::APUT##kind: { \
3452 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
3453 break; \
3454 }
3455
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003456 ARRAY_XX(, DataType::Type::kInt32);
3457 ARRAY_XX(_WIDE, DataType::Type::kInt64);
3458 ARRAY_XX(_OBJECT, DataType::Type::kReference);
3459 ARRAY_XX(_BOOLEAN, DataType::Type::kBool);
3460 ARRAY_XX(_BYTE, DataType::Type::kInt8);
3461 ARRAY_XX(_CHAR, DataType::Type::kUint16);
3462 ARRAY_XX(_SHORT, DataType::Type::kInt16);
David Brazdildee58d62016-04-07 09:54:26 +00003463
3464 case Instruction::ARRAY_LENGTH: {
David Brazdilc120bbe2016-04-22 16:57:00 +01003465 HInstruction* object = LoadNullCheckedLocal(instruction.VRegB_12x(), dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01003466 AppendInstruction(new (allocator_) HArrayLength(object, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00003467 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
3468 break;
3469 }
3470
3471 case Instruction::CONST_STRING: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08003472 dex::StringIndex string_index(instruction.VRegB_21c());
Vladimir Marko28e012a2017-12-07 11:22:59 +00003473 BuildLoadString(string_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003474 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
3475 break;
3476 }
3477
3478 case Instruction::CONST_STRING_JUMBO: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08003479 dex::StringIndex string_index(instruction.VRegB_31c());
Vladimir Marko28e012a2017-12-07 11:22:59 +00003480 BuildLoadString(string_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003481 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
3482 break;
3483 }
3484
3485 case Instruction::CONST_CLASS: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003486 dex::TypeIndex type_index(instruction.VRegB_21c());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00003487 BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003488 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
3489 break;
3490 }
3491
Orion Hodsondbaa5c72018-05-10 08:22:46 +01003492 case Instruction::CONST_METHOD_HANDLE: {
3493 uint16_t method_handle_idx = instruction.VRegB_21c();
3494 BuildLoadMethodHandle(method_handle_idx, dex_pc);
3495 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
3496 break;
3497 }
3498
Orion Hodson18259d72018-04-12 11:18:23 +01003499 case Instruction::CONST_METHOD_TYPE: {
Orion Hodson06d10a72018-05-14 08:53:38 +01003500 dex::ProtoIndex proto_idx(instruction.VRegB_21c());
Orion Hodson18259d72018-04-12 11:18:23 +01003501 BuildLoadMethodType(proto_idx, dex_pc);
3502 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
3503 break;
3504 }
3505
David Brazdildee58d62016-04-07 09:54:26 +00003506 case Instruction::MOVE_EXCEPTION: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003507 AppendInstruction(new (allocator_) HLoadException(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00003508 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
Vladimir Markoca6fff82017-10-03 14:49:14 +01003509 AppendInstruction(new (allocator_) HClearException(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00003510 break;
3511 }
3512
3513 case Instruction::THROW: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003514 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01003515 AppendInstruction(new (allocator_) HThrow(exception, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00003516 // We finished building this block. Set the current block to null to avoid
3517 // adding dead instructions to it.
3518 current_block_ = nullptr;
3519 break;
3520 }
3521
3522 case Instruction::INSTANCE_OF: {
3523 uint8_t destination = instruction.VRegA_22c();
3524 uint8_t reference = instruction.VRegB_22c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08003525 dex::TypeIndex type_index(instruction.VRegC_22c());
David Brazdildee58d62016-04-07 09:54:26 +00003526 BuildTypeCheck(instruction, destination, reference, type_index, dex_pc);
3527 break;
3528 }
3529
3530 case Instruction::CHECK_CAST: {
3531 uint8_t reference = instruction.VRegA_21c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08003532 dex::TypeIndex type_index(instruction.VRegB_21c());
David Brazdildee58d62016-04-07 09:54:26 +00003533 BuildTypeCheck(instruction, -1, reference, type_index, dex_pc);
3534 break;
3535 }
3536
3537 case Instruction::MONITOR_ENTER: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003538 AppendInstruction(new (allocator_) HMonitorOperation(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003539 LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference),
David Brazdildee58d62016-04-07 09:54:26 +00003540 HMonitorOperation::OperationKind::kEnter,
3541 dex_pc));
Artem Serov2808be82018-12-20 19:15:11 +00003542 graph_->SetHasMonitorOperations(true);
David Brazdildee58d62016-04-07 09:54:26 +00003543 break;
3544 }
3545
3546 case Instruction::MONITOR_EXIT: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003547 AppendInstruction(new (allocator_) HMonitorOperation(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003548 LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference),
David Brazdildee58d62016-04-07 09:54:26 +00003549 HMonitorOperation::OperationKind::kExit,
3550 dex_pc));
Artem Serov2808be82018-12-20 19:15:11 +00003551 graph_->SetHasMonitorOperations(true);
David Brazdildee58d62016-04-07 09:54:26 +00003552 break;
3553 }
3554
3555 case Instruction::SPARSE_SWITCH:
3556 case Instruction::PACKED_SWITCH: {
3557 BuildSwitch(instruction, dex_pc);
3558 break;
3559 }
3560
David Srbeckyc5cd5892021-03-23 08:09:15 +00003561 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
David Srbecky61c62422021-04-27 16:22:48 +01003562 case Instruction::UNUSED_73:
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003563 case Instruction::UNUSED_79:
3564 case Instruction::UNUSED_7A:
David Srbeckyc5cd5892021-03-23 08:09:15 +00003565 case Instruction::UNUSED_E3 ... Instruction::UNUSED_F9: {
David Brazdildee58d62016-04-07 09:54:26 +00003566 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07003567 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00003568 << " because of unhandled instruction "
3569 << instruction.Name();
Igor Murashkin1e065a52017-08-09 13:20:34 -07003570 MaybeRecordStat(compilation_stats_,
3571 MethodCompilationStat::kNotCompiledUnhandledInstruction);
David Brazdildee58d62016-04-07 09:54:26 +00003572 return false;
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003573 }
David Brazdildee58d62016-04-07 09:54:26 +00003574 }
3575 return true;
3576} // NOLINT(readability/fn_size)
3577
Vladimir Marko8d6768d2017-03-14 10:13:21 +00003578ObjPtr<mirror::Class> HInstructionBuilder::LookupResolvedType(
3579 dex::TypeIndex type_index,
3580 const DexCompilationUnit& compilation_unit) const {
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003581 return compilation_unit.GetClassLinker()->LookupResolvedType(
Vladimir Marko8d6768d2017-03-14 10:13:21 +00003582 type_index, compilation_unit.GetDexCache().Get(), compilation_unit.GetClassLoader().Get());
3583}
3584
3585ObjPtr<mirror::Class> HInstructionBuilder::LookupReferrerClass() const {
3586 // TODO: Cache the result in a Handle<mirror::Class>.
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003587 const dex::MethodId& method_id =
Vladimir Marko8d6768d2017-03-14 10:13:21 +00003588 dex_compilation_unit_->GetDexFile()->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
3589 return LookupResolvedType(method_id.class_idx_, *dex_compilation_unit_);
3590}
3591
David Brazdildee58d62016-04-07 09:54:26 +00003592} // namespace art