blob: cd68b2a7c9a4d81cc6adfe59b42f67c005e29ce1 [file] [log] [blame]
David Brazdildee58d62016-04-07 09:54:26 +00001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "instruction_builder.h"
18
Matthew Gharrity465ecc82016-07-19 21:32:52 +000019#include "art_method-inl.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010020#include "base/arena_bit_vector.h"
21#include "base/bit_vector-inl.h"
Andreas Gampe85f1c572018-11-21 13:52:48 -080022#include "base/logging.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010023#include "block_builder.h"
Vladimir Marko3b506202018-10-31 14:33:58 +000024#include "class_linker-inl.h"
25#include "code_generator.h"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010026#include "data_type-inl.h"
David Sehr312f3b22018-03-19 08:39:26 -070027#include "dex/bytecode_utils.h"
David Sehr9e734c72018-01-04 17:56:19 -080028#include "dex/dex_instruction-inl.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010029#include "driver/dex_compilation_unit.h"
David Brazdildee58d62016-04-07 09:54:26 +000030#include "driver/compiler_options.h"
Andreas Gampe75a7db62016-09-26 12:04:26 -070031#include "imtable-inl.h"
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +000032#include "jit/jit.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010033#include "mirror/dex_cache.h"
Nicolas Geoffray58cc1cb2017-11-20 13:27:29 +000034#include "oat_file.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010035#include "optimizing_compiler_stats.h"
Mathieu Chartierde4b08f2017-07-10 14:13:41 -070036#include "quicken_info.h"
Nicolas Geoffray396198b2020-06-16 12:02:45 +010037#include "reflective_handle_scope-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070038#include "scoped_thread_state_change-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070039#include "sharpening.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010040#include "ssa_builder.h"
Andreas Gampea7c83ac2017-09-11 08:14:23 -070041#include "well_known_classes.h"
David Brazdildee58d62016-04-07 09:54:26 +000042
Vladimir Marko0a516052019-10-14 13:00:44 +000043namespace art {
David Brazdildee58d62016-04-07 09:54:26 +000044
Mathieu Chartier808c7a52017-12-15 11:19:33 -080045HInstructionBuilder::HInstructionBuilder(HGraph* graph,
46 HBasicBlockBuilder* block_builder,
47 SsaBuilder* ssa_builder,
48 const DexFile* dex_file,
49 const CodeItemDebugInfoAccessor& accessor,
50 DataType::Type return_type,
51 const DexCompilationUnit* dex_compilation_unit,
52 const DexCompilationUnit* outer_compilation_unit,
Mathieu Chartier808c7a52017-12-15 11:19:33 -080053 CodeGenerator* code_generator,
Mathieu Chartier210531f2018-01-12 10:15:51 -080054 ArrayRef<const uint8_t> interpreter_metadata,
Mathieu Chartier808c7a52017-12-15 11:19:33 -080055 OptimizingCompilerStats* compiler_stats,
Mathieu Chartier808c7a52017-12-15 11:19:33 -080056 ScopedArenaAllocator* local_allocator)
57 : allocator_(graph->GetAllocator()),
58 graph_(graph),
Mathieu Chartier808c7a52017-12-15 11:19:33 -080059 dex_file_(dex_file),
60 code_item_accessor_(accessor),
61 return_type_(return_type),
62 block_builder_(block_builder),
63 ssa_builder_(ssa_builder),
Mathieu Chartier808c7a52017-12-15 11:19:33 -080064 code_generator_(code_generator),
65 dex_compilation_unit_(dex_compilation_unit),
66 outer_compilation_unit_(outer_compilation_unit),
67 quicken_info_(interpreter_metadata),
68 compilation_stats_(compiler_stats),
69 local_allocator_(local_allocator),
70 locals_for_(local_allocator->Adapter(kArenaAllocGraphBuilder)),
71 current_block_(nullptr),
72 current_locals_(nullptr),
73 latest_result_(nullptr),
74 current_this_parameter_(nullptr),
Vladimir Marko3b506202018-10-31 14:33:58 +000075 loop_headers_(local_allocator->Adapter(kArenaAllocGraphBuilder)),
76 class_cache_(std::less<dex::TypeIndex>(), local_allocator->Adapter(kArenaAllocGraphBuilder)) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -080077 loop_headers_.reserve(kDefaultNumberOfLoops);
78}
79
David Brazdildee58d62016-04-07 09:54:26 +000080HBasicBlock* HInstructionBuilder::FindBlockStartingAt(uint32_t dex_pc) const {
81 return block_builder_->GetBlockAt(dex_pc);
82}
83
Vladimir Marko69d310e2017-10-09 14:12:23 +010084inline ScopedArenaVector<HInstruction*>* HInstructionBuilder::GetLocalsFor(HBasicBlock* block) {
85 ScopedArenaVector<HInstruction*>* locals = &locals_for_[block->GetBlockId()];
David Brazdildee58d62016-04-07 09:54:26 +000086 const size_t vregs = graph_->GetNumberOfVRegs();
Mingyao Yang01b47b02017-02-03 12:09:57 -080087 if (locals->size() == vregs) {
88 return locals;
89 }
90 return GetLocalsForWithAllocation(block, locals, vregs);
91}
David Brazdildee58d62016-04-07 09:54:26 +000092
Vladimir Marko69d310e2017-10-09 14:12:23 +010093ScopedArenaVector<HInstruction*>* HInstructionBuilder::GetLocalsForWithAllocation(
Mingyao Yang01b47b02017-02-03 12:09:57 -080094 HBasicBlock* block,
Vladimir Marko69d310e2017-10-09 14:12:23 +010095 ScopedArenaVector<HInstruction*>* locals,
Mingyao Yang01b47b02017-02-03 12:09:57 -080096 const size_t vregs) {
97 DCHECK_NE(locals->size(), vregs);
98 locals->resize(vregs, nullptr);
99 if (block->IsCatchBlock()) {
100 // We record incoming inputs of catch phis at throwing instructions and
101 // must therefore eagerly create the phis. Phis for undefined vregs will
102 // be deleted when the first throwing instruction with the vreg undefined
103 // is encountered. Unused phis will be removed by dead phi analysis.
104 for (size_t i = 0; i < vregs; ++i) {
105 // No point in creating the catch phi if it is already undefined at
106 // the first throwing instruction.
107 HInstruction* current_local_value = (*current_locals_)[i];
108 if (current_local_value != nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100109 HPhi* phi = new (allocator_) HPhi(
110 allocator_,
Mingyao Yang01b47b02017-02-03 12:09:57 -0800111 i,
112 0,
113 current_local_value->GetType());
114 block->AddPhi(phi);
115 (*locals)[i] = phi;
David Brazdildee58d62016-04-07 09:54:26 +0000116 }
117 }
118 }
119 return locals;
120}
121
Mingyao Yang01b47b02017-02-03 12:09:57 -0800122inline HInstruction* HInstructionBuilder::ValueOfLocalAt(HBasicBlock* block, size_t local) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100123 ScopedArenaVector<HInstruction*>* locals = GetLocalsFor(block);
David Brazdildee58d62016-04-07 09:54:26 +0000124 return (*locals)[local];
125}
126
127void HInstructionBuilder::InitializeBlockLocals() {
128 current_locals_ = GetLocalsFor(current_block_);
129
130 if (current_block_->IsCatchBlock()) {
131 // Catch phis were already created and inputs collected from throwing sites.
132 if (kIsDebugBuild) {
133 // Make sure there was at least one throwing instruction which initialized
134 // locals (guaranteed by HGraphBuilder) and that all try blocks have been
135 // visited already (from HTryBoundary scoping and reverse post order).
136 bool catch_block_visited = false;
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100137 for (HBasicBlock* current : graph_->GetReversePostOrder()) {
David Brazdildee58d62016-04-07 09:54:26 +0000138 if (current == current_block_) {
139 catch_block_visited = true;
140 } else if (current->IsTryBlock()) {
141 const HTryBoundary& try_entry = current->GetTryCatchInformation()->GetTryEntry();
142 if (try_entry.HasExceptionHandler(*current_block_)) {
143 DCHECK(!catch_block_visited) << "Catch block visited before its try block.";
144 }
145 }
146 }
147 DCHECK_EQ(current_locals_->size(), graph_->GetNumberOfVRegs())
148 << "No instructions throwing into a live catch block.";
149 }
150 } else if (current_block_->IsLoopHeader()) {
151 // If the block is a loop header, we know we only have visited the pre header
152 // because we are visiting in reverse post order. We create phis for all initialized
153 // locals from the pre header. Their inputs will be populated at the end of
154 // the analysis.
155 for (size_t local = 0; local < current_locals_->size(); ++local) {
156 HInstruction* incoming =
157 ValueOfLocalAt(current_block_->GetLoopInformation()->GetPreHeader(), local);
158 if (incoming != nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100159 HPhi* phi = new (allocator_) HPhi(
160 allocator_,
David Brazdildee58d62016-04-07 09:54:26 +0000161 local,
162 0,
163 incoming->GetType());
164 current_block_->AddPhi(phi);
165 (*current_locals_)[local] = phi;
166 }
167 }
168
169 // Save the loop header so that the last phase of the analysis knows which
170 // blocks need to be updated.
171 loop_headers_.push_back(current_block_);
172 } else if (current_block_->GetPredecessors().size() > 0) {
173 // All predecessors have already been visited because we are visiting in reverse post order.
174 // We merge the values of all locals, creating phis if those values differ.
175 for (size_t local = 0; local < current_locals_->size(); ++local) {
176 bool one_predecessor_has_no_value = false;
177 bool is_different = false;
178 HInstruction* value = ValueOfLocalAt(current_block_->GetPredecessors()[0], local);
179
180 for (HBasicBlock* predecessor : current_block_->GetPredecessors()) {
181 HInstruction* current = ValueOfLocalAt(predecessor, local);
182 if (current == nullptr) {
183 one_predecessor_has_no_value = true;
184 break;
185 } else if (current != value) {
186 is_different = true;
187 }
188 }
189
190 if (one_predecessor_has_no_value) {
191 // If one predecessor has no value for this local, we trust the verifier has
192 // successfully checked that there is a store dominating any read after this block.
193 continue;
194 }
195
196 if (is_different) {
197 HInstruction* first_input = ValueOfLocalAt(current_block_->GetPredecessors()[0], local);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100198 HPhi* phi = new (allocator_) HPhi(
199 allocator_,
David Brazdildee58d62016-04-07 09:54:26 +0000200 local,
201 current_block_->GetPredecessors().size(),
202 first_input->GetType());
203 for (size_t i = 0; i < current_block_->GetPredecessors().size(); i++) {
204 HInstruction* pred_value = ValueOfLocalAt(current_block_->GetPredecessors()[i], local);
205 phi->SetRawInputAt(i, pred_value);
206 }
207 current_block_->AddPhi(phi);
208 value = phi;
209 }
210 (*current_locals_)[local] = value;
211 }
212 }
213}
214
215void HInstructionBuilder::PropagateLocalsToCatchBlocks() {
216 const HTryBoundary& try_entry = current_block_->GetTryCatchInformation()->GetTryEntry();
217 for (HBasicBlock* catch_block : try_entry.GetExceptionHandlers()) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100218 ScopedArenaVector<HInstruction*>* handler_locals = GetLocalsFor(catch_block);
David Brazdildee58d62016-04-07 09:54:26 +0000219 DCHECK_EQ(handler_locals->size(), current_locals_->size());
220 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
221 HInstruction* handler_value = (*handler_locals)[vreg];
222 if (handler_value == nullptr) {
223 // Vreg was undefined at a previously encountered throwing instruction
224 // and the catch phi was deleted. Do not record the local value.
225 continue;
226 }
227 DCHECK(handler_value->IsPhi());
228
229 HInstruction* local_value = (*current_locals_)[vreg];
230 if (local_value == nullptr) {
231 // This is the first instruction throwing into `catch_block` where
232 // `vreg` is undefined. Delete the catch phi.
233 catch_block->RemovePhi(handler_value->AsPhi());
234 (*handler_locals)[vreg] = nullptr;
235 } else {
236 // Vreg has been defined at all instructions throwing into `catch_block`
237 // encountered so far. Record the local value in the catch phi.
238 handler_value->AsPhi()->AddInput(local_value);
239 }
240 }
241 }
242}
243
244void HInstructionBuilder::AppendInstruction(HInstruction* instruction) {
245 current_block_->AddInstruction(instruction);
246 InitializeInstruction(instruction);
247}
248
249void HInstructionBuilder::InsertInstructionAtTop(HInstruction* instruction) {
250 if (current_block_->GetInstructions().IsEmpty()) {
251 current_block_->AddInstruction(instruction);
252 } else {
253 current_block_->InsertInstructionBefore(instruction, current_block_->GetFirstInstruction());
254 }
255 InitializeInstruction(instruction);
256}
257
258void HInstructionBuilder::InitializeInstruction(HInstruction* instruction) {
259 if (instruction->NeedsEnvironment()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100260 HEnvironment* environment = new (allocator_) HEnvironment(
261 allocator_,
David Brazdildee58d62016-04-07 09:54:26 +0000262 current_locals_->size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000263 graph_->GetArtMethod(),
David Brazdildee58d62016-04-07 09:54:26 +0000264 instruction->GetDexPc(),
David Brazdildee58d62016-04-07 09:54:26 +0000265 instruction);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100266 environment->CopyFrom(ArrayRef<HInstruction* const>(*current_locals_));
David Brazdildee58d62016-04-07 09:54:26 +0000267 instruction->SetRawEnvironment(environment);
268 }
269}
270
David Brazdilc120bbe2016-04-22 16:57:00 +0100271HInstruction* HInstructionBuilder::LoadNullCheckedLocal(uint32_t register_index, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100272 HInstruction* ref = LoadLocal(register_index, DataType::Type::kReference);
David Brazdilc120bbe2016-04-22 16:57:00 +0100273 if (!ref->CanBeNull()) {
274 return ref;
275 }
276
Vladimir Markoca6fff82017-10-03 14:49:14 +0100277 HNullCheck* null_check = new (allocator_) HNullCheck(ref, dex_pc);
David Brazdilc120bbe2016-04-22 16:57:00 +0100278 AppendInstruction(null_check);
279 return null_check;
280}
281
David Brazdildee58d62016-04-07 09:54:26 +0000282void HInstructionBuilder::SetLoopHeaderPhiInputs() {
283 for (size_t i = loop_headers_.size(); i > 0; --i) {
284 HBasicBlock* block = loop_headers_[i - 1];
285 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
286 HPhi* phi = it.Current()->AsPhi();
287 size_t vreg = phi->GetRegNumber();
288 for (HBasicBlock* predecessor : block->GetPredecessors()) {
289 HInstruction* value = ValueOfLocalAt(predecessor, vreg);
290 if (value == nullptr) {
291 // Vreg is undefined at this predecessor. Mark it dead and leave with
292 // fewer inputs than predecessors. SsaChecker will fail if not removed.
293 phi->SetDead();
294 break;
295 } else {
296 phi->AddInput(value);
297 }
298 }
299 }
300 }
301}
302
303static bool IsBlockPopulated(HBasicBlock* block) {
304 if (block->IsLoopHeader()) {
305 // Suspend checks were inserted into loop headers during building of dominator tree.
306 DCHECK(block->GetFirstInstruction()->IsSuspendCheck());
307 return block->GetFirstInstruction() != block->GetLastInstruction();
308 } else {
309 return !block->GetInstructions().IsEmpty();
310 }
311}
312
313bool HInstructionBuilder::Build() {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800314 DCHECK(code_item_accessor_.HasCodeItem());
Vladimir Marko69d310e2017-10-09 14:12:23 +0100315 locals_for_.resize(
316 graph_->GetBlocks().size(),
317 ScopedArenaVector<HInstruction*>(local_allocator_->Adapter(kArenaAllocGraphBuilder)));
David Brazdildee58d62016-04-07 09:54:26 +0000318
319 // Find locations where we want to generate extra stackmaps for native debugging.
320 // This allows us to generate the info only at interesting points (for example,
321 // at start of java statement) rather than before every dex instruction.
Vladimir Marko3b506202018-10-31 14:33:58 +0000322 const bool native_debuggable = code_generator_ != nullptr &&
323 code_generator_->GetCompilerOptions().GetNativeDebuggable();
David Brazdildee58d62016-04-07 09:54:26 +0000324 ArenaBitVector* native_debug_info_locations = nullptr;
325 if (native_debuggable) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100326 native_debug_info_locations = FindNativeDebugInfoLocations();
David Brazdildee58d62016-04-07 09:54:26 +0000327 }
328
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100329 for (HBasicBlock* block : graph_->GetReversePostOrder()) {
330 current_block_ = block;
David Brazdildee58d62016-04-07 09:54:26 +0000331 uint32_t block_dex_pc = current_block_->GetDexPc();
332
333 InitializeBlockLocals();
334
335 if (current_block_->IsEntryBlock()) {
336 InitializeParameters();
Vladimir Markoca6fff82017-10-03 14:49:14 +0100337 AppendInstruction(new (allocator_) HSuspendCheck(0u));
338 AppendInstruction(new (allocator_) HGoto(0u));
David Brazdildee58d62016-04-07 09:54:26 +0000339 continue;
340 } else if (current_block_->IsExitBlock()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100341 AppendInstruction(new (allocator_) HExit());
David Brazdildee58d62016-04-07 09:54:26 +0000342 continue;
343 } else if (current_block_->IsLoopHeader()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100344 HSuspendCheck* suspend_check = new (allocator_) HSuspendCheck(current_block_->GetDexPc());
David Brazdildee58d62016-04-07 09:54:26 +0000345 current_block_->GetLoopInformation()->SetSuspendCheck(suspend_check);
346 // This is slightly odd because the loop header might not be empty (TryBoundary).
347 // But we're still creating the environment with locals from the top of the block.
348 InsertInstructionAtTop(suspend_check);
349 }
350
351 if (block_dex_pc == kNoDexPc || current_block_ != block_builder_->GetBlockAt(block_dex_pc)) {
352 // Synthetic block that does not need to be populated.
353 DCHECK(IsBlockPopulated(current_block_));
354 continue;
355 }
356
357 DCHECK(!IsBlockPopulated(current_block_));
358
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700359 uint32_t quicken_index = 0;
360 if (CanDecodeQuickenedInfo()) {
361 quicken_index = block_builder_->GetQuickenIndex(block_dex_pc);
362 }
363
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800364 for (const DexInstructionPcPair& pair : code_item_accessor_.InstructionsFrom(block_dex_pc)) {
David Brazdildee58d62016-04-07 09:54:26 +0000365 if (current_block_ == nullptr) {
366 // The previous instruction ended this block.
367 break;
368 }
369
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800370 const uint32_t dex_pc = pair.DexPc();
David Brazdildee58d62016-04-07 09:54:26 +0000371 if (dex_pc != block_dex_pc && FindBlockStartingAt(dex_pc) != nullptr) {
372 // This dex_pc starts a new basic block.
373 break;
374 }
375
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800376 if (current_block_->IsTryBlock() && IsThrowingDexInstruction(pair.Inst())) {
David Brazdildee58d62016-04-07 09:54:26 +0000377 PropagateLocalsToCatchBlocks();
378 }
379
380 if (native_debuggable && native_debug_info_locations->IsBitSet(dex_pc)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100381 AppendInstruction(new (allocator_) HNativeDebugInfo(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000382 }
383
Vladimir Markof91fc122020-05-13 09:21:00 +0100384 // Note: There may be no Thread for gtests.
385 DCHECK(Thread::Current() == nullptr || !Thread::Current()->IsExceptionPending())
Nicolas Geoffray605c5912020-04-08 15:12:39 +0100386 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
387 << " " << pair.Inst().Name() << "@" << dex_pc;
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800388 if (!ProcessDexInstruction(pair.Inst(), dex_pc, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +0000389 return false;
390 }
Vladimir Markof91fc122020-05-13 09:21:00 +0100391 DCHECK(Thread::Current() == nullptr || !Thread::Current()->IsExceptionPending())
Nicolas Geoffrayfbf53b52020-04-01 15:20:14 +0100392 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
Nicolas Geoffray605c5912020-04-08 15:12:39 +0100393 << " " << pair.Inst().Name() << "@" << dex_pc;
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700394
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800395 if (QuickenInfoTable::NeedsIndexForInstruction(&pair.Inst())) {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700396 ++quicken_index;
397 }
David Brazdildee58d62016-04-07 09:54:26 +0000398 }
399
400 if (current_block_ != nullptr) {
401 // Branching instructions clear current_block, so we know the last
402 // instruction of the current block is not a branching instruction.
403 // We add an unconditional Goto to the next block.
404 DCHECK_EQ(current_block_->GetSuccessors().size(), 1u);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100405 AppendInstruction(new (allocator_) HGoto());
David Brazdildee58d62016-04-07 09:54:26 +0000406 }
407 }
408
409 SetLoopHeaderPhiInputs();
410
411 return true;
412}
413
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000414void HInstructionBuilder::BuildIntrinsic(ArtMethod* method) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800415 DCHECK(!code_item_accessor_.HasCodeItem());
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000416 DCHECK(method->IsIntrinsic());
417
418 locals_for_.resize(
419 graph_->GetBlocks().size(),
420 ScopedArenaVector<HInstruction*>(local_allocator_->Adapter(kArenaAllocGraphBuilder)));
421
422 // Fill the entry block. Do not add suspend check, we do not want a suspend
423 // check in intrinsics; intrinsic methods are supposed to be fast.
424 current_block_ = graph_->GetEntryBlock();
425 InitializeBlockLocals();
426 InitializeParameters();
427 AppendInstruction(new (allocator_) HGoto(0u));
428
429 // Fill the body.
430 current_block_ = current_block_->GetSingleSuccessor();
431 InitializeBlockLocals();
432 DCHECK(!IsBlockPopulated(current_block_));
433
Vladimir Marko5f846072020-04-09 13:20:11 +0100434 // Add the intermediate representation, if available, or invoke instruction.
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000435 size_t in_vregs = graph_->GetNumberOfInVRegs();
436 size_t number_of_arguments =
437 in_vregs - std::count(current_locals_->end() - in_vregs, current_locals_->end(), nullptr);
438 uint32_t method_idx = dex_compilation_unit_->GetDexMethodIndex();
Vladimir Marko5f846072020-04-09 13:20:11 +0100439 const char* shorty = dex_file_->GetMethodShorty(method_idx);
Treehugger Robot2c5827a2018-05-17 22:26:08 +0000440 RangeInstructionOperands operands(graph_->GetNumberOfVRegs() - in_vregs, in_vregs);
Vladimir Marko5f846072020-04-09 13:20:11 +0100441 if (!BuildSimpleIntrinsic(method, kNoDexPc, operands, shorty)) {
442 // Some intrinsics without intermediate representation still yield a leaf method,
443 // so build the invoke. Use HInvokeStaticOrDirect even for methods that would
444 // normally use an HInvokeVirtual (sharpen the call).
445 MethodReference target_method(dex_file_, method_idx);
446 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
447 HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall,
448 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
449 /* method_load_data= */ 0u
450 };
451 InvokeType invoke_type = dex_compilation_unit_->IsStatic() ? kStatic : kDirect;
452 HInvokeStaticOrDirect* invoke = new (allocator_) HInvokeStaticOrDirect(
453 allocator_,
454 number_of_arguments,
455 return_type_,
456 kNoDexPc,
457 method_idx,
458 method,
459 dispatch_info,
460 invoke_type,
461 target_method,
462 HInvokeStaticOrDirect::ClinitCheckRequirement::kNone);
463 HandleInvoke(invoke, operands, shorty, /* is_unresolved= */ false);
464 }
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000465
466 // Add the return instruction.
467 if (return_type_ == DataType::Type::kVoid) {
468 AppendInstruction(new (allocator_) HReturnVoid());
469 } else {
Vladimir Marko5f846072020-04-09 13:20:11 +0100470 AppendInstruction(new (allocator_) HReturn(latest_result_));
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000471 }
472
473 // Fill the exit block.
474 DCHECK_EQ(current_block_->GetSingleSuccessor(), graph_->GetExitBlock());
475 current_block_ = graph_->GetExitBlock();
476 InitializeBlockLocals();
477 AppendInstruction(new (allocator_) HExit());
478}
479
Vladimir Marko69d310e2017-10-09 14:12:23 +0100480ArenaBitVector* HInstructionBuilder::FindNativeDebugInfoLocations() {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100481 ArenaBitVector* locations = ArenaBitVector::Create(local_allocator_,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800482 code_item_accessor_.InsnsSizeInCodeUnits(),
Andreas Gampe3db70682018-12-26 15:12:03 -0800483 /* expandable= */ false,
Vladimir Marko69d310e2017-10-09 14:12:23 +0100484 kArenaAllocGraphBuilder);
485 locations->ClearAllBits();
Mathieu Chartier3e2e1232018-09-11 12:35:30 -0700486 // The visitor gets called when the line number changes.
487 // In other words, it marks the start of new java statement.
488 code_item_accessor_.DecodeDebugPositionInfo([&](const DexFile::PositionInfo& entry) {
489 locations->SetBit(entry.address_);
490 return false;
491 });
David Brazdildee58d62016-04-07 09:54:26 +0000492 // Instruction-specific tweaks.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800493 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800494 switch (inst->Opcode()) {
David Brazdildee58d62016-04-07 09:54:26 +0000495 case Instruction::MOVE_EXCEPTION: {
496 // Stop in native debugger after the exception has been moved.
497 // The compiler also expects the move at the start of basic block so
498 // we do not want to interfere by inserting native-debug-info before it.
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800499 locations->ClearBit(inst.DexPc());
500 DexInstructionIterator next = std::next(DexInstructionIterator(inst));
501 DCHECK(next.DexPc() != inst.DexPc());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800502 if (next != code_item_accessor_.end()) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -0700503 locations->SetBit(next.DexPc());
David Brazdildee58d62016-04-07 09:54:26 +0000504 }
505 break;
506 }
507 default:
508 break;
509 }
510 }
Vladimir Marko69d310e2017-10-09 14:12:23 +0100511 return locations;
David Brazdildee58d62016-04-07 09:54:26 +0000512}
513
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100514HInstruction* HInstructionBuilder::LoadLocal(uint32_t reg_number, DataType::Type type) const {
David Brazdildee58d62016-04-07 09:54:26 +0000515 HInstruction* value = (*current_locals_)[reg_number];
516 DCHECK(value != nullptr);
517
518 // If the operation requests a specific type, we make sure its input is of that type.
519 if (type != value->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100520 if (DataType::IsFloatingPointType(type)) {
Aart Bik31883642016-06-06 15:02:44 -0700521 value = ssa_builder_->GetFloatOrDoubleEquivalent(value, type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100522 } else if (type == DataType::Type::kReference) {
Aart Bik31883642016-06-06 15:02:44 -0700523 value = ssa_builder_->GetReferenceTypeEquivalent(value);
David Brazdildee58d62016-04-07 09:54:26 +0000524 }
Aart Bik31883642016-06-06 15:02:44 -0700525 DCHECK(value != nullptr);
David Brazdildee58d62016-04-07 09:54:26 +0000526 }
527
528 return value;
529}
530
531void HInstructionBuilder::UpdateLocal(uint32_t reg_number, HInstruction* stored_value) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100532 DataType::Type stored_type = stored_value->GetType();
533 DCHECK_NE(stored_type, DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +0000534
535 // Storing into vreg `reg_number` may implicitly invalidate the surrounding
536 // registers. Consider the following cases:
537 // (1) Storing a wide value must overwrite previous values in both `reg_number`
538 // and `reg_number+1`. We store `nullptr` in `reg_number+1`.
539 // (2) If vreg `reg_number-1` holds a wide value, writing into `reg_number`
540 // must invalidate it. We store `nullptr` in `reg_number-1`.
541 // Consequently, storing a wide value into the high vreg of another wide value
542 // will invalidate both `reg_number-1` and `reg_number+1`.
543
544 if (reg_number != 0) {
545 HInstruction* local_low = (*current_locals_)[reg_number - 1];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100546 if (local_low != nullptr && DataType::Is64BitType(local_low->GetType())) {
David Brazdildee58d62016-04-07 09:54:26 +0000547 // The vreg we are storing into was previously the high vreg of a pair.
548 // We need to invalidate its low vreg.
549 DCHECK((*current_locals_)[reg_number] == nullptr);
550 (*current_locals_)[reg_number - 1] = nullptr;
551 }
552 }
553
554 (*current_locals_)[reg_number] = stored_value;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100555 if (DataType::Is64BitType(stored_type)) {
David Brazdildee58d62016-04-07 09:54:26 +0000556 // We are storing a pair. Invalidate the instruction in the high vreg.
557 (*current_locals_)[reg_number + 1] = nullptr;
558 }
559}
560
561void HInstructionBuilder::InitializeParameters() {
562 DCHECK(current_block_->IsEntryBlock());
563
Vladimir Marko69d310e2017-10-09 14:12:23 +0100564 // outer_compilation_unit_ is null only when unit testing.
565 if (outer_compilation_unit_ == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +0000566 return;
567 }
568
569 const char* shorty = dex_compilation_unit_->GetShorty();
570 uint16_t number_of_parameters = graph_->GetNumberOfInVRegs();
571 uint16_t locals_index = graph_->GetNumberOfLocalVRegs();
572 uint16_t parameter_index = 0;
573
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800574 const dex::MethodId& referrer_method_id =
David Brazdildee58d62016-04-07 09:54:26 +0000575 dex_file_->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
576 if (!dex_compilation_unit_->IsStatic()) {
577 // Add the implicit 'this' argument, not expressed in the signature.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100578 HParameterValue* parameter = new (allocator_) HParameterValue(*dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +0000579 referrer_method_id.class_idx_,
580 parameter_index++,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100581 DataType::Type::kReference,
Andreas Gampe3db70682018-12-26 15:12:03 -0800582 /* is_this= */ true);
David Brazdildee58d62016-04-07 09:54:26 +0000583 AppendInstruction(parameter);
584 UpdateLocal(locals_index++, parameter);
585 number_of_parameters--;
Igor Murashkind01745e2017-04-05 16:40:31 -0700586 current_this_parameter_ = parameter;
587 } else {
588 DCHECK(current_this_parameter_ == nullptr);
David Brazdildee58d62016-04-07 09:54:26 +0000589 }
590
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800591 const dex::ProtoId& proto = dex_file_->GetMethodPrototype(referrer_method_id);
592 const dex::TypeList* arg_types = dex_file_->GetProtoParameters(proto);
David Brazdildee58d62016-04-07 09:54:26 +0000593 for (int i = 0, shorty_pos = 1; i < number_of_parameters; i++) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100594 HParameterValue* parameter = new (allocator_) HParameterValue(
David Brazdildee58d62016-04-07 09:54:26 +0000595 *dex_file_,
596 arg_types->GetTypeItem(shorty_pos - 1).type_idx_,
597 parameter_index++,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100598 DataType::FromShorty(shorty[shorty_pos]),
Andreas Gampe3db70682018-12-26 15:12:03 -0800599 /* is_this= */ false);
David Brazdildee58d62016-04-07 09:54:26 +0000600 ++shorty_pos;
601 AppendInstruction(parameter);
602 // Store the parameter value in the local that the dex code will use
603 // to reference that parameter.
604 UpdateLocal(locals_index++, parameter);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100605 if (DataType::Is64BitType(parameter->GetType())) {
David Brazdildee58d62016-04-07 09:54:26 +0000606 i++;
607 locals_index++;
608 parameter_index++;
609 }
610 }
611}
612
613template<typename T>
614void HInstructionBuilder::If_22t(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100615 HInstruction* first = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
616 HInstruction* second = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100617 T* comparison = new (allocator_) T(first, second, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000618 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100619 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000620 current_block_ = nullptr;
621}
622
623template<typename T>
624void HInstructionBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100625 HInstruction* value = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100626 T* comparison = new (allocator_) T(value, graph_->GetIntConstant(0, dex_pc), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000627 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100628 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000629 current_block_ = nullptr;
630}
631
632template<typename T>
633void HInstructionBuilder::Unop_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100634 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000635 uint32_t dex_pc) {
636 HInstruction* first = LoadLocal(instruction.VRegB(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100637 AppendInstruction(new (allocator_) T(type, first, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000638 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
639}
640
641void HInstructionBuilder::Conversion_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100642 DataType::Type input_type,
643 DataType::Type result_type,
David Brazdildee58d62016-04-07 09:54:26 +0000644 uint32_t dex_pc) {
645 HInstruction* first = LoadLocal(instruction.VRegB(), input_type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100646 AppendInstruction(new (allocator_) HTypeConversion(result_type, first, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000647 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
648}
649
650template<typename T>
651void HInstructionBuilder::Binop_23x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100652 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000653 uint32_t dex_pc) {
654 HInstruction* first = LoadLocal(instruction.VRegB(), type);
655 HInstruction* second = LoadLocal(instruction.VRegC(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100656 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000657 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
658}
659
660template<typename T>
661void HInstructionBuilder::Binop_23x_shift(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100662 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000663 uint32_t dex_pc) {
664 HInstruction* first = LoadLocal(instruction.VRegB(), type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100665 HInstruction* second = LoadLocal(instruction.VRegC(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100666 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000667 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
668}
669
670void HInstructionBuilder::Binop_23x_cmp(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100671 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000672 ComparisonBias bias,
673 uint32_t dex_pc) {
674 HInstruction* first = LoadLocal(instruction.VRegB(), type);
675 HInstruction* second = LoadLocal(instruction.VRegC(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100676 AppendInstruction(new (allocator_) HCompare(type, first, second, bias, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000677 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
678}
679
680template<typename T>
681void HInstructionBuilder::Binop_12x_shift(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100682 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000683 uint32_t dex_pc) {
684 HInstruction* first = LoadLocal(instruction.VRegA(), type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100685 HInstruction* second = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100686 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000687 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
688}
689
690template<typename T>
691void HInstructionBuilder::Binop_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100692 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000693 uint32_t dex_pc) {
694 HInstruction* first = LoadLocal(instruction.VRegA(), type);
695 HInstruction* second = LoadLocal(instruction.VRegB(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100696 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000697 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
698}
699
700template<typename T>
701void HInstructionBuilder::Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100702 HInstruction* first = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000703 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s(), dex_pc);
704 if (reverse) {
705 std::swap(first, second);
706 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100707 AppendInstruction(new (allocator_) T(DataType::Type::kInt32, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000708 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
709}
710
711template<typename T>
712void HInstructionBuilder::Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100713 HInstruction* first = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000714 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b(), dex_pc);
715 if (reverse) {
716 std::swap(first, second);
717 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100718 AppendInstruction(new (allocator_) T(DataType::Type::kInt32, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000719 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
720}
721
Igor Murashkind01745e2017-04-05 16:40:31 -0700722// Does the method being compiled need any constructor barriers being inserted?
723// (Always 'false' for methods that aren't <init>.)
Vladimir Markoc1c34522018-10-31 13:56:49 +0000724static bool RequiresConstructorBarrier(const DexCompilationUnit* cu) {
Igor Murashkin032cacd2017-04-06 14:40:08 -0700725 // Can be null in unit tests only.
726 if (UNLIKELY(cu == nullptr)) {
727 return false;
728 }
729
Vladimir Markoc1c34522018-10-31 13:56:49 +0000730 // Constructor barriers are applicable only for <init> methods.
731 if (LIKELY(!cu->IsConstructor() || cu->IsStatic())) {
732 return false;
733 }
734
735 return cu->RequiresConstructorBarrier();
David Brazdildee58d62016-04-07 09:54:26 +0000736}
737
738// Returns true if `block` has only one successor which starts at the next
739// dex_pc after `instruction` at `dex_pc`.
740static bool IsFallthroughInstruction(const Instruction& instruction,
741 uint32_t dex_pc,
742 HBasicBlock* block) {
743 uint32_t next_dex_pc = dex_pc + instruction.SizeInCodeUnits();
744 return block->GetSingleSuccessor()->GetDexPc() == next_dex_pc;
745}
746
747void HInstructionBuilder::BuildSwitch(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100748 HInstruction* value = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000749 DexSwitchTable table(instruction, dex_pc);
750
751 if (table.GetNumEntries() == 0) {
752 // Empty Switch. Code falls through to the next block.
753 DCHECK(IsFallthroughInstruction(instruction, dex_pc, current_block_));
Vladimir Markoca6fff82017-10-03 14:49:14 +0100754 AppendInstruction(new (allocator_) HGoto(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000755 } else if (table.ShouldBuildDecisionTree()) {
756 for (DexSwitchTableIterator it(table); !it.Done(); it.Advance()) {
757 HInstruction* case_value = graph_->GetIntConstant(it.CurrentKey(), dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100758 HEqual* comparison = new (allocator_) HEqual(value, case_value, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000759 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100760 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000761
762 if (!it.IsLast()) {
763 current_block_ = FindBlockStartingAt(it.GetDexPcForCurrentIndex());
764 }
765 }
766 } else {
767 AppendInstruction(
Vladimir Markoca6fff82017-10-03 14:49:14 +0100768 new (allocator_) HPackedSwitch(table.GetEntryAt(0), table.GetNumEntries(), value, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000769 }
770
771 current_block_ = nullptr;
772}
773
774void HInstructionBuilder::BuildReturn(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100775 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000776 uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100777 if (type == DataType::Type::kVoid) {
Igor Murashkind01745e2017-04-05 16:40:31 -0700778 // Only <init> (which is a return-void) could possibly have a constructor fence.
Igor Murashkin032cacd2017-04-06 14:40:08 -0700779 // This may insert additional redundant constructor fences from the super constructors.
780 // TODO: remove redundant constructor fences (b/36656456).
Vladimir Markoc1c34522018-10-31 13:56:49 +0000781 if (RequiresConstructorBarrier(dex_compilation_unit_)) {
Igor Murashkind01745e2017-04-05 16:40:31 -0700782 // Compiling instance constructor.
Vladimir Markoba118822017-06-12 15:41:56 +0100783 DCHECK_STREQ("<init>", graph_->GetMethodName());
Igor Murashkind01745e2017-04-05 16:40:31 -0700784
785 HInstruction* fence_target = current_this_parameter_;
786 DCHECK(fence_target != nullptr);
787
Vladimir Markoca6fff82017-10-03 14:49:14 +0100788 AppendInstruction(new (allocator_) HConstructorFence(fence_target, dex_pc, allocator_));
Igor Murashkin6ef45672017-08-08 13:59:55 -0700789 MaybeRecordStat(
790 compilation_stats_,
791 MethodCompilationStat::kConstructorFenceGeneratedFinal);
David Brazdildee58d62016-04-07 09:54:26 +0000792 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100793 AppendInstruction(new (allocator_) HReturnVoid(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000794 } else {
Vladimir Markoc1c34522018-10-31 13:56:49 +0000795 DCHECK(!RequiresConstructorBarrier(dex_compilation_unit_));
David Brazdildee58d62016-04-07 09:54:26 +0000796 HInstruction* value = LoadLocal(instruction.VRegA(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100797 AppendInstruction(new (allocator_) HReturn(value, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000798 }
799 current_block_ = nullptr;
800}
801
802static InvokeType GetInvokeTypeFromOpCode(Instruction::Code opcode) {
803 switch (opcode) {
804 case Instruction::INVOKE_STATIC:
805 case Instruction::INVOKE_STATIC_RANGE:
806 return kStatic;
807 case Instruction::INVOKE_DIRECT:
808 case Instruction::INVOKE_DIRECT_RANGE:
809 return kDirect;
810 case Instruction::INVOKE_VIRTUAL:
811 case Instruction::INVOKE_VIRTUAL_QUICK:
812 case Instruction::INVOKE_VIRTUAL_RANGE:
813 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
814 return kVirtual;
815 case Instruction::INVOKE_INTERFACE:
816 case Instruction::INVOKE_INTERFACE_RANGE:
817 return kInterface;
818 case Instruction::INVOKE_SUPER_RANGE:
819 case Instruction::INVOKE_SUPER:
820 return kSuper;
821 default:
822 LOG(FATAL) << "Unexpected invoke opcode: " << opcode;
823 UNREACHABLE();
824 }
825}
826
Vladimir Marko2f40d242020-04-08 12:56:45 +0100827// Try to resolve a method using the class linker. Return null if a method could
828// not be resolved or the resolved method cannot be used for some reason.
829// Also retrieve method data needed for creating the invoke intermediate
830// representation while we hold the mutator lock here.
831static ArtMethod* ResolveMethod(uint16_t method_idx,
832 ArtMethod* referrer,
833 const DexCompilationUnit& dex_compilation_unit,
834 /*inout*/InvokeType* invoke_type,
835 /*out*/MethodReference* target_method,
836 /*out*/bool* is_string_constructor) {
David Brazdildee58d62016-04-07 09:54:26 +0000837 ScopedObjectAccess soa(Thread::Current());
David Brazdildee58d62016-04-07 09:54:26 +0000838
Vladimir Marko2f40d242020-04-08 12:56:45 +0100839 ClassLinker* class_linker = dex_compilation_unit.GetClassLinker();
840 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit.GetClassLoader();
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100841
Vladimir Markoba118822017-06-12 15:41:56 +0100842 ArtMethod* resolved_method =
843 class_linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
Vladimir Markoba118822017-06-12 15:41:56 +0100844 method_idx,
Vladimir Marko2f40d242020-04-08 12:56:45 +0100845 dex_compilation_unit.GetDexCache(),
Vladimir Markoba118822017-06-12 15:41:56 +0100846 class_loader,
Vladimir Marko2f40d242020-04-08 12:56:45 +0100847 referrer,
848 *invoke_type);
David Brazdildee58d62016-04-07 09:54:26 +0000849
850 if (UNLIKELY(resolved_method == nullptr)) {
851 // Clean up any exception left by type resolution.
852 soa.Self()->ClearException();
853 return nullptr;
854 }
Nicolas Geoffray605c5912020-04-08 15:12:39 +0100855 DCHECK(!soa.Self()->IsExceptionPending());
David Brazdildee58d62016-04-07 09:54:26 +0000856
Vladimir Markoba118822017-06-12 15:41:56 +0100857 // The referrer may be unresolved for AOT if we're compiling a class that cannot be
858 // resolved because, for example, we don't find a superclass in the classpath.
Vladimir Marko2f40d242020-04-08 12:56:45 +0100859 if (referrer == nullptr) {
Vladimir Markoba118822017-06-12 15:41:56 +0100860 // The class linker cannot check access without a referrer, so we have to do it.
861 // Fall back to HInvokeUnresolved if the method isn't public.
David Brazdildee58d62016-04-07 09:54:26 +0000862 if (!resolved_method->IsPublic()) {
863 return nullptr;
864 }
David Brazdildee58d62016-04-07 09:54:26 +0000865 }
866
867 // We have to special case the invoke-super case, as ClassLinker::ResolveMethod does not.
868 // We need to look at the referrer's super class vtable. We need to do this to know if we need to
869 // make this an invoke-unresolved to handle cross-dex invokes or abstract super methods, both of
870 // which require runtime handling.
Vladimir Marko2f40d242020-04-08 12:56:45 +0100871 if (*invoke_type == kSuper) {
872 ObjPtr<mirror::Class> compiling_class = dex_compilation_unit.GetCompilingClass().Get();
Andreas Gampefa4333d2017-02-14 11:10:34 -0800873 if (compiling_class == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +0000874 // We could not determine the method's class we need to wait until runtime.
875 DCHECK(Runtime::Current()->IsAotCompiler());
876 return nullptr;
877 }
Vladimir Markoba118822017-06-12 15:41:56 +0100878 ObjPtr<mirror::Class> referenced_class = class_linker->LookupResolvedType(
Vladimir Marko2f40d242020-04-08 12:56:45 +0100879 dex_compilation_unit.GetDexFile()->GetMethodId(method_idx).class_idx_,
880 dex_compilation_unit.GetDexCache().Get(),
Vladimir Markoba118822017-06-12 15:41:56 +0100881 class_loader.Get());
882 DCHECK(referenced_class != nullptr); // We have already resolved a method from this class.
883 if (!referenced_class->IsAssignableFrom(compiling_class)) {
Aart Bikf663e342016-04-04 17:28:59 -0700884 // We cannot statically determine the target method. The runtime will throw a
885 // NoSuchMethodError on this one.
886 return nullptr;
887 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100888 ArtMethod* actual_method;
Vladimir Markoba118822017-06-12 15:41:56 +0100889 if (referenced_class->IsInterface()) {
890 actual_method = referenced_class->FindVirtualMethodForInterfaceSuper(
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100891 resolved_method, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000892 } else {
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100893 uint16_t vtable_index = resolved_method->GetMethodIndex();
894 actual_method = compiling_class->GetSuperClass()->GetVTableEntry(
895 vtable_index, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000896 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100897 if (actual_method != resolved_method &&
Vladimir Marko2f40d242020-04-08 12:56:45 +0100898 !IsSameDexFile(*actual_method->GetDexFile(), *dex_compilation_unit.GetDexFile())) {
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100899 // The back-end code generator relies on this check in order to ensure that it will not
900 // attempt to read the dex_cache with a dex_method_index that is not from the correct
901 // dex_file. If we didn't do this check then the dex_method_index will not be updated in the
Vladimir Markoc1c34522018-10-31 13:56:49 +0000902 // builder, which means that the code-generator (and sharpening and inliner, maybe)
903 // might invoke an incorrect method.
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100904 // TODO: The actual method could still be referenced in the current dex file, so we
905 // could try locating it.
906 // TODO: Remove the dex_file restriction.
907 return nullptr;
908 }
909 if (!actual_method->IsInvokable()) {
910 // Fail if the actual method cannot be invoked. Otherwise, the runtime resolution stub
911 // could resolve the callee to the wrong method.
912 return nullptr;
913 }
914 resolved_method = actual_method;
David Brazdildee58d62016-04-07 09:54:26 +0000915 }
916
Vladimir Marko2f40d242020-04-08 12:56:45 +0100917 if (*invoke_type == kInterface) {
918 if (resolved_method->GetDeclaringClass()->IsObjectClass()) {
919 // If the resolved method is from j.l.Object, emit a virtual call instead.
920 // The IMT conflict stub only handles interface methods.
921 *invoke_type = kVirtual;
922 } else {
923 DCHECK(resolved_method->GetDeclaringClass()->IsInterface());
924 }
925 }
David Brazdildee58d62016-04-07 09:54:26 +0000926
Vladimir Marko2f40d242020-04-08 12:56:45 +0100927 if (*invoke_type == kDirect || *invoke_type == kStatic || *invoke_type == kSuper) {
928 // Record the target method needed for HInvokeStaticOrDirect.
929 *target_method =
930 MethodReference(resolved_method->GetDexFile(), resolved_method->GetDexMethodIndex());
931 } else if (*invoke_type == kVirtual) {
932 // For HInvokeVirtual we need the vtable index.
933 *target_method = MethodReference(/*file=*/ nullptr, resolved_method->GetVtableIndex());
934 } else {
935 DCHECK_EQ(*invoke_type, kInterface);
936 // For HInvokeInterface we need the IMT index.
937 *target_method = MethodReference(/*file=*/ nullptr, ImTable::GetImtIndex(resolved_method));
938 }
939
940 *is_string_constructor =
941 resolved_method->IsConstructor() && resolved_method->GetDeclaringClass()->IsStringClass();
942
943 return resolved_method;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100944}
945
David Brazdildee58d62016-04-07 09:54:26 +0000946bool HInstructionBuilder::BuildInvoke(const Instruction& instruction,
947 uint32_t dex_pc,
948 uint32_t method_idx,
Treehugger Robot2c5827a2018-05-17 22:26:08 +0000949 const InstructionOperands& operands) {
David Brazdildee58d62016-04-07 09:54:26 +0000950 InvokeType invoke_type = GetInvokeTypeFromOpCode(instruction.Opcode());
Orion Hodson4c8e12e2018-05-18 08:33:20 +0100951 const char* shorty = dex_file_->GetMethodShorty(method_idx);
952 DataType::Type return_type = DataType::FromShorty(shorty[0]);
David Brazdildee58d62016-04-07 09:54:26 +0000953
954 // Remove the return type from the 'proto'.
Orion Hodson4c8e12e2018-05-18 08:33:20 +0100955 size_t number_of_arguments = strlen(shorty) - 1;
David Brazdildee58d62016-04-07 09:54:26 +0000956 if (invoke_type != kStatic) { // instance call
957 // One extra argument for 'this'.
958 number_of_arguments++;
959 }
960
Vladimir Marko2f40d242020-04-08 12:56:45 +0100961 MethodReference target_method(nullptr, 0u);
962 bool is_string_constructor = false;
963 ArtMethod* resolved_method = ResolveMethod(method_idx,
964 graph_->GetArtMethod(),
965 *dex_compilation_unit_,
966 &invoke_type,
967 &target_method,
968 &is_string_constructor);
David Brazdildee58d62016-04-07 09:54:26 +0000969
970 if (UNLIKELY(resolved_method == nullptr)) {
Nicolas Geoffray605c5912020-04-08 15:12:39 +0100971 DCHECK(!Thread::Current()->IsExceptionPending());
Igor Murashkin1e065a52017-08-09 13:20:34 -0700972 MaybeRecordStat(compilation_stats_,
973 MethodCompilationStat::kUnresolvedMethod);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100974 HInvoke* invoke = new (allocator_) HInvokeUnresolved(allocator_,
975 number_of_arguments,
976 return_type,
977 dex_pc,
978 method_idx,
979 invoke_type);
Andreas Gampe3db70682018-12-26 15:12:03 -0800980 return HandleInvoke(invoke, operands, shorty, /* is_unresolved= */ true);
David Brazdildee58d62016-04-07 09:54:26 +0000981 }
982
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100983 // Replace calls to String.<init> with StringFactory.
Vladimir Marko2f40d242020-04-08 12:56:45 +0100984 if (is_string_constructor) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100985 uint32_t string_init_entry_point = WellKnownClasses::StringInitToEntryPoint(resolved_method);
986 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
987 HInvokeStaticOrDirect::MethodLoadKind::kStringInit,
988 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000989 dchecked_integral_cast<uint64_t>(string_init_entry_point)
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100990 };
Nicolas Geoffrayf665f842018-02-15 12:29:06 +0000991 // We pass null for the resolved_method to ensure optimizations
992 // don't rely on it.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100993 HInvoke* invoke = new (allocator_) HInvokeStaticOrDirect(
994 allocator_,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100995 number_of_arguments - 1,
Andreas Gampe3db70682018-12-26 15:12:03 -0800996 /* return_type= */ DataType::Type::kReference,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100997 dex_pc,
998 method_idx,
Andreas Gampe3db70682018-12-26 15:12:03 -0800999 /* resolved_method= */ nullptr,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01001000 dispatch_info,
1001 invoke_type,
1002 target_method,
1003 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001004 return HandleStringInit(invoke, operands, shorty);
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01001005 }
1006
David Brazdildee58d62016-04-07 09:54:26 +00001007 // Potential class initialization check, in the case of a static method call.
Vladimir Marko2f40d242020-04-08 12:56:45 +01001008 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement =
1009 HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
David Brazdildee58d62016-04-07 09:54:26 +00001010 HClinitCheck* clinit_check = nullptr;
Vladimir Marko2f40d242020-04-08 12:56:45 +01001011 if (invoke_type == kStatic) {
1012 clinit_check = ProcessClinitCheckForInvoke(dex_pc, resolved_method, &clinit_check_requirement);
1013 }
1014
Vladimir Marko5f846072020-04-09 13:20:11 +01001015 // Try to build an HIR replacement for the intrinsic.
1016 if (UNLIKELY(resolved_method->IsIntrinsic())) {
1017 // All intrinsics are in the primary boot image, so their class can always be referenced
1018 // and we do not need to rely on the implicit class initialization check. The class should
1019 // be initialized but we do not require that here.
1020 DCHECK_NE(clinit_check_requirement, HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit);
1021 if (BuildSimpleIntrinsic(resolved_method, dex_pc, operands, shorty)) {
1022 return true;
1023 }
1024 }
1025
David Brazdildee58d62016-04-07 09:54:26 +00001026 HInvoke* invoke = nullptr;
1027 if (invoke_type == kDirect || invoke_type == kStatic || invoke_type == kSuper) {
Vladimir Marko2f40d242020-04-08 12:56:45 +01001028 if (invoke_type == kSuper) {
1029 if (IsSameDexFile(*target_method.dex_file, *dex_compilation_unit_->GetDexFile())) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001030 // Update the method index to the one resolved. Note that this may be a no-op if
David Brazdildee58d62016-04-07 09:54:26 +00001031 // we resolved to the method referenced by the instruction.
Vladimir Marko2f40d242020-04-08 12:56:45 +01001032 method_idx = target_method.index;
David Brazdildee58d62016-04-07 09:54:26 +00001033 }
1034 }
1035
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01001036 HInvokeStaticOrDirect::DispatchInfo dispatch_info =
1037 HSharpening::SharpenInvokeStaticOrDirect(resolved_method, code_generator_);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001038 invoke = new (allocator_) HInvokeStaticOrDirect(allocator_,
1039 number_of_arguments,
1040 return_type,
1041 dex_pc,
1042 method_idx,
1043 resolved_method,
1044 dispatch_info,
1045 invoke_type,
1046 target_method,
1047 clinit_check_requirement);
Vladimir Marko5f846072020-04-09 13:20:11 +01001048 if (clinit_check != nullptr) {
1049 // Add the class initialization check as last input of `invoke`.
1050 DCHECK_EQ(clinit_check_requirement, HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit);
1051 size_t clinit_check_index = invoke->InputCount() - 1u;
1052 DCHECK(invoke->InputAt(clinit_check_index) == nullptr);
1053 invoke->SetArgumentAt(clinit_check_index, clinit_check);
1054 }
David Brazdildee58d62016-04-07 09:54:26 +00001055 } else if (invoke_type == kVirtual) {
Vladimir Marko2f40d242020-04-08 12:56:45 +01001056 DCHECK(target_method.dex_file == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001057 invoke = new (allocator_) HInvokeVirtual(allocator_,
1058 number_of_arguments,
1059 return_type,
1060 dex_pc,
1061 method_idx,
1062 resolved_method,
Vladimir Marko2f40d242020-04-08 12:56:45 +01001063 /*vtable_index=*/ target_method.index);
David Brazdildee58d62016-04-07 09:54:26 +00001064 } else {
1065 DCHECK_EQ(invoke_type, kInterface);
Vladimir Marko2f40d242020-04-08 12:56:45 +01001066 invoke = new (allocator_) HInvokeInterface(allocator_,
Vladimir Markoca6fff82017-10-03 14:49:14 +01001067 number_of_arguments,
1068 return_type,
1069 dex_pc,
1070 method_idx,
1071 resolved_method,
Vladimir Marko2f40d242020-04-08 12:56:45 +01001072 /*imt_index=*/ target_method.index);
David Brazdildee58d62016-04-07 09:54:26 +00001073 }
Vladimir Marko5f846072020-04-09 13:20:11 +01001074 return HandleInvoke(invoke, operands, shorty, /* is_unresolved= */ false);
David Brazdildee58d62016-04-07 09:54:26 +00001075}
1076
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001077bool HInstructionBuilder::BuildInvokePolymorphic(uint32_t dex_pc,
Orion Hodsonac141392017-01-13 11:53:47 +00001078 uint32_t method_idx,
Orion Hodson06d10a72018-05-14 08:53:38 +01001079 dex::ProtoIndex proto_idx,
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001080 const InstructionOperands& operands) {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001081 const char* shorty = dex_file_->GetShorty(proto_idx);
1082 DCHECK_EQ(1 + ArtMethod::NumArgRegisters(shorty), operands.GetNumberOfOperands());
1083 DataType::Type return_type = DataType::FromShorty(shorty[0]);
1084 size_t number_of_arguments = strlen(shorty);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001085 HInvoke* invoke = new (allocator_) HInvokePolymorphic(allocator_,
1086 number_of_arguments,
1087 return_type,
1088 dex_pc,
1089 method_idx);
Andreas Gampe3db70682018-12-26 15:12:03 -08001090 return HandleInvoke(invoke, operands, shorty, /* is_unresolved= */ false);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001091}
1092
1093
1094bool HInstructionBuilder::BuildInvokeCustom(uint32_t dex_pc,
1095 uint32_t call_site_idx,
1096 const InstructionOperands& operands) {
1097 dex::ProtoIndex proto_idx = dex_file_->GetProtoIndexForCallSite(call_site_idx);
1098 const char* shorty = dex_file_->GetShorty(proto_idx);
1099 DataType::Type return_type = DataType::FromShorty(shorty[0]);
1100 size_t number_of_arguments = strlen(shorty) - 1;
1101 HInvoke* invoke = new (allocator_) HInvokeCustom(allocator_,
1102 number_of_arguments,
1103 call_site_idx,
1104 return_type,
1105 dex_pc);
Andreas Gampe3db70682018-12-26 15:12:03 -08001106 return HandleInvoke(invoke, operands, shorty, /* is_unresolved= */ false);
Orion Hodsonac141392017-01-13 11:53:47 +00001107}
1108
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001109HNewInstance* HInstructionBuilder::BuildNewInstance(dex::TypeIndex type_index, uint32_t dex_pc) {
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001110 ScopedObjectAccess soa(Thread::Current());
David Brazdildee58d62016-04-07 09:54:26 +00001111
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001112 HLoadClass* load_class = BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001113
David Brazdildee58d62016-04-07 09:54:26 +00001114 HInstruction* cls = load_class;
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001115 Handle<mirror::Class> klass = load_class->GetClass();
1116
Vladimir Marko2f40d242020-04-08 12:56:45 +01001117 if (!IsInitialized(klass.Get())) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001118 cls = new (allocator_) HClinitCheck(load_class, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001119 AppendInstruction(cls);
1120 }
1121
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001122 // Only the access check entrypoint handles the finalizable class case. If we
1123 // need access checks, then we haven't resolved the method and the class may
1124 // again be finalizable.
1125 QuickEntrypointEnum entrypoint = kQuickAllocObjectInitialized;
1126 if (load_class->NeedsAccessCheck() || klass->IsFinalizable() || !klass->IsInstantiable()) {
1127 entrypoint = kQuickAllocObjectWithChecks;
1128 }
Alex Lightd109e302018-06-27 10:25:41 -07001129 // We will always be able to resolve the string class since it is in the BCP.
1130 if (!klass.IsNull() && klass->IsStringClass()) {
1131 entrypoint = kQuickAllocStringObject;
1132 }
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001133
1134 // Consider classes we haven't resolved as potentially finalizable.
Andreas Gampefa4333d2017-02-14 11:10:34 -08001135 bool finalizable = (klass == nullptr) || klass->IsFinalizable();
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001136
Vladimir Markoca6fff82017-10-03 14:49:14 +01001137 HNewInstance* new_instance = new (allocator_) HNewInstance(
David Brazdildee58d62016-04-07 09:54:26 +00001138 cls,
David Brazdildee58d62016-04-07 09:54:26 +00001139 dex_pc,
1140 type_index,
1141 *dex_compilation_unit_->GetDexFile(),
David Brazdildee58d62016-04-07 09:54:26 +00001142 finalizable,
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001143 entrypoint);
1144 AppendInstruction(new_instance);
1145
1146 return new_instance;
1147}
1148
1149void HInstructionBuilder::BuildConstructorFenceForAllocation(HInstruction* allocation) {
1150 DCHECK(allocation != nullptr &&
George Burgess IVf2072992017-05-23 15:36:41 -07001151 (allocation->IsNewInstance() ||
1152 allocation->IsNewArray())); // corresponding to "new" keyword in JLS.
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001153
1154 if (allocation->IsNewInstance()) {
1155 // STRING SPECIAL HANDLING:
1156 // -------------------------------
1157 // Strings have a real HNewInstance node but they end up always having 0 uses.
1158 // All uses of a String HNewInstance are always transformed to replace their input
1159 // of the HNewInstance with an input of the invoke to StringFactory.
1160 //
1161 // Do not emit an HConstructorFence here since it can inhibit some String new-instance
1162 // optimizations (to pass checker tests that rely on those optimizations).
1163 HNewInstance* new_inst = allocation->AsNewInstance();
1164 HLoadClass* load_class = new_inst->GetLoadClass();
1165
1166 Thread* self = Thread::Current();
1167 ScopedObjectAccess soa(self);
1168 StackHandleScope<1> hs(self);
1169 Handle<mirror::Class> klass = load_class->GetClass();
1170 if (klass != nullptr && klass->IsStringClass()) {
1171 return;
1172 // Note: Do not use allocation->IsStringAlloc which requires
1173 // a valid ReferenceTypeInfo, but that doesn't get made until after reference type
1174 // propagation (and instruction builder is too early).
1175 }
1176 // (In terms of correctness, the StringFactory needs to provide its own
1177 // default initialization barrier, see below.)
1178 }
1179
1180 // JLS 17.4.5 "Happens-before Order" describes:
1181 //
1182 // The default initialization of any object happens-before any other actions (other than
1183 // default-writes) of a program.
1184 //
1185 // In our implementation the default initialization of an object to type T means
1186 // setting all of its initial data (object[0..size)) to 0, and setting the
1187 // object's class header (i.e. object.getClass() == T.class).
1188 //
1189 // In practice this fence ensures that the writes to the object header
1190 // are visible to other threads if this object escapes the current thread.
1191 // (and in theory the 0-initializing, but that happens automatically
1192 // when new memory pages are mapped in by the OS).
1193 HConstructorFence* ctor_fence =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001194 new (allocator_) HConstructorFence(allocation, allocation->GetDexPc(), allocator_);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001195 AppendInstruction(ctor_fence);
Igor Murashkin6ef45672017-08-08 13:59:55 -07001196 MaybeRecordStat(
1197 compilation_stats_,
1198 MethodCompilationStat::kConstructorFenceGeneratedNew);
David Brazdildee58d62016-04-07 09:54:26 +00001199}
1200
Vladimir Marko2ab1bdd2018-07-12 09:59:56 +01001201static bool IsInBootImage(ObjPtr<mirror::Class> cls, const CompilerOptions& compiler_options)
1202 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko6a67bea2020-01-20 13:05:55 +00001203 if (Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(cls)) {
1204 return true;
1205 }
1206 if (compiler_options.IsBootImage() || compiler_options.IsBootImageExtension()) {
Vladimir Marko2ab1bdd2018-07-12 09:59:56 +01001207 std::string temp;
1208 const char* descriptor = cls->GetDescriptor(&temp);
1209 return compiler_options.IsImageClass(descriptor);
1210 } else {
Vladimir Marko6a67bea2020-01-20 13:05:55 +00001211 return false;
Vladimir Marko2ab1bdd2018-07-12 09:59:56 +01001212 }
1213}
1214
1215static bool IsSubClass(ObjPtr<mirror::Class> to_test, ObjPtr<mirror::Class> super_class)
1216 REQUIRES_SHARED(Locks::mutator_lock_) {
1217 return to_test != nullptr && !to_test->IsInterface() && to_test->IsSubClass(super_class);
1218}
1219
1220static bool HasTrivialClinit(ObjPtr<mirror::Class> klass, PointerSize pointer_size)
1221 REQUIRES_SHARED(Locks::mutator_lock_) {
1222 // Check if the class has encoded fields that trigger bytecode execution.
1223 // (Encoded fields are just a different representation of <clinit>.)
1224 if (klass->NumStaticFields() != 0u) {
1225 DCHECK(klass->GetClassDef() != nullptr);
1226 EncodedStaticFieldValueIterator it(klass->GetDexFile(), *klass->GetClassDef());
1227 for (; it.HasNext(); it.Next()) {
1228 switch (it.GetValueType()) {
1229 case EncodedArrayValueIterator::ValueType::kBoolean:
1230 case EncodedArrayValueIterator::ValueType::kByte:
1231 case EncodedArrayValueIterator::ValueType::kShort:
1232 case EncodedArrayValueIterator::ValueType::kChar:
1233 case EncodedArrayValueIterator::ValueType::kInt:
1234 case EncodedArrayValueIterator::ValueType::kLong:
1235 case EncodedArrayValueIterator::ValueType::kFloat:
1236 case EncodedArrayValueIterator::ValueType::kDouble:
1237 case EncodedArrayValueIterator::ValueType::kNull:
1238 case EncodedArrayValueIterator::ValueType::kString:
1239 // Primitive, null or j.l.String initialization is permitted.
1240 break;
1241 case EncodedArrayValueIterator::ValueType::kType:
1242 // Type initialization can load classes and execute bytecode through a class loader
1243 // which can execute arbitrary bytecode. We do not optimize for known class loaders;
1244 // kType is rarely used (if ever).
1245 return false;
1246 default:
1247 // Other types in the encoded static field list are rejected by the DexFileVerifier.
1248 LOG(FATAL) << "Unexpected type " << it.GetValueType();
1249 UNREACHABLE();
1250 }
1251 }
1252 }
1253 // Check if the class has <clinit> that executes arbitrary code.
1254 // Initialization of static fields of the class itself with constants is allowed.
1255 ArtMethod* clinit = klass->FindClassInitializer(pointer_size);
1256 if (clinit != nullptr) {
1257 const DexFile& dex_file = *clinit->GetDexFile();
1258 CodeItemInstructionAccessor accessor(dex_file, clinit->GetCodeItem());
1259 for (DexInstructionPcPair it : accessor) {
1260 switch (it->Opcode()) {
1261 case Instruction::CONST_4:
1262 case Instruction::CONST_16:
1263 case Instruction::CONST:
1264 case Instruction::CONST_HIGH16:
1265 case Instruction::CONST_WIDE_16:
1266 case Instruction::CONST_WIDE_32:
1267 case Instruction::CONST_WIDE:
1268 case Instruction::CONST_WIDE_HIGH16:
1269 case Instruction::CONST_STRING:
1270 case Instruction::CONST_STRING_JUMBO:
1271 // Primitive, null or j.l.String initialization is permitted.
1272 break;
1273 case Instruction::RETURN_VOID:
1274 case Instruction::RETURN_VOID_NO_BARRIER:
1275 break;
1276 case Instruction::SPUT:
1277 case Instruction::SPUT_WIDE:
1278 case Instruction::SPUT_OBJECT:
1279 case Instruction::SPUT_BOOLEAN:
1280 case Instruction::SPUT_BYTE:
1281 case Instruction::SPUT_CHAR:
1282 case Instruction::SPUT_SHORT:
1283 // Only initialization of a static field of the same class is permitted.
1284 if (dex_file.GetFieldId(it->VRegB_21c()).class_idx_ != klass->GetDexTypeIndex()) {
1285 return false;
1286 }
1287 break;
1288 case Instruction::NEW_ARRAY:
1289 // Only primitive arrays are permitted.
1290 if (Primitive::GetType(dex_file.GetTypeDescriptor(dex_file.GetTypeId(
1291 dex::TypeIndex(it->VRegC_22c())))[1]) == Primitive::kPrimNot) {
1292 return false;
1293 }
1294 break;
1295 case Instruction::APUT:
1296 case Instruction::APUT_WIDE:
1297 case Instruction::APUT_BOOLEAN:
1298 case Instruction::APUT_BYTE:
1299 case Instruction::APUT_CHAR:
1300 case Instruction::APUT_SHORT:
1301 case Instruction::FILL_ARRAY_DATA:
1302 case Instruction::NOP:
1303 // Allow initialization of primitive arrays (only constants can be stored).
1304 // Note: We expect NOPs used for fill-array-data-payload but accept all NOPs
1305 // (even unreferenced switch payloads if they make it through the verifier).
1306 break;
1307 default:
1308 return false;
1309 }
1310 }
1311 }
1312 return true;
1313}
1314
1315static bool HasTrivialInitialization(ObjPtr<mirror::Class> cls,
1316 const CompilerOptions& compiler_options)
1317 REQUIRES_SHARED(Locks::mutator_lock_) {
1318 Runtime* runtime = Runtime::Current();
1319 PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
1320
1321 // Check the superclass chain.
1322 for (ObjPtr<mirror::Class> klass = cls; klass != nullptr; klass = klass->GetSuperClass()) {
1323 if (klass->IsInitialized() && IsInBootImage(klass, compiler_options)) {
1324 break; // `klass` and its superclasses are already initialized in the boot image.
1325 }
1326 if (!HasTrivialClinit(klass, pointer_size)) {
1327 return false;
1328 }
1329 }
1330
1331 // Also check interfaces with default methods as they need to be initialized as well.
1332 ObjPtr<mirror::IfTable> iftable = cls->GetIfTable();
1333 DCHECK(iftable != nullptr);
1334 for (int32_t i = 0, count = iftable->Count(); i != count; ++i) {
1335 ObjPtr<mirror::Class> iface = iftable->GetInterface(i);
1336 if (!iface->HasDefaultMethods()) {
1337 continue; // Initializing `cls` does not initialize this interface.
1338 }
1339 if (iface->IsInitialized() && IsInBootImage(iface, compiler_options)) {
1340 continue; // This interface is already initialized in the boot image.
1341 }
1342 if (!HasTrivialClinit(iface, pointer_size)) {
1343 return false;
1344 }
1345 }
1346 return true;
1347}
1348
Vladimir Marko2f40d242020-04-08 12:56:45 +01001349bool HInstructionBuilder::IsInitialized(ObjPtr<mirror::Class> cls) const {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001350 if (cls == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +00001351 return false;
1352 }
1353
Vladimir Marko51b8aaf2017-06-09 15:17:05 +01001354 // Check if the class will be initialized at runtime.
1355 if (cls->IsInitialized()) {
Vladimir Marko695348f2020-05-19 14:42:02 +01001356 const CompilerOptions& compiler_options = code_generator_->GetCompilerOptions();
1357 if (compiler_options.IsAotCompiler()) {
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +00001358 // Assume loaded only if klass is in the boot image. App classes cannot be assumed
1359 // loaded because we don't even know what class loader will be used to load them.
Vladimir Marko695348f2020-05-19 14:42:02 +01001360 if (IsInBootImage(cls, compiler_options)) {
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +00001361 return true;
1362 }
1363 } else {
Vladimir Marko695348f2020-05-19 14:42:02 +01001364 DCHECK(compiler_options.IsJitCompiler());
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +00001365 if (Runtime::Current()->GetJit()->CanAssumeInitialized(
Vladimir Marko2f40d242020-04-08 12:56:45 +01001366 cls,
Vladimir Marko695348f2020-05-19 14:42:02 +01001367 compiler_options.IsJitCompilerForSharedCode())) {
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +00001368 // For JIT, the class cannot revert to an uninitialized state.
1369 return true;
1370 }
Vladimir Marko51b8aaf2017-06-09 15:17:05 +01001371 }
1372 }
1373
Vladimir Marko7f260d42018-10-30 18:09:55 +00001374 // We can avoid the class initialization check for `cls` in static methods and constructors
1375 // in the very same class; invoking a static method involves a class initialization check
1376 // and so does the instance allocation that must be executed before invoking a constructor.
1377 // Other instance methods of the same class can run on an escaped instance
Vladimir Marko51b8aaf2017-06-09 15:17:05 +01001378 // of an erroneous class. Even a superclass may need to be checked as the subclass
1379 // can be completely initialized while the superclass is initializing and the subclass
1380 // remains initialized when the superclass initializer throws afterwards. b/62478025
1381 // Note: The HClinitCheck+HInvokeStaticOrDirect merging can still apply.
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001382 auto is_static_method_or_constructor_of_cls = [cls](const DexCompilationUnit& compilation_unit)
1383 REQUIRES_SHARED(Locks::mutator_lock_) {
1384 return (compilation_unit.GetAccessFlags() & (kAccStatic | kAccConstructor)) != 0u &&
Vladimir Marko2f40d242020-04-08 12:56:45 +01001385 compilation_unit.GetCompilingClass().Get() == cls;
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001386 };
1387 if (is_static_method_or_constructor_of_cls(*outer_compilation_unit_) ||
1388 // Check also the innermost method. Though excessive copies of ClinitCheck can be
1389 // eliminated by GVN, that happens only after the decision whether to inline the
1390 // graph or not and that may depend on the presence of the ClinitCheck.
1391 // TODO: We should walk over the entire inlined method chain, but we don't pass that
1392 // information to the builder.
1393 is_static_method_or_constructor_of_cls(*dex_compilation_unit_)) {
Vladimir Marko2ab1bdd2018-07-12 09:59:56 +01001394 return true;
1395 }
David Brazdildee58d62016-04-07 09:54:26 +00001396
Vladimir Marko2ab1bdd2018-07-12 09:59:56 +01001397 // Otherwise, we may be able to avoid the check if `cls` is a superclass of a method being
1398 // compiled here (anywhere in the inlining chain) as the `cls` must have started initializing
1399 // before calling any `cls` or subclass methods. Static methods require a clinit check and
1400 // instance methods require an instance which cannot be created before doing a clinit check.
1401 // When a subclass of `cls` starts initializing, it starts initializing its superclass
1402 // chain up to `cls` without running any bytecode, i.e. without any opportunity for circular
1403 // initialization weirdness.
1404 //
1405 // If the initialization of `cls` is trivial (`cls` and its superclasses and superinterfaces
1406 // with default methods initialize only their own static fields using constant values), it must
1407 // complete, either successfully or by throwing and marking `cls` erroneous, without allocating
1408 // any instances of `cls` or subclasses (or any other class) and without calling any methods.
1409 // If it completes by throwing, no instances of `cls` shall be created and no subclass method
1410 // bytecode shall execute (see above), therefore the instruction we're building shall be
1411 // unreachable. By reaching the instruction, we know that `cls` was initialized successfully.
1412 //
1413 // TODO: We should walk over the entire inlined methods chain, but we don't pass that
1414 // information to the builder. (We could also check if we're guaranteed a non-null instance
1415 // of `cls` at this location but that's outside the scope of the instruction builder.)
Vladimir Marko2f40d242020-04-08 12:56:45 +01001416 bool is_subclass = IsSubClass(outer_compilation_unit_->GetCompilingClass().Get(), cls);
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001417 if (dex_compilation_unit_ != outer_compilation_unit_) {
1418 is_subclass = is_subclass ||
Vladimir Marko2f40d242020-04-08 12:56:45 +01001419 IsSubClass(dex_compilation_unit_->GetCompilingClass().Get(), cls);
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001420 }
Vladimir Marko2f40d242020-04-08 12:56:45 +01001421 if (is_subclass && HasTrivialInitialization(cls, code_generator_->GetCompilerOptions())) {
Vladimir Marko2ab1bdd2018-07-12 09:59:56 +01001422 return true;
1423 }
David Brazdildee58d62016-04-07 09:54:26 +00001424
1425 return false;
1426}
1427
1428HClinitCheck* HInstructionBuilder::ProcessClinitCheckForInvoke(
Vladimir Markofca0b492018-07-23 15:30:52 +01001429 uint32_t dex_pc,
1430 ArtMethod* resolved_method,
1431 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
Vladimir Marko2f40d242020-04-08 12:56:45 +01001432 ScopedObjectAccess soa(Thread::Current());
1433 ObjPtr<mirror::Class> klass = resolved_method->GetDeclaringClass();
David Brazdildee58d62016-04-07 09:54:26 +00001434
1435 HClinitCheck* clinit_check = nullptr;
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001436 if (IsInitialized(klass)) {
David Brazdildee58d62016-04-07 09:54:26 +00001437 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001438 } else {
Vladimir Marko02ca05a2020-05-12 13:58:51 +01001439 Handle<mirror::Class> h_klass = graph_->GetHandleCache()->NewHandle(klass);
Vladimir Marko2f40d242020-04-08 12:56:45 +01001440 HLoadClass* cls = BuildLoadClass(h_klass->GetDexTypeIndex(),
1441 h_klass->GetDexFile(),
1442 h_klass,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001443 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08001444 /* needs_access_check= */ false);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001445 if (cls != nullptr) {
1446 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
Vladimir Markoca6fff82017-10-03 14:49:14 +01001447 clinit_check = new (allocator_) HClinitCheck(cls, dex_pc);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001448 AppendInstruction(clinit_check);
Vladimir Marko2f40d242020-04-08 12:56:45 +01001449 } else {
1450 // Let the invoke handle this with an implicit class initialization check.
1451 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001452 }
David Brazdildee58d62016-04-07 09:54:26 +00001453 }
1454 return clinit_check;
1455}
1456
Vladimir Marko5f846072020-04-09 13:20:11 +01001457bool HInstructionBuilder::SetupInvokeArguments(HInstruction* invoke,
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001458 const InstructionOperands& operands,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001459 const char* shorty,
Vladimir Marko5f846072020-04-09 13:20:11 +01001460 ReceiverArg receiver_arg) {
1461 // Note: The `invoke` can be an intrinsic replacement, so not necessaritly HInvoke.
1462 // 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 +01001463 uint32_t shorty_index = 1; // Skip the return type.
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001464 const size_t number_of_operands = operands.GetNumberOfOperands();
Vladimir Marko5f846072020-04-09 13:20:11 +01001465 bool argument_length_error = false;
1466
1467 size_t start_index = 0u;
1468 size_t argument_index = 0u;
1469 if (receiver_arg != ReceiverArg::kNone) {
1470 if (number_of_operands == 0u) {
1471 argument_length_error = true;
1472 } else {
1473 start_index = 1u;
1474 if (receiver_arg != ReceiverArg::kIgnored) {
1475 uint32_t obj_reg = operands.GetOperand(0u);
1476 HInstruction* arg = (receiver_arg == ReceiverArg::kPlainArg)
1477 ? LoadLocal(obj_reg, DataType::Type::kReference)
1478 : LoadNullCheckedLocal(obj_reg, invoke->GetDexPc());
1479 if (receiver_arg != ReceiverArg::kNullCheckedOnly) {
1480 invoke->SetRawInputAt(0u, arg);
1481 argument_index = 1u;
1482 }
1483 }
1484 }
1485 }
1486
1487 for (size_t i = start_index; i < number_of_operands; ++i, ++argument_index) {
1488 // Make sure we don't go over the expected arguments or over the number of
1489 // dex registers given. If the instruction was seen as dead by the verifier,
1490 // it hasn't been properly checked.
1491 if (UNLIKELY(shorty[shorty_index] == 0)) {
1492 argument_length_error = true;
1493 break;
1494 }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001495 DataType::Type type = DataType::FromShorty(shorty[shorty_index++]);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001496 bool is_wide = (type == DataType::Type::kInt64) || (type == DataType::Type::kFloat64);
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001497 if (is_wide && ((i + 1 == number_of_operands) ||
1498 (operands.GetOperand(i) + 1 != operands.GetOperand(i + 1)))) {
Vladimir Marko5f846072020-04-09 13:20:11 +01001499 if (invoke->IsInvoke()) {
1500 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
1501 // reject any class where this is violated. However, the verifier only does these checks
1502 // on non trivially dead instructions, so we just bailout the compilation.
1503 VLOG(compiler) << "Did not compile "
1504 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
1505 << " because of non-sequential dex register pair in wide argument";
1506 MaybeRecordStat(compilation_stats_,
1507 MethodCompilationStat::kNotCompiledMalformedOpcode);
1508 }
David Brazdildee58d62016-04-07 09:54:26 +00001509 return false;
1510 }
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001511 HInstruction* arg = LoadLocal(operands.GetOperand(i), type);
Vladimir Marko5f846072020-04-09 13:20:11 +01001512 DCHECK(invoke->InputAt(argument_index) == nullptr);
1513 invoke->SetRawInputAt(argument_index, arg);
David Brazdildee58d62016-04-07 09:54:26 +00001514 if (is_wide) {
Vladimir Marko5f846072020-04-09 13:20:11 +01001515 ++i;
David Brazdildee58d62016-04-07 09:54:26 +00001516 }
1517 }
1518
Vladimir Marko5f846072020-04-09 13:20:11 +01001519 argument_length_error = argument_length_error || shorty[shorty_index] != 0;
1520 if (argument_length_error) {
1521 if (invoke->IsInvoke()) {
1522 VLOG(compiler) << "Did not compile "
1523 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
1524 << " because of wrong number of arguments in invoke instruction";
1525 MaybeRecordStat(compilation_stats_,
1526 MethodCompilationStat::kNotCompiledMalformedOpcode);
1527 }
David Brazdildee58d62016-04-07 09:54:26 +00001528 return false;
1529 }
1530
1531 if (invoke->IsInvokeStaticOrDirect() &&
1532 HInvokeStaticOrDirect::NeedsCurrentMethodInput(
1533 invoke->AsInvokeStaticOrDirect()->GetMethodLoadKind())) {
Vladimir Marko5f846072020-04-09 13:20:11 +01001534 DCHECK_EQ(argument_index, invoke->AsInvokeStaticOrDirect()->GetSpecialInputIndex());
1535 DCHECK(invoke->InputAt(argument_index) == nullptr);
1536 invoke->SetRawInputAt(argument_index, graph_->GetCurrentMethod());
David Brazdildee58d62016-04-07 09:54:26 +00001537 }
1538
1539 return true;
1540}
1541
1542bool HInstructionBuilder::HandleInvoke(HInvoke* invoke,
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001543 const InstructionOperands& operands,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001544 const char* shorty,
Vladimir Marko5f846072020-04-09 13:20:11 +01001545 bool is_unresolved) {
David Brazdildee58d62016-04-07 09:54:26 +00001546 DCHECK(!invoke->IsInvokeStaticOrDirect() || !invoke->AsInvokeStaticOrDirect()->IsStringInit());
1547
Vladimir Marko5f846072020-04-09 13:20:11 +01001548 ReceiverArg receiver_arg = (invoke->GetInvokeType() == InvokeType::kStatic)
1549 ? ReceiverArg::kNone
1550 : (is_unresolved ? ReceiverArg::kPlainArg : ReceiverArg::kNullCheckedArg);
1551 if (!SetupInvokeArguments(invoke, operands, shorty, receiver_arg)) {
David Brazdildee58d62016-04-07 09:54:26 +00001552 return false;
1553 }
1554
David Brazdildee58d62016-04-07 09:54:26 +00001555 AppendInstruction(invoke);
1556 latest_result_ = invoke;
1557
1558 return true;
1559}
1560
Vladimir Marko5f846072020-04-09 13:20:11 +01001561bool HInstructionBuilder::BuildSimpleIntrinsic(ArtMethod* method,
1562 uint32_t dex_pc,
1563 const InstructionOperands& operands,
1564 const char* shorty) {
1565 Intrinsics intrinsic = static_cast<Intrinsics>(method->GetIntrinsic());
1566 DCHECK_NE(intrinsic, Intrinsics::kNone);
1567 constexpr DataType::Type kInt32 = DataType::Type::kInt32;
1568 constexpr DataType::Type kInt64 = DataType::Type::kInt64;
1569 constexpr DataType::Type kFloat32 = DataType::Type::kFloat32;
1570 constexpr DataType::Type kFloat64 = DataType::Type::kFloat64;
1571 ReceiverArg receiver_arg = method->IsStatic() ? ReceiverArg::kNone : ReceiverArg::kNullCheckedArg;
1572 HInstruction* instruction = nullptr;
1573 switch (intrinsic) {
1574 case Intrinsics::kIntegerRotateRight:
1575 case Intrinsics::kIntegerRotateLeft:
1576 // For rotate left, we negate the distance below.
1577 instruction = new (allocator_) HRor(kInt32, /*value=*/ nullptr, /*distance=*/ nullptr);
1578 break;
1579 case Intrinsics::kLongRotateRight:
1580 case Intrinsics::kLongRotateLeft:
1581 // For rotate left, we negate the distance below.
1582 instruction = new (allocator_) HRor(kInt64, /*value=*/ nullptr, /*distance=*/ nullptr);
1583 break;
1584 case Intrinsics::kIntegerCompare:
1585 instruction = new (allocator_) HCompare(
1586 kInt32, /*first=*/ nullptr, /*second=*/ nullptr, ComparisonBias::kNoBias, dex_pc);
1587 break;
1588 case Intrinsics::kLongCompare:
1589 instruction = new (allocator_) HCompare(
1590 kInt64, /*first=*/ nullptr, /*second=*/ nullptr, ComparisonBias::kNoBias, dex_pc);
1591 break;
1592 case Intrinsics::kIntegerSignum:
1593 instruction = new (allocator_) HCompare(
1594 kInt32, /*first=*/ nullptr, graph_->GetIntConstant(0), ComparisonBias::kNoBias, dex_pc);
1595 break;
1596 case Intrinsics::kLongSignum:
1597 instruction = new (allocator_) HCompare(
1598 kInt64, /*first=*/ nullptr, graph_->GetLongConstant(0), ComparisonBias::kNoBias, dex_pc);
1599 break;
1600 case Intrinsics::kFloatIsNaN:
1601 case Intrinsics::kDoubleIsNaN: {
1602 // IsNaN(x) is the same as x != x.
1603 instruction = new (allocator_) HNotEqual(/*first=*/ nullptr, /*second=*/ nullptr, dex_pc);
1604 instruction->AsCondition()->SetBias(ComparisonBias::kLtBias);
1605 break;
1606 }
1607 case Intrinsics::kStringCharAt:
1608 // We treat String as an array to allow DCE and BCE to seamlessly work on strings.
1609 instruction = new (allocator_) HArrayGet(/*array=*/ nullptr,
1610 /*index=*/ nullptr,
1611 DataType::Type::kUint16,
1612 SideEffects::None(), // Strings are immutable.
1613 dex_pc,
1614 /*is_string_char_at=*/ true);
1615 break;
1616 case Intrinsics::kStringIsEmpty:
1617 case Intrinsics::kStringLength:
1618 // We treat String as an array to allow DCE and BCE to seamlessly work on strings.
1619 // For String.isEmpty(), we add a comparison with 0 below.
1620 instruction =
1621 new (allocator_) HArrayLength(/*array=*/ nullptr, dex_pc, /* is_string_length= */ true);
1622 break;
1623 case Intrinsics::kUnsafeLoadFence:
1624 receiver_arg = ReceiverArg::kNullCheckedOnly;
1625 instruction = new (allocator_) HMemoryBarrier(MemBarrierKind::kLoadAny, dex_pc);
1626 break;
1627 case Intrinsics::kUnsafeStoreFence:
1628 receiver_arg = ReceiverArg::kNullCheckedOnly;
1629 instruction = new (allocator_) HMemoryBarrier(MemBarrierKind::kAnyStore, dex_pc);
1630 break;
1631 case Intrinsics::kUnsafeFullFence:
1632 receiver_arg = ReceiverArg::kNullCheckedOnly;
1633 instruction = new (allocator_) HMemoryBarrier(MemBarrierKind::kAnyAny, dex_pc);
1634 break;
1635 case Intrinsics::kVarHandleFullFence:
1636 instruction = new (allocator_) HMemoryBarrier(MemBarrierKind::kAnyAny, dex_pc);
1637 break;
1638 case Intrinsics::kVarHandleAcquireFence:
1639 instruction = new (allocator_) HMemoryBarrier(MemBarrierKind::kLoadAny, dex_pc);
1640 break;
1641 case Intrinsics::kVarHandleReleaseFence:
1642 instruction = new (allocator_) HMemoryBarrier(MemBarrierKind::kAnyStore, dex_pc);
1643 break;
1644 case Intrinsics::kVarHandleLoadLoadFence:
1645 instruction = new (allocator_) HMemoryBarrier(MemBarrierKind::kLoadAny, dex_pc);
1646 break;
1647 case Intrinsics::kVarHandleStoreStoreFence:
1648 instruction = new (allocator_) HMemoryBarrier(MemBarrierKind::kStoreStore, dex_pc);
1649 break;
1650 case Intrinsics::kMathMinIntInt:
1651 instruction = new (allocator_) HMin(kInt32, /*left=*/ nullptr, /*right=*/ nullptr, dex_pc);
1652 break;
1653 case Intrinsics::kMathMinLongLong:
1654 instruction = new (allocator_) HMin(kInt64, /*left=*/ nullptr, /*right=*/ nullptr, dex_pc);
1655 break;
1656 case Intrinsics::kMathMinFloatFloat:
1657 instruction = new (allocator_) HMin(kFloat32, /*left=*/ nullptr, /*right=*/ nullptr, dex_pc);
1658 break;
1659 case Intrinsics::kMathMinDoubleDouble:
1660 instruction = new (allocator_) HMin(kFloat64, /*left=*/ nullptr, /*right=*/ nullptr, dex_pc);
1661 break;
1662 case Intrinsics::kMathMaxIntInt:
1663 instruction = new (allocator_) HMax(kInt32, /*left=*/ nullptr, /*right=*/ nullptr, dex_pc);
1664 break;
1665 case Intrinsics::kMathMaxLongLong:
1666 instruction = new (allocator_) HMax(kInt64, /*left=*/ nullptr, /*right=*/ nullptr, dex_pc);
1667 break;
1668 case Intrinsics::kMathMaxFloatFloat:
1669 instruction = new (allocator_) HMax(kFloat32, /*left=*/ nullptr, /*right=*/ nullptr, dex_pc);
1670 break;
1671 case Intrinsics::kMathMaxDoubleDouble:
1672 instruction = new (allocator_) HMax(kFloat64, /*left=*/ nullptr, /*right=*/ nullptr, dex_pc);
1673 break;
1674 case Intrinsics::kMathAbsInt:
1675 instruction = new (allocator_) HAbs(kInt32, /*input=*/ nullptr, dex_pc);
1676 break;
1677 case Intrinsics::kMathAbsLong:
1678 instruction = new (allocator_) HAbs(kInt64, /*input=*/ nullptr, dex_pc);
1679 break;
1680 case Intrinsics::kMathAbsFloat:
1681 instruction = new (allocator_) HAbs(kFloat32, /*input=*/ nullptr, dex_pc);
1682 break;
1683 case Intrinsics::kMathAbsDouble:
1684 instruction = new (allocator_) HAbs(kFloat64, /*input=*/ nullptr, dex_pc);
1685 break;
1686 default:
1687 // We do not have intermediate representation for other intrinsics.
1688 return false;
1689 }
1690 DCHECK(instruction != nullptr);
1691 if (!SetupInvokeArguments(instruction, operands, shorty, receiver_arg)) {
1692 return false;
1693 }
1694
1695 switch (intrinsic) {
1696 case Intrinsics::kIntegerRotateLeft:
1697 case Intrinsics::kLongRotateLeft: {
1698 // Negate the distance value for rotate left.
1699 DCHECK(instruction->IsRor());
1700 HNeg* neg = new (allocator_) HNeg(kInt32, instruction->InputAt(1u));
1701 AppendInstruction(neg);
1702 instruction->SetRawInputAt(1u, neg);
1703 break;
1704 }
1705 case Intrinsics::kFloatIsNaN:
1706 case Intrinsics::kDoubleIsNaN:
1707 // Set the second input to be the same as first.
1708 DCHECK(instruction->IsNotEqual());
1709 DCHECK(instruction->InputAt(1u) == nullptr);
1710 instruction->SetRawInputAt(1u, instruction->InputAt(0u));
1711 break;
1712 case Intrinsics::kStringCharAt: {
1713 // Add bounds check.
1714 HInstruction* array = instruction->InputAt(0u);
1715 HInstruction* index = instruction->InputAt(1u);
1716 HInstruction* length =
1717 new (allocator_) HArrayLength(array, dex_pc, /*is_string_length=*/ true);
1718 AppendInstruction(length);
1719 HBoundsCheck* bounds_check =
1720 new (allocator_) HBoundsCheck(index, length, dex_pc, /*is_string_char_at=*/ true);
1721 AppendInstruction(bounds_check);
1722 graph_->SetHasBoundsChecks(true);
1723 instruction->SetRawInputAt(1u, bounds_check);
1724 break;
1725 }
1726 case Intrinsics::kStringIsEmpty: {
1727 // Compare the length with 0.
1728 DCHECK(instruction->IsArrayLength());
1729 AppendInstruction(instruction);
1730 HEqual* equal = new (allocator_) HEqual(instruction, graph_->GetIntConstant(0), dex_pc);
1731 instruction = equal;
1732 break;
1733 }
1734 default:
1735 break;
1736 }
1737
1738 AppendInstruction(instruction);
1739 latest_result_ = instruction;
1740
1741 return true;
1742}
1743
David Brazdildee58d62016-04-07 09:54:26 +00001744bool HInstructionBuilder::HandleStringInit(HInvoke* invoke,
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001745 const InstructionOperands& operands,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001746 const char* shorty) {
David Brazdildee58d62016-04-07 09:54:26 +00001747 DCHECK(invoke->IsInvokeStaticOrDirect());
1748 DCHECK(invoke->AsInvokeStaticOrDirect()->IsStringInit());
1749
Vladimir Marko5f846072020-04-09 13:20:11 +01001750 if (!SetupInvokeArguments(invoke, operands, shorty, ReceiverArg::kIgnored)) {
David Brazdildee58d62016-04-07 09:54:26 +00001751 return false;
1752 }
1753
1754 AppendInstruction(invoke);
1755
1756 // This is a StringFactory call, not an actual String constructor. Its result
1757 // replaces the empty String pre-allocated by NewInstance.
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001758 uint32_t orig_this_reg = operands.GetOperand(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001759 HInstruction* arg_this = LoadLocal(orig_this_reg, DataType::Type::kReference);
David Brazdildee58d62016-04-07 09:54:26 +00001760
1761 // Replacing the NewInstance might render it redundant. Keep a list of these
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +01001762 // to be visited once it is clear whether it has remaining uses.
David Brazdildee58d62016-04-07 09:54:26 +00001763 if (arg_this->IsNewInstance()) {
1764 ssa_builder_->AddUninitializedString(arg_this->AsNewInstance());
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +01001765 } else {
1766 DCHECK(arg_this->IsPhi());
1767 // We can get a phi as input of a String.<init> if there is a loop between the
1768 // allocation and the String.<init> call. As we don't know which other phis might alias
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +01001769 // with `arg_this`, we keep a record of those invocations so we can later replace
1770 // the allocation with the invocation.
1771 // Add the actual 'this' input so the analysis knows what is the allocation instruction.
1772 // The input will be removed during the analysis.
1773 invoke->AddInput(arg_this);
1774 ssa_builder_->AddUninitializedStringPhi(invoke);
1775 }
1776 // Walk over all vregs and replace any occurrence of `arg_this` with `invoke`.
1777 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
1778 if ((*current_locals_)[vreg] == arg_this) {
1779 (*current_locals_)[vreg] = invoke;
1780 }
David Brazdildee58d62016-04-07 09:54:26 +00001781 }
David Brazdildee58d62016-04-07 09:54:26 +00001782 return true;
1783}
1784
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001785static DataType::Type GetFieldAccessType(const DexFile& dex_file, uint16_t field_index) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001786 const dex::FieldId& field_id = dex_file.GetFieldId(field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001787 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001788 return DataType::FromShorty(type[0]);
David Brazdildee58d62016-04-07 09:54:26 +00001789}
1790
1791bool HInstructionBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
1792 uint32_t dex_pc,
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001793 bool is_put,
1794 size_t quicken_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001795 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1796 uint32_t obj_reg = instruction.VRegB_22c();
1797 uint16_t field_index;
1798 if (instruction.IsQuickened()) {
1799 if (!CanDecodeQuickenedInfo()) {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00001800 VLOG(compiler) << "Not compiled: Could not decode quickened instruction "
1801 << instruction.Opcode();
David Brazdildee58d62016-04-07 09:54:26 +00001802 return false;
1803 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001804 field_index = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00001805 } else {
1806 field_index = instruction.VRegC_22c();
1807 }
1808
1809 ScopedObjectAccess soa(Thread::Current());
Andreas Gampe3db70682018-12-26 15:12:03 -08001810 ArtField* resolved_field = ResolveField(field_index, /* is_static= */ false, is_put);
David Brazdildee58d62016-04-07 09:54:26 +00001811
Aart Bik14154132016-06-02 17:53:58 -07001812 // Generate an explicit null check on the reference, unless the field access
1813 // is unresolved. In that case, we rely on the runtime to perform various
1814 // checks first, followed by a null check.
1815 HInstruction* object = (resolved_field == nullptr)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001816 ? LoadLocal(obj_reg, DataType::Type::kReference)
Aart Bik14154132016-06-02 17:53:58 -07001817 : LoadNullCheckedLocal(obj_reg, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001818
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001819 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001820 if (is_put) {
1821 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1822 HInstruction* field_set = nullptr;
1823 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001824 MaybeRecordStat(compilation_stats_,
1825 MethodCompilationStat::kUnresolvedField);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001826 field_set = new (allocator_) HUnresolvedInstanceFieldSet(object,
1827 value,
1828 field_type,
1829 field_index,
1830 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001831 } else {
1832 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Vladimir Markoca6fff82017-10-03 14:49:14 +01001833 field_set = new (allocator_) HInstanceFieldSet(object,
1834 value,
1835 resolved_field,
1836 field_type,
1837 resolved_field->GetOffset(),
1838 resolved_field->IsVolatile(),
1839 field_index,
1840 class_def_index,
1841 *dex_file_,
1842 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001843 }
1844 AppendInstruction(field_set);
1845 } else {
1846 HInstruction* field_get = nullptr;
1847 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001848 MaybeRecordStat(compilation_stats_,
1849 MethodCompilationStat::kUnresolvedField);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001850 field_get = new (allocator_) HUnresolvedInstanceFieldGet(object,
1851 field_type,
1852 field_index,
1853 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001854 } else {
1855 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Vladimir Markoca6fff82017-10-03 14:49:14 +01001856 field_get = new (allocator_) HInstanceFieldGet(object,
1857 resolved_field,
1858 field_type,
1859 resolved_field->GetOffset(),
1860 resolved_field->IsVolatile(),
1861 field_index,
1862 class_def_index,
1863 *dex_file_,
1864 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001865 }
1866 AppendInstruction(field_get);
1867 UpdateLocal(source_or_dest_reg, field_get);
1868 }
1869
1870 return true;
1871}
1872
David Brazdildee58d62016-04-07 09:54:26 +00001873void HInstructionBuilder::BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001874 uint32_t dex_pc,
1875 bool is_put,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001876 DataType::Type field_type) {
David Brazdildee58d62016-04-07 09:54:26 +00001877 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1878 uint16_t field_index = instruction.VRegB_21c();
1879
1880 if (is_put) {
1881 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1882 AppendInstruction(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001883 new (allocator_) HUnresolvedStaticFieldSet(value, field_type, field_index, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001884 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001885 AppendInstruction(new (allocator_) HUnresolvedStaticFieldGet(field_type, field_index, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001886 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1887 }
1888}
1889
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001890ArtField* HInstructionBuilder::ResolveField(uint16_t field_idx, bool is_static, bool is_put) {
1891 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001892
1893 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001894 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001895
Vladimir Markoe11dd502017-12-08 14:09:45 +00001896 ArtField* resolved_field = class_linker->ResolveField(field_idx,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001897 dex_compilation_unit_->GetDexCache(),
1898 class_loader,
1899 is_static);
Nicolas Geoffrayf3688822020-03-25 15:04:03 +00001900 DCHECK_EQ(resolved_field == nullptr, soa.Self()->IsExceptionPending())
1901 << "field="
1902 << ((resolved_field == nullptr) ? "null" : resolved_field->PrettyField())
1903 << ", exception="
1904 << (soa.Self()->IsExceptionPending() ? soa.Self()->GetException()->Dump() : "null");
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001905 if (UNLIKELY(resolved_field == nullptr)) {
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001906 // Clean up any exception left by field resolution.
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001907 soa.Self()->ClearException();
1908 return nullptr;
1909 }
1910
1911 // Check static/instance. The class linker has a fast path for looking into the dex cache
1912 // and does not check static/instance if it hits it.
1913 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
1914 return nullptr;
1915 }
1916
1917 // Check access.
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001918 Handle<mirror::Class> compiling_class = dex_compilation_unit_->GetCompilingClass();
Andreas Gampefa4333d2017-02-14 11:10:34 -08001919 if (compiling_class == nullptr) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001920 if (!resolved_field->IsPublic()) {
1921 return nullptr;
1922 }
1923 } else if (!compiling_class->CanAccessResolvedField(resolved_field->GetDeclaringClass(),
1924 resolved_field,
1925 dex_compilation_unit_->GetDexCache().Get(),
1926 field_idx)) {
1927 return nullptr;
1928 }
1929
1930 if (is_put &&
1931 resolved_field->IsFinal() &&
1932 (compiling_class.Get() != resolved_field->GetDeclaringClass())) {
1933 // Final fields can only be updated within their own class.
1934 // TODO: Only allow it in constructors. b/34966607.
1935 return nullptr;
1936 }
1937
Nicolas Geoffray396198b2020-06-16 12:02:45 +01001938 StackArtFieldHandleScope<1> rhs(soa.Self());
1939 ReflectiveHandle<ArtField> resolved_field_handle(rhs.NewHandle(resolved_field));
1940 if (resolved_field->ResolveType().IsNull()) {
1941 // ArtField::ResolveType() may fail as evidenced with a dexing bug (b/78788577).
1942 soa.Self()->ClearException();
1943 return nullptr; // Failure
1944 }
1945 return resolved_field_handle.Get();
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001946}
1947
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00001948void HInstructionBuilder::BuildStaticFieldAccess(const Instruction& instruction,
David Brazdildee58d62016-04-07 09:54:26 +00001949 uint32_t dex_pc,
1950 bool is_put) {
1951 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1952 uint16_t field_index = instruction.VRegB_21c();
1953
1954 ScopedObjectAccess soa(Thread::Current());
Andreas Gampe3db70682018-12-26 15:12:03 -08001955 ArtField* resolved_field = ResolveField(field_index, /* is_static= */ true, is_put);
David Brazdildee58d62016-04-07 09:54:26 +00001956
1957 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001958 MaybeRecordStat(compilation_stats_,
1959 MethodCompilationStat::kUnresolvedField);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001960 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001961 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00001962 return;
David Brazdildee58d62016-04-07 09:54:26 +00001963 }
1964
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001965 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001966
Vladimir Marko02ca05a2020-05-12 13:58:51 +01001967 Handle<mirror::Class> klass =
1968 graph_->GetHandleCache()->NewHandle(resolved_field->GetDeclaringClass());
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001969 HLoadClass* constant = BuildLoadClass(klass->GetDexTypeIndex(),
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001970 klass->GetDexFile(),
1971 klass,
1972 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08001973 /* needs_access_check= */ false);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001974
1975 if (constant == nullptr) {
1976 // The class cannot be referenced from this compiled code. Generate
1977 // an unresolved access.
Igor Murashkin1e065a52017-08-09 13:20:34 -07001978 MaybeRecordStat(compilation_stats_,
1979 MethodCompilationStat::kUnresolvedFieldNotAFastAccess);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001980 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00001981 return;
David Brazdildee58d62016-04-07 09:54:26 +00001982 }
1983
David Brazdildee58d62016-04-07 09:54:26 +00001984 HInstruction* cls = constant;
Vladimir Marko2f40d242020-04-08 12:56:45 +01001985 if (!IsInitialized(klass.Get())) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001986 cls = new (allocator_) HClinitCheck(constant, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001987 AppendInstruction(cls);
1988 }
1989
1990 uint16_t class_def_index = klass->GetDexClassDefIndex();
1991 if (is_put) {
1992 // We need to keep the class alive before loading the value.
1993 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1994 DCHECK_EQ(HPhi::ToPhiType(value->GetType()), HPhi::ToPhiType(field_type));
Vladimir Markoca6fff82017-10-03 14:49:14 +01001995 AppendInstruction(new (allocator_) HStaticFieldSet(cls,
1996 value,
1997 resolved_field,
1998 field_type,
1999 resolved_field->GetOffset(),
2000 resolved_field->IsVolatile(),
2001 field_index,
2002 class_def_index,
2003 *dex_file_,
2004 dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002005 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002006 AppendInstruction(new (allocator_) HStaticFieldGet(cls,
2007 resolved_field,
2008 field_type,
2009 resolved_field->GetOffset(),
2010 resolved_field->IsVolatile(),
2011 field_index,
2012 class_def_index,
2013 *dex_file_,
2014 dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002015 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
2016 }
David Brazdildee58d62016-04-07 09:54:26 +00002017}
2018
2019void HInstructionBuilder::BuildCheckedDivRem(uint16_t out_vreg,
Vladimir Markoca6fff82017-10-03 14:49:14 +01002020 uint16_t first_vreg,
2021 int64_t second_vreg_or_constant,
2022 uint32_t dex_pc,
2023 DataType::Type type,
2024 bool second_is_constant,
2025 bool isDiv) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002026 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
David Brazdildee58d62016-04-07 09:54:26 +00002027
2028 HInstruction* first = LoadLocal(first_vreg, type);
2029 HInstruction* second = nullptr;
2030 if (second_is_constant) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002031 if (type == DataType::Type::kInt32) {
David Brazdildee58d62016-04-07 09:54:26 +00002032 second = graph_->GetIntConstant(second_vreg_or_constant, dex_pc);
2033 } else {
2034 second = graph_->GetLongConstant(second_vreg_or_constant, dex_pc);
2035 }
2036 } else {
2037 second = LoadLocal(second_vreg_or_constant, type);
2038 }
2039
2040 if (!second_is_constant
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002041 || (type == DataType::Type::kInt32 && second->AsIntConstant()->GetValue() == 0)
2042 || (type == DataType::Type::kInt64 && second->AsLongConstant()->GetValue() == 0)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002043 second = new (allocator_) HDivZeroCheck(second, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002044 AppendInstruction(second);
2045 }
2046
2047 if (isDiv) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002048 AppendInstruction(new (allocator_) HDiv(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002049 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002050 AppendInstruction(new (allocator_) HRem(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002051 }
2052 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
2053}
2054
2055void HInstructionBuilder::BuildArrayAccess(const Instruction& instruction,
2056 uint32_t dex_pc,
2057 bool is_put,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002058 DataType::Type anticipated_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002059 uint8_t source_or_dest_reg = instruction.VRegA_23x();
2060 uint8_t array_reg = instruction.VRegB_23x();
2061 uint8_t index_reg = instruction.VRegC_23x();
2062
David Brazdilc120bbe2016-04-22 16:57:00 +01002063 HInstruction* object = LoadNullCheckedLocal(array_reg, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002064 HInstruction* length = new (allocator_) HArrayLength(object, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002065 AppendInstruction(length);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002066 HInstruction* index = LoadLocal(index_reg, DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002067 index = new (allocator_) HBoundsCheck(index, length, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002068 AppendInstruction(index);
2069 if (is_put) {
2070 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
2071 // TODO: Insert a type check node if the type is Object.
Vladimir Markoca6fff82017-10-03 14:49:14 +01002072 HArraySet* aset = new (allocator_) HArraySet(object, index, value, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002073 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
2074 AppendInstruction(aset);
2075 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002076 HArrayGet* aget = new (allocator_) HArrayGet(object, index, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002077 ssa_builder_->MaybeAddAmbiguousArrayGet(aget);
2078 AppendInstruction(aget);
2079 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
2080 }
2081 graph_->SetHasBoundsChecks(true);
2082}
2083
Vladimir Markob5461632018-10-15 14:24:21 +01002084HNewArray* HInstructionBuilder::BuildNewArray(uint32_t dex_pc,
2085 dex::TypeIndex type_index,
2086 HInstruction* length) {
2087 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
2088
2089 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(type_index));
2090 DCHECK_EQ(descriptor[0], '[');
2091 size_t component_type_shift = Primitive::ComponentSizeShift(Primitive::GetType(descriptor[1]));
2092
2093 HNewArray* new_array = new (allocator_) HNewArray(cls, length, dex_pc, component_type_shift);
2094 AppendInstruction(new_array);
2095 return new_array;
2096}
2097
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002098HNewArray* HInstructionBuilder::BuildFilledNewArray(uint32_t dex_pc,
2099 dex::TypeIndex type_index,
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002100 const InstructionOperands& operands) {
2101 const size_t number_of_operands = operands.GetNumberOfOperands();
2102 HInstruction* length = graph_->GetIntConstant(number_of_operands, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002103
Vladimir Markob5461632018-10-15 14:24:21 +01002104 HNewArray* new_array = BuildNewArray(dex_pc, type_index, length);
David Brazdildee58d62016-04-07 09:54:26 +00002105 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
2106 DCHECK_EQ(descriptor[0], '[') << descriptor;
2107 char primitive = descriptor[1];
2108 DCHECK(primitive == 'I'
2109 || primitive == 'L'
2110 || primitive == '[') << descriptor;
2111 bool is_reference_array = (primitive == 'L') || (primitive == '[');
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002112 DataType::Type type = is_reference_array ? DataType::Type::kReference : DataType::Type::kInt32;
David Brazdildee58d62016-04-07 09:54:26 +00002113
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002114 for (size_t i = 0; i < number_of_operands; ++i) {
2115 HInstruction* value = LoadLocal(operands.GetOperand(i), type);
David Brazdildee58d62016-04-07 09:54:26 +00002116 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
Vladimir Markob5461632018-10-15 14:24:21 +01002117 HArraySet* aset = new (allocator_) HArraySet(new_array, index, value, type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002118 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
2119 AppendInstruction(aset);
2120 }
Vladimir Markob5461632018-10-15 14:24:21 +01002121 latest_result_ = new_array;
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002122
Vladimir Markob5461632018-10-15 14:24:21 +01002123 return new_array;
David Brazdildee58d62016-04-07 09:54:26 +00002124}
2125
2126template <typename T>
2127void HInstructionBuilder::BuildFillArrayData(HInstruction* object,
2128 const T* data,
2129 uint32_t element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002130 DataType::Type anticipated_type,
David Brazdildee58d62016-04-07 09:54:26 +00002131 uint32_t dex_pc) {
2132 for (uint32_t i = 0; i < element_count; ++i) {
2133 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
2134 HInstruction* value = graph_->GetIntConstant(data[i], dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002135 HArraySet* aset = new (allocator_) HArraySet(object, index, value, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002136 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
2137 AppendInstruction(aset);
2138 }
2139}
2140
2141void HInstructionBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
David Brazdilc120bbe2016-04-22 16:57:00 +01002142 HInstruction* array = LoadNullCheckedLocal(instruction.VRegA_31t(), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002143
2144 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
2145 const Instruction::ArrayDataPayload* payload =
Mathieu Chartier808c7a52017-12-15 11:19:33 -08002146 reinterpret_cast<const Instruction::ArrayDataPayload*>(
2147 code_item_accessor_.Insns() + payload_offset);
David Brazdildee58d62016-04-07 09:54:26 +00002148 const uint8_t* data = payload->data;
2149 uint32_t element_count = payload->element_count;
2150
Vladimir Markoc69fba22016-09-06 16:49:15 +01002151 if (element_count == 0u) {
2152 // For empty payload we emit only the null check above.
2153 return;
2154 }
2155
Vladimir Markoca6fff82017-10-03 14:49:14 +01002156 HInstruction* length = new (allocator_) HArrayLength(array, dex_pc);
Vladimir Markoc69fba22016-09-06 16:49:15 +01002157 AppendInstruction(length);
2158
David Brazdildee58d62016-04-07 09:54:26 +00002159 // Implementation of this DEX instruction seems to be that the bounds check is
2160 // done before doing any stores.
2161 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002162 AppendInstruction(new (allocator_) HBoundsCheck(last_index, length, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002163
2164 switch (payload->element_width) {
2165 case 1:
David Brazdilc120bbe2016-04-22 16:57:00 +01002166 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00002167 reinterpret_cast<const int8_t*>(data),
2168 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002169 DataType::Type::kInt8,
David Brazdildee58d62016-04-07 09:54:26 +00002170 dex_pc);
2171 break;
2172 case 2:
David Brazdilc120bbe2016-04-22 16:57:00 +01002173 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00002174 reinterpret_cast<const int16_t*>(data),
2175 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002176 DataType::Type::kInt16,
David Brazdildee58d62016-04-07 09:54:26 +00002177 dex_pc);
2178 break;
2179 case 4:
David Brazdilc120bbe2016-04-22 16:57:00 +01002180 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00002181 reinterpret_cast<const int32_t*>(data),
2182 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002183 DataType::Type::kInt32,
David Brazdildee58d62016-04-07 09:54:26 +00002184 dex_pc);
2185 break;
2186 case 8:
David Brazdilc120bbe2016-04-22 16:57:00 +01002187 BuildFillWideArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00002188 reinterpret_cast<const int64_t*>(data),
2189 element_count,
2190 dex_pc);
2191 break;
2192 default:
2193 LOG(FATAL) << "Unknown element width for " << payload->element_width;
2194 }
2195 graph_->SetHasBoundsChecks(true);
2196}
2197
2198void HInstructionBuilder::BuildFillWideArrayData(HInstruction* object,
2199 const int64_t* data,
2200 uint32_t element_count,
2201 uint32_t dex_pc) {
2202 for (uint32_t i = 0; i < element_count; ++i) {
2203 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
2204 HInstruction* value = graph_->GetLongConstant(data[i], dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002205 HArraySet* aset =
2206 new (allocator_) HArraySet(object, index, value, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002207 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
2208 AppendInstruction(aset);
2209 }
2210}
2211
Vladimir Marko28e012a2017-12-07 11:22:59 +00002212void HInstructionBuilder::BuildLoadString(dex::StringIndex string_index, uint32_t dex_pc) {
2213 HLoadString* load_string =
2214 new (allocator_) HLoadString(graph_->GetCurrentMethod(), string_index, *dex_file_, dex_pc);
2215 HSharpening::ProcessLoadString(load_string,
2216 code_generator_,
Vladimir Marko28e012a2017-12-07 11:22:59 +00002217 *dex_compilation_unit_,
Vladimir Marko02ca05a2020-05-12 13:58:51 +01002218 graph_->GetHandleCache()->GetHandles());
Vladimir Marko28e012a2017-12-07 11:22:59 +00002219 AppendInstruction(load_string);
2220}
2221
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002222HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index, uint32_t dex_pc) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +00002223 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002224 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
Vladimir Marko175e7862018-03-27 09:03:13 +00002225 Handle<mirror::Class> klass = ResolveClass(soa, type_index);
Vladimir Marko2f40d242020-04-08 12:56:45 +01002226 bool needs_access_check = LoadClassNeedsAccessCheck(klass.Get());
Vladimir Markoa2c211c2018-11-01 09:50:52 +00002227 return BuildLoadClass(type_index, dex_file, klass, dex_pc, needs_access_check);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002228}
2229
Vladimir Markoa2c211c2018-11-01 09:50:52 +00002230HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002231 const DexFile& dex_file,
2232 Handle<mirror::Class> klass,
2233 uint32_t dex_pc,
2234 bool needs_access_check) {
2235 // Try to find a reference in the compiling dex file.
2236 const DexFile* actual_dex_file = &dex_file;
2237 if (!IsSameDexFile(dex_file, *dex_compilation_unit_->GetDexFile())) {
2238 dex::TypeIndex local_type_index =
2239 klass->FindTypeIndexInOtherDexFile(*dex_compilation_unit_->GetDexFile());
2240 if (local_type_index.IsValid()) {
2241 type_index = local_type_index;
2242 actual_dex_file = dex_compilation_unit_->GetDexFile();
2243 }
2244 }
2245
Vladimir Marko02ca05a2020-05-12 13:58:51 +01002246 // Note: `klass` must be from `graph_->GetHandleCache()`.
Vladimir Markoa2c211c2018-11-01 09:50:52 +00002247 bool is_referrers_class =
2248 (klass != nullptr) && (outer_compilation_unit_->GetCompilingClass().Get() == klass.Get());
Vladimir Markoca6fff82017-10-03 14:49:14 +01002249 HLoadClass* load_class = new (allocator_) HLoadClass(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00002250 graph_->GetCurrentMethod(),
2251 type_index,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002252 *actual_dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00002253 klass,
Vladimir Markofca0b492018-07-23 15:30:52 +01002254 is_referrers_class,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00002255 dex_pc,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002256 needs_access_check);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00002257
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002258 HLoadClass::LoadKind load_kind = HSharpening::ComputeLoadClassKind(load_class,
2259 code_generator_,
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002260 *dex_compilation_unit_);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002261
2262 if (load_kind == HLoadClass::LoadKind::kInvalid) {
2263 // We actually cannot reference this class, we're forced to bail.
2264 return nullptr;
2265 }
Vladimir Marko28e012a2017-12-07 11:22:59 +00002266 // Load kind must be set before inserting the instruction into the graph.
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002267 load_class->SetLoadKind(load_kind);
Vladimir Marko28e012a2017-12-07 11:22:59 +00002268 AppendInstruction(load_class);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00002269 return load_class;
2270}
2271
Vladimir Marko175e7862018-03-27 09:03:13 +00002272Handle<mirror::Class> HInstructionBuilder::ResolveClass(ScopedObjectAccess& soa,
2273 dex::TypeIndex type_index) {
Vladimir Marko3b506202018-10-31 14:33:58 +00002274 auto it = class_cache_.find(type_index);
2275 if (it != class_cache_.end()) {
2276 return it->second;
2277 }
2278
2279 ObjPtr<mirror::Class> klass = dex_compilation_unit_->GetClassLinker()->ResolveType(
2280 type_index, dex_compilation_unit_->GetDexCache(), dex_compilation_unit_->GetClassLoader());
2281 DCHECK_EQ(klass == nullptr, soa.Self()->IsExceptionPending());
2282 soa.Self()->ClearException(); // Clean up the exception left by type resolution if any.
2283
Vladimir Marko02ca05a2020-05-12 13:58:51 +01002284 Handle<mirror::Class> h_klass = graph_->GetHandleCache()->NewHandle(klass);
Vladimir Marko3b506202018-10-31 14:33:58 +00002285 class_cache_.Put(type_index, h_klass);
2286 return h_klass;
Vladimir Marko175e7862018-03-27 09:03:13 +00002287}
2288
Vladimir Marko2f40d242020-04-08 12:56:45 +01002289bool HInstructionBuilder::LoadClassNeedsAccessCheck(ObjPtr<mirror::Class> klass) {
Vladimir Marko175e7862018-03-27 09:03:13 +00002290 if (klass == nullptr) {
2291 return true;
2292 } else if (klass->IsPublic()) {
2293 return false;
2294 } else {
Vladimir Markoa2c211c2018-11-01 09:50:52 +00002295 ObjPtr<mirror::Class> compiling_class = dex_compilation_unit_->GetCompilingClass().Get();
Vladimir Marko2f40d242020-04-08 12:56:45 +01002296 return compiling_class == nullptr || !compiling_class->CanAccess(klass);
Vladimir Marko175e7862018-03-27 09:03:13 +00002297 }
2298}
2299
Orion Hodson06d10a72018-05-14 08:53:38 +01002300void HInstructionBuilder::BuildLoadMethodHandle(uint16_t method_handle_index, uint32_t dex_pc) {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01002301 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
Orion Hodson06d10a72018-05-14 08:53:38 +01002302 HLoadMethodHandle* load_method_handle = new (allocator_) HLoadMethodHandle(
2303 graph_->GetCurrentMethod(), method_handle_index, dex_file, dex_pc);
Orion Hodsondbaa5c72018-05-10 08:22:46 +01002304 AppendInstruction(load_method_handle);
2305}
2306
Orion Hodson06d10a72018-05-14 08:53:38 +01002307void HInstructionBuilder::BuildLoadMethodType(dex::ProtoIndex proto_index, uint32_t dex_pc) {
Orion Hodson18259d72018-04-12 11:18:23 +01002308 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
2309 HLoadMethodType* load_method_type =
Orion Hodson06d10a72018-05-14 08:53:38 +01002310 new (allocator_) HLoadMethodType(graph_->GetCurrentMethod(), proto_index, dex_file, dex_pc);
Orion Hodson18259d72018-04-12 11:18:23 +01002311 AppendInstruction(load_method_type);
2312}
2313
David Brazdildee58d62016-04-07 09:54:26 +00002314void HInstructionBuilder::BuildTypeCheck(const Instruction& instruction,
2315 uint8_t destination,
2316 uint8_t reference,
Andreas Gampea5b09a62016-11-17 15:21:22 -08002317 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +00002318 uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002319 HInstruction* object = LoadLocal(reference, DataType::Type::kReference);
David Brazdildee58d62016-04-07 09:54:26 +00002320
Nicolas Geoffray5247c082017-01-13 14:17:29 +00002321 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko175e7862018-03-27 09:03:13 +00002322 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
2323 Handle<mirror::Class> klass = ResolveClass(soa, type_index);
Vladimir Marko2f40d242020-04-08 12:56:45 +01002324 bool needs_access_check = LoadClassNeedsAccessCheck(klass.Get());
Vladimir Marko175e7862018-03-27 09:03:13 +00002325 TypeCheckKind check_kind = HSharpening::ComputeTypeCheckKind(
Vladimir Markodc4bcce2018-06-21 16:15:42 +01002326 klass.Get(), code_generator_, needs_access_check);
Vladimir Marko175e7862018-03-27 09:03:13 +00002327
2328 HInstruction* class_or_null = nullptr;
2329 HIntConstant* bitstring_path_to_root = nullptr;
2330 HIntConstant* bitstring_mask = nullptr;
2331 if (check_kind == TypeCheckKind::kBitstringCheck) {
2332 // TODO: Allow using the bitstring check also if we need an access check.
2333 DCHECK(!needs_access_check);
2334 class_or_null = graph_->GetNullConstant(dex_pc);
2335 MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_);
2336 uint32_t path_to_root =
2337 SubtypeCheck<ObjPtr<mirror::Class>>::GetEncodedPathToRootForTarget(klass.Get());
2338 uint32_t mask = SubtypeCheck<ObjPtr<mirror::Class>>::GetEncodedPathToRootMask(klass.Get());
2339 bitstring_path_to_root = graph_->GetIntConstant(static_cast<int32_t>(path_to_root), dex_pc);
2340 bitstring_mask = graph_->GetIntConstant(static_cast<int32_t>(mask), dex_pc);
2341 } else {
Vladimir Markoa2c211c2018-11-01 09:50:52 +00002342 class_or_null = BuildLoadClass(type_index, dex_file, klass, dex_pc, needs_access_check);
Vladimir Marko175e7862018-03-27 09:03:13 +00002343 }
2344 DCHECK(class_or_null != nullptr);
2345
David Brazdildee58d62016-04-07 09:54:26 +00002346 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
Vladimir Marko175e7862018-03-27 09:03:13 +00002347 AppendInstruction(new (allocator_) HInstanceOf(object,
2348 class_or_null,
2349 check_kind,
2350 klass,
2351 dex_pc,
2352 allocator_,
2353 bitstring_path_to_root,
2354 bitstring_mask));
David Brazdildee58d62016-04-07 09:54:26 +00002355 UpdateLocal(destination, current_block_->GetLastInstruction());
2356 } else {
2357 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
2358 // We emit a CheckCast followed by a BoundType. CheckCast is a statement
2359 // which may throw. If it succeeds BoundType sets the new type of `object`
2360 // for all subsequent uses.
Vladimir Marko175e7862018-03-27 09:03:13 +00002361 AppendInstruction(
2362 new (allocator_) HCheckCast(object,
2363 class_or_null,
2364 check_kind,
2365 klass,
2366 dex_pc,
2367 allocator_,
2368 bitstring_path_to_root,
2369 bitstring_mask));
Vladimir Markoca6fff82017-10-03 14:49:14 +01002370 AppendInstruction(new (allocator_) HBoundType(object, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002371 UpdateLocal(reference, current_block_->GetLastInstruction());
2372 }
2373}
2374
David Brazdildee58d62016-04-07 09:54:26 +00002375bool HInstructionBuilder::CanDecodeQuickenedInfo() const {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002376 return !quicken_info_.IsNull();
David Brazdildee58d62016-04-07 09:54:26 +00002377}
2378
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002379uint16_t HInstructionBuilder::LookupQuickenedInfo(uint32_t quicken_index) {
2380 DCHECK(CanDecodeQuickenedInfo());
2381 return quicken_info_.GetData(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00002382}
2383
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002384bool HInstructionBuilder::ProcessDexInstruction(const Instruction& instruction,
2385 uint32_t dex_pc,
2386 size_t quicken_index) {
David Brazdildee58d62016-04-07 09:54:26 +00002387 switch (instruction.Opcode()) {
2388 case Instruction::CONST_4: {
2389 int32_t register_index = instruction.VRegA();
2390 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n(), dex_pc);
2391 UpdateLocal(register_index, constant);
2392 break;
2393 }
2394
2395 case Instruction::CONST_16: {
2396 int32_t register_index = instruction.VRegA();
2397 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s(), dex_pc);
2398 UpdateLocal(register_index, constant);
2399 break;
2400 }
2401
2402 case Instruction::CONST: {
2403 int32_t register_index = instruction.VRegA();
2404 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i(), dex_pc);
2405 UpdateLocal(register_index, constant);
2406 break;
2407 }
2408
2409 case Instruction::CONST_HIGH16: {
2410 int32_t register_index = instruction.VRegA();
2411 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16, dex_pc);
2412 UpdateLocal(register_index, constant);
2413 break;
2414 }
2415
2416 case Instruction::CONST_WIDE_16: {
2417 int32_t register_index = instruction.VRegA();
2418 // Get 16 bits of constant value, sign extended to 64 bits.
2419 int64_t value = instruction.VRegB_21s();
2420 value <<= 48;
2421 value >>= 48;
2422 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
2423 UpdateLocal(register_index, constant);
2424 break;
2425 }
2426
2427 case Instruction::CONST_WIDE_32: {
2428 int32_t register_index = instruction.VRegA();
2429 // Get 32 bits of constant value, sign extended to 64 bits.
2430 int64_t value = instruction.VRegB_31i();
2431 value <<= 32;
2432 value >>= 32;
2433 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
2434 UpdateLocal(register_index, constant);
2435 break;
2436 }
2437
2438 case Instruction::CONST_WIDE: {
2439 int32_t register_index = instruction.VRegA();
2440 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l(), dex_pc);
2441 UpdateLocal(register_index, constant);
2442 break;
2443 }
2444
2445 case Instruction::CONST_WIDE_HIGH16: {
2446 int32_t register_index = instruction.VRegA();
2447 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
2448 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
2449 UpdateLocal(register_index, constant);
2450 break;
2451 }
2452
2453 // Note that the SSA building will refine the types.
2454 case Instruction::MOVE:
2455 case Instruction::MOVE_FROM16:
2456 case Instruction::MOVE_16: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002457 HInstruction* value = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +00002458 UpdateLocal(instruction.VRegA(), value);
2459 break;
2460 }
2461
2462 // Note that the SSA building will refine the types.
2463 case Instruction::MOVE_WIDE:
2464 case Instruction::MOVE_WIDE_FROM16:
2465 case Instruction::MOVE_WIDE_16: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002466 HInstruction* value = LoadLocal(instruction.VRegB(), DataType::Type::kInt64);
David Brazdildee58d62016-04-07 09:54:26 +00002467 UpdateLocal(instruction.VRegA(), value);
2468 break;
2469 }
2470
2471 case Instruction::MOVE_OBJECT:
2472 case Instruction::MOVE_OBJECT_16:
2473 case Instruction::MOVE_OBJECT_FROM16: {
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01002474 // The verifier has no notion of a null type, so a move-object of constant 0
2475 // will lead to the same constant 0 in the destination register. To mimic
2476 // this behavior, we just pretend we haven't seen a type change (int to reference)
2477 // for the 0 constant and phis. We rely on our type propagation to eventually get the
2478 // types correct.
2479 uint32_t reg_number = instruction.VRegB();
2480 HInstruction* value = (*current_locals_)[reg_number];
2481 if (value->IsIntConstant()) {
2482 DCHECK_EQ(value->AsIntConstant()->GetValue(), 0);
2483 } else if (value->IsPhi()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002484 DCHECK(value->GetType() == DataType::Type::kInt32 ||
2485 value->GetType() == DataType::Type::kReference);
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01002486 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002487 value = LoadLocal(reg_number, DataType::Type::kReference);
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01002488 }
David Brazdildee58d62016-04-07 09:54:26 +00002489 UpdateLocal(instruction.VRegA(), value);
2490 break;
2491 }
2492
2493 case Instruction::RETURN_VOID_NO_BARRIER:
2494 case Instruction::RETURN_VOID: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002495 BuildReturn(instruction, DataType::Type::kVoid, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002496 break;
2497 }
2498
2499#define IF_XX(comparison, cond) \
2500 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
2501 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
2502
2503 IF_XX(HEqual, EQ);
2504 IF_XX(HNotEqual, NE);
2505 IF_XX(HLessThan, LT);
2506 IF_XX(HLessThanOrEqual, LE);
2507 IF_XX(HGreaterThan, GT);
2508 IF_XX(HGreaterThanOrEqual, GE);
2509
2510 case Instruction::GOTO:
2511 case Instruction::GOTO_16:
2512 case Instruction::GOTO_32: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002513 AppendInstruction(new (allocator_) HGoto(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002514 current_block_ = nullptr;
2515 break;
2516 }
2517
2518 case Instruction::RETURN: {
2519 BuildReturn(instruction, return_type_, dex_pc);
2520 break;
2521 }
2522
2523 case Instruction::RETURN_OBJECT: {
2524 BuildReturn(instruction, return_type_, dex_pc);
2525 break;
2526 }
2527
2528 case Instruction::RETURN_WIDE: {
2529 BuildReturn(instruction, return_type_, dex_pc);
2530 break;
2531 }
2532
2533 case Instruction::INVOKE_DIRECT:
2534 case Instruction::INVOKE_INTERFACE:
2535 case Instruction::INVOKE_STATIC:
2536 case Instruction::INVOKE_SUPER:
2537 case Instruction::INVOKE_VIRTUAL:
2538 case Instruction::INVOKE_VIRTUAL_QUICK: {
2539 uint16_t method_idx;
2540 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_QUICK) {
2541 if (!CanDecodeQuickenedInfo()) {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002542 VLOG(compiler) << "Not compiled: Could not decode quickened instruction "
2543 << instruction.Opcode();
David Brazdildee58d62016-04-07 09:54:26 +00002544 return false;
2545 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002546 method_idx = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00002547 } else {
2548 method_idx = instruction.VRegB_35c();
2549 }
David Brazdildee58d62016-04-07 09:54:26 +00002550 uint32_t args[5];
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002551 uint32_t number_of_vreg_arguments = instruction.GetVarArgs(args);
2552 VarArgsInstructionOperands operands(args, number_of_vreg_arguments);
2553 if (!BuildInvoke(instruction, dex_pc, method_idx, operands)) {
David Brazdildee58d62016-04-07 09:54:26 +00002554 return false;
2555 }
2556 break;
2557 }
2558
2559 case Instruction::INVOKE_DIRECT_RANGE:
2560 case Instruction::INVOKE_INTERFACE_RANGE:
2561 case Instruction::INVOKE_STATIC_RANGE:
2562 case Instruction::INVOKE_SUPER_RANGE:
2563 case Instruction::INVOKE_VIRTUAL_RANGE:
2564 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2565 uint16_t method_idx;
2566 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK) {
2567 if (!CanDecodeQuickenedInfo()) {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002568 VLOG(compiler) << "Not compiled: Could not decode quickened instruction "
2569 << instruction.Opcode();
David Brazdildee58d62016-04-07 09:54:26 +00002570 return false;
2571 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002572 method_idx = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00002573 } else {
2574 method_idx = instruction.VRegB_3rc();
2575 }
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002576 RangeInstructionOperands operands(instruction.VRegC(), instruction.VRegA_3rc());
2577 if (!BuildInvoke(instruction, dex_pc, method_idx, operands)) {
David Brazdildee58d62016-04-07 09:54:26 +00002578 return false;
2579 }
2580 break;
2581 }
2582
Orion Hodsonac141392017-01-13 11:53:47 +00002583 case Instruction::INVOKE_POLYMORPHIC: {
2584 uint16_t method_idx = instruction.VRegB_45cc();
Orion Hodson06d10a72018-05-14 08:53:38 +01002585 dex::ProtoIndex proto_idx(instruction.VRegH_45cc());
Orion Hodsonac141392017-01-13 11:53:47 +00002586 uint32_t args[5];
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002587 uint32_t number_of_vreg_arguments = instruction.GetVarArgs(args);
2588 VarArgsInstructionOperands operands(args, number_of_vreg_arguments);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002589 return BuildInvokePolymorphic(dex_pc, method_idx, proto_idx, operands);
Orion Hodsonac141392017-01-13 11:53:47 +00002590 }
2591
2592 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
2593 uint16_t method_idx = instruction.VRegB_4rcc();
Orion Hodson06d10a72018-05-14 08:53:38 +01002594 dex::ProtoIndex proto_idx(instruction.VRegH_4rcc());
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002595 RangeInstructionOperands operands(instruction.VRegC_4rcc(), instruction.VRegA_4rcc());
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002596 return BuildInvokePolymorphic(dex_pc, method_idx, proto_idx, operands);
2597 }
2598
2599 case Instruction::INVOKE_CUSTOM: {
2600 uint16_t call_site_idx = instruction.VRegB_35c();
2601 uint32_t args[5];
2602 uint32_t number_of_vreg_arguments = instruction.GetVarArgs(args);
2603 VarArgsInstructionOperands operands(args, number_of_vreg_arguments);
2604 return BuildInvokeCustom(dex_pc, call_site_idx, operands);
2605 }
2606
2607 case Instruction::INVOKE_CUSTOM_RANGE: {
2608 uint16_t call_site_idx = instruction.VRegB_3rc();
2609 RangeInstructionOperands operands(instruction.VRegC_3rc(), instruction.VRegA_3rc());
2610 return BuildInvokeCustom(dex_pc, call_site_idx, operands);
Orion Hodsonac141392017-01-13 11:53:47 +00002611 }
2612
David Brazdildee58d62016-04-07 09:54:26 +00002613 case Instruction::NEG_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002614 Unop_12x<HNeg>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002615 break;
2616 }
2617
2618 case Instruction::NEG_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002619 Unop_12x<HNeg>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002620 break;
2621 }
2622
2623 case Instruction::NEG_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002624 Unop_12x<HNeg>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002625 break;
2626 }
2627
2628 case Instruction::NEG_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002629 Unop_12x<HNeg>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002630 break;
2631 }
2632
2633 case Instruction::NOT_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002634 Unop_12x<HNot>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002635 break;
2636 }
2637
2638 case Instruction::NOT_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002639 Unop_12x<HNot>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002640 break;
2641 }
2642
2643 case Instruction::INT_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002644 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002645 break;
2646 }
2647
2648 case Instruction::INT_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002649 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002650 break;
2651 }
2652
2653 case Instruction::INT_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002654 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002655 break;
2656 }
2657
2658 case Instruction::LONG_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002659 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002660 break;
2661 }
2662
2663 case Instruction::LONG_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002664 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002665 break;
2666 }
2667
2668 case Instruction::LONG_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002669 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002670 break;
2671 }
2672
2673 case Instruction::FLOAT_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002674 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002675 break;
2676 }
2677
2678 case Instruction::FLOAT_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002679 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002680 break;
2681 }
2682
2683 case Instruction::FLOAT_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002684 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002685 break;
2686 }
2687
2688 case Instruction::DOUBLE_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002689 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002690 break;
2691 }
2692
2693 case Instruction::DOUBLE_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002694 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002695 break;
2696 }
2697
2698 case Instruction::DOUBLE_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002699 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002700 break;
2701 }
2702
2703 case Instruction::INT_TO_BYTE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002704 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt8, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002705 break;
2706 }
2707
2708 case Instruction::INT_TO_SHORT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002709 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt16, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002710 break;
2711 }
2712
2713 case Instruction::INT_TO_CHAR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002714 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kUint16, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002715 break;
2716 }
2717
2718 case Instruction::ADD_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002719 Binop_23x<HAdd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002720 break;
2721 }
2722
2723 case Instruction::ADD_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002724 Binop_23x<HAdd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002725 break;
2726 }
2727
2728 case Instruction::ADD_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002729 Binop_23x<HAdd>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002730 break;
2731 }
2732
2733 case Instruction::ADD_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002734 Binop_23x<HAdd>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002735 break;
2736 }
2737
2738 case Instruction::SUB_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002739 Binop_23x<HSub>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002740 break;
2741 }
2742
2743 case Instruction::SUB_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002744 Binop_23x<HSub>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002745 break;
2746 }
2747
2748 case Instruction::SUB_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002749 Binop_23x<HSub>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002750 break;
2751 }
2752
2753 case Instruction::SUB_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002754 Binop_23x<HSub>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002755 break;
2756 }
2757
2758 case Instruction::ADD_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002759 Binop_12x<HAdd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002760 break;
2761 }
2762
2763 case Instruction::MUL_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002764 Binop_23x<HMul>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002765 break;
2766 }
2767
2768 case Instruction::MUL_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002769 Binop_23x<HMul>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002770 break;
2771 }
2772
2773 case Instruction::MUL_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002774 Binop_23x<HMul>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002775 break;
2776 }
2777
2778 case Instruction::MUL_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002779 Binop_23x<HMul>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002780 break;
2781 }
2782
2783 case Instruction::DIV_INT: {
2784 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002785 dex_pc, DataType::Type::kInt32, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002786 break;
2787 }
2788
2789 case Instruction::DIV_LONG: {
2790 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002791 dex_pc, DataType::Type::kInt64, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002792 break;
2793 }
2794
2795 case Instruction::DIV_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002796 Binop_23x<HDiv>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002797 break;
2798 }
2799
2800 case Instruction::DIV_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002801 Binop_23x<HDiv>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002802 break;
2803 }
2804
2805 case Instruction::REM_INT: {
2806 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002807 dex_pc, DataType::Type::kInt32, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002808 break;
2809 }
2810
2811 case Instruction::REM_LONG: {
2812 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002813 dex_pc, DataType::Type::kInt64, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002814 break;
2815 }
2816
2817 case Instruction::REM_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002818 Binop_23x<HRem>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002819 break;
2820 }
2821
2822 case Instruction::REM_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002823 Binop_23x<HRem>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002824 break;
2825 }
2826
2827 case Instruction::AND_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002828 Binop_23x<HAnd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002829 break;
2830 }
2831
2832 case Instruction::AND_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002833 Binop_23x<HAnd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002834 break;
2835 }
2836
2837 case Instruction::SHL_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002838 Binop_23x_shift<HShl>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002839 break;
2840 }
2841
2842 case Instruction::SHL_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002843 Binop_23x_shift<HShl>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002844 break;
2845 }
2846
2847 case Instruction::SHR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002848 Binop_23x_shift<HShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002849 break;
2850 }
2851
2852 case Instruction::SHR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002853 Binop_23x_shift<HShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002854 break;
2855 }
2856
2857 case Instruction::USHR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002858 Binop_23x_shift<HUShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002859 break;
2860 }
2861
2862 case Instruction::USHR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002863 Binop_23x_shift<HUShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002864 break;
2865 }
2866
2867 case Instruction::OR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002868 Binop_23x<HOr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002869 break;
2870 }
2871
2872 case Instruction::OR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002873 Binop_23x<HOr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002874 break;
2875 }
2876
2877 case Instruction::XOR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002878 Binop_23x<HXor>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002879 break;
2880 }
2881
2882 case Instruction::XOR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002883 Binop_23x<HXor>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002884 break;
2885 }
2886
2887 case Instruction::ADD_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002888 Binop_12x<HAdd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002889 break;
2890 }
2891
2892 case Instruction::ADD_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002893 Binop_12x<HAdd>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002894 break;
2895 }
2896
2897 case Instruction::ADD_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002898 Binop_12x<HAdd>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002899 break;
2900 }
2901
2902 case Instruction::SUB_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002903 Binop_12x<HSub>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002904 break;
2905 }
2906
2907 case Instruction::SUB_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002908 Binop_12x<HSub>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002909 break;
2910 }
2911
2912 case Instruction::SUB_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002913 Binop_12x<HSub>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002914 break;
2915 }
2916
2917 case Instruction::SUB_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002918 Binop_12x<HSub>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002919 break;
2920 }
2921
2922 case Instruction::MUL_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002923 Binop_12x<HMul>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002924 break;
2925 }
2926
2927 case Instruction::MUL_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002928 Binop_12x<HMul>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002929 break;
2930 }
2931
2932 case Instruction::MUL_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002933 Binop_12x<HMul>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002934 break;
2935 }
2936
2937 case Instruction::MUL_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002938 Binop_12x<HMul>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002939 break;
2940 }
2941
2942 case Instruction::DIV_INT_2ADDR: {
2943 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002944 dex_pc, DataType::Type::kInt32, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002945 break;
2946 }
2947
2948 case Instruction::DIV_LONG_2ADDR: {
2949 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002950 dex_pc, DataType::Type::kInt64, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002951 break;
2952 }
2953
2954 case Instruction::REM_INT_2ADDR: {
2955 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002956 dex_pc, DataType::Type::kInt32, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002957 break;
2958 }
2959
2960 case Instruction::REM_LONG_2ADDR: {
2961 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002962 dex_pc, DataType::Type::kInt64, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002963 break;
2964 }
2965
2966 case Instruction::REM_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002967 Binop_12x<HRem>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002968 break;
2969 }
2970
2971 case Instruction::REM_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002972 Binop_12x<HRem>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002973 break;
2974 }
2975
2976 case Instruction::SHL_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002977 Binop_12x_shift<HShl>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002978 break;
2979 }
2980
2981 case Instruction::SHL_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002982 Binop_12x_shift<HShl>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002983 break;
2984 }
2985
2986 case Instruction::SHR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002987 Binop_12x_shift<HShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002988 break;
2989 }
2990
2991 case Instruction::SHR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002992 Binop_12x_shift<HShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002993 break;
2994 }
2995
2996 case Instruction::USHR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002997 Binop_12x_shift<HUShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002998 break;
2999 }
3000
3001 case Instruction::USHR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003002 Binop_12x_shift<HUShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003003 break;
3004 }
3005
3006 case Instruction::DIV_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003007 Binop_12x<HDiv>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003008 break;
3009 }
3010
3011 case Instruction::DIV_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003012 Binop_12x<HDiv>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003013 break;
3014 }
3015
3016 case Instruction::AND_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003017 Binop_12x<HAnd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003018 break;
3019 }
3020
3021 case Instruction::AND_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003022 Binop_12x<HAnd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003023 break;
3024 }
3025
3026 case Instruction::OR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003027 Binop_12x<HOr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003028 break;
3029 }
3030
3031 case Instruction::OR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003032 Binop_12x<HOr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003033 break;
3034 }
3035
3036 case Instruction::XOR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003037 Binop_12x<HXor>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003038 break;
3039 }
3040
3041 case Instruction::XOR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003042 Binop_12x<HXor>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003043 break;
3044 }
3045
3046 case Instruction::ADD_INT_LIT16: {
3047 Binop_22s<HAdd>(instruction, false, dex_pc);
3048 break;
3049 }
3050
3051 case Instruction::AND_INT_LIT16: {
3052 Binop_22s<HAnd>(instruction, false, dex_pc);
3053 break;
3054 }
3055
3056 case Instruction::OR_INT_LIT16: {
3057 Binop_22s<HOr>(instruction, false, dex_pc);
3058 break;
3059 }
3060
3061 case Instruction::XOR_INT_LIT16: {
3062 Binop_22s<HXor>(instruction, false, dex_pc);
3063 break;
3064 }
3065
3066 case Instruction::RSUB_INT: {
3067 Binop_22s<HSub>(instruction, true, dex_pc);
3068 break;
3069 }
3070
3071 case Instruction::MUL_INT_LIT16: {
3072 Binop_22s<HMul>(instruction, false, dex_pc);
3073 break;
3074 }
3075
3076 case Instruction::ADD_INT_LIT8: {
3077 Binop_22b<HAdd>(instruction, false, dex_pc);
3078 break;
3079 }
3080
3081 case Instruction::AND_INT_LIT8: {
3082 Binop_22b<HAnd>(instruction, false, dex_pc);
3083 break;
3084 }
3085
3086 case Instruction::OR_INT_LIT8: {
3087 Binop_22b<HOr>(instruction, false, dex_pc);
3088 break;
3089 }
3090
3091 case Instruction::XOR_INT_LIT8: {
3092 Binop_22b<HXor>(instruction, false, dex_pc);
3093 break;
3094 }
3095
3096 case Instruction::RSUB_INT_LIT8: {
3097 Binop_22b<HSub>(instruction, true, dex_pc);
3098 break;
3099 }
3100
3101 case Instruction::MUL_INT_LIT8: {
3102 Binop_22b<HMul>(instruction, false, dex_pc);
3103 break;
3104 }
3105
3106 case Instruction::DIV_INT_LIT16:
3107 case Instruction::DIV_INT_LIT8: {
3108 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003109 dex_pc, DataType::Type::kInt32, true, true);
David Brazdildee58d62016-04-07 09:54:26 +00003110 break;
3111 }
3112
3113 case Instruction::REM_INT_LIT16:
3114 case Instruction::REM_INT_LIT8: {
3115 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003116 dex_pc, DataType::Type::kInt32, true, false);
David Brazdildee58d62016-04-07 09:54:26 +00003117 break;
3118 }
3119
3120 case Instruction::SHL_INT_LIT8: {
3121 Binop_22b<HShl>(instruction, false, dex_pc);
3122 break;
3123 }
3124
3125 case Instruction::SHR_INT_LIT8: {
3126 Binop_22b<HShr>(instruction, false, dex_pc);
3127 break;
3128 }
3129
3130 case Instruction::USHR_INT_LIT8: {
3131 Binop_22b<HUShr>(instruction, false, dex_pc);
3132 break;
3133 }
3134
3135 case Instruction::NEW_INSTANCE: {
Igor Murashkin79d8fa72017-04-18 09:37:23 -07003136 HNewInstance* new_instance =
3137 BuildNewInstance(dex::TypeIndex(instruction.VRegB_21c()), dex_pc);
3138 DCHECK(new_instance != nullptr);
3139
David Brazdildee58d62016-04-07 09:54:26 +00003140 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
Igor Murashkin79d8fa72017-04-18 09:37:23 -07003141 BuildConstructorFenceForAllocation(new_instance);
David Brazdildee58d62016-04-07 09:54:26 +00003142 break;
3143 }
3144
3145 case Instruction::NEW_ARRAY: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003146 dex::TypeIndex type_index(instruction.VRegC_22c());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003147 HInstruction* length = LoadLocal(instruction.VRegB_22c(), DataType::Type::kInt32);
Vladimir Markob5461632018-10-15 14:24:21 +01003148 HNewArray* new_array = BuildNewArray(dex_pc, type_index, length);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07003149
David Brazdildee58d62016-04-07 09:54:26 +00003150 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
Igor Murashkin79d8fa72017-04-18 09:37:23 -07003151 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00003152 break;
3153 }
3154
3155 case Instruction::FILLED_NEW_ARRAY: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003156 dex::TypeIndex type_index(instruction.VRegB_35c());
David Brazdildee58d62016-04-07 09:54:26 +00003157 uint32_t args[5];
Treehugger Robot2c5827a2018-05-17 22:26:08 +00003158 uint32_t number_of_vreg_arguments = instruction.GetVarArgs(args);
3159 VarArgsInstructionOperands operands(args, number_of_vreg_arguments);
3160 HNewArray* new_array = BuildFilledNewArray(dex_pc, type_index, operands);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07003161 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00003162 break;
3163 }
3164
3165 case Instruction::FILLED_NEW_ARRAY_RANGE: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003166 dex::TypeIndex type_index(instruction.VRegB_3rc());
Treehugger Robot2c5827a2018-05-17 22:26:08 +00003167 RangeInstructionOperands operands(instruction.VRegC_3rc(), instruction.VRegA_3rc());
3168 HNewArray* new_array = BuildFilledNewArray(dex_pc, type_index, operands);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07003169 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00003170 break;
3171 }
3172
3173 case Instruction::FILL_ARRAY_DATA: {
3174 BuildFillArrayData(instruction, dex_pc);
3175 break;
3176 }
3177
3178 case Instruction::MOVE_RESULT:
3179 case Instruction::MOVE_RESULT_WIDE:
3180 case Instruction::MOVE_RESULT_OBJECT: {
3181 DCHECK(latest_result_ != nullptr);
3182 UpdateLocal(instruction.VRegA(), latest_result_);
3183 latest_result_ = nullptr;
3184 break;
3185 }
3186
3187 case Instruction::CMP_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003188 Binop_23x_cmp(instruction, DataType::Type::kInt64, ComparisonBias::kNoBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003189 break;
3190 }
3191
3192 case Instruction::CMPG_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003193 Binop_23x_cmp(instruction, DataType::Type::kFloat32, ComparisonBias::kGtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003194 break;
3195 }
3196
3197 case Instruction::CMPG_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003198 Binop_23x_cmp(instruction, DataType::Type::kFloat64, ComparisonBias::kGtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003199 break;
3200 }
3201
3202 case Instruction::CMPL_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003203 Binop_23x_cmp(instruction, DataType::Type::kFloat32, ComparisonBias::kLtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003204 break;
3205 }
3206
3207 case Instruction::CMPL_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003208 Binop_23x_cmp(instruction, DataType::Type::kFloat64, ComparisonBias::kLtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003209 break;
3210 }
3211
3212 case Instruction::NOP:
3213 break;
3214
3215 case Instruction::IGET:
3216 case Instruction::IGET_QUICK:
3217 case Instruction::IGET_WIDE:
3218 case Instruction::IGET_WIDE_QUICK:
3219 case Instruction::IGET_OBJECT:
3220 case Instruction::IGET_OBJECT_QUICK:
3221 case Instruction::IGET_BOOLEAN:
3222 case Instruction::IGET_BOOLEAN_QUICK:
3223 case Instruction::IGET_BYTE:
3224 case Instruction::IGET_BYTE_QUICK:
3225 case Instruction::IGET_CHAR:
3226 case Instruction::IGET_CHAR_QUICK:
3227 case Instruction::IGET_SHORT:
3228 case Instruction::IGET_SHORT_QUICK: {
Andreas Gampe3db70682018-12-26 15:12:03 -08003229 if (!BuildInstanceFieldAccess(instruction, dex_pc, /* is_put= */ false, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00003230 return false;
3231 }
3232 break;
3233 }
3234
3235 case Instruction::IPUT:
3236 case Instruction::IPUT_QUICK:
3237 case Instruction::IPUT_WIDE:
3238 case Instruction::IPUT_WIDE_QUICK:
3239 case Instruction::IPUT_OBJECT:
3240 case Instruction::IPUT_OBJECT_QUICK:
3241 case Instruction::IPUT_BOOLEAN:
3242 case Instruction::IPUT_BOOLEAN_QUICK:
3243 case Instruction::IPUT_BYTE:
3244 case Instruction::IPUT_BYTE_QUICK:
3245 case Instruction::IPUT_CHAR:
3246 case Instruction::IPUT_CHAR_QUICK:
3247 case Instruction::IPUT_SHORT:
3248 case Instruction::IPUT_SHORT_QUICK: {
Andreas Gampe3db70682018-12-26 15:12:03 -08003249 if (!BuildInstanceFieldAccess(instruction, dex_pc, /* is_put= */ true, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00003250 return false;
3251 }
3252 break;
3253 }
3254
3255 case Instruction::SGET:
3256 case Instruction::SGET_WIDE:
3257 case Instruction::SGET_OBJECT:
3258 case Instruction::SGET_BOOLEAN:
3259 case Instruction::SGET_BYTE:
3260 case Instruction::SGET_CHAR:
3261 case Instruction::SGET_SHORT: {
Andreas Gampe3db70682018-12-26 15:12:03 -08003262 BuildStaticFieldAccess(instruction, dex_pc, /* is_put= */ false);
David Brazdildee58d62016-04-07 09:54:26 +00003263 break;
3264 }
3265
3266 case Instruction::SPUT:
3267 case Instruction::SPUT_WIDE:
3268 case Instruction::SPUT_OBJECT:
3269 case Instruction::SPUT_BOOLEAN:
3270 case Instruction::SPUT_BYTE:
3271 case Instruction::SPUT_CHAR:
3272 case Instruction::SPUT_SHORT: {
Andreas Gampe3db70682018-12-26 15:12:03 -08003273 BuildStaticFieldAccess(instruction, dex_pc, /* is_put= */ true);
David Brazdildee58d62016-04-07 09:54:26 +00003274 break;
3275 }
3276
3277#define ARRAY_XX(kind, anticipated_type) \
3278 case Instruction::AGET##kind: { \
3279 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
3280 break; \
3281 } \
3282 case Instruction::APUT##kind: { \
3283 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
3284 break; \
3285 }
3286
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003287 ARRAY_XX(, DataType::Type::kInt32);
3288 ARRAY_XX(_WIDE, DataType::Type::kInt64);
3289 ARRAY_XX(_OBJECT, DataType::Type::kReference);
3290 ARRAY_XX(_BOOLEAN, DataType::Type::kBool);
3291 ARRAY_XX(_BYTE, DataType::Type::kInt8);
3292 ARRAY_XX(_CHAR, DataType::Type::kUint16);
3293 ARRAY_XX(_SHORT, DataType::Type::kInt16);
David Brazdildee58d62016-04-07 09:54:26 +00003294
3295 case Instruction::ARRAY_LENGTH: {
David Brazdilc120bbe2016-04-22 16:57:00 +01003296 HInstruction* object = LoadNullCheckedLocal(instruction.VRegB_12x(), dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01003297 AppendInstruction(new (allocator_) HArrayLength(object, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00003298 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
3299 break;
3300 }
3301
3302 case Instruction::CONST_STRING: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08003303 dex::StringIndex string_index(instruction.VRegB_21c());
Vladimir Marko28e012a2017-12-07 11:22:59 +00003304 BuildLoadString(string_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003305 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
3306 break;
3307 }
3308
3309 case Instruction::CONST_STRING_JUMBO: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08003310 dex::StringIndex string_index(instruction.VRegB_31c());
Vladimir Marko28e012a2017-12-07 11:22:59 +00003311 BuildLoadString(string_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003312 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
3313 break;
3314 }
3315
3316 case Instruction::CONST_CLASS: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003317 dex::TypeIndex type_index(instruction.VRegB_21c());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00003318 BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003319 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
3320 break;
3321 }
3322
Orion Hodsondbaa5c72018-05-10 08:22:46 +01003323 case Instruction::CONST_METHOD_HANDLE: {
3324 uint16_t method_handle_idx = instruction.VRegB_21c();
3325 BuildLoadMethodHandle(method_handle_idx, dex_pc);
3326 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
3327 break;
3328 }
3329
Orion Hodson18259d72018-04-12 11:18:23 +01003330 case Instruction::CONST_METHOD_TYPE: {
Orion Hodson06d10a72018-05-14 08:53:38 +01003331 dex::ProtoIndex proto_idx(instruction.VRegB_21c());
Orion Hodson18259d72018-04-12 11:18:23 +01003332 BuildLoadMethodType(proto_idx, dex_pc);
3333 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
3334 break;
3335 }
3336
David Brazdildee58d62016-04-07 09:54:26 +00003337 case Instruction::MOVE_EXCEPTION: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003338 AppendInstruction(new (allocator_) HLoadException(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00003339 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
Vladimir Markoca6fff82017-10-03 14:49:14 +01003340 AppendInstruction(new (allocator_) HClearException(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00003341 break;
3342 }
3343
3344 case Instruction::THROW: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003345 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01003346 AppendInstruction(new (allocator_) HThrow(exception, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00003347 // We finished building this block. Set the current block to null to avoid
3348 // adding dead instructions to it.
3349 current_block_ = nullptr;
3350 break;
3351 }
3352
3353 case Instruction::INSTANCE_OF: {
3354 uint8_t destination = instruction.VRegA_22c();
3355 uint8_t reference = instruction.VRegB_22c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08003356 dex::TypeIndex type_index(instruction.VRegC_22c());
David Brazdildee58d62016-04-07 09:54:26 +00003357 BuildTypeCheck(instruction, destination, reference, type_index, dex_pc);
3358 break;
3359 }
3360
3361 case Instruction::CHECK_CAST: {
3362 uint8_t reference = instruction.VRegA_21c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08003363 dex::TypeIndex type_index(instruction.VRegB_21c());
David Brazdildee58d62016-04-07 09:54:26 +00003364 BuildTypeCheck(instruction, -1, reference, type_index, dex_pc);
3365 break;
3366 }
3367
3368 case Instruction::MONITOR_ENTER: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003369 AppendInstruction(new (allocator_) HMonitorOperation(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003370 LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference),
David Brazdildee58d62016-04-07 09:54:26 +00003371 HMonitorOperation::OperationKind::kEnter,
3372 dex_pc));
Artem Serov2808be82018-12-20 19:15:11 +00003373 graph_->SetHasMonitorOperations(true);
David Brazdildee58d62016-04-07 09:54:26 +00003374 break;
3375 }
3376
3377 case Instruction::MONITOR_EXIT: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003378 AppendInstruction(new (allocator_) HMonitorOperation(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003379 LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference),
David Brazdildee58d62016-04-07 09:54:26 +00003380 HMonitorOperation::OperationKind::kExit,
3381 dex_pc));
Artem Serov2808be82018-12-20 19:15:11 +00003382 graph_->SetHasMonitorOperations(true);
David Brazdildee58d62016-04-07 09:54:26 +00003383 break;
3384 }
3385
3386 case Instruction::SPARSE_SWITCH:
3387 case Instruction::PACKED_SWITCH: {
3388 BuildSwitch(instruction, dex_pc);
3389 break;
3390 }
3391
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003392 case Instruction::UNUSED_3E:
3393 case Instruction::UNUSED_3F:
3394 case Instruction::UNUSED_40:
3395 case Instruction::UNUSED_41:
3396 case Instruction::UNUSED_42:
3397 case Instruction::UNUSED_43:
3398 case Instruction::UNUSED_79:
3399 case Instruction::UNUSED_7A:
3400 case Instruction::UNUSED_F3:
3401 case Instruction::UNUSED_F4:
3402 case Instruction::UNUSED_F5:
3403 case Instruction::UNUSED_F6:
3404 case Instruction::UNUSED_F7:
3405 case Instruction::UNUSED_F8:
3406 case Instruction::UNUSED_F9: {
David Brazdildee58d62016-04-07 09:54:26 +00003407 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07003408 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00003409 << " because of unhandled instruction "
3410 << instruction.Name();
Igor Murashkin1e065a52017-08-09 13:20:34 -07003411 MaybeRecordStat(compilation_stats_,
3412 MethodCompilationStat::kNotCompiledUnhandledInstruction);
David Brazdildee58d62016-04-07 09:54:26 +00003413 return false;
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003414 }
David Brazdildee58d62016-04-07 09:54:26 +00003415 }
3416 return true;
3417} // NOLINT(readability/fn_size)
3418
Vladimir Marko8d6768d2017-03-14 10:13:21 +00003419ObjPtr<mirror::Class> HInstructionBuilder::LookupResolvedType(
3420 dex::TypeIndex type_index,
3421 const DexCompilationUnit& compilation_unit) const {
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003422 return compilation_unit.GetClassLinker()->LookupResolvedType(
Vladimir Marko8d6768d2017-03-14 10:13:21 +00003423 type_index, compilation_unit.GetDexCache().Get(), compilation_unit.GetClassLoader().Get());
3424}
3425
3426ObjPtr<mirror::Class> HInstructionBuilder::LookupReferrerClass() const {
3427 // TODO: Cache the result in a Handle<mirror::Class>.
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003428 const dex::MethodId& method_id =
Vladimir Marko8d6768d2017-03-14 10:13:21 +00003429 dex_compilation_unit_->GetDexFile()->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
3430 return LookupResolvedType(method_id.class_idx_, *dex_compilation_unit_);
3431}
3432
David Brazdildee58d62016-04-07 09:54:26 +00003433} // namespace art