blob: df236c17c1ae2b6a769ca43669ca33232840ef86 [file] [log] [blame]
David Brazdildee58d62016-04-07 09:54:26 +00001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "instruction_builder.h"
18
Matthew Gharrity465ecc82016-07-19 21:32:52 +000019#include "art_method-inl.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010020#include "base/arena_bit_vector.h"
21#include "base/bit_vector-inl.h"
Andreas Gampe85f1c572018-11-21 13:52:48 -080022#include "base/logging.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010023#include "block_builder.h"
Vladimir Marko3b506202018-10-31 14:33:58 +000024#include "class_linker-inl.h"
25#include "code_generator.h"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010026#include "data_type-inl.h"
David Sehr312f3b22018-03-19 08:39:26 -070027#include "dex/bytecode_utils.h"
David Sehr9e734c72018-01-04 17:56:19 -080028#include "dex/dex_instruction-inl.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010029#include "driver/dex_compilation_unit.h"
David Brazdildee58d62016-04-07 09:54:26 +000030#include "driver/compiler_options.h"
Andreas Gampe75a7db62016-09-26 12:04:26 -070031#include "imtable-inl.h"
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +000032#include "jit/jit.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010033#include "mirror/dex_cache.h"
Nicolas Geoffray58cc1cb2017-11-20 13:27:29 +000034#include "oat_file.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010035#include "optimizing_compiler_stats.h"
Mathieu Chartierde4b08f2017-07-10 14:13:41 -070036#include "quicken_info.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070037#include "scoped_thread_state_change-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070038#include "sharpening.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010039#include "ssa_builder.h"
Andreas Gampea7c83ac2017-09-11 08:14:23 -070040#include "well_known_classes.h"
David Brazdildee58d62016-04-07 09:54:26 +000041
Vladimir Marko0a516052019-10-14 13:00:44 +000042namespace art {
David Brazdildee58d62016-04-07 09:54:26 +000043
Mathieu Chartier808c7a52017-12-15 11:19:33 -080044HInstructionBuilder::HInstructionBuilder(HGraph* graph,
45 HBasicBlockBuilder* block_builder,
46 SsaBuilder* ssa_builder,
47 const DexFile* dex_file,
48 const CodeItemDebugInfoAccessor& accessor,
49 DataType::Type return_type,
50 const DexCompilationUnit* dex_compilation_unit,
51 const DexCompilationUnit* outer_compilation_unit,
Mathieu Chartier808c7a52017-12-15 11:19:33 -080052 CodeGenerator* code_generator,
Mathieu Chartier210531f2018-01-12 10:15:51 -080053 ArrayRef<const uint8_t> interpreter_metadata,
Mathieu Chartier808c7a52017-12-15 11:19:33 -080054 OptimizingCompilerStats* compiler_stats,
Mathieu Chartier808c7a52017-12-15 11:19:33 -080055 ScopedArenaAllocator* local_allocator)
56 : allocator_(graph->GetAllocator()),
57 graph_(graph),
Mathieu Chartier808c7a52017-12-15 11:19:33 -080058 dex_file_(dex_file),
59 code_item_accessor_(accessor),
60 return_type_(return_type),
61 block_builder_(block_builder),
62 ssa_builder_(ssa_builder),
Mathieu Chartier808c7a52017-12-15 11:19:33 -080063 code_generator_(code_generator),
64 dex_compilation_unit_(dex_compilation_unit),
65 outer_compilation_unit_(outer_compilation_unit),
66 quicken_info_(interpreter_metadata),
67 compilation_stats_(compiler_stats),
68 local_allocator_(local_allocator),
69 locals_for_(local_allocator->Adapter(kArenaAllocGraphBuilder)),
70 current_block_(nullptr),
71 current_locals_(nullptr),
72 latest_result_(nullptr),
73 current_this_parameter_(nullptr),
Vladimir Marko3b506202018-10-31 14:33:58 +000074 loop_headers_(local_allocator->Adapter(kArenaAllocGraphBuilder)),
75 class_cache_(std::less<dex::TypeIndex>(), local_allocator->Adapter(kArenaAllocGraphBuilder)) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -080076 loop_headers_.reserve(kDefaultNumberOfLoops);
77}
78
David Brazdildee58d62016-04-07 09:54:26 +000079HBasicBlock* HInstructionBuilder::FindBlockStartingAt(uint32_t dex_pc) const {
80 return block_builder_->GetBlockAt(dex_pc);
81}
82
Vladimir Marko69d310e2017-10-09 14:12:23 +010083inline ScopedArenaVector<HInstruction*>* HInstructionBuilder::GetLocalsFor(HBasicBlock* block) {
84 ScopedArenaVector<HInstruction*>* locals = &locals_for_[block->GetBlockId()];
David Brazdildee58d62016-04-07 09:54:26 +000085 const size_t vregs = graph_->GetNumberOfVRegs();
Mingyao Yang01b47b02017-02-03 12:09:57 -080086 if (locals->size() == vregs) {
87 return locals;
88 }
89 return GetLocalsForWithAllocation(block, locals, vregs);
90}
David Brazdildee58d62016-04-07 09:54:26 +000091
Vladimir Marko69d310e2017-10-09 14:12:23 +010092ScopedArenaVector<HInstruction*>* HInstructionBuilder::GetLocalsForWithAllocation(
Mingyao Yang01b47b02017-02-03 12:09:57 -080093 HBasicBlock* block,
Vladimir Marko69d310e2017-10-09 14:12:23 +010094 ScopedArenaVector<HInstruction*>* locals,
Mingyao Yang01b47b02017-02-03 12:09:57 -080095 const size_t vregs) {
96 DCHECK_NE(locals->size(), vregs);
97 locals->resize(vregs, nullptr);
98 if (block->IsCatchBlock()) {
99 // We record incoming inputs of catch phis at throwing instructions and
100 // must therefore eagerly create the phis. Phis for undefined vregs will
101 // be deleted when the first throwing instruction with the vreg undefined
102 // is encountered. Unused phis will be removed by dead phi analysis.
103 for (size_t i = 0; i < vregs; ++i) {
104 // No point in creating the catch phi if it is already undefined at
105 // the first throwing instruction.
106 HInstruction* current_local_value = (*current_locals_)[i];
107 if (current_local_value != nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100108 HPhi* phi = new (allocator_) HPhi(
109 allocator_,
Mingyao Yang01b47b02017-02-03 12:09:57 -0800110 i,
111 0,
112 current_local_value->GetType());
113 block->AddPhi(phi);
114 (*locals)[i] = phi;
David Brazdildee58d62016-04-07 09:54:26 +0000115 }
116 }
117 }
118 return locals;
119}
120
Mingyao Yang01b47b02017-02-03 12:09:57 -0800121inline HInstruction* HInstructionBuilder::ValueOfLocalAt(HBasicBlock* block, size_t local) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100122 ScopedArenaVector<HInstruction*>* locals = GetLocalsFor(block);
David Brazdildee58d62016-04-07 09:54:26 +0000123 return (*locals)[local];
124}
125
126void HInstructionBuilder::InitializeBlockLocals() {
127 current_locals_ = GetLocalsFor(current_block_);
128
129 if (current_block_->IsCatchBlock()) {
130 // Catch phis were already created and inputs collected from throwing sites.
131 if (kIsDebugBuild) {
132 // Make sure there was at least one throwing instruction which initialized
133 // locals (guaranteed by HGraphBuilder) and that all try blocks have been
134 // visited already (from HTryBoundary scoping and reverse post order).
135 bool catch_block_visited = false;
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100136 for (HBasicBlock* current : graph_->GetReversePostOrder()) {
David Brazdildee58d62016-04-07 09:54:26 +0000137 if (current == current_block_) {
138 catch_block_visited = true;
139 } else if (current->IsTryBlock()) {
140 const HTryBoundary& try_entry = current->GetTryCatchInformation()->GetTryEntry();
141 if (try_entry.HasExceptionHandler(*current_block_)) {
142 DCHECK(!catch_block_visited) << "Catch block visited before its try block.";
143 }
144 }
145 }
146 DCHECK_EQ(current_locals_->size(), graph_->GetNumberOfVRegs())
147 << "No instructions throwing into a live catch block.";
148 }
149 } else if (current_block_->IsLoopHeader()) {
150 // If the block is a loop header, we know we only have visited the pre header
151 // because we are visiting in reverse post order. We create phis for all initialized
152 // locals from the pre header. Their inputs will be populated at the end of
153 // the analysis.
154 for (size_t local = 0; local < current_locals_->size(); ++local) {
155 HInstruction* incoming =
156 ValueOfLocalAt(current_block_->GetLoopInformation()->GetPreHeader(), local);
157 if (incoming != nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100158 HPhi* phi = new (allocator_) HPhi(
159 allocator_,
David Brazdildee58d62016-04-07 09:54:26 +0000160 local,
161 0,
162 incoming->GetType());
163 current_block_->AddPhi(phi);
164 (*current_locals_)[local] = phi;
165 }
166 }
167
168 // Save the loop header so that the last phase of the analysis knows which
169 // blocks need to be updated.
170 loop_headers_.push_back(current_block_);
171 } else if (current_block_->GetPredecessors().size() > 0) {
172 // All predecessors have already been visited because we are visiting in reverse post order.
173 // We merge the values of all locals, creating phis if those values differ.
174 for (size_t local = 0; local < current_locals_->size(); ++local) {
175 bool one_predecessor_has_no_value = false;
176 bool is_different = false;
177 HInstruction* value = ValueOfLocalAt(current_block_->GetPredecessors()[0], local);
178
179 for (HBasicBlock* predecessor : current_block_->GetPredecessors()) {
180 HInstruction* current = ValueOfLocalAt(predecessor, local);
181 if (current == nullptr) {
182 one_predecessor_has_no_value = true;
183 break;
184 } else if (current != value) {
185 is_different = true;
186 }
187 }
188
189 if (one_predecessor_has_no_value) {
190 // If one predecessor has no value for this local, we trust the verifier has
191 // successfully checked that there is a store dominating any read after this block.
192 continue;
193 }
194
195 if (is_different) {
196 HInstruction* first_input = ValueOfLocalAt(current_block_->GetPredecessors()[0], local);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100197 HPhi* phi = new (allocator_) HPhi(
198 allocator_,
David Brazdildee58d62016-04-07 09:54:26 +0000199 local,
200 current_block_->GetPredecessors().size(),
201 first_input->GetType());
202 for (size_t i = 0; i < current_block_->GetPredecessors().size(); i++) {
203 HInstruction* pred_value = ValueOfLocalAt(current_block_->GetPredecessors()[i], local);
204 phi->SetRawInputAt(i, pred_value);
205 }
206 current_block_->AddPhi(phi);
207 value = phi;
208 }
209 (*current_locals_)[local] = value;
210 }
211 }
212}
213
214void HInstructionBuilder::PropagateLocalsToCatchBlocks() {
215 const HTryBoundary& try_entry = current_block_->GetTryCatchInformation()->GetTryEntry();
216 for (HBasicBlock* catch_block : try_entry.GetExceptionHandlers()) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100217 ScopedArenaVector<HInstruction*>* handler_locals = GetLocalsFor(catch_block);
David Brazdildee58d62016-04-07 09:54:26 +0000218 DCHECK_EQ(handler_locals->size(), current_locals_->size());
219 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
220 HInstruction* handler_value = (*handler_locals)[vreg];
221 if (handler_value == nullptr) {
222 // Vreg was undefined at a previously encountered throwing instruction
223 // and the catch phi was deleted. Do not record the local value.
224 continue;
225 }
226 DCHECK(handler_value->IsPhi());
227
228 HInstruction* local_value = (*current_locals_)[vreg];
229 if (local_value == nullptr) {
230 // This is the first instruction throwing into `catch_block` where
231 // `vreg` is undefined. Delete the catch phi.
232 catch_block->RemovePhi(handler_value->AsPhi());
233 (*handler_locals)[vreg] = nullptr;
234 } else {
235 // Vreg has been defined at all instructions throwing into `catch_block`
236 // encountered so far. Record the local value in the catch phi.
237 handler_value->AsPhi()->AddInput(local_value);
238 }
239 }
240 }
241}
242
243void HInstructionBuilder::AppendInstruction(HInstruction* instruction) {
244 current_block_->AddInstruction(instruction);
245 InitializeInstruction(instruction);
246}
247
248void HInstructionBuilder::InsertInstructionAtTop(HInstruction* instruction) {
249 if (current_block_->GetInstructions().IsEmpty()) {
250 current_block_->AddInstruction(instruction);
251 } else {
252 current_block_->InsertInstructionBefore(instruction, current_block_->GetFirstInstruction());
253 }
254 InitializeInstruction(instruction);
255}
256
257void HInstructionBuilder::InitializeInstruction(HInstruction* instruction) {
258 if (instruction->NeedsEnvironment()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100259 HEnvironment* environment = new (allocator_) HEnvironment(
260 allocator_,
David Brazdildee58d62016-04-07 09:54:26 +0000261 current_locals_->size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000262 graph_->GetArtMethod(),
David Brazdildee58d62016-04-07 09:54:26 +0000263 instruction->GetDexPc(),
David Brazdildee58d62016-04-07 09:54:26 +0000264 instruction);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100265 environment->CopyFrom(ArrayRef<HInstruction* const>(*current_locals_));
David Brazdildee58d62016-04-07 09:54:26 +0000266 instruction->SetRawEnvironment(environment);
267 }
268}
269
David Brazdilc120bbe2016-04-22 16:57:00 +0100270HInstruction* HInstructionBuilder::LoadNullCheckedLocal(uint32_t register_index, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100271 HInstruction* ref = LoadLocal(register_index, DataType::Type::kReference);
David Brazdilc120bbe2016-04-22 16:57:00 +0100272 if (!ref->CanBeNull()) {
273 return ref;
274 }
275
Vladimir Markoca6fff82017-10-03 14:49:14 +0100276 HNullCheck* null_check = new (allocator_) HNullCheck(ref, dex_pc);
David Brazdilc120bbe2016-04-22 16:57:00 +0100277 AppendInstruction(null_check);
278 return null_check;
279}
280
David Brazdildee58d62016-04-07 09:54:26 +0000281void HInstructionBuilder::SetLoopHeaderPhiInputs() {
282 for (size_t i = loop_headers_.size(); i > 0; --i) {
283 HBasicBlock* block = loop_headers_[i - 1];
284 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
285 HPhi* phi = it.Current()->AsPhi();
286 size_t vreg = phi->GetRegNumber();
287 for (HBasicBlock* predecessor : block->GetPredecessors()) {
288 HInstruction* value = ValueOfLocalAt(predecessor, vreg);
289 if (value == nullptr) {
290 // Vreg is undefined at this predecessor. Mark it dead and leave with
291 // fewer inputs than predecessors. SsaChecker will fail if not removed.
292 phi->SetDead();
293 break;
294 } else {
295 phi->AddInput(value);
296 }
297 }
298 }
299 }
300}
301
302static bool IsBlockPopulated(HBasicBlock* block) {
303 if (block->IsLoopHeader()) {
304 // Suspend checks were inserted into loop headers during building of dominator tree.
305 DCHECK(block->GetFirstInstruction()->IsSuspendCheck());
306 return block->GetFirstInstruction() != block->GetLastInstruction();
307 } else {
308 return !block->GetInstructions().IsEmpty();
309 }
310}
311
312bool HInstructionBuilder::Build() {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800313 DCHECK(code_item_accessor_.HasCodeItem());
Vladimir Marko69d310e2017-10-09 14:12:23 +0100314 locals_for_.resize(
315 graph_->GetBlocks().size(),
316 ScopedArenaVector<HInstruction*>(local_allocator_->Adapter(kArenaAllocGraphBuilder)));
David Brazdildee58d62016-04-07 09:54:26 +0000317
318 // Find locations where we want to generate extra stackmaps for native debugging.
319 // This allows us to generate the info only at interesting points (for example,
320 // at start of java statement) rather than before every dex instruction.
Vladimir Marko3b506202018-10-31 14:33:58 +0000321 const bool native_debuggable = code_generator_ != nullptr &&
322 code_generator_->GetCompilerOptions().GetNativeDebuggable();
David Brazdildee58d62016-04-07 09:54:26 +0000323 ArenaBitVector* native_debug_info_locations = nullptr;
324 if (native_debuggable) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100325 native_debug_info_locations = FindNativeDebugInfoLocations();
David Brazdildee58d62016-04-07 09:54:26 +0000326 }
327
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100328 for (HBasicBlock* block : graph_->GetReversePostOrder()) {
329 current_block_ = block;
David Brazdildee58d62016-04-07 09:54:26 +0000330 uint32_t block_dex_pc = current_block_->GetDexPc();
331
332 InitializeBlockLocals();
333
334 if (current_block_->IsEntryBlock()) {
335 InitializeParameters();
Vladimir Markoca6fff82017-10-03 14:49:14 +0100336 AppendInstruction(new (allocator_) HSuspendCheck(0u));
337 AppendInstruction(new (allocator_) HGoto(0u));
David Brazdildee58d62016-04-07 09:54:26 +0000338 continue;
339 } else if (current_block_->IsExitBlock()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100340 AppendInstruction(new (allocator_) HExit());
David Brazdildee58d62016-04-07 09:54:26 +0000341 continue;
342 } else if (current_block_->IsLoopHeader()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100343 HSuspendCheck* suspend_check = new (allocator_) HSuspendCheck(current_block_->GetDexPc());
David Brazdildee58d62016-04-07 09:54:26 +0000344 current_block_->GetLoopInformation()->SetSuspendCheck(suspend_check);
345 // This is slightly odd because the loop header might not be empty (TryBoundary).
346 // But we're still creating the environment with locals from the top of the block.
347 InsertInstructionAtTop(suspend_check);
348 }
349
350 if (block_dex_pc == kNoDexPc || current_block_ != block_builder_->GetBlockAt(block_dex_pc)) {
351 // Synthetic block that does not need to be populated.
352 DCHECK(IsBlockPopulated(current_block_));
353 continue;
354 }
355
356 DCHECK(!IsBlockPopulated(current_block_));
357
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700358 uint32_t quicken_index = 0;
359 if (CanDecodeQuickenedInfo()) {
360 quicken_index = block_builder_->GetQuickenIndex(block_dex_pc);
361 }
362
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800363 for (const DexInstructionPcPair& pair : code_item_accessor_.InstructionsFrom(block_dex_pc)) {
David Brazdildee58d62016-04-07 09:54:26 +0000364 if (current_block_ == nullptr) {
365 // The previous instruction ended this block.
366 break;
367 }
368
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800369 const uint32_t dex_pc = pair.DexPc();
David Brazdildee58d62016-04-07 09:54:26 +0000370 if (dex_pc != block_dex_pc && FindBlockStartingAt(dex_pc) != nullptr) {
371 // This dex_pc starts a new basic block.
372 break;
373 }
374
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800375 if (current_block_->IsTryBlock() && IsThrowingDexInstruction(pair.Inst())) {
David Brazdildee58d62016-04-07 09:54:26 +0000376 PropagateLocalsToCatchBlocks();
377 }
378
379 if (native_debuggable && native_debug_info_locations->IsBitSet(dex_pc)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100380 AppendInstruction(new (allocator_) HNativeDebugInfo(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000381 }
382
Vladimir Markof91fc122020-05-13 09:21:00 +0100383 // Note: There may be no Thread for gtests.
384 DCHECK(Thread::Current() == nullptr || !Thread::Current()->IsExceptionPending())
Nicolas Geoffray605c5912020-04-08 15:12:39 +0100385 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
386 << " " << pair.Inst().Name() << "@" << dex_pc;
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800387 if (!ProcessDexInstruction(pair.Inst(), dex_pc, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +0000388 return false;
389 }
Vladimir Markof91fc122020-05-13 09:21:00 +0100390 DCHECK(Thread::Current() == nullptr || !Thread::Current()->IsExceptionPending())
Nicolas Geoffrayfbf53b52020-04-01 15:20:14 +0100391 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
Nicolas Geoffray605c5912020-04-08 15:12:39 +0100392 << " " << pair.Inst().Name() << "@" << dex_pc;
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700393
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800394 if (QuickenInfoTable::NeedsIndexForInstruction(&pair.Inst())) {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700395 ++quicken_index;
396 }
David Brazdildee58d62016-04-07 09:54:26 +0000397 }
398
399 if (current_block_ != nullptr) {
400 // Branching instructions clear current_block, so we know the last
401 // instruction of the current block is not a branching instruction.
402 // We add an unconditional Goto to the next block.
403 DCHECK_EQ(current_block_->GetSuccessors().size(), 1u);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100404 AppendInstruction(new (allocator_) HGoto());
David Brazdildee58d62016-04-07 09:54:26 +0000405 }
406 }
407
408 SetLoopHeaderPhiInputs();
409
410 return true;
411}
412
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000413void HInstructionBuilder::BuildIntrinsic(ArtMethod* method) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800414 DCHECK(!code_item_accessor_.HasCodeItem());
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000415 DCHECK(method->IsIntrinsic());
416
417 locals_for_.resize(
418 graph_->GetBlocks().size(),
419 ScopedArenaVector<HInstruction*>(local_allocator_->Adapter(kArenaAllocGraphBuilder)));
420
421 // Fill the entry block. Do not add suspend check, we do not want a suspend
422 // check in intrinsics; intrinsic methods are supposed to be fast.
423 current_block_ = graph_->GetEntryBlock();
424 InitializeBlockLocals();
425 InitializeParameters();
426 AppendInstruction(new (allocator_) HGoto(0u));
427
428 // Fill the body.
429 current_block_ = current_block_->GetSingleSuccessor();
430 InitializeBlockLocals();
431 DCHECK(!IsBlockPopulated(current_block_));
432
Vladimir Marko5f846072020-04-09 13:20:11 +0100433 // Add the intermediate representation, if available, or invoke instruction.
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000434 size_t in_vregs = graph_->GetNumberOfInVRegs();
435 size_t number_of_arguments =
436 in_vregs - std::count(current_locals_->end() - in_vregs, current_locals_->end(), nullptr);
437 uint32_t method_idx = dex_compilation_unit_->GetDexMethodIndex();
Vladimir Marko5f846072020-04-09 13:20:11 +0100438 const char* shorty = dex_file_->GetMethodShorty(method_idx);
Treehugger Robot2c5827a2018-05-17 22:26:08 +0000439 RangeInstructionOperands operands(graph_->GetNumberOfVRegs() - in_vregs, in_vregs);
Vladimir Marko5f846072020-04-09 13:20:11 +0100440 if (!BuildSimpleIntrinsic(method, kNoDexPc, operands, shorty)) {
441 // Some intrinsics without intermediate representation still yield a leaf method,
442 // so build the invoke. Use HInvokeStaticOrDirect even for methods that would
443 // normally use an HInvokeVirtual (sharpen the call).
444 MethodReference target_method(dex_file_, method_idx);
445 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
446 HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall,
447 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
448 /* method_load_data= */ 0u
449 };
450 InvokeType invoke_type = dex_compilation_unit_->IsStatic() ? kStatic : kDirect;
451 HInvokeStaticOrDirect* invoke = new (allocator_) HInvokeStaticOrDirect(
452 allocator_,
453 number_of_arguments,
454 return_type_,
455 kNoDexPc,
456 method_idx,
457 method,
458 dispatch_info,
459 invoke_type,
460 target_method,
461 HInvokeStaticOrDirect::ClinitCheckRequirement::kNone);
462 HandleInvoke(invoke, operands, shorty, /* is_unresolved= */ false);
463 }
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000464
465 // Add the return instruction.
466 if (return_type_ == DataType::Type::kVoid) {
467 AppendInstruction(new (allocator_) HReturnVoid());
468 } else {
Vladimir Marko5f846072020-04-09 13:20:11 +0100469 AppendInstruction(new (allocator_) HReturn(latest_result_));
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000470 }
471
472 // Fill the exit block.
473 DCHECK_EQ(current_block_->GetSingleSuccessor(), graph_->GetExitBlock());
474 current_block_ = graph_->GetExitBlock();
475 InitializeBlockLocals();
476 AppendInstruction(new (allocator_) HExit());
477}
478
Vladimir Marko69d310e2017-10-09 14:12:23 +0100479ArenaBitVector* HInstructionBuilder::FindNativeDebugInfoLocations() {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100480 ArenaBitVector* locations = ArenaBitVector::Create(local_allocator_,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800481 code_item_accessor_.InsnsSizeInCodeUnits(),
Andreas Gampe3db70682018-12-26 15:12:03 -0800482 /* expandable= */ false,
Vladimir Marko69d310e2017-10-09 14:12:23 +0100483 kArenaAllocGraphBuilder);
484 locations->ClearAllBits();
Mathieu Chartier3e2e1232018-09-11 12:35:30 -0700485 // The visitor gets called when the line number changes.
486 // In other words, it marks the start of new java statement.
487 code_item_accessor_.DecodeDebugPositionInfo([&](const DexFile::PositionInfo& entry) {
488 locations->SetBit(entry.address_);
489 return false;
490 });
David Brazdildee58d62016-04-07 09:54:26 +0000491 // Instruction-specific tweaks.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800492 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800493 switch (inst->Opcode()) {
David Brazdildee58d62016-04-07 09:54:26 +0000494 case Instruction::MOVE_EXCEPTION: {
495 // Stop in native debugger after the exception has been moved.
496 // The compiler also expects the move at the start of basic block so
497 // we do not want to interfere by inserting native-debug-info before it.
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800498 locations->ClearBit(inst.DexPc());
499 DexInstructionIterator next = std::next(DexInstructionIterator(inst));
500 DCHECK(next.DexPc() != inst.DexPc());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800501 if (next != code_item_accessor_.end()) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -0700502 locations->SetBit(next.DexPc());
David Brazdildee58d62016-04-07 09:54:26 +0000503 }
504 break;
505 }
506 default:
507 break;
508 }
509 }
Vladimir Marko69d310e2017-10-09 14:12:23 +0100510 return locations;
David Brazdildee58d62016-04-07 09:54:26 +0000511}
512
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100513HInstruction* HInstructionBuilder::LoadLocal(uint32_t reg_number, DataType::Type type) const {
David Brazdildee58d62016-04-07 09:54:26 +0000514 HInstruction* value = (*current_locals_)[reg_number];
515 DCHECK(value != nullptr);
516
517 // If the operation requests a specific type, we make sure its input is of that type.
518 if (type != value->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100519 if (DataType::IsFloatingPointType(type)) {
Aart Bik31883642016-06-06 15:02:44 -0700520 value = ssa_builder_->GetFloatOrDoubleEquivalent(value, type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100521 } else if (type == DataType::Type::kReference) {
Aart Bik31883642016-06-06 15:02:44 -0700522 value = ssa_builder_->GetReferenceTypeEquivalent(value);
David Brazdildee58d62016-04-07 09:54:26 +0000523 }
Aart Bik31883642016-06-06 15:02:44 -0700524 DCHECK(value != nullptr);
David Brazdildee58d62016-04-07 09:54:26 +0000525 }
526
527 return value;
528}
529
530void HInstructionBuilder::UpdateLocal(uint32_t reg_number, HInstruction* stored_value) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100531 DataType::Type stored_type = stored_value->GetType();
532 DCHECK_NE(stored_type, DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +0000533
534 // Storing into vreg `reg_number` may implicitly invalidate the surrounding
535 // registers. Consider the following cases:
536 // (1) Storing a wide value must overwrite previous values in both `reg_number`
537 // and `reg_number+1`. We store `nullptr` in `reg_number+1`.
538 // (2) If vreg `reg_number-1` holds a wide value, writing into `reg_number`
539 // must invalidate it. We store `nullptr` in `reg_number-1`.
540 // Consequently, storing a wide value into the high vreg of another wide value
541 // will invalidate both `reg_number-1` and `reg_number+1`.
542
543 if (reg_number != 0) {
544 HInstruction* local_low = (*current_locals_)[reg_number - 1];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100545 if (local_low != nullptr && DataType::Is64BitType(local_low->GetType())) {
David Brazdildee58d62016-04-07 09:54:26 +0000546 // The vreg we are storing into was previously the high vreg of a pair.
547 // We need to invalidate its low vreg.
548 DCHECK((*current_locals_)[reg_number] == nullptr);
549 (*current_locals_)[reg_number - 1] = nullptr;
550 }
551 }
552
553 (*current_locals_)[reg_number] = stored_value;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100554 if (DataType::Is64BitType(stored_type)) {
David Brazdildee58d62016-04-07 09:54:26 +0000555 // We are storing a pair. Invalidate the instruction in the high vreg.
556 (*current_locals_)[reg_number + 1] = nullptr;
557 }
558}
559
560void HInstructionBuilder::InitializeParameters() {
561 DCHECK(current_block_->IsEntryBlock());
562
Vladimir Marko69d310e2017-10-09 14:12:23 +0100563 // outer_compilation_unit_ is null only when unit testing.
564 if (outer_compilation_unit_ == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +0000565 return;
566 }
567
568 const char* shorty = dex_compilation_unit_->GetShorty();
569 uint16_t number_of_parameters = graph_->GetNumberOfInVRegs();
570 uint16_t locals_index = graph_->GetNumberOfLocalVRegs();
571 uint16_t parameter_index = 0;
572
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800573 const dex::MethodId& referrer_method_id =
David Brazdildee58d62016-04-07 09:54:26 +0000574 dex_file_->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
575 if (!dex_compilation_unit_->IsStatic()) {
576 // Add the implicit 'this' argument, not expressed in the signature.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100577 HParameterValue* parameter = new (allocator_) HParameterValue(*dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +0000578 referrer_method_id.class_idx_,
579 parameter_index++,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100580 DataType::Type::kReference,
Andreas Gampe3db70682018-12-26 15:12:03 -0800581 /* is_this= */ true);
David Brazdildee58d62016-04-07 09:54:26 +0000582 AppendInstruction(parameter);
583 UpdateLocal(locals_index++, parameter);
584 number_of_parameters--;
Igor Murashkind01745e2017-04-05 16:40:31 -0700585 current_this_parameter_ = parameter;
586 } else {
587 DCHECK(current_this_parameter_ == nullptr);
David Brazdildee58d62016-04-07 09:54:26 +0000588 }
589
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800590 const dex::ProtoId& proto = dex_file_->GetMethodPrototype(referrer_method_id);
591 const dex::TypeList* arg_types = dex_file_->GetProtoParameters(proto);
David Brazdildee58d62016-04-07 09:54:26 +0000592 for (int i = 0, shorty_pos = 1; i < number_of_parameters; i++) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100593 HParameterValue* parameter = new (allocator_) HParameterValue(
David Brazdildee58d62016-04-07 09:54:26 +0000594 *dex_file_,
595 arg_types->GetTypeItem(shorty_pos - 1).type_idx_,
596 parameter_index++,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100597 DataType::FromShorty(shorty[shorty_pos]),
Andreas Gampe3db70682018-12-26 15:12:03 -0800598 /* is_this= */ false);
David Brazdildee58d62016-04-07 09:54:26 +0000599 ++shorty_pos;
600 AppendInstruction(parameter);
601 // Store the parameter value in the local that the dex code will use
602 // to reference that parameter.
603 UpdateLocal(locals_index++, parameter);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100604 if (DataType::Is64BitType(parameter->GetType())) {
David Brazdildee58d62016-04-07 09:54:26 +0000605 i++;
606 locals_index++;
607 parameter_index++;
608 }
609 }
610}
611
612template<typename T>
613void HInstructionBuilder::If_22t(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100614 HInstruction* first = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
615 HInstruction* second = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100616 T* comparison = new (allocator_) T(first, second, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000617 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100618 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000619 current_block_ = nullptr;
620}
621
622template<typename T>
623void HInstructionBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100624 HInstruction* value = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100625 T* comparison = new (allocator_) T(value, graph_->GetIntConstant(0, dex_pc), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000626 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100627 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000628 current_block_ = nullptr;
629}
630
631template<typename T>
632void HInstructionBuilder::Unop_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100633 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000634 uint32_t dex_pc) {
635 HInstruction* first = LoadLocal(instruction.VRegB(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100636 AppendInstruction(new (allocator_) T(type, first, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000637 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
638}
639
640void HInstructionBuilder::Conversion_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100641 DataType::Type input_type,
642 DataType::Type result_type,
David Brazdildee58d62016-04-07 09:54:26 +0000643 uint32_t dex_pc) {
644 HInstruction* first = LoadLocal(instruction.VRegB(), input_type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100645 AppendInstruction(new (allocator_) HTypeConversion(result_type, first, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000646 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
647}
648
649template<typename T>
650void HInstructionBuilder::Binop_23x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100651 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000652 uint32_t dex_pc) {
653 HInstruction* first = LoadLocal(instruction.VRegB(), type);
654 HInstruction* second = LoadLocal(instruction.VRegC(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100655 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000656 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
657}
658
659template<typename T>
660void HInstructionBuilder::Binop_23x_shift(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100661 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000662 uint32_t dex_pc) {
663 HInstruction* first = LoadLocal(instruction.VRegB(), type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100664 HInstruction* second = LoadLocal(instruction.VRegC(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100665 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000666 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
667}
668
669void HInstructionBuilder::Binop_23x_cmp(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100670 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000671 ComparisonBias bias,
672 uint32_t dex_pc) {
673 HInstruction* first = LoadLocal(instruction.VRegB(), type);
674 HInstruction* second = LoadLocal(instruction.VRegC(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100675 AppendInstruction(new (allocator_) HCompare(type, first, second, bias, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000676 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
677}
678
679template<typename T>
680void HInstructionBuilder::Binop_12x_shift(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100681 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000682 uint32_t dex_pc) {
683 HInstruction* first = LoadLocal(instruction.VRegA(), type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100684 HInstruction* second = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100685 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000686 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
687}
688
689template<typename T>
690void HInstructionBuilder::Binop_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100691 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000692 uint32_t dex_pc) {
693 HInstruction* first = LoadLocal(instruction.VRegA(), type);
694 HInstruction* second = LoadLocal(instruction.VRegB(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100695 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000696 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
697}
698
699template<typename T>
700void HInstructionBuilder::Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100701 HInstruction* first = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000702 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s(), dex_pc);
703 if (reverse) {
704 std::swap(first, second);
705 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100706 AppendInstruction(new (allocator_) T(DataType::Type::kInt32, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000707 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
708}
709
710template<typename T>
711void HInstructionBuilder::Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100712 HInstruction* first = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000713 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b(), dex_pc);
714 if (reverse) {
715 std::swap(first, second);
716 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100717 AppendInstruction(new (allocator_) T(DataType::Type::kInt32, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000718 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
719}
720
Igor Murashkind01745e2017-04-05 16:40:31 -0700721// Does the method being compiled need any constructor barriers being inserted?
722// (Always 'false' for methods that aren't <init>.)
Vladimir Markoc1c34522018-10-31 13:56:49 +0000723static bool RequiresConstructorBarrier(const DexCompilationUnit* cu) {
Igor Murashkin032cacd2017-04-06 14:40:08 -0700724 // Can be null in unit tests only.
725 if (UNLIKELY(cu == nullptr)) {
726 return false;
727 }
728
Vladimir Markoc1c34522018-10-31 13:56:49 +0000729 // Constructor barriers are applicable only for <init> methods.
730 if (LIKELY(!cu->IsConstructor() || cu->IsStatic())) {
731 return false;
732 }
733
734 return cu->RequiresConstructorBarrier();
David Brazdildee58d62016-04-07 09:54:26 +0000735}
736
737// Returns true if `block` has only one successor which starts at the next
738// dex_pc after `instruction` at `dex_pc`.
739static bool IsFallthroughInstruction(const Instruction& instruction,
740 uint32_t dex_pc,
741 HBasicBlock* block) {
742 uint32_t next_dex_pc = dex_pc + instruction.SizeInCodeUnits();
743 return block->GetSingleSuccessor()->GetDexPc() == next_dex_pc;
744}
745
746void HInstructionBuilder::BuildSwitch(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100747 HInstruction* value = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000748 DexSwitchTable table(instruction, dex_pc);
749
750 if (table.GetNumEntries() == 0) {
751 // Empty Switch. Code falls through to the next block.
752 DCHECK(IsFallthroughInstruction(instruction, dex_pc, current_block_));
Vladimir Markoca6fff82017-10-03 14:49:14 +0100753 AppendInstruction(new (allocator_) HGoto(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000754 } else if (table.ShouldBuildDecisionTree()) {
755 for (DexSwitchTableIterator it(table); !it.Done(); it.Advance()) {
756 HInstruction* case_value = graph_->GetIntConstant(it.CurrentKey(), dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100757 HEqual* comparison = new (allocator_) HEqual(value, case_value, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000758 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100759 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000760
761 if (!it.IsLast()) {
762 current_block_ = FindBlockStartingAt(it.GetDexPcForCurrentIndex());
763 }
764 }
765 } else {
766 AppendInstruction(
Vladimir Markoca6fff82017-10-03 14:49:14 +0100767 new (allocator_) HPackedSwitch(table.GetEntryAt(0), table.GetNumEntries(), value, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000768 }
769
770 current_block_ = nullptr;
771}
772
773void HInstructionBuilder::BuildReturn(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100774 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000775 uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100776 if (type == DataType::Type::kVoid) {
Igor Murashkind01745e2017-04-05 16:40:31 -0700777 // Only <init> (which is a return-void) could possibly have a constructor fence.
Igor Murashkin032cacd2017-04-06 14:40:08 -0700778 // This may insert additional redundant constructor fences from the super constructors.
779 // TODO: remove redundant constructor fences (b/36656456).
Vladimir Markoc1c34522018-10-31 13:56:49 +0000780 if (RequiresConstructorBarrier(dex_compilation_unit_)) {
Igor Murashkind01745e2017-04-05 16:40:31 -0700781 // Compiling instance constructor.
Vladimir Markoba118822017-06-12 15:41:56 +0100782 DCHECK_STREQ("<init>", graph_->GetMethodName());
Igor Murashkind01745e2017-04-05 16:40:31 -0700783
784 HInstruction* fence_target = current_this_parameter_;
785 DCHECK(fence_target != nullptr);
786
Vladimir Markoca6fff82017-10-03 14:49:14 +0100787 AppendInstruction(new (allocator_) HConstructorFence(fence_target, dex_pc, allocator_));
Igor Murashkin6ef45672017-08-08 13:59:55 -0700788 MaybeRecordStat(
789 compilation_stats_,
790 MethodCompilationStat::kConstructorFenceGeneratedFinal);
David Brazdildee58d62016-04-07 09:54:26 +0000791 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100792 AppendInstruction(new (allocator_) HReturnVoid(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000793 } else {
Vladimir Markoc1c34522018-10-31 13:56:49 +0000794 DCHECK(!RequiresConstructorBarrier(dex_compilation_unit_));
David Brazdildee58d62016-04-07 09:54:26 +0000795 HInstruction* value = LoadLocal(instruction.VRegA(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100796 AppendInstruction(new (allocator_) HReturn(value, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000797 }
798 current_block_ = nullptr;
799}
800
801static InvokeType GetInvokeTypeFromOpCode(Instruction::Code opcode) {
802 switch (opcode) {
803 case Instruction::INVOKE_STATIC:
804 case Instruction::INVOKE_STATIC_RANGE:
805 return kStatic;
806 case Instruction::INVOKE_DIRECT:
807 case Instruction::INVOKE_DIRECT_RANGE:
808 return kDirect;
809 case Instruction::INVOKE_VIRTUAL:
810 case Instruction::INVOKE_VIRTUAL_QUICK:
811 case Instruction::INVOKE_VIRTUAL_RANGE:
812 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
813 return kVirtual;
814 case Instruction::INVOKE_INTERFACE:
815 case Instruction::INVOKE_INTERFACE_RANGE:
816 return kInterface;
817 case Instruction::INVOKE_SUPER_RANGE:
818 case Instruction::INVOKE_SUPER:
819 return kSuper;
820 default:
821 LOG(FATAL) << "Unexpected invoke opcode: " << opcode;
822 UNREACHABLE();
823 }
824}
825
Vladimir Marko2f40d242020-04-08 12:56:45 +0100826// Try to resolve a method using the class linker. Return null if a method could
827// not be resolved or the resolved method cannot be used for some reason.
828// Also retrieve method data needed for creating the invoke intermediate
829// representation while we hold the mutator lock here.
830static ArtMethod* ResolveMethod(uint16_t method_idx,
831 ArtMethod* referrer,
832 const DexCompilationUnit& dex_compilation_unit,
833 /*inout*/InvokeType* invoke_type,
834 /*out*/MethodReference* target_method,
835 /*out*/bool* is_string_constructor) {
David Brazdildee58d62016-04-07 09:54:26 +0000836 ScopedObjectAccess soa(Thread::Current());
David Brazdildee58d62016-04-07 09:54:26 +0000837
Vladimir Marko2f40d242020-04-08 12:56:45 +0100838 ClassLinker* class_linker = dex_compilation_unit.GetClassLinker();
839 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit.GetClassLoader();
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100840
Vladimir Markoba118822017-06-12 15:41:56 +0100841 ArtMethod* resolved_method =
842 class_linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
Vladimir Markoba118822017-06-12 15:41:56 +0100843 method_idx,
Vladimir Marko2f40d242020-04-08 12:56:45 +0100844 dex_compilation_unit.GetDexCache(),
Vladimir Markoba118822017-06-12 15:41:56 +0100845 class_loader,
Vladimir Marko2f40d242020-04-08 12:56:45 +0100846 referrer,
847 *invoke_type);
David Brazdildee58d62016-04-07 09:54:26 +0000848
849 if (UNLIKELY(resolved_method == nullptr)) {
850 // Clean up any exception left by type resolution.
851 soa.Self()->ClearException();
852 return nullptr;
853 }
Nicolas Geoffray605c5912020-04-08 15:12:39 +0100854 DCHECK(!soa.Self()->IsExceptionPending());
David Brazdildee58d62016-04-07 09:54:26 +0000855
Vladimir Markoba118822017-06-12 15:41:56 +0100856 // The referrer may be unresolved for AOT if we're compiling a class that cannot be
857 // resolved because, for example, we don't find a superclass in the classpath.
Vladimir Marko2f40d242020-04-08 12:56:45 +0100858 if (referrer == nullptr) {
Vladimir Markoba118822017-06-12 15:41:56 +0100859 // The class linker cannot check access without a referrer, so we have to do it.
860 // Fall back to HInvokeUnresolved if the method isn't public.
David Brazdildee58d62016-04-07 09:54:26 +0000861 if (!resolved_method->IsPublic()) {
862 return nullptr;
863 }
David Brazdildee58d62016-04-07 09:54:26 +0000864 }
865
866 // We have to special case the invoke-super case, as ClassLinker::ResolveMethod does not.
867 // We need to look at the referrer's super class vtable. We need to do this to know if we need to
868 // make this an invoke-unresolved to handle cross-dex invokes or abstract super methods, both of
869 // which require runtime handling.
Vladimir Marko2f40d242020-04-08 12:56:45 +0100870 if (*invoke_type == kSuper) {
871 ObjPtr<mirror::Class> compiling_class = dex_compilation_unit.GetCompilingClass().Get();
Andreas Gampefa4333d2017-02-14 11:10:34 -0800872 if (compiling_class == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +0000873 // We could not determine the method's class we need to wait until runtime.
874 DCHECK(Runtime::Current()->IsAotCompiler());
875 return nullptr;
876 }
Vladimir Markoba118822017-06-12 15:41:56 +0100877 ObjPtr<mirror::Class> referenced_class = class_linker->LookupResolvedType(
Vladimir Marko2f40d242020-04-08 12:56:45 +0100878 dex_compilation_unit.GetDexFile()->GetMethodId(method_idx).class_idx_,
879 dex_compilation_unit.GetDexCache().Get(),
Vladimir Markoba118822017-06-12 15:41:56 +0100880 class_loader.Get());
881 DCHECK(referenced_class != nullptr); // We have already resolved a method from this class.
882 if (!referenced_class->IsAssignableFrom(compiling_class)) {
Aart Bikf663e342016-04-04 17:28:59 -0700883 // We cannot statically determine the target method. The runtime will throw a
884 // NoSuchMethodError on this one.
885 return nullptr;
886 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100887 ArtMethod* actual_method;
Vladimir Markoba118822017-06-12 15:41:56 +0100888 if (referenced_class->IsInterface()) {
889 actual_method = referenced_class->FindVirtualMethodForInterfaceSuper(
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100890 resolved_method, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000891 } else {
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100892 uint16_t vtable_index = resolved_method->GetMethodIndex();
893 actual_method = compiling_class->GetSuperClass()->GetVTableEntry(
894 vtable_index, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000895 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100896 if (actual_method != resolved_method &&
Vladimir Marko2f40d242020-04-08 12:56:45 +0100897 !IsSameDexFile(*actual_method->GetDexFile(), *dex_compilation_unit.GetDexFile())) {
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100898 // The back-end code generator relies on this check in order to ensure that it will not
899 // attempt to read the dex_cache with a dex_method_index that is not from the correct
900 // 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 +0000901 // builder, which means that the code-generator (and sharpening and inliner, maybe)
902 // might invoke an incorrect method.
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100903 // TODO: The actual method could still be referenced in the current dex file, so we
904 // could try locating it.
905 // TODO: Remove the dex_file restriction.
906 return nullptr;
907 }
908 if (!actual_method->IsInvokable()) {
909 // Fail if the actual method cannot be invoked. Otherwise, the runtime resolution stub
910 // could resolve the callee to the wrong method.
911 return nullptr;
912 }
913 resolved_method = actual_method;
David Brazdildee58d62016-04-07 09:54:26 +0000914 }
915
Vladimir Marko2f40d242020-04-08 12:56:45 +0100916 if (*invoke_type == kInterface) {
917 if (resolved_method->GetDeclaringClass()->IsObjectClass()) {
918 // If the resolved method is from j.l.Object, emit a virtual call instead.
919 // The IMT conflict stub only handles interface methods.
920 *invoke_type = kVirtual;
921 } else {
922 DCHECK(resolved_method->GetDeclaringClass()->IsInterface());
923 }
924 }
David Brazdildee58d62016-04-07 09:54:26 +0000925
Vladimir Marko2f40d242020-04-08 12:56:45 +0100926 if (*invoke_type == kDirect || *invoke_type == kStatic || *invoke_type == kSuper) {
927 // Record the target method needed for HInvokeStaticOrDirect.
928 *target_method =
929 MethodReference(resolved_method->GetDexFile(), resolved_method->GetDexMethodIndex());
930 } else if (*invoke_type == kVirtual) {
931 // For HInvokeVirtual we need the vtable index.
932 *target_method = MethodReference(/*file=*/ nullptr, resolved_method->GetVtableIndex());
933 } else {
934 DCHECK_EQ(*invoke_type, kInterface);
935 // For HInvokeInterface we need the IMT index.
936 *target_method = MethodReference(/*file=*/ nullptr, ImTable::GetImtIndex(resolved_method));
937 }
938
939 *is_string_constructor =
940 resolved_method->IsConstructor() && resolved_method->GetDeclaringClass()->IsStringClass();
941
942 return resolved_method;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100943}
944
David Brazdildee58d62016-04-07 09:54:26 +0000945bool HInstructionBuilder::BuildInvoke(const Instruction& instruction,
946 uint32_t dex_pc,
947 uint32_t method_idx,
Treehugger Robot2c5827a2018-05-17 22:26:08 +0000948 const InstructionOperands& operands) {
David Brazdildee58d62016-04-07 09:54:26 +0000949 InvokeType invoke_type = GetInvokeTypeFromOpCode(instruction.Opcode());
Orion Hodson4c8e12e2018-05-18 08:33:20 +0100950 const char* shorty = dex_file_->GetMethodShorty(method_idx);
951 DataType::Type return_type = DataType::FromShorty(shorty[0]);
David Brazdildee58d62016-04-07 09:54:26 +0000952
953 // Remove the return type from the 'proto'.
Orion Hodson4c8e12e2018-05-18 08:33:20 +0100954 size_t number_of_arguments = strlen(shorty) - 1;
David Brazdildee58d62016-04-07 09:54:26 +0000955 if (invoke_type != kStatic) { // instance call
956 // One extra argument for 'this'.
957 number_of_arguments++;
958 }
959
Vladimir Marko2f40d242020-04-08 12:56:45 +0100960 MethodReference target_method(nullptr, 0u);
961 bool is_string_constructor = false;
962 ArtMethod* resolved_method = ResolveMethod(method_idx,
963 graph_->GetArtMethod(),
964 *dex_compilation_unit_,
965 &invoke_type,
966 &target_method,
967 &is_string_constructor);
David Brazdildee58d62016-04-07 09:54:26 +0000968
969 if (UNLIKELY(resolved_method == nullptr)) {
Nicolas Geoffray605c5912020-04-08 15:12:39 +0100970 DCHECK(!Thread::Current()->IsExceptionPending());
Igor Murashkin1e065a52017-08-09 13:20:34 -0700971 MaybeRecordStat(compilation_stats_,
972 MethodCompilationStat::kUnresolvedMethod);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100973 HInvoke* invoke = new (allocator_) HInvokeUnresolved(allocator_,
974 number_of_arguments,
975 return_type,
976 dex_pc,
977 method_idx,
978 invoke_type);
Andreas Gampe3db70682018-12-26 15:12:03 -0800979 return HandleInvoke(invoke, operands, shorty, /* is_unresolved= */ true);
David Brazdildee58d62016-04-07 09:54:26 +0000980 }
981
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100982 // Replace calls to String.<init> with StringFactory.
Vladimir Marko2f40d242020-04-08 12:56:45 +0100983 if (is_string_constructor) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100984 uint32_t string_init_entry_point = WellKnownClasses::StringInitToEntryPoint(resolved_method);
985 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
986 HInvokeStaticOrDirect::MethodLoadKind::kStringInit,
987 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000988 dchecked_integral_cast<uint64_t>(string_init_entry_point)
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100989 };
Nicolas Geoffrayf665f842018-02-15 12:29:06 +0000990 // We pass null for the resolved_method to ensure optimizations
991 // don't rely on it.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100992 HInvoke* invoke = new (allocator_) HInvokeStaticOrDirect(
993 allocator_,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100994 number_of_arguments - 1,
Andreas Gampe3db70682018-12-26 15:12:03 -0800995 /* return_type= */ DataType::Type::kReference,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100996 dex_pc,
997 method_idx,
Andreas Gampe3db70682018-12-26 15:12:03 -0800998 /* resolved_method= */ nullptr,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100999 dispatch_info,
1000 invoke_type,
1001 target_method,
1002 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001003 return HandleStringInit(invoke, operands, shorty);
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01001004 }
1005
David Brazdildee58d62016-04-07 09:54:26 +00001006 // Potential class initialization check, in the case of a static method call.
Vladimir Marko2f40d242020-04-08 12:56:45 +01001007 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement =
1008 HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
David Brazdildee58d62016-04-07 09:54:26 +00001009 HClinitCheck* clinit_check = nullptr;
Vladimir Marko2f40d242020-04-08 12:56:45 +01001010 if (invoke_type == kStatic) {
1011 clinit_check = ProcessClinitCheckForInvoke(dex_pc, resolved_method, &clinit_check_requirement);
1012 }
1013
Vladimir Marko5f846072020-04-09 13:20:11 +01001014 // Try to build an HIR replacement for the intrinsic.
1015 if (UNLIKELY(resolved_method->IsIntrinsic())) {
1016 // All intrinsics are in the primary boot image, so their class can always be referenced
1017 // and we do not need to rely on the implicit class initialization check. The class should
1018 // be initialized but we do not require that here.
1019 DCHECK_NE(clinit_check_requirement, HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit);
1020 if (BuildSimpleIntrinsic(resolved_method, dex_pc, operands, shorty)) {
1021 return true;
1022 }
1023 }
1024
David Brazdildee58d62016-04-07 09:54:26 +00001025 HInvoke* invoke = nullptr;
1026 if (invoke_type == kDirect || invoke_type == kStatic || invoke_type == kSuper) {
Vladimir Marko2f40d242020-04-08 12:56:45 +01001027 if (invoke_type == kSuper) {
1028 if (IsSameDexFile(*target_method.dex_file, *dex_compilation_unit_->GetDexFile())) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001029 // Update the method index to the one resolved. Note that this may be a no-op if
David Brazdildee58d62016-04-07 09:54:26 +00001030 // we resolved to the method referenced by the instruction.
Vladimir Marko2f40d242020-04-08 12:56:45 +01001031 method_idx = target_method.index;
David Brazdildee58d62016-04-07 09:54:26 +00001032 }
1033 }
1034
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01001035 HInvokeStaticOrDirect::DispatchInfo dispatch_info =
1036 HSharpening::SharpenInvokeStaticOrDirect(resolved_method, code_generator_);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001037 invoke = new (allocator_) HInvokeStaticOrDirect(allocator_,
1038 number_of_arguments,
1039 return_type,
1040 dex_pc,
1041 method_idx,
1042 resolved_method,
1043 dispatch_info,
1044 invoke_type,
1045 target_method,
1046 clinit_check_requirement);
Vladimir Marko5f846072020-04-09 13:20:11 +01001047 if (clinit_check != nullptr) {
1048 // Add the class initialization check as last input of `invoke`.
1049 DCHECK_EQ(clinit_check_requirement, HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit);
1050 size_t clinit_check_index = invoke->InputCount() - 1u;
1051 DCHECK(invoke->InputAt(clinit_check_index) == nullptr);
1052 invoke->SetArgumentAt(clinit_check_index, clinit_check);
1053 }
David Brazdildee58d62016-04-07 09:54:26 +00001054 } else if (invoke_type == kVirtual) {
Vladimir Marko2f40d242020-04-08 12:56:45 +01001055 DCHECK(target_method.dex_file == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001056 invoke = new (allocator_) HInvokeVirtual(allocator_,
1057 number_of_arguments,
1058 return_type,
1059 dex_pc,
1060 method_idx,
1061 resolved_method,
Vladimir Marko2f40d242020-04-08 12:56:45 +01001062 /*vtable_index=*/ target_method.index);
David Brazdildee58d62016-04-07 09:54:26 +00001063 } else {
1064 DCHECK_EQ(invoke_type, kInterface);
Vladimir Marko2f40d242020-04-08 12:56:45 +01001065 invoke = new (allocator_) HInvokeInterface(allocator_,
Vladimir Markoca6fff82017-10-03 14:49:14 +01001066 number_of_arguments,
1067 return_type,
1068 dex_pc,
1069 method_idx,
1070 resolved_method,
Vladimir Marko2f40d242020-04-08 12:56:45 +01001071 /*imt_index=*/ target_method.index);
David Brazdildee58d62016-04-07 09:54:26 +00001072 }
Vladimir Marko5f846072020-04-09 13:20:11 +01001073 return HandleInvoke(invoke, operands, shorty, /* is_unresolved= */ false);
David Brazdildee58d62016-04-07 09:54:26 +00001074}
1075
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001076bool HInstructionBuilder::BuildInvokePolymorphic(uint32_t dex_pc,
Orion Hodsonac141392017-01-13 11:53:47 +00001077 uint32_t method_idx,
Orion Hodson06d10a72018-05-14 08:53:38 +01001078 dex::ProtoIndex proto_idx,
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001079 const InstructionOperands& operands) {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001080 const char* shorty = dex_file_->GetShorty(proto_idx);
1081 DCHECK_EQ(1 + ArtMethod::NumArgRegisters(shorty), operands.GetNumberOfOperands());
1082 DataType::Type return_type = DataType::FromShorty(shorty[0]);
1083 size_t number_of_arguments = strlen(shorty);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001084 HInvoke* invoke = new (allocator_) HInvokePolymorphic(allocator_,
1085 number_of_arguments,
1086 return_type,
1087 dex_pc,
1088 method_idx);
Andreas Gampe3db70682018-12-26 15:12:03 -08001089 return HandleInvoke(invoke, operands, shorty, /* is_unresolved= */ false);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001090}
1091
1092
1093bool HInstructionBuilder::BuildInvokeCustom(uint32_t dex_pc,
1094 uint32_t call_site_idx,
1095 const InstructionOperands& operands) {
1096 dex::ProtoIndex proto_idx = dex_file_->GetProtoIndexForCallSite(call_site_idx);
1097 const char* shorty = dex_file_->GetShorty(proto_idx);
1098 DataType::Type return_type = DataType::FromShorty(shorty[0]);
1099 size_t number_of_arguments = strlen(shorty) - 1;
1100 HInvoke* invoke = new (allocator_) HInvokeCustom(allocator_,
1101 number_of_arguments,
1102 call_site_idx,
1103 return_type,
1104 dex_pc);
Andreas Gampe3db70682018-12-26 15:12:03 -08001105 return HandleInvoke(invoke, operands, shorty, /* is_unresolved= */ false);
Orion Hodsonac141392017-01-13 11:53:47 +00001106}
1107
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001108HNewInstance* HInstructionBuilder::BuildNewInstance(dex::TypeIndex type_index, uint32_t dex_pc) {
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001109 ScopedObjectAccess soa(Thread::Current());
David Brazdildee58d62016-04-07 09:54:26 +00001110
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001111 HLoadClass* load_class = BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001112
David Brazdildee58d62016-04-07 09:54:26 +00001113 HInstruction* cls = load_class;
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001114 Handle<mirror::Class> klass = load_class->GetClass();
1115
Vladimir Marko2f40d242020-04-08 12:56:45 +01001116 if (!IsInitialized(klass.Get())) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001117 cls = new (allocator_) HClinitCheck(load_class, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001118 AppendInstruction(cls);
1119 }
1120
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001121 // Only the access check entrypoint handles the finalizable class case. If we
1122 // need access checks, then we haven't resolved the method and the class may
1123 // again be finalizable.
1124 QuickEntrypointEnum entrypoint = kQuickAllocObjectInitialized;
1125 if (load_class->NeedsAccessCheck() || klass->IsFinalizable() || !klass->IsInstantiable()) {
1126 entrypoint = kQuickAllocObjectWithChecks;
1127 }
Alex Lightd109e302018-06-27 10:25:41 -07001128 // We will always be able to resolve the string class since it is in the BCP.
1129 if (!klass.IsNull() && klass->IsStringClass()) {
1130 entrypoint = kQuickAllocStringObject;
1131 }
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001132
1133 // Consider classes we haven't resolved as potentially finalizable.
Andreas Gampefa4333d2017-02-14 11:10:34 -08001134 bool finalizable = (klass == nullptr) || klass->IsFinalizable();
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001135
Vladimir Markoca6fff82017-10-03 14:49:14 +01001136 HNewInstance* new_instance = new (allocator_) HNewInstance(
David Brazdildee58d62016-04-07 09:54:26 +00001137 cls,
David Brazdildee58d62016-04-07 09:54:26 +00001138 dex_pc,
1139 type_index,
1140 *dex_compilation_unit_->GetDexFile(),
David Brazdildee58d62016-04-07 09:54:26 +00001141 finalizable,
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001142 entrypoint);
1143 AppendInstruction(new_instance);
1144
1145 return new_instance;
1146}
1147
1148void HInstructionBuilder::BuildConstructorFenceForAllocation(HInstruction* allocation) {
1149 DCHECK(allocation != nullptr &&
George Burgess IVf2072992017-05-23 15:36:41 -07001150 (allocation->IsNewInstance() ||
1151 allocation->IsNewArray())); // corresponding to "new" keyword in JLS.
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001152
1153 if (allocation->IsNewInstance()) {
1154 // STRING SPECIAL HANDLING:
1155 // -------------------------------
1156 // Strings have a real HNewInstance node but they end up always having 0 uses.
1157 // All uses of a String HNewInstance are always transformed to replace their input
1158 // of the HNewInstance with an input of the invoke to StringFactory.
1159 //
1160 // Do not emit an HConstructorFence here since it can inhibit some String new-instance
1161 // optimizations (to pass checker tests that rely on those optimizations).
1162 HNewInstance* new_inst = allocation->AsNewInstance();
1163 HLoadClass* load_class = new_inst->GetLoadClass();
1164
1165 Thread* self = Thread::Current();
1166 ScopedObjectAccess soa(self);
1167 StackHandleScope<1> hs(self);
1168 Handle<mirror::Class> klass = load_class->GetClass();
1169 if (klass != nullptr && klass->IsStringClass()) {
1170 return;
1171 // Note: Do not use allocation->IsStringAlloc which requires
1172 // a valid ReferenceTypeInfo, but that doesn't get made until after reference type
1173 // propagation (and instruction builder is too early).
1174 }
1175 // (In terms of correctness, the StringFactory needs to provide its own
1176 // default initialization barrier, see below.)
1177 }
1178
1179 // JLS 17.4.5 "Happens-before Order" describes:
1180 //
1181 // The default initialization of any object happens-before any other actions (other than
1182 // default-writes) of a program.
1183 //
1184 // In our implementation the default initialization of an object to type T means
1185 // setting all of its initial data (object[0..size)) to 0, and setting the
1186 // object's class header (i.e. object.getClass() == T.class).
1187 //
1188 // In practice this fence ensures that the writes to the object header
1189 // are visible to other threads if this object escapes the current thread.
1190 // (and in theory the 0-initializing, but that happens automatically
1191 // when new memory pages are mapped in by the OS).
1192 HConstructorFence* ctor_fence =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001193 new (allocator_) HConstructorFence(allocation, allocation->GetDexPc(), allocator_);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001194 AppendInstruction(ctor_fence);
Igor Murashkin6ef45672017-08-08 13:59:55 -07001195 MaybeRecordStat(
1196 compilation_stats_,
1197 MethodCompilationStat::kConstructorFenceGeneratedNew);
David Brazdildee58d62016-04-07 09:54:26 +00001198}
1199
Vladimir Marko2ab1bdd2018-07-12 09:59:56 +01001200static bool IsInBootImage(ObjPtr<mirror::Class> cls, const CompilerOptions& compiler_options)
1201 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko6a67bea2020-01-20 13:05:55 +00001202 if (Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(cls)) {
1203 return true;
1204 }
1205 if (compiler_options.IsBootImage() || compiler_options.IsBootImageExtension()) {
Vladimir Marko2ab1bdd2018-07-12 09:59:56 +01001206 std::string temp;
1207 const char* descriptor = cls->GetDescriptor(&temp);
1208 return compiler_options.IsImageClass(descriptor);
1209 } else {
Vladimir Marko6a67bea2020-01-20 13:05:55 +00001210 return false;
Vladimir Marko2ab1bdd2018-07-12 09:59:56 +01001211 }
1212}
1213
1214static bool IsSubClass(ObjPtr<mirror::Class> to_test, ObjPtr<mirror::Class> super_class)
1215 REQUIRES_SHARED(Locks::mutator_lock_) {
1216 return to_test != nullptr && !to_test->IsInterface() && to_test->IsSubClass(super_class);
1217}
1218
1219static bool HasTrivialClinit(ObjPtr<mirror::Class> klass, PointerSize pointer_size)
1220 REQUIRES_SHARED(Locks::mutator_lock_) {
1221 // Check if the class has encoded fields that trigger bytecode execution.
1222 // (Encoded fields are just a different representation of <clinit>.)
1223 if (klass->NumStaticFields() != 0u) {
1224 DCHECK(klass->GetClassDef() != nullptr);
1225 EncodedStaticFieldValueIterator it(klass->GetDexFile(), *klass->GetClassDef());
1226 for (; it.HasNext(); it.Next()) {
1227 switch (it.GetValueType()) {
1228 case EncodedArrayValueIterator::ValueType::kBoolean:
1229 case EncodedArrayValueIterator::ValueType::kByte:
1230 case EncodedArrayValueIterator::ValueType::kShort:
1231 case EncodedArrayValueIterator::ValueType::kChar:
1232 case EncodedArrayValueIterator::ValueType::kInt:
1233 case EncodedArrayValueIterator::ValueType::kLong:
1234 case EncodedArrayValueIterator::ValueType::kFloat:
1235 case EncodedArrayValueIterator::ValueType::kDouble:
1236 case EncodedArrayValueIterator::ValueType::kNull:
1237 case EncodedArrayValueIterator::ValueType::kString:
1238 // Primitive, null or j.l.String initialization is permitted.
1239 break;
1240 case EncodedArrayValueIterator::ValueType::kType:
1241 // Type initialization can load classes and execute bytecode through a class loader
1242 // which can execute arbitrary bytecode. We do not optimize for known class loaders;
1243 // kType is rarely used (if ever).
1244 return false;
1245 default:
1246 // Other types in the encoded static field list are rejected by the DexFileVerifier.
1247 LOG(FATAL) << "Unexpected type " << it.GetValueType();
1248 UNREACHABLE();
1249 }
1250 }
1251 }
1252 // Check if the class has <clinit> that executes arbitrary code.
1253 // Initialization of static fields of the class itself with constants is allowed.
1254 ArtMethod* clinit = klass->FindClassInitializer(pointer_size);
1255 if (clinit != nullptr) {
1256 const DexFile& dex_file = *clinit->GetDexFile();
1257 CodeItemInstructionAccessor accessor(dex_file, clinit->GetCodeItem());
1258 for (DexInstructionPcPair it : accessor) {
1259 switch (it->Opcode()) {
1260 case Instruction::CONST_4:
1261 case Instruction::CONST_16:
1262 case Instruction::CONST:
1263 case Instruction::CONST_HIGH16:
1264 case Instruction::CONST_WIDE_16:
1265 case Instruction::CONST_WIDE_32:
1266 case Instruction::CONST_WIDE:
1267 case Instruction::CONST_WIDE_HIGH16:
1268 case Instruction::CONST_STRING:
1269 case Instruction::CONST_STRING_JUMBO:
1270 // Primitive, null or j.l.String initialization is permitted.
1271 break;
1272 case Instruction::RETURN_VOID:
1273 case Instruction::RETURN_VOID_NO_BARRIER:
1274 break;
1275 case Instruction::SPUT:
1276 case Instruction::SPUT_WIDE:
1277 case Instruction::SPUT_OBJECT:
1278 case Instruction::SPUT_BOOLEAN:
1279 case Instruction::SPUT_BYTE:
1280 case Instruction::SPUT_CHAR:
1281 case Instruction::SPUT_SHORT:
1282 // Only initialization of a static field of the same class is permitted.
1283 if (dex_file.GetFieldId(it->VRegB_21c()).class_idx_ != klass->GetDexTypeIndex()) {
1284 return false;
1285 }
1286 break;
1287 case Instruction::NEW_ARRAY:
1288 // Only primitive arrays are permitted.
1289 if (Primitive::GetType(dex_file.GetTypeDescriptor(dex_file.GetTypeId(
1290 dex::TypeIndex(it->VRegC_22c())))[1]) == Primitive::kPrimNot) {
1291 return false;
1292 }
1293 break;
1294 case Instruction::APUT:
1295 case Instruction::APUT_WIDE:
1296 case Instruction::APUT_BOOLEAN:
1297 case Instruction::APUT_BYTE:
1298 case Instruction::APUT_CHAR:
1299 case Instruction::APUT_SHORT:
1300 case Instruction::FILL_ARRAY_DATA:
1301 case Instruction::NOP:
1302 // Allow initialization of primitive arrays (only constants can be stored).
1303 // Note: We expect NOPs used for fill-array-data-payload but accept all NOPs
1304 // (even unreferenced switch payloads if they make it through the verifier).
1305 break;
1306 default:
1307 return false;
1308 }
1309 }
1310 }
1311 return true;
1312}
1313
1314static bool HasTrivialInitialization(ObjPtr<mirror::Class> cls,
1315 const CompilerOptions& compiler_options)
1316 REQUIRES_SHARED(Locks::mutator_lock_) {
1317 Runtime* runtime = Runtime::Current();
1318 PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
1319
1320 // Check the superclass chain.
1321 for (ObjPtr<mirror::Class> klass = cls; klass != nullptr; klass = klass->GetSuperClass()) {
1322 if (klass->IsInitialized() && IsInBootImage(klass, compiler_options)) {
1323 break; // `klass` and its superclasses are already initialized in the boot image.
1324 }
1325 if (!HasTrivialClinit(klass, pointer_size)) {
1326 return false;
1327 }
1328 }
1329
1330 // Also check interfaces with default methods as they need to be initialized as well.
1331 ObjPtr<mirror::IfTable> iftable = cls->GetIfTable();
1332 DCHECK(iftable != nullptr);
1333 for (int32_t i = 0, count = iftable->Count(); i != count; ++i) {
1334 ObjPtr<mirror::Class> iface = iftable->GetInterface(i);
1335 if (!iface->HasDefaultMethods()) {
1336 continue; // Initializing `cls` does not initialize this interface.
1337 }
1338 if (iface->IsInitialized() && IsInBootImage(iface, compiler_options)) {
1339 continue; // This interface is already initialized in the boot image.
1340 }
1341 if (!HasTrivialClinit(iface, pointer_size)) {
1342 return false;
1343 }
1344 }
1345 return true;
1346}
1347
Vladimir Marko2f40d242020-04-08 12:56:45 +01001348bool HInstructionBuilder::IsInitialized(ObjPtr<mirror::Class> cls) const {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001349 if (cls == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +00001350 return false;
1351 }
1352
Vladimir Marko51b8aaf2017-06-09 15:17:05 +01001353 // Check if the class will be initialized at runtime.
1354 if (cls->IsInitialized()) {
Vladimir Marko695348f2020-05-19 14:42:02 +01001355 const CompilerOptions& compiler_options = code_generator_->GetCompilerOptions();
1356 if (compiler_options.IsAotCompiler()) {
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +00001357 // Assume loaded only if klass is in the boot image. App classes cannot be assumed
1358 // loaded because we don't even know what class loader will be used to load them.
Vladimir Marko695348f2020-05-19 14:42:02 +01001359 if (IsInBootImage(cls, compiler_options)) {
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +00001360 return true;
1361 }
1362 } else {
Vladimir Marko695348f2020-05-19 14:42:02 +01001363 DCHECK(compiler_options.IsJitCompiler());
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +00001364 if (Runtime::Current()->GetJit()->CanAssumeInitialized(
Vladimir Marko2f40d242020-04-08 12:56:45 +01001365 cls,
Vladimir Marko695348f2020-05-19 14:42:02 +01001366 compiler_options.IsJitCompilerForSharedCode())) {
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +00001367 // For JIT, the class cannot revert to an uninitialized state.
1368 return true;
1369 }
Vladimir Marko51b8aaf2017-06-09 15:17:05 +01001370 }
1371 }
1372
Vladimir Marko7f260d42018-10-30 18:09:55 +00001373 // We can avoid the class initialization check for `cls` in static methods and constructors
1374 // in the very same class; invoking a static method involves a class initialization check
1375 // and so does the instance allocation that must be executed before invoking a constructor.
1376 // Other instance methods of the same class can run on an escaped instance
Vladimir Marko51b8aaf2017-06-09 15:17:05 +01001377 // of an erroneous class. Even a superclass may need to be checked as the subclass
1378 // can be completely initialized while the superclass is initializing and the subclass
1379 // remains initialized when the superclass initializer throws afterwards. b/62478025
1380 // Note: The HClinitCheck+HInvokeStaticOrDirect merging can still apply.
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001381 auto is_static_method_or_constructor_of_cls = [cls](const DexCompilationUnit& compilation_unit)
1382 REQUIRES_SHARED(Locks::mutator_lock_) {
1383 return (compilation_unit.GetAccessFlags() & (kAccStatic | kAccConstructor)) != 0u &&
Vladimir Marko2f40d242020-04-08 12:56:45 +01001384 compilation_unit.GetCompilingClass().Get() == cls;
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001385 };
1386 if (is_static_method_or_constructor_of_cls(*outer_compilation_unit_) ||
1387 // Check also the innermost method. Though excessive copies of ClinitCheck can be
1388 // eliminated by GVN, that happens only after the decision whether to inline the
1389 // graph or not and that may depend on the presence of the ClinitCheck.
1390 // TODO: We should walk over the entire inlined method chain, but we don't pass that
1391 // information to the builder.
1392 is_static_method_or_constructor_of_cls(*dex_compilation_unit_)) {
Vladimir Marko2ab1bdd2018-07-12 09:59:56 +01001393 return true;
1394 }
David Brazdildee58d62016-04-07 09:54:26 +00001395
Vladimir Marko2ab1bdd2018-07-12 09:59:56 +01001396 // Otherwise, we may be able to avoid the check if `cls` is a superclass of a method being
1397 // compiled here (anywhere in the inlining chain) as the `cls` must have started initializing
1398 // before calling any `cls` or subclass methods. Static methods require a clinit check and
1399 // instance methods require an instance which cannot be created before doing a clinit check.
1400 // When a subclass of `cls` starts initializing, it starts initializing its superclass
1401 // chain up to `cls` without running any bytecode, i.e. without any opportunity for circular
1402 // initialization weirdness.
1403 //
1404 // If the initialization of `cls` is trivial (`cls` and its superclasses and superinterfaces
1405 // with default methods initialize only their own static fields using constant values), it must
1406 // complete, either successfully or by throwing and marking `cls` erroneous, without allocating
1407 // any instances of `cls` or subclasses (or any other class) and without calling any methods.
1408 // If it completes by throwing, no instances of `cls` shall be created and no subclass method
1409 // bytecode shall execute (see above), therefore the instruction we're building shall be
1410 // unreachable. By reaching the instruction, we know that `cls` was initialized successfully.
1411 //
1412 // TODO: We should walk over the entire inlined methods chain, but we don't pass that
1413 // information to the builder. (We could also check if we're guaranteed a non-null instance
1414 // of `cls` at this location but that's outside the scope of the instruction builder.)
Vladimir Marko2f40d242020-04-08 12:56:45 +01001415 bool is_subclass = IsSubClass(outer_compilation_unit_->GetCompilingClass().Get(), cls);
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001416 if (dex_compilation_unit_ != outer_compilation_unit_) {
1417 is_subclass = is_subclass ||
Vladimir Marko2f40d242020-04-08 12:56:45 +01001418 IsSubClass(dex_compilation_unit_->GetCompilingClass().Get(), cls);
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001419 }
Vladimir Marko2f40d242020-04-08 12:56:45 +01001420 if (is_subclass && HasTrivialInitialization(cls, code_generator_->GetCompilerOptions())) {
Vladimir Marko2ab1bdd2018-07-12 09:59:56 +01001421 return true;
1422 }
David Brazdildee58d62016-04-07 09:54:26 +00001423
1424 return false;
1425}
1426
1427HClinitCheck* HInstructionBuilder::ProcessClinitCheckForInvoke(
Vladimir Markofca0b492018-07-23 15:30:52 +01001428 uint32_t dex_pc,
1429 ArtMethod* resolved_method,
1430 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
Vladimir Marko2f40d242020-04-08 12:56:45 +01001431 ScopedObjectAccess soa(Thread::Current());
1432 ObjPtr<mirror::Class> klass = resolved_method->GetDeclaringClass();
David Brazdildee58d62016-04-07 09:54:26 +00001433
1434 HClinitCheck* clinit_check = nullptr;
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001435 if (IsInitialized(klass)) {
David Brazdildee58d62016-04-07 09:54:26 +00001436 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001437 } else {
Vladimir Marko02ca05a2020-05-12 13:58:51 +01001438 Handle<mirror::Class> h_klass = graph_->GetHandleCache()->NewHandle(klass);
Vladimir Marko2f40d242020-04-08 12:56:45 +01001439 HLoadClass* cls = BuildLoadClass(h_klass->GetDexTypeIndex(),
1440 h_klass->GetDexFile(),
1441 h_klass,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001442 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08001443 /* needs_access_check= */ false);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001444 if (cls != nullptr) {
1445 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
Vladimir Markoca6fff82017-10-03 14:49:14 +01001446 clinit_check = new (allocator_) HClinitCheck(cls, dex_pc);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001447 AppendInstruction(clinit_check);
Vladimir Marko2f40d242020-04-08 12:56:45 +01001448 } else {
1449 // Let the invoke handle this with an implicit class initialization check.
1450 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001451 }
David Brazdildee58d62016-04-07 09:54:26 +00001452 }
1453 return clinit_check;
1454}
1455
Vladimir Marko5f846072020-04-09 13:20:11 +01001456bool HInstructionBuilder::SetupInvokeArguments(HInstruction* invoke,
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001457 const InstructionOperands& operands,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001458 const char* shorty,
Vladimir Marko5f846072020-04-09 13:20:11 +01001459 ReceiverArg receiver_arg) {
1460 // Note: The `invoke` can be an intrinsic replacement, so not necessaritly HInvoke.
1461 // 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 +01001462 uint32_t shorty_index = 1; // Skip the return type.
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001463 const size_t number_of_operands = operands.GetNumberOfOperands();
Vladimir Marko5f846072020-04-09 13:20:11 +01001464 bool argument_length_error = false;
1465
1466 size_t start_index = 0u;
1467 size_t argument_index = 0u;
1468 if (receiver_arg != ReceiverArg::kNone) {
1469 if (number_of_operands == 0u) {
1470 argument_length_error = true;
1471 } else {
1472 start_index = 1u;
1473 if (receiver_arg != ReceiverArg::kIgnored) {
1474 uint32_t obj_reg = operands.GetOperand(0u);
1475 HInstruction* arg = (receiver_arg == ReceiverArg::kPlainArg)
1476 ? LoadLocal(obj_reg, DataType::Type::kReference)
1477 : LoadNullCheckedLocal(obj_reg, invoke->GetDexPc());
1478 if (receiver_arg != ReceiverArg::kNullCheckedOnly) {
1479 invoke->SetRawInputAt(0u, arg);
1480 argument_index = 1u;
1481 }
1482 }
1483 }
1484 }
1485
1486 for (size_t i = start_index; i < number_of_operands; ++i, ++argument_index) {
1487 // Make sure we don't go over the expected arguments or over the number of
1488 // dex registers given. If the instruction was seen as dead by the verifier,
1489 // it hasn't been properly checked.
1490 if (UNLIKELY(shorty[shorty_index] == 0)) {
1491 argument_length_error = true;
1492 break;
1493 }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001494 DataType::Type type = DataType::FromShorty(shorty[shorty_index++]);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001495 bool is_wide = (type == DataType::Type::kInt64) || (type == DataType::Type::kFloat64);
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001496 if (is_wide && ((i + 1 == number_of_operands) ||
1497 (operands.GetOperand(i) + 1 != operands.GetOperand(i + 1)))) {
Vladimir Marko5f846072020-04-09 13:20:11 +01001498 if (invoke->IsInvoke()) {
1499 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
1500 // reject any class where this is violated. However, the verifier only does these checks
1501 // on non trivially dead instructions, so we just bailout the compilation.
1502 VLOG(compiler) << "Did not compile "
1503 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
1504 << " because of non-sequential dex register pair in wide argument";
1505 MaybeRecordStat(compilation_stats_,
1506 MethodCompilationStat::kNotCompiledMalformedOpcode);
1507 }
David Brazdildee58d62016-04-07 09:54:26 +00001508 return false;
1509 }
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001510 HInstruction* arg = LoadLocal(operands.GetOperand(i), type);
Vladimir Marko5f846072020-04-09 13:20:11 +01001511 DCHECK(invoke->InputAt(argument_index) == nullptr);
1512 invoke->SetRawInputAt(argument_index, arg);
David Brazdildee58d62016-04-07 09:54:26 +00001513 if (is_wide) {
Vladimir Marko5f846072020-04-09 13:20:11 +01001514 ++i;
David Brazdildee58d62016-04-07 09:54:26 +00001515 }
1516 }
1517
Vladimir Marko5f846072020-04-09 13:20:11 +01001518 argument_length_error = argument_length_error || shorty[shorty_index] != 0;
1519 if (argument_length_error) {
1520 if (invoke->IsInvoke()) {
1521 VLOG(compiler) << "Did not compile "
1522 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
1523 << " because of wrong number of arguments in invoke instruction";
1524 MaybeRecordStat(compilation_stats_,
1525 MethodCompilationStat::kNotCompiledMalformedOpcode);
1526 }
David Brazdildee58d62016-04-07 09:54:26 +00001527 return false;
1528 }
1529
1530 if (invoke->IsInvokeStaticOrDirect() &&
1531 HInvokeStaticOrDirect::NeedsCurrentMethodInput(
1532 invoke->AsInvokeStaticOrDirect()->GetMethodLoadKind())) {
Vladimir Marko5f846072020-04-09 13:20:11 +01001533 DCHECK_EQ(argument_index, invoke->AsInvokeStaticOrDirect()->GetSpecialInputIndex());
1534 DCHECK(invoke->InputAt(argument_index) == nullptr);
1535 invoke->SetRawInputAt(argument_index, graph_->GetCurrentMethod());
David Brazdildee58d62016-04-07 09:54:26 +00001536 }
1537
1538 return true;
1539}
1540
1541bool HInstructionBuilder::HandleInvoke(HInvoke* invoke,
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001542 const InstructionOperands& operands,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001543 const char* shorty,
Vladimir Marko5f846072020-04-09 13:20:11 +01001544 bool is_unresolved) {
David Brazdildee58d62016-04-07 09:54:26 +00001545 DCHECK(!invoke->IsInvokeStaticOrDirect() || !invoke->AsInvokeStaticOrDirect()->IsStringInit());
1546
Vladimir Marko5f846072020-04-09 13:20:11 +01001547 ReceiverArg receiver_arg = (invoke->GetInvokeType() == InvokeType::kStatic)
1548 ? ReceiverArg::kNone
1549 : (is_unresolved ? ReceiverArg::kPlainArg : ReceiverArg::kNullCheckedArg);
1550 if (!SetupInvokeArguments(invoke, operands, shorty, receiver_arg)) {
David Brazdildee58d62016-04-07 09:54:26 +00001551 return false;
1552 }
1553
David Brazdildee58d62016-04-07 09:54:26 +00001554 AppendInstruction(invoke);
1555 latest_result_ = invoke;
1556
1557 return true;
1558}
1559
Vladimir Marko5f846072020-04-09 13:20:11 +01001560bool HInstructionBuilder::BuildSimpleIntrinsic(ArtMethod* method,
1561 uint32_t dex_pc,
1562 const InstructionOperands& operands,
1563 const char* shorty) {
1564 Intrinsics intrinsic = static_cast<Intrinsics>(method->GetIntrinsic());
1565 DCHECK_NE(intrinsic, Intrinsics::kNone);
1566 constexpr DataType::Type kInt32 = DataType::Type::kInt32;
1567 constexpr DataType::Type kInt64 = DataType::Type::kInt64;
1568 constexpr DataType::Type kFloat32 = DataType::Type::kFloat32;
1569 constexpr DataType::Type kFloat64 = DataType::Type::kFloat64;
1570 ReceiverArg receiver_arg = method->IsStatic() ? ReceiverArg::kNone : ReceiverArg::kNullCheckedArg;
1571 HInstruction* instruction = nullptr;
1572 switch (intrinsic) {
1573 case Intrinsics::kIntegerRotateRight:
1574 case Intrinsics::kIntegerRotateLeft:
1575 // For rotate left, we negate the distance below.
1576 instruction = new (allocator_) HRor(kInt32, /*value=*/ nullptr, /*distance=*/ nullptr);
1577 break;
1578 case Intrinsics::kLongRotateRight:
1579 case Intrinsics::kLongRotateLeft:
1580 // For rotate left, we negate the distance below.
1581 instruction = new (allocator_) HRor(kInt64, /*value=*/ nullptr, /*distance=*/ nullptr);
1582 break;
1583 case Intrinsics::kIntegerCompare:
1584 instruction = new (allocator_) HCompare(
1585 kInt32, /*first=*/ nullptr, /*second=*/ nullptr, ComparisonBias::kNoBias, dex_pc);
1586 break;
1587 case Intrinsics::kLongCompare:
1588 instruction = new (allocator_) HCompare(
1589 kInt64, /*first=*/ nullptr, /*second=*/ nullptr, ComparisonBias::kNoBias, dex_pc);
1590 break;
1591 case Intrinsics::kIntegerSignum:
1592 instruction = new (allocator_) HCompare(
1593 kInt32, /*first=*/ nullptr, graph_->GetIntConstant(0), ComparisonBias::kNoBias, dex_pc);
1594 break;
1595 case Intrinsics::kLongSignum:
1596 instruction = new (allocator_) HCompare(
1597 kInt64, /*first=*/ nullptr, graph_->GetLongConstant(0), ComparisonBias::kNoBias, dex_pc);
1598 break;
1599 case Intrinsics::kFloatIsNaN:
1600 case Intrinsics::kDoubleIsNaN: {
1601 // IsNaN(x) is the same as x != x.
1602 instruction = new (allocator_) HNotEqual(/*first=*/ nullptr, /*second=*/ nullptr, dex_pc);
1603 instruction->AsCondition()->SetBias(ComparisonBias::kLtBias);
1604 break;
1605 }
1606 case Intrinsics::kStringCharAt:
1607 // We treat String as an array to allow DCE and BCE to seamlessly work on strings.
1608 instruction = new (allocator_) HArrayGet(/*array=*/ nullptr,
1609 /*index=*/ nullptr,
1610 DataType::Type::kUint16,
1611 SideEffects::None(), // Strings are immutable.
1612 dex_pc,
1613 /*is_string_char_at=*/ true);
1614 break;
1615 case Intrinsics::kStringIsEmpty:
1616 case Intrinsics::kStringLength:
1617 // We treat String as an array to allow DCE and BCE to seamlessly work on strings.
1618 // For String.isEmpty(), we add a comparison with 0 below.
1619 instruction =
1620 new (allocator_) HArrayLength(/*array=*/ nullptr, dex_pc, /* is_string_length= */ true);
1621 break;
1622 case Intrinsics::kUnsafeLoadFence:
1623 receiver_arg = ReceiverArg::kNullCheckedOnly;
1624 instruction = new (allocator_) HMemoryBarrier(MemBarrierKind::kLoadAny, dex_pc);
1625 break;
1626 case Intrinsics::kUnsafeStoreFence:
1627 receiver_arg = ReceiverArg::kNullCheckedOnly;
1628 instruction = new (allocator_) HMemoryBarrier(MemBarrierKind::kAnyStore, dex_pc);
1629 break;
1630 case Intrinsics::kUnsafeFullFence:
1631 receiver_arg = ReceiverArg::kNullCheckedOnly;
1632 instruction = new (allocator_) HMemoryBarrier(MemBarrierKind::kAnyAny, dex_pc);
1633 break;
1634 case Intrinsics::kVarHandleFullFence:
1635 instruction = new (allocator_) HMemoryBarrier(MemBarrierKind::kAnyAny, dex_pc);
1636 break;
1637 case Intrinsics::kVarHandleAcquireFence:
1638 instruction = new (allocator_) HMemoryBarrier(MemBarrierKind::kLoadAny, dex_pc);
1639 break;
1640 case Intrinsics::kVarHandleReleaseFence:
1641 instruction = new (allocator_) HMemoryBarrier(MemBarrierKind::kAnyStore, dex_pc);
1642 break;
1643 case Intrinsics::kVarHandleLoadLoadFence:
1644 instruction = new (allocator_) HMemoryBarrier(MemBarrierKind::kLoadAny, dex_pc);
1645 break;
1646 case Intrinsics::kVarHandleStoreStoreFence:
1647 instruction = new (allocator_) HMemoryBarrier(MemBarrierKind::kStoreStore, dex_pc);
1648 break;
1649 case Intrinsics::kMathMinIntInt:
1650 instruction = new (allocator_) HMin(kInt32, /*left=*/ nullptr, /*right=*/ nullptr, dex_pc);
1651 break;
1652 case Intrinsics::kMathMinLongLong:
1653 instruction = new (allocator_) HMin(kInt64, /*left=*/ nullptr, /*right=*/ nullptr, dex_pc);
1654 break;
1655 case Intrinsics::kMathMinFloatFloat:
1656 instruction = new (allocator_) HMin(kFloat32, /*left=*/ nullptr, /*right=*/ nullptr, dex_pc);
1657 break;
1658 case Intrinsics::kMathMinDoubleDouble:
1659 instruction = new (allocator_) HMin(kFloat64, /*left=*/ nullptr, /*right=*/ nullptr, dex_pc);
1660 break;
1661 case Intrinsics::kMathMaxIntInt:
1662 instruction = new (allocator_) HMax(kInt32, /*left=*/ nullptr, /*right=*/ nullptr, dex_pc);
1663 break;
1664 case Intrinsics::kMathMaxLongLong:
1665 instruction = new (allocator_) HMax(kInt64, /*left=*/ nullptr, /*right=*/ nullptr, dex_pc);
1666 break;
1667 case Intrinsics::kMathMaxFloatFloat:
1668 instruction = new (allocator_) HMax(kFloat32, /*left=*/ nullptr, /*right=*/ nullptr, dex_pc);
1669 break;
1670 case Intrinsics::kMathMaxDoubleDouble:
1671 instruction = new (allocator_) HMax(kFloat64, /*left=*/ nullptr, /*right=*/ nullptr, dex_pc);
1672 break;
1673 case Intrinsics::kMathAbsInt:
1674 instruction = new (allocator_) HAbs(kInt32, /*input=*/ nullptr, dex_pc);
1675 break;
1676 case Intrinsics::kMathAbsLong:
1677 instruction = new (allocator_) HAbs(kInt64, /*input=*/ nullptr, dex_pc);
1678 break;
1679 case Intrinsics::kMathAbsFloat:
1680 instruction = new (allocator_) HAbs(kFloat32, /*input=*/ nullptr, dex_pc);
1681 break;
1682 case Intrinsics::kMathAbsDouble:
1683 instruction = new (allocator_) HAbs(kFloat64, /*input=*/ nullptr, dex_pc);
1684 break;
1685 default:
1686 // We do not have intermediate representation for other intrinsics.
1687 return false;
1688 }
1689 DCHECK(instruction != nullptr);
1690 if (!SetupInvokeArguments(instruction, operands, shorty, receiver_arg)) {
1691 return false;
1692 }
1693
1694 switch (intrinsic) {
1695 case Intrinsics::kIntegerRotateLeft:
1696 case Intrinsics::kLongRotateLeft: {
1697 // Negate the distance value for rotate left.
1698 DCHECK(instruction->IsRor());
1699 HNeg* neg = new (allocator_) HNeg(kInt32, instruction->InputAt(1u));
1700 AppendInstruction(neg);
1701 instruction->SetRawInputAt(1u, neg);
1702 break;
1703 }
1704 case Intrinsics::kFloatIsNaN:
1705 case Intrinsics::kDoubleIsNaN:
1706 // Set the second input to be the same as first.
1707 DCHECK(instruction->IsNotEqual());
1708 DCHECK(instruction->InputAt(1u) == nullptr);
1709 instruction->SetRawInputAt(1u, instruction->InputAt(0u));
1710 break;
1711 case Intrinsics::kStringCharAt: {
1712 // Add bounds check.
1713 HInstruction* array = instruction->InputAt(0u);
1714 HInstruction* index = instruction->InputAt(1u);
1715 HInstruction* length =
1716 new (allocator_) HArrayLength(array, dex_pc, /*is_string_length=*/ true);
1717 AppendInstruction(length);
1718 HBoundsCheck* bounds_check =
1719 new (allocator_) HBoundsCheck(index, length, dex_pc, /*is_string_char_at=*/ true);
1720 AppendInstruction(bounds_check);
1721 graph_->SetHasBoundsChecks(true);
1722 instruction->SetRawInputAt(1u, bounds_check);
1723 break;
1724 }
1725 case Intrinsics::kStringIsEmpty: {
1726 // Compare the length with 0.
1727 DCHECK(instruction->IsArrayLength());
1728 AppendInstruction(instruction);
1729 HEqual* equal = new (allocator_) HEqual(instruction, graph_->GetIntConstant(0), dex_pc);
1730 instruction = equal;
1731 break;
1732 }
1733 default:
1734 break;
1735 }
1736
1737 AppendInstruction(instruction);
1738 latest_result_ = instruction;
1739
1740 return true;
1741}
1742
David Brazdildee58d62016-04-07 09:54:26 +00001743bool HInstructionBuilder::HandleStringInit(HInvoke* invoke,
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001744 const InstructionOperands& operands,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001745 const char* shorty) {
David Brazdildee58d62016-04-07 09:54:26 +00001746 DCHECK(invoke->IsInvokeStaticOrDirect());
1747 DCHECK(invoke->AsInvokeStaticOrDirect()->IsStringInit());
1748
Vladimir Marko5f846072020-04-09 13:20:11 +01001749 if (!SetupInvokeArguments(invoke, operands, shorty, ReceiverArg::kIgnored)) {
David Brazdildee58d62016-04-07 09:54:26 +00001750 return false;
1751 }
1752
1753 AppendInstruction(invoke);
1754
1755 // This is a StringFactory call, not an actual String constructor. Its result
1756 // replaces the empty String pre-allocated by NewInstance.
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001757 uint32_t orig_this_reg = operands.GetOperand(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001758 HInstruction* arg_this = LoadLocal(orig_this_reg, DataType::Type::kReference);
David Brazdildee58d62016-04-07 09:54:26 +00001759
1760 // Replacing the NewInstance might render it redundant. Keep a list of these
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +01001761 // to be visited once it is clear whether it has remaining uses.
David Brazdildee58d62016-04-07 09:54:26 +00001762 if (arg_this->IsNewInstance()) {
1763 ssa_builder_->AddUninitializedString(arg_this->AsNewInstance());
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +01001764 } else {
1765 DCHECK(arg_this->IsPhi());
1766 // We can get a phi as input of a String.<init> if there is a loop between the
1767 // allocation and the String.<init> call. As we don't know which other phis might alias
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +01001768 // with `arg_this`, we keep a record of those invocations so we can later replace
1769 // the allocation with the invocation.
1770 // Add the actual 'this' input so the analysis knows what is the allocation instruction.
1771 // The input will be removed during the analysis.
1772 invoke->AddInput(arg_this);
1773 ssa_builder_->AddUninitializedStringPhi(invoke);
1774 }
1775 // Walk over all vregs and replace any occurrence of `arg_this` with `invoke`.
1776 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
1777 if ((*current_locals_)[vreg] == arg_this) {
1778 (*current_locals_)[vreg] = invoke;
1779 }
David Brazdildee58d62016-04-07 09:54:26 +00001780 }
David Brazdildee58d62016-04-07 09:54:26 +00001781 return true;
1782}
1783
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001784static DataType::Type GetFieldAccessType(const DexFile& dex_file, uint16_t field_index) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001785 const dex::FieldId& field_id = dex_file.GetFieldId(field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001786 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001787 return DataType::FromShorty(type[0]);
David Brazdildee58d62016-04-07 09:54:26 +00001788}
1789
1790bool HInstructionBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
1791 uint32_t dex_pc,
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001792 bool is_put,
1793 size_t quicken_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001794 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1795 uint32_t obj_reg = instruction.VRegB_22c();
1796 uint16_t field_index;
1797 if (instruction.IsQuickened()) {
1798 if (!CanDecodeQuickenedInfo()) {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00001799 VLOG(compiler) << "Not compiled: Could not decode quickened instruction "
1800 << instruction.Opcode();
David Brazdildee58d62016-04-07 09:54:26 +00001801 return false;
1802 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001803 field_index = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00001804 } else {
1805 field_index = instruction.VRegC_22c();
1806 }
1807
1808 ScopedObjectAccess soa(Thread::Current());
Andreas Gampe3db70682018-12-26 15:12:03 -08001809 ArtField* resolved_field = ResolveField(field_index, /* is_static= */ false, is_put);
David Brazdildee58d62016-04-07 09:54:26 +00001810
Aart Bik14154132016-06-02 17:53:58 -07001811 // Generate an explicit null check on the reference, unless the field access
1812 // is unresolved. In that case, we rely on the runtime to perform various
1813 // checks first, followed by a null check.
1814 HInstruction* object = (resolved_field == nullptr)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001815 ? LoadLocal(obj_reg, DataType::Type::kReference)
Aart Bik14154132016-06-02 17:53:58 -07001816 : LoadNullCheckedLocal(obj_reg, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001817
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001818 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001819 if (is_put) {
1820 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1821 HInstruction* field_set = nullptr;
1822 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001823 MaybeRecordStat(compilation_stats_,
1824 MethodCompilationStat::kUnresolvedField);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001825 field_set = new (allocator_) HUnresolvedInstanceFieldSet(object,
1826 value,
1827 field_type,
1828 field_index,
1829 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001830 } else {
1831 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Vladimir Markoca6fff82017-10-03 14:49:14 +01001832 field_set = new (allocator_) HInstanceFieldSet(object,
1833 value,
1834 resolved_field,
1835 field_type,
1836 resolved_field->GetOffset(),
1837 resolved_field->IsVolatile(),
1838 field_index,
1839 class_def_index,
1840 *dex_file_,
1841 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001842 }
1843 AppendInstruction(field_set);
1844 } else {
1845 HInstruction* field_get = nullptr;
1846 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001847 MaybeRecordStat(compilation_stats_,
1848 MethodCompilationStat::kUnresolvedField);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001849 field_get = new (allocator_) HUnresolvedInstanceFieldGet(object,
1850 field_type,
1851 field_index,
1852 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001853 } else {
1854 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Vladimir Markoca6fff82017-10-03 14:49:14 +01001855 field_get = new (allocator_) HInstanceFieldGet(object,
1856 resolved_field,
1857 field_type,
1858 resolved_field->GetOffset(),
1859 resolved_field->IsVolatile(),
1860 field_index,
1861 class_def_index,
1862 *dex_file_,
1863 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001864 }
1865 AppendInstruction(field_get);
1866 UpdateLocal(source_or_dest_reg, field_get);
1867 }
1868
1869 return true;
1870}
1871
David Brazdildee58d62016-04-07 09:54:26 +00001872void HInstructionBuilder::BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001873 uint32_t dex_pc,
1874 bool is_put,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001875 DataType::Type field_type) {
David Brazdildee58d62016-04-07 09:54:26 +00001876 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1877 uint16_t field_index = instruction.VRegB_21c();
1878
1879 if (is_put) {
1880 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1881 AppendInstruction(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001882 new (allocator_) HUnresolvedStaticFieldSet(value, field_type, field_index, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001883 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001884 AppendInstruction(new (allocator_) HUnresolvedStaticFieldGet(field_type, field_index, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001885 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1886 }
1887}
1888
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001889ArtField* HInstructionBuilder::ResolveField(uint16_t field_idx, bool is_static, bool is_put) {
1890 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001891
1892 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001893 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001894
Vladimir Markoe11dd502017-12-08 14:09:45 +00001895 ArtField* resolved_field = class_linker->ResolveField(field_idx,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001896 dex_compilation_unit_->GetDexCache(),
1897 class_loader,
1898 is_static);
Nicolas Geoffrayf3688822020-03-25 15:04:03 +00001899 DCHECK_EQ(resolved_field == nullptr, soa.Self()->IsExceptionPending())
1900 << "field="
1901 << ((resolved_field == nullptr) ? "null" : resolved_field->PrettyField())
1902 << ", exception="
1903 << (soa.Self()->IsExceptionPending() ? soa.Self()->GetException()->Dump() : "null");
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001904 if (UNLIKELY(resolved_field == nullptr)) {
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001905 // Clean up any exception left by field resolution.
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001906 soa.Self()->ClearException();
1907 return nullptr;
1908 }
1909
1910 // Check static/instance. The class linker has a fast path for looking into the dex cache
1911 // and does not check static/instance if it hits it.
1912 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
1913 return nullptr;
1914 }
1915
1916 // Check access.
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001917 Handle<mirror::Class> compiling_class = dex_compilation_unit_->GetCompilingClass();
Andreas Gampefa4333d2017-02-14 11:10:34 -08001918 if (compiling_class == nullptr) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001919 if (!resolved_field->IsPublic()) {
1920 return nullptr;
1921 }
1922 } else if (!compiling_class->CanAccessResolvedField(resolved_field->GetDeclaringClass(),
1923 resolved_field,
1924 dex_compilation_unit_->GetDexCache().Get(),
1925 field_idx)) {
1926 return nullptr;
1927 }
1928
1929 if (is_put &&
1930 resolved_field->IsFinal() &&
1931 (compiling_class.Get() != resolved_field->GetDeclaringClass())) {
1932 // Final fields can only be updated within their own class.
1933 // TODO: Only allow it in constructors. b/34966607.
1934 return nullptr;
1935 }
1936
1937 return resolved_field;
1938}
1939
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00001940void HInstructionBuilder::BuildStaticFieldAccess(const Instruction& instruction,
David Brazdildee58d62016-04-07 09:54:26 +00001941 uint32_t dex_pc,
1942 bool is_put) {
1943 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1944 uint16_t field_index = instruction.VRegB_21c();
1945
1946 ScopedObjectAccess soa(Thread::Current());
Andreas Gampe3db70682018-12-26 15:12:03 -08001947 ArtField* resolved_field = ResolveField(field_index, /* is_static= */ true, is_put);
David Brazdildee58d62016-04-07 09:54:26 +00001948
1949 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001950 MaybeRecordStat(compilation_stats_,
1951 MethodCompilationStat::kUnresolvedField);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001952 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001953 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00001954 return;
David Brazdildee58d62016-04-07 09:54:26 +00001955 }
1956
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001957 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001958
Vladimir Marko02ca05a2020-05-12 13:58:51 +01001959 Handle<mirror::Class> klass =
1960 graph_->GetHandleCache()->NewHandle(resolved_field->GetDeclaringClass());
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001961 HLoadClass* constant = BuildLoadClass(klass->GetDexTypeIndex(),
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001962 klass->GetDexFile(),
1963 klass,
1964 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08001965 /* needs_access_check= */ false);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001966
1967 if (constant == nullptr) {
1968 // The class cannot be referenced from this compiled code. Generate
1969 // an unresolved access.
Igor Murashkin1e065a52017-08-09 13:20:34 -07001970 MaybeRecordStat(compilation_stats_,
1971 MethodCompilationStat::kUnresolvedFieldNotAFastAccess);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001972 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00001973 return;
David Brazdildee58d62016-04-07 09:54:26 +00001974 }
1975
David Brazdildee58d62016-04-07 09:54:26 +00001976 HInstruction* cls = constant;
Vladimir Marko2f40d242020-04-08 12:56:45 +01001977 if (!IsInitialized(klass.Get())) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001978 cls = new (allocator_) HClinitCheck(constant, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001979 AppendInstruction(cls);
1980 }
1981
1982 uint16_t class_def_index = klass->GetDexClassDefIndex();
1983 if (is_put) {
1984 // We need to keep the class alive before loading the value.
1985 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1986 DCHECK_EQ(HPhi::ToPhiType(value->GetType()), HPhi::ToPhiType(field_type));
Vladimir Markoca6fff82017-10-03 14:49:14 +01001987 AppendInstruction(new (allocator_) HStaticFieldSet(cls,
1988 value,
1989 resolved_field,
1990 field_type,
1991 resolved_field->GetOffset(),
1992 resolved_field->IsVolatile(),
1993 field_index,
1994 class_def_index,
1995 *dex_file_,
1996 dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001997 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001998 AppendInstruction(new (allocator_) HStaticFieldGet(cls,
1999 resolved_field,
2000 field_type,
2001 resolved_field->GetOffset(),
2002 resolved_field->IsVolatile(),
2003 field_index,
2004 class_def_index,
2005 *dex_file_,
2006 dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002007 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
2008 }
David Brazdildee58d62016-04-07 09:54:26 +00002009}
2010
2011void HInstructionBuilder::BuildCheckedDivRem(uint16_t out_vreg,
Vladimir Markoca6fff82017-10-03 14:49:14 +01002012 uint16_t first_vreg,
2013 int64_t second_vreg_or_constant,
2014 uint32_t dex_pc,
2015 DataType::Type type,
2016 bool second_is_constant,
2017 bool isDiv) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002018 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
David Brazdildee58d62016-04-07 09:54:26 +00002019
2020 HInstruction* first = LoadLocal(first_vreg, type);
2021 HInstruction* second = nullptr;
2022 if (second_is_constant) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002023 if (type == DataType::Type::kInt32) {
David Brazdildee58d62016-04-07 09:54:26 +00002024 second = graph_->GetIntConstant(second_vreg_or_constant, dex_pc);
2025 } else {
2026 second = graph_->GetLongConstant(second_vreg_or_constant, dex_pc);
2027 }
2028 } else {
2029 second = LoadLocal(second_vreg_or_constant, type);
2030 }
2031
2032 if (!second_is_constant
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002033 || (type == DataType::Type::kInt32 && second->AsIntConstant()->GetValue() == 0)
2034 || (type == DataType::Type::kInt64 && second->AsLongConstant()->GetValue() == 0)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002035 second = new (allocator_) HDivZeroCheck(second, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002036 AppendInstruction(second);
2037 }
2038
2039 if (isDiv) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002040 AppendInstruction(new (allocator_) HDiv(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002041 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002042 AppendInstruction(new (allocator_) HRem(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002043 }
2044 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
2045}
2046
2047void HInstructionBuilder::BuildArrayAccess(const Instruction& instruction,
2048 uint32_t dex_pc,
2049 bool is_put,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002050 DataType::Type anticipated_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002051 uint8_t source_or_dest_reg = instruction.VRegA_23x();
2052 uint8_t array_reg = instruction.VRegB_23x();
2053 uint8_t index_reg = instruction.VRegC_23x();
2054
David Brazdilc120bbe2016-04-22 16:57:00 +01002055 HInstruction* object = LoadNullCheckedLocal(array_reg, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002056 HInstruction* length = new (allocator_) HArrayLength(object, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002057 AppendInstruction(length);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002058 HInstruction* index = LoadLocal(index_reg, DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002059 index = new (allocator_) HBoundsCheck(index, length, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002060 AppendInstruction(index);
2061 if (is_put) {
2062 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
2063 // TODO: Insert a type check node if the type is Object.
Vladimir Markoca6fff82017-10-03 14:49:14 +01002064 HArraySet* aset = new (allocator_) HArraySet(object, index, value, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002065 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
2066 AppendInstruction(aset);
2067 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002068 HArrayGet* aget = new (allocator_) HArrayGet(object, index, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002069 ssa_builder_->MaybeAddAmbiguousArrayGet(aget);
2070 AppendInstruction(aget);
2071 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
2072 }
2073 graph_->SetHasBoundsChecks(true);
2074}
2075
Vladimir Markob5461632018-10-15 14:24:21 +01002076HNewArray* HInstructionBuilder::BuildNewArray(uint32_t dex_pc,
2077 dex::TypeIndex type_index,
2078 HInstruction* length) {
2079 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
2080
2081 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(type_index));
2082 DCHECK_EQ(descriptor[0], '[');
2083 size_t component_type_shift = Primitive::ComponentSizeShift(Primitive::GetType(descriptor[1]));
2084
2085 HNewArray* new_array = new (allocator_) HNewArray(cls, length, dex_pc, component_type_shift);
2086 AppendInstruction(new_array);
2087 return new_array;
2088}
2089
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002090HNewArray* HInstructionBuilder::BuildFilledNewArray(uint32_t dex_pc,
2091 dex::TypeIndex type_index,
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002092 const InstructionOperands& operands) {
2093 const size_t number_of_operands = operands.GetNumberOfOperands();
2094 HInstruction* length = graph_->GetIntConstant(number_of_operands, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002095
Vladimir Markob5461632018-10-15 14:24:21 +01002096 HNewArray* new_array = BuildNewArray(dex_pc, type_index, length);
David Brazdildee58d62016-04-07 09:54:26 +00002097 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
2098 DCHECK_EQ(descriptor[0], '[') << descriptor;
2099 char primitive = descriptor[1];
2100 DCHECK(primitive == 'I'
2101 || primitive == 'L'
2102 || primitive == '[') << descriptor;
2103 bool is_reference_array = (primitive == 'L') || (primitive == '[');
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002104 DataType::Type type = is_reference_array ? DataType::Type::kReference : DataType::Type::kInt32;
David Brazdildee58d62016-04-07 09:54:26 +00002105
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002106 for (size_t i = 0; i < number_of_operands; ++i) {
2107 HInstruction* value = LoadLocal(operands.GetOperand(i), type);
David Brazdildee58d62016-04-07 09:54:26 +00002108 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
Vladimir Markob5461632018-10-15 14:24:21 +01002109 HArraySet* aset = new (allocator_) HArraySet(new_array, index, value, type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002110 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
2111 AppendInstruction(aset);
2112 }
Vladimir Markob5461632018-10-15 14:24:21 +01002113 latest_result_ = new_array;
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002114
Vladimir Markob5461632018-10-15 14:24:21 +01002115 return new_array;
David Brazdildee58d62016-04-07 09:54:26 +00002116}
2117
2118template <typename T>
2119void HInstructionBuilder::BuildFillArrayData(HInstruction* object,
2120 const T* data,
2121 uint32_t element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002122 DataType::Type anticipated_type,
David Brazdildee58d62016-04-07 09:54:26 +00002123 uint32_t dex_pc) {
2124 for (uint32_t i = 0; i < element_count; ++i) {
2125 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
2126 HInstruction* value = graph_->GetIntConstant(data[i], dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002127 HArraySet* aset = new (allocator_) HArraySet(object, index, value, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002128 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
2129 AppendInstruction(aset);
2130 }
2131}
2132
2133void HInstructionBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
David Brazdilc120bbe2016-04-22 16:57:00 +01002134 HInstruction* array = LoadNullCheckedLocal(instruction.VRegA_31t(), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002135
2136 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
2137 const Instruction::ArrayDataPayload* payload =
Mathieu Chartier808c7a52017-12-15 11:19:33 -08002138 reinterpret_cast<const Instruction::ArrayDataPayload*>(
2139 code_item_accessor_.Insns() + payload_offset);
David Brazdildee58d62016-04-07 09:54:26 +00002140 const uint8_t* data = payload->data;
2141 uint32_t element_count = payload->element_count;
2142
Vladimir Markoc69fba22016-09-06 16:49:15 +01002143 if (element_count == 0u) {
2144 // For empty payload we emit only the null check above.
2145 return;
2146 }
2147
Vladimir Markoca6fff82017-10-03 14:49:14 +01002148 HInstruction* length = new (allocator_) HArrayLength(array, dex_pc);
Vladimir Markoc69fba22016-09-06 16:49:15 +01002149 AppendInstruction(length);
2150
David Brazdildee58d62016-04-07 09:54:26 +00002151 // Implementation of this DEX instruction seems to be that the bounds check is
2152 // done before doing any stores.
2153 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002154 AppendInstruction(new (allocator_) HBoundsCheck(last_index, length, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002155
2156 switch (payload->element_width) {
2157 case 1:
David Brazdilc120bbe2016-04-22 16:57:00 +01002158 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00002159 reinterpret_cast<const int8_t*>(data),
2160 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002161 DataType::Type::kInt8,
David Brazdildee58d62016-04-07 09:54:26 +00002162 dex_pc);
2163 break;
2164 case 2:
David Brazdilc120bbe2016-04-22 16:57:00 +01002165 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00002166 reinterpret_cast<const int16_t*>(data),
2167 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002168 DataType::Type::kInt16,
David Brazdildee58d62016-04-07 09:54:26 +00002169 dex_pc);
2170 break;
2171 case 4:
David Brazdilc120bbe2016-04-22 16:57:00 +01002172 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00002173 reinterpret_cast<const int32_t*>(data),
2174 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002175 DataType::Type::kInt32,
David Brazdildee58d62016-04-07 09:54:26 +00002176 dex_pc);
2177 break;
2178 case 8:
David Brazdilc120bbe2016-04-22 16:57:00 +01002179 BuildFillWideArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00002180 reinterpret_cast<const int64_t*>(data),
2181 element_count,
2182 dex_pc);
2183 break;
2184 default:
2185 LOG(FATAL) << "Unknown element width for " << payload->element_width;
2186 }
2187 graph_->SetHasBoundsChecks(true);
2188}
2189
2190void HInstructionBuilder::BuildFillWideArrayData(HInstruction* object,
2191 const int64_t* data,
2192 uint32_t element_count,
2193 uint32_t dex_pc) {
2194 for (uint32_t i = 0; i < element_count; ++i) {
2195 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
2196 HInstruction* value = graph_->GetLongConstant(data[i], dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002197 HArraySet* aset =
2198 new (allocator_) HArraySet(object, index, value, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002199 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
2200 AppendInstruction(aset);
2201 }
2202}
2203
Vladimir Marko28e012a2017-12-07 11:22:59 +00002204void HInstructionBuilder::BuildLoadString(dex::StringIndex string_index, uint32_t dex_pc) {
2205 HLoadString* load_string =
2206 new (allocator_) HLoadString(graph_->GetCurrentMethod(), string_index, *dex_file_, dex_pc);
2207 HSharpening::ProcessLoadString(load_string,
2208 code_generator_,
Vladimir Marko28e012a2017-12-07 11:22:59 +00002209 *dex_compilation_unit_,
Vladimir Marko02ca05a2020-05-12 13:58:51 +01002210 graph_->GetHandleCache()->GetHandles());
Vladimir Marko28e012a2017-12-07 11:22:59 +00002211 AppendInstruction(load_string);
2212}
2213
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002214HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index, uint32_t dex_pc) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +00002215 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002216 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
Vladimir Marko175e7862018-03-27 09:03:13 +00002217 Handle<mirror::Class> klass = ResolveClass(soa, type_index);
Vladimir Marko2f40d242020-04-08 12:56:45 +01002218 bool needs_access_check = LoadClassNeedsAccessCheck(klass.Get());
Vladimir Markoa2c211c2018-11-01 09:50:52 +00002219 return BuildLoadClass(type_index, dex_file, klass, dex_pc, needs_access_check);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002220}
2221
Vladimir Markoa2c211c2018-11-01 09:50:52 +00002222HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002223 const DexFile& dex_file,
2224 Handle<mirror::Class> klass,
2225 uint32_t dex_pc,
2226 bool needs_access_check) {
2227 // Try to find a reference in the compiling dex file.
2228 const DexFile* actual_dex_file = &dex_file;
2229 if (!IsSameDexFile(dex_file, *dex_compilation_unit_->GetDexFile())) {
2230 dex::TypeIndex local_type_index =
2231 klass->FindTypeIndexInOtherDexFile(*dex_compilation_unit_->GetDexFile());
2232 if (local_type_index.IsValid()) {
2233 type_index = local_type_index;
2234 actual_dex_file = dex_compilation_unit_->GetDexFile();
2235 }
2236 }
2237
Vladimir Marko02ca05a2020-05-12 13:58:51 +01002238 // Note: `klass` must be from `graph_->GetHandleCache()`.
Vladimir Markoa2c211c2018-11-01 09:50:52 +00002239 bool is_referrers_class =
2240 (klass != nullptr) && (outer_compilation_unit_->GetCompilingClass().Get() == klass.Get());
Vladimir Markoca6fff82017-10-03 14:49:14 +01002241 HLoadClass* load_class = new (allocator_) HLoadClass(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00002242 graph_->GetCurrentMethod(),
2243 type_index,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002244 *actual_dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00002245 klass,
Vladimir Markofca0b492018-07-23 15:30:52 +01002246 is_referrers_class,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00002247 dex_pc,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002248 needs_access_check);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00002249
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002250 HLoadClass::LoadKind load_kind = HSharpening::ComputeLoadClassKind(load_class,
2251 code_generator_,
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002252 *dex_compilation_unit_);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002253
2254 if (load_kind == HLoadClass::LoadKind::kInvalid) {
2255 // We actually cannot reference this class, we're forced to bail.
2256 return nullptr;
2257 }
Vladimir Marko28e012a2017-12-07 11:22:59 +00002258 // Load kind must be set before inserting the instruction into the graph.
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002259 load_class->SetLoadKind(load_kind);
Vladimir Marko28e012a2017-12-07 11:22:59 +00002260 AppendInstruction(load_class);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00002261 return load_class;
2262}
2263
Vladimir Marko175e7862018-03-27 09:03:13 +00002264Handle<mirror::Class> HInstructionBuilder::ResolveClass(ScopedObjectAccess& soa,
2265 dex::TypeIndex type_index) {
Vladimir Marko3b506202018-10-31 14:33:58 +00002266 auto it = class_cache_.find(type_index);
2267 if (it != class_cache_.end()) {
2268 return it->second;
2269 }
2270
2271 ObjPtr<mirror::Class> klass = dex_compilation_unit_->GetClassLinker()->ResolveType(
2272 type_index, dex_compilation_unit_->GetDexCache(), dex_compilation_unit_->GetClassLoader());
2273 DCHECK_EQ(klass == nullptr, soa.Self()->IsExceptionPending());
2274 soa.Self()->ClearException(); // Clean up the exception left by type resolution if any.
2275
Vladimir Marko02ca05a2020-05-12 13:58:51 +01002276 Handle<mirror::Class> h_klass = graph_->GetHandleCache()->NewHandle(klass);
Vladimir Marko3b506202018-10-31 14:33:58 +00002277 class_cache_.Put(type_index, h_klass);
2278 return h_klass;
Vladimir Marko175e7862018-03-27 09:03:13 +00002279}
2280
Vladimir Marko2f40d242020-04-08 12:56:45 +01002281bool HInstructionBuilder::LoadClassNeedsAccessCheck(ObjPtr<mirror::Class> klass) {
Vladimir Marko175e7862018-03-27 09:03:13 +00002282 if (klass == nullptr) {
2283 return true;
2284 } else if (klass->IsPublic()) {
2285 return false;
2286 } else {
Vladimir Markoa2c211c2018-11-01 09:50:52 +00002287 ObjPtr<mirror::Class> compiling_class = dex_compilation_unit_->GetCompilingClass().Get();
Vladimir Marko2f40d242020-04-08 12:56:45 +01002288 return compiling_class == nullptr || !compiling_class->CanAccess(klass);
Vladimir Marko175e7862018-03-27 09:03:13 +00002289 }
2290}
2291
Orion Hodson06d10a72018-05-14 08:53:38 +01002292void HInstructionBuilder::BuildLoadMethodHandle(uint16_t method_handle_index, uint32_t dex_pc) {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01002293 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
Orion Hodson06d10a72018-05-14 08:53:38 +01002294 HLoadMethodHandle* load_method_handle = new (allocator_) HLoadMethodHandle(
2295 graph_->GetCurrentMethod(), method_handle_index, dex_file, dex_pc);
Orion Hodsondbaa5c72018-05-10 08:22:46 +01002296 AppendInstruction(load_method_handle);
2297}
2298
Orion Hodson06d10a72018-05-14 08:53:38 +01002299void HInstructionBuilder::BuildLoadMethodType(dex::ProtoIndex proto_index, uint32_t dex_pc) {
Orion Hodson18259d72018-04-12 11:18:23 +01002300 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
2301 HLoadMethodType* load_method_type =
Orion Hodson06d10a72018-05-14 08:53:38 +01002302 new (allocator_) HLoadMethodType(graph_->GetCurrentMethod(), proto_index, dex_file, dex_pc);
Orion Hodson18259d72018-04-12 11:18:23 +01002303 AppendInstruction(load_method_type);
2304}
2305
David Brazdildee58d62016-04-07 09:54:26 +00002306void HInstructionBuilder::BuildTypeCheck(const Instruction& instruction,
2307 uint8_t destination,
2308 uint8_t reference,
Andreas Gampea5b09a62016-11-17 15:21:22 -08002309 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +00002310 uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002311 HInstruction* object = LoadLocal(reference, DataType::Type::kReference);
David Brazdildee58d62016-04-07 09:54:26 +00002312
Nicolas Geoffray5247c082017-01-13 14:17:29 +00002313 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko175e7862018-03-27 09:03:13 +00002314 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
2315 Handle<mirror::Class> klass = ResolveClass(soa, type_index);
Vladimir Marko2f40d242020-04-08 12:56:45 +01002316 bool needs_access_check = LoadClassNeedsAccessCheck(klass.Get());
Vladimir Marko175e7862018-03-27 09:03:13 +00002317 TypeCheckKind check_kind = HSharpening::ComputeTypeCheckKind(
Vladimir Markodc4bcce2018-06-21 16:15:42 +01002318 klass.Get(), code_generator_, needs_access_check);
Vladimir Marko175e7862018-03-27 09:03:13 +00002319
2320 HInstruction* class_or_null = nullptr;
2321 HIntConstant* bitstring_path_to_root = nullptr;
2322 HIntConstant* bitstring_mask = nullptr;
2323 if (check_kind == TypeCheckKind::kBitstringCheck) {
2324 // TODO: Allow using the bitstring check also if we need an access check.
2325 DCHECK(!needs_access_check);
2326 class_or_null = graph_->GetNullConstant(dex_pc);
2327 MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_);
2328 uint32_t path_to_root =
2329 SubtypeCheck<ObjPtr<mirror::Class>>::GetEncodedPathToRootForTarget(klass.Get());
2330 uint32_t mask = SubtypeCheck<ObjPtr<mirror::Class>>::GetEncodedPathToRootMask(klass.Get());
2331 bitstring_path_to_root = graph_->GetIntConstant(static_cast<int32_t>(path_to_root), dex_pc);
2332 bitstring_mask = graph_->GetIntConstant(static_cast<int32_t>(mask), dex_pc);
2333 } else {
Vladimir Markoa2c211c2018-11-01 09:50:52 +00002334 class_or_null = BuildLoadClass(type_index, dex_file, klass, dex_pc, needs_access_check);
Vladimir Marko175e7862018-03-27 09:03:13 +00002335 }
2336 DCHECK(class_or_null != nullptr);
2337
David Brazdildee58d62016-04-07 09:54:26 +00002338 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
Vladimir Marko175e7862018-03-27 09:03:13 +00002339 AppendInstruction(new (allocator_) HInstanceOf(object,
2340 class_or_null,
2341 check_kind,
2342 klass,
2343 dex_pc,
2344 allocator_,
2345 bitstring_path_to_root,
2346 bitstring_mask));
David Brazdildee58d62016-04-07 09:54:26 +00002347 UpdateLocal(destination, current_block_->GetLastInstruction());
2348 } else {
2349 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
2350 // We emit a CheckCast followed by a BoundType. CheckCast is a statement
2351 // which may throw. If it succeeds BoundType sets the new type of `object`
2352 // for all subsequent uses.
Vladimir Marko175e7862018-03-27 09:03:13 +00002353 AppendInstruction(
2354 new (allocator_) HCheckCast(object,
2355 class_or_null,
2356 check_kind,
2357 klass,
2358 dex_pc,
2359 allocator_,
2360 bitstring_path_to_root,
2361 bitstring_mask));
Vladimir Markoca6fff82017-10-03 14:49:14 +01002362 AppendInstruction(new (allocator_) HBoundType(object, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002363 UpdateLocal(reference, current_block_->GetLastInstruction());
2364 }
2365}
2366
David Brazdildee58d62016-04-07 09:54:26 +00002367bool HInstructionBuilder::CanDecodeQuickenedInfo() const {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002368 return !quicken_info_.IsNull();
David Brazdildee58d62016-04-07 09:54:26 +00002369}
2370
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002371uint16_t HInstructionBuilder::LookupQuickenedInfo(uint32_t quicken_index) {
2372 DCHECK(CanDecodeQuickenedInfo());
2373 return quicken_info_.GetData(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00002374}
2375
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002376bool HInstructionBuilder::ProcessDexInstruction(const Instruction& instruction,
2377 uint32_t dex_pc,
2378 size_t quicken_index) {
David Brazdildee58d62016-04-07 09:54:26 +00002379 switch (instruction.Opcode()) {
2380 case Instruction::CONST_4: {
2381 int32_t register_index = instruction.VRegA();
2382 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n(), dex_pc);
2383 UpdateLocal(register_index, constant);
2384 break;
2385 }
2386
2387 case Instruction::CONST_16: {
2388 int32_t register_index = instruction.VRegA();
2389 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s(), dex_pc);
2390 UpdateLocal(register_index, constant);
2391 break;
2392 }
2393
2394 case Instruction::CONST: {
2395 int32_t register_index = instruction.VRegA();
2396 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i(), dex_pc);
2397 UpdateLocal(register_index, constant);
2398 break;
2399 }
2400
2401 case Instruction::CONST_HIGH16: {
2402 int32_t register_index = instruction.VRegA();
2403 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16, dex_pc);
2404 UpdateLocal(register_index, constant);
2405 break;
2406 }
2407
2408 case Instruction::CONST_WIDE_16: {
2409 int32_t register_index = instruction.VRegA();
2410 // Get 16 bits of constant value, sign extended to 64 bits.
2411 int64_t value = instruction.VRegB_21s();
2412 value <<= 48;
2413 value >>= 48;
2414 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
2415 UpdateLocal(register_index, constant);
2416 break;
2417 }
2418
2419 case Instruction::CONST_WIDE_32: {
2420 int32_t register_index = instruction.VRegA();
2421 // Get 32 bits of constant value, sign extended to 64 bits.
2422 int64_t value = instruction.VRegB_31i();
2423 value <<= 32;
2424 value >>= 32;
2425 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
2426 UpdateLocal(register_index, constant);
2427 break;
2428 }
2429
2430 case Instruction::CONST_WIDE: {
2431 int32_t register_index = instruction.VRegA();
2432 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l(), dex_pc);
2433 UpdateLocal(register_index, constant);
2434 break;
2435 }
2436
2437 case Instruction::CONST_WIDE_HIGH16: {
2438 int32_t register_index = instruction.VRegA();
2439 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
2440 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
2441 UpdateLocal(register_index, constant);
2442 break;
2443 }
2444
2445 // Note that the SSA building will refine the types.
2446 case Instruction::MOVE:
2447 case Instruction::MOVE_FROM16:
2448 case Instruction::MOVE_16: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002449 HInstruction* value = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +00002450 UpdateLocal(instruction.VRegA(), value);
2451 break;
2452 }
2453
2454 // Note that the SSA building will refine the types.
2455 case Instruction::MOVE_WIDE:
2456 case Instruction::MOVE_WIDE_FROM16:
2457 case Instruction::MOVE_WIDE_16: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002458 HInstruction* value = LoadLocal(instruction.VRegB(), DataType::Type::kInt64);
David Brazdildee58d62016-04-07 09:54:26 +00002459 UpdateLocal(instruction.VRegA(), value);
2460 break;
2461 }
2462
2463 case Instruction::MOVE_OBJECT:
2464 case Instruction::MOVE_OBJECT_16:
2465 case Instruction::MOVE_OBJECT_FROM16: {
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01002466 // The verifier has no notion of a null type, so a move-object of constant 0
2467 // will lead to the same constant 0 in the destination register. To mimic
2468 // this behavior, we just pretend we haven't seen a type change (int to reference)
2469 // for the 0 constant and phis. We rely on our type propagation to eventually get the
2470 // types correct.
2471 uint32_t reg_number = instruction.VRegB();
2472 HInstruction* value = (*current_locals_)[reg_number];
2473 if (value->IsIntConstant()) {
2474 DCHECK_EQ(value->AsIntConstant()->GetValue(), 0);
2475 } else if (value->IsPhi()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002476 DCHECK(value->GetType() == DataType::Type::kInt32 ||
2477 value->GetType() == DataType::Type::kReference);
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01002478 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002479 value = LoadLocal(reg_number, DataType::Type::kReference);
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01002480 }
David Brazdildee58d62016-04-07 09:54:26 +00002481 UpdateLocal(instruction.VRegA(), value);
2482 break;
2483 }
2484
2485 case Instruction::RETURN_VOID_NO_BARRIER:
2486 case Instruction::RETURN_VOID: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002487 BuildReturn(instruction, DataType::Type::kVoid, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002488 break;
2489 }
2490
2491#define IF_XX(comparison, cond) \
2492 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
2493 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
2494
2495 IF_XX(HEqual, EQ);
2496 IF_XX(HNotEqual, NE);
2497 IF_XX(HLessThan, LT);
2498 IF_XX(HLessThanOrEqual, LE);
2499 IF_XX(HGreaterThan, GT);
2500 IF_XX(HGreaterThanOrEqual, GE);
2501
2502 case Instruction::GOTO:
2503 case Instruction::GOTO_16:
2504 case Instruction::GOTO_32: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002505 AppendInstruction(new (allocator_) HGoto(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002506 current_block_ = nullptr;
2507 break;
2508 }
2509
2510 case Instruction::RETURN: {
2511 BuildReturn(instruction, return_type_, dex_pc);
2512 break;
2513 }
2514
2515 case Instruction::RETURN_OBJECT: {
2516 BuildReturn(instruction, return_type_, dex_pc);
2517 break;
2518 }
2519
2520 case Instruction::RETURN_WIDE: {
2521 BuildReturn(instruction, return_type_, dex_pc);
2522 break;
2523 }
2524
2525 case Instruction::INVOKE_DIRECT:
2526 case Instruction::INVOKE_INTERFACE:
2527 case Instruction::INVOKE_STATIC:
2528 case Instruction::INVOKE_SUPER:
2529 case Instruction::INVOKE_VIRTUAL:
2530 case Instruction::INVOKE_VIRTUAL_QUICK: {
2531 uint16_t method_idx;
2532 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_QUICK) {
2533 if (!CanDecodeQuickenedInfo()) {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002534 VLOG(compiler) << "Not compiled: Could not decode quickened instruction "
2535 << instruction.Opcode();
David Brazdildee58d62016-04-07 09:54:26 +00002536 return false;
2537 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002538 method_idx = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00002539 } else {
2540 method_idx = instruction.VRegB_35c();
2541 }
David Brazdildee58d62016-04-07 09:54:26 +00002542 uint32_t args[5];
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002543 uint32_t number_of_vreg_arguments = instruction.GetVarArgs(args);
2544 VarArgsInstructionOperands operands(args, number_of_vreg_arguments);
2545 if (!BuildInvoke(instruction, dex_pc, method_idx, operands)) {
David Brazdildee58d62016-04-07 09:54:26 +00002546 return false;
2547 }
2548 break;
2549 }
2550
2551 case Instruction::INVOKE_DIRECT_RANGE:
2552 case Instruction::INVOKE_INTERFACE_RANGE:
2553 case Instruction::INVOKE_STATIC_RANGE:
2554 case Instruction::INVOKE_SUPER_RANGE:
2555 case Instruction::INVOKE_VIRTUAL_RANGE:
2556 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2557 uint16_t method_idx;
2558 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK) {
2559 if (!CanDecodeQuickenedInfo()) {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002560 VLOG(compiler) << "Not compiled: Could not decode quickened instruction "
2561 << instruction.Opcode();
David Brazdildee58d62016-04-07 09:54:26 +00002562 return false;
2563 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002564 method_idx = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00002565 } else {
2566 method_idx = instruction.VRegB_3rc();
2567 }
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002568 RangeInstructionOperands operands(instruction.VRegC(), instruction.VRegA_3rc());
2569 if (!BuildInvoke(instruction, dex_pc, method_idx, operands)) {
David Brazdildee58d62016-04-07 09:54:26 +00002570 return false;
2571 }
2572 break;
2573 }
2574
Orion Hodsonac141392017-01-13 11:53:47 +00002575 case Instruction::INVOKE_POLYMORPHIC: {
2576 uint16_t method_idx = instruction.VRegB_45cc();
Orion Hodson06d10a72018-05-14 08:53:38 +01002577 dex::ProtoIndex proto_idx(instruction.VRegH_45cc());
Orion Hodsonac141392017-01-13 11:53:47 +00002578 uint32_t args[5];
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002579 uint32_t number_of_vreg_arguments = instruction.GetVarArgs(args);
2580 VarArgsInstructionOperands operands(args, number_of_vreg_arguments);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002581 return BuildInvokePolymorphic(dex_pc, method_idx, proto_idx, operands);
Orion Hodsonac141392017-01-13 11:53:47 +00002582 }
2583
2584 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
2585 uint16_t method_idx = instruction.VRegB_4rcc();
Orion Hodson06d10a72018-05-14 08:53:38 +01002586 dex::ProtoIndex proto_idx(instruction.VRegH_4rcc());
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002587 RangeInstructionOperands operands(instruction.VRegC_4rcc(), instruction.VRegA_4rcc());
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002588 return BuildInvokePolymorphic(dex_pc, method_idx, proto_idx, operands);
2589 }
2590
2591 case Instruction::INVOKE_CUSTOM: {
2592 uint16_t call_site_idx = instruction.VRegB_35c();
2593 uint32_t args[5];
2594 uint32_t number_of_vreg_arguments = instruction.GetVarArgs(args);
2595 VarArgsInstructionOperands operands(args, number_of_vreg_arguments);
2596 return BuildInvokeCustom(dex_pc, call_site_idx, operands);
2597 }
2598
2599 case Instruction::INVOKE_CUSTOM_RANGE: {
2600 uint16_t call_site_idx = instruction.VRegB_3rc();
2601 RangeInstructionOperands operands(instruction.VRegC_3rc(), instruction.VRegA_3rc());
2602 return BuildInvokeCustom(dex_pc, call_site_idx, operands);
Orion Hodsonac141392017-01-13 11:53:47 +00002603 }
2604
David Brazdildee58d62016-04-07 09:54:26 +00002605 case Instruction::NEG_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002606 Unop_12x<HNeg>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002607 break;
2608 }
2609
2610 case Instruction::NEG_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002611 Unop_12x<HNeg>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002612 break;
2613 }
2614
2615 case Instruction::NEG_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002616 Unop_12x<HNeg>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002617 break;
2618 }
2619
2620 case Instruction::NEG_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002621 Unop_12x<HNeg>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002622 break;
2623 }
2624
2625 case Instruction::NOT_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002626 Unop_12x<HNot>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002627 break;
2628 }
2629
2630 case Instruction::NOT_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002631 Unop_12x<HNot>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002632 break;
2633 }
2634
2635 case Instruction::INT_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002636 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002637 break;
2638 }
2639
2640 case Instruction::INT_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002641 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002642 break;
2643 }
2644
2645 case Instruction::INT_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002646 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002647 break;
2648 }
2649
2650 case Instruction::LONG_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002651 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002652 break;
2653 }
2654
2655 case Instruction::LONG_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002656 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002657 break;
2658 }
2659
2660 case Instruction::LONG_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002661 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002662 break;
2663 }
2664
2665 case Instruction::FLOAT_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002666 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002667 break;
2668 }
2669
2670 case Instruction::FLOAT_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002671 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002672 break;
2673 }
2674
2675 case Instruction::FLOAT_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002676 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002677 break;
2678 }
2679
2680 case Instruction::DOUBLE_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002681 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002682 break;
2683 }
2684
2685 case Instruction::DOUBLE_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002686 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002687 break;
2688 }
2689
2690 case Instruction::DOUBLE_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002691 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002692 break;
2693 }
2694
2695 case Instruction::INT_TO_BYTE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002696 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt8, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002697 break;
2698 }
2699
2700 case Instruction::INT_TO_SHORT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002701 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt16, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002702 break;
2703 }
2704
2705 case Instruction::INT_TO_CHAR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002706 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kUint16, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002707 break;
2708 }
2709
2710 case Instruction::ADD_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002711 Binop_23x<HAdd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002712 break;
2713 }
2714
2715 case Instruction::ADD_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002716 Binop_23x<HAdd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002717 break;
2718 }
2719
2720 case Instruction::ADD_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002721 Binop_23x<HAdd>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002722 break;
2723 }
2724
2725 case Instruction::ADD_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002726 Binop_23x<HAdd>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002727 break;
2728 }
2729
2730 case Instruction::SUB_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002731 Binop_23x<HSub>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002732 break;
2733 }
2734
2735 case Instruction::SUB_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002736 Binop_23x<HSub>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002737 break;
2738 }
2739
2740 case Instruction::SUB_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002741 Binop_23x<HSub>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002742 break;
2743 }
2744
2745 case Instruction::SUB_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002746 Binop_23x<HSub>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002747 break;
2748 }
2749
2750 case Instruction::ADD_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002751 Binop_12x<HAdd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002752 break;
2753 }
2754
2755 case Instruction::MUL_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002756 Binop_23x<HMul>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002757 break;
2758 }
2759
2760 case Instruction::MUL_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002761 Binop_23x<HMul>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002762 break;
2763 }
2764
2765 case Instruction::MUL_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002766 Binop_23x<HMul>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002767 break;
2768 }
2769
2770 case Instruction::MUL_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002771 Binop_23x<HMul>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002772 break;
2773 }
2774
2775 case Instruction::DIV_INT: {
2776 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002777 dex_pc, DataType::Type::kInt32, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002778 break;
2779 }
2780
2781 case Instruction::DIV_LONG: {
2782 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002783 dex_pc, DataType::Type::kInt64, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002784 break;
2785 }
2786
2787 case Instruction::DIV_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002788 Binop_23x<HDiv>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002789 break;
2790 }
2791
2792 case Instruction::DIV_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002793 Binop_23x<HDiv>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002794 break;
2795 }
2796
2797 case Instruction::REM_INT: {
2798 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002799 dex_pc, DataType::Type::kInt32, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002800 break;
2801 }
2802
2803 case Instruction::REM_LONG: {
2804 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002805 dex_pc, DataType::Type::kInt64, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002806 break;
2807 }
2808
2809 case Instruction::REM_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002810 Binop_23x<HRem>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002811 break;
2812 }
2813
2814 case Instruction::REM_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002815 Binop_23x<HRem>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002816 break;
2817 }
2818
2819 case Instruction::AND_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002820 Binop_23x<HAnd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002821 break;
2822 }
2823
2824 case Instruction::AND_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002825 Binop_23x<HAnd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002826 break;
2827 }
2828
2829 case Instruction::SHL_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002830 Binop_23x_shift<HShl>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002831 break;
2832 }
2833
2834 case Instruction::SHL_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002835 Binop_23x_shift<HShl>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002836 break;
2837 }
2838
2839 case Instruction::SHR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002840 Binop_23x_shift<HShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002841 break;
2842 }
2843
2844 case Instruction::SHR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002845 Binop_23x_shift<HShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002846 break;
2847 }
2848
2849 case Instruction::USHR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002850 Binop_23x_shift<HUShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002851 break;
2852 }
2853
2854 case Instruction::USHR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002855 Binop_23x_shift<HUShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002856 break;
2857 }
2858
2859 case Instruction::OR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002860 Binop_23x<HOr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002861 break;
2862 }
2863
2864 case Instruction::OR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002865 Binop_23x<HOr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002866 break;
2867 }
2868
2869 case Instruction::XOR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002870 Binop_23x<HXor>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002871 break;
2872 }
2873
2874 case Instruction::XOR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002875 Binop_23x<HXor>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002876 break;
2877 }
2878
2879 case Instruction::ADD_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002880 Binop_12x<HAdd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002881 break;
2882 }
2883
2884 case Instruction::ADD_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002885 Binop_12x<HAdd>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002886 break;
2887 }
2888
2889 case Instruction::ADD_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002890 Binop_12x<HAdd>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002891 break;
2892 }
2893
2894 case Instruction::SUB_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002895 Binop_12x<HSub>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002896 break;
2897 }
2898
2899 case Instruction::SUB_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002900 Binop_12x<HSub>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002901 break;
2902 }
2903
2904 case Instruction::SUB_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002905 Binop_12x<HSub>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002906 break;
2907 }
2908
2909 case Instruction::SUB_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002910 Binop_12x<HSub>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002911 break;
2912 }
2913
2914 case Instruction::MUL_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002915 Binop_12x<HMul>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002916 break;
2917 }
2918
2919 case Instruction::MUL_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002920 Binop_12x<HMul>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002921 break;
2922 }
2923
2924 case Instruction::MUL_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002925 Binop_12x<HMul>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002926 break;
2927 }
2928
2929 case Instruction::MUL_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002930 Binop_12x<HMul>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002931 break;
2932 }
2933
2934 case Instruction::DIV_INT_2ADDR: {
2935 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002936 dex_pc, DataType::Type::kInt32, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002937 break;
2938 }
2939
2940 case Instruction::DIV_LONG_2ADDR: {
2941 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002942 dex_pc, DataType::Type::kInt64, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002943 break;
2944 }
2945
2946 case Instruction::REM_INT_2ADDR: {
2947 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002948 dex_pc, DataType::Type::kInt32, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002949 break;
2950 }
2951
2952 case Instruction::REM_LONG_2ADDR: {
2953 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002954 dex_pc, DataType::Type::kInt64, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002955 break;
2956 }
2957
2958 case Instruction::REM_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002959 Binop_12x<HRem>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002960 break;
2961 }
2962
2963 case Instruction::REM_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002964 Binop_12x<HRem>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002965 break;
2966 }
2967
2968 case Instruction::SHL_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002969 Binop_12x_shift<HShl>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002970 break;
2971 }
2972
2973 case Instruction::SHL_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002974 Binop_12x_shift<HShl>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002975 break;
2976 }
2977
2978 case Instruction::SHR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002979 Binop_12x_shift<HShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002980 break;
2981 }
2982
2983 case Instruction::SHR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002984 Binop_12x_shift<HShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002985 break;
2986 }
2987
2988 case Instruction::USHR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002989 Binop_12x_shift<HUShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002990 break;
2991 }
2992
2993 case Instruction::USHR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002994 Binop_12x_shift<HUShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002995 break;
2996 }
2997
2998 case Instruction::DIV_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002999 Binop_12x<HDiv>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003000 break;
3001 }
3002
3003 case Instruction::DIV_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003004 Binop_12x<HDiv>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003005 break;
3006 }
3007
3008 case Instruction::AND_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003009 Binop_12x<HAnd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003010 break;
3011 }
3012
3013 case Instruction::AND_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003014 Binop_12x<HAnd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003015 break;
3016 }
3017
3018 case Instruction::OR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003019 Binop_12x<HOr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003020 break;
3021 }
3022
3023 case Instruction::OR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003024 Binop_12x<HOr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003025 break;
3026 }
3027
3028 case Instruction::XOR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003029 Binop_12x<HXor>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003030 break;
3031 }
3032
3033 case Instruction::XOR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003034 Binop_12x<HXor>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003035 break;
3036 }
3037
3038 case Instruction::ADD_INT_LIT16: {
3039 Binop_22s<HAdd>(instruction, false, dex_pc);
3040 break;
3041 }
3042
3043 case Instruction::AND_INT_LIT16: {
3044 Binop_22s<HAnd>(instruction, false, dex_pc);
3045 break;
3046 }
3047
3048 case Instruction::OR_INT_LIT16: {
3049 Binop_22s<HOr>(instruction, false, dex_pc);
3050 break;
3051 }
3052
3053 case Instruction::XOR_INT_LIT16: {
3054 Binop_22s<HXor>(instruction, false, dex_pc);
3055 break;
3056 }
3057
3058 case Instruction::RSUB_INT: {
3059 Binop_22s<HSub>(instruction, true, dex_pc);
3060 break;
3061 }
3062
3063 case Instruction::MUL_INT_LIT16: {
3064 Binop_22s<HMul>(instruction, false, dex_pc);
3065 break;
3066 }
3067
3068 case Instruction::ADD_INT_LIT8: {
3069 Binop_22b<HAdd>(instruction, false, dex_pc);
3070 break;
3071 }
3072
3073 case Instruction::AND_INT_LIT8: {
3074 Binop_22b<HAnd>(instruction, false, dex_pc);
3075 break;
3076 }
3077
3078 case Instruction::OR_INT_LIT8: {
3079 Binop_22b<HOr>(instruction, false, dex_pc);
3080 break;
3081 }
3082
3083 case Instruction::XOR_INT_LIT8: {
3084 Binop_22b<HXor>(instruction, false, dex_pc);
3085 break;
3086 }
3087
3088 case Instruction::RSUB_INT_LIT8: {
3089 Binop_22b<HSub>(instruction, true, dex_pc);
3090 break;
3091 }
3092
3093 case Instruction::MUL_INT_LIT8: {
3094 Binop_22b<HMul>(instruction, false, dex_pc);
3095 break;
3096 }
3097
3098 case Instruction::DIV_INT_LIT16:
3099 case Instruction::DIV_INT_LIT8: {
3100 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003101 dex_pc, DataType::Type::kInt32, true, true);
David Brazdildee58d62016-04-07 09:54:26 +00003102 break;
3103 }
3104
3105 case Instruction::REM_INT_LIT16:
3106 case Instruction::REM_INT_LIT8: {
3107 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003108 dex_pc, DataType::Type::kInt32, true, false);
David Brazdildee58d62016-04-07 09:54:26 +00003109 break;
3110 }
3111
3112 case Instruction::SHL_INT_LIT8: {
3113 Binop_22b<HShl>(instruction, false, dex_pc);
3114 break;
3115 }
3116
3117 case Instruction::SHR_INT_LIT8: {
3118 Binop_22b<HShr>(instruction, false, dex_pc);
3119 break;
3120 }
3121
3122 case Instruction::USHR_INT_LIT8: {
3123 Binop_22b<HUShr>(instruction, false, dex_pc);
3124 break;
3125 }
3126
3127 case Instruction::NEW_INSTANCE: {
Igor Murashkin79d8fa72017-04-18 09:37:23 -07003128 HNewInstance* new_instance =
3129 BuildNewInstance(dex::TypeIndex(instruction.VRegB_21c()), dex_pc);
3130 DCHECK(new_instance != nullptr);
3131
David Brazdildee58d62016-04-07 09:54:26 +00003132 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
Igor Murashkin79d8fa72017-04-18 09:37:23 -07003133 BuildConstructorFenceForAllocation(new_instance);
David Brazdildee58d62016-04-07 09:54:26 +00003134 break;
3135 }
3136
3137 case Instruction::NEW_ARRAY: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003138 dex::TypeIndex type_index(instruction.VRegC_22c());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003139 HInstruction* length = LoadLocal(instruction.VRegB_22c(), DataType::Type::kInt32);
Vladimir Markob5461632018-10-15 14:24:21 +01003140 HNewArray* new_array = BuildNewArray(dex_pc, type_index, length);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07003141
David Brazdildee58d62016-04-07 09:54:26 +00003142 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
Igor Murashkin79d8fa72017-04-18 09:37:23 -07003143 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00003144 break;
3145 }
3146
3147 case Instruction::FILLED_NEW_ARRAY: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003148 dex::TypeIndex type_index(instruction.VRegB_35c());
David Brazdildee58d62016-04-07 09:54:26 +00003149 uint32_t args[5];
Treehugger Robot2c5827a2018-05-17 22:26:08 +00003150 uint32_t number_of_vreg_arguments = instruction.GetVarArgs(args);
3151 VarArgsInstructionOperands operands(args, number_of_vreg_arguments);
3152 HNewArray* new_array = BuildFilledNewArray(dex_pc, type_index, operands);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07003153 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00003154 break;
3155 }
3156
3157 case Instruction::FILLED_NEW_ARRAY_RANGE: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003158 dex::TypeIndex type_index(instruction.VRegB_3rc());
Treehugger Robot2c5827a2018-05-17 22:26:08 +00003159 RangeInstructionOperands operands(instruction.VRegC_3rc(), instruction.VRegA_3rc());
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::FILL_ARRAY_DATA: {
3166 BuildFillArrayData(instruction, dex_pc);
3167 break;
3168 }
3169
3170 case Instruction::MOVE_RESULT:
3171 case Instruction::MOVE_RESULT_WIDE:
3172 case Instruction::MOVE_RESULT_OBJECT: {
3173 DCHECK(latest_result_ != nullptr);
3174 UpdateLocal(instruction.VRegA(), latest_result_);
3175 latest_result_ = nullptr;
3176 break;
3177 }
3178
3179 case Instruction::CMP_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003180 Binop_23x_cmp(instruction, DataType::Type::kInt64, ComparisonBias::kNoBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003181 break;
3182 }
3183
3184 case Instruction::CMPG_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003185 Binop_23x_cmp(instruction, DataType::Type::kFloat32, ComparisonBias::kGtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003186 break;
3187 }
3188
3189 case Instruction::CMPG_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003190 Binop_23x_cmp(instruction, DataType::Type::kFloat64, ComparisonBias::kGtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003191 break;
3192 }
3193
3194 case Instruction::CMPL_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003195 Binop_23x_cmp(instruction, DataType::Type::kFloat32, ComparisonBias::kLtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003196 break;
3197 }
3198
3199 case Instruction::CMPL_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003200 Binop_23x_cmp(instruction, DataType::Type::kFloat64, ComparisonBias::kLtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003201 break;
3202 }
3203
3204 case Instruction::NOP:
3205 break;
3206
3207 case Instruction::IGET:
3208 case Instruction::IGET_QUICK:
3209 case Instruction::IGET_WIDE:
3210 case Instruction::IGET_WIDE_QUICK:
3211 case Instruction::IGET_OBJECT:
3212 case Instruction::IGET_OBJECT_QUICK:
3213 case Instruction::IGET_BOOLEAN:
3214 case Instruction::IGET_BOOLEAN_QUICK:
3215 case Instruction::IGET_BYTE:
3216 case Instruction::IGET_BYTE_QUICK:
3217 case Instruction::IGET_CHAR:
3218 case Instruction::IGET_CHAR_QUICK:
3219 case Instruction::IGET_SHORT:
3220 case Instruction::IGET_SHORT_QUICK: {
Andreas Gampe3db70682018-12-26 15:12:03 -08003221 if (!BuildInstanceFieldAccess(instruction, dex_pc, /* is_put= */ false, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00003222 return false;
3223 }
3224 break;
3225 }
3226
3227 case Instruction::IPUT:
3228 case Instruction::IPUT_QUICK:
3229 case Instruction::IPUT_WIDE:
3230 case Instruction::IPUT_WIDE_QUICK:
3231 case Instruction::IPUT_OBJECT:
3232 case Instruction::IPUT_OBJECT_QUICK:
3233 case Instruction::IPUT_BOOLEAN:
3234 case Instruction::IPUT_BOOLEAN_QUICK:
3235 case Instruction::IPUT_BYTE:
3236 case Instruction::IPUT_BYTE_QUICK:
3237 case Instruction::IPUT_CHAR:
3238 case Instruction::IPUT_CHAR_QUICK:
3239 case Instruction::IPUT_SHORT:
3240 case Instruction::IPUT_SHORT_QUICK: {
Andreas Gampe3db70682018-12-26 15:12:03 -08003241 if (!BuildInstanceFieldAccess(instruction, dex_pc, /* is_put= */ true, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00003242 return false;
3243 }
3244 break;
3245 }
3246
3247 case Instruction::SGET:
3248 case Instruction::SGET_WIDE:
3249 case Instruction::SGET_OBJECT:
3250 case Instruction::SGET_BOOLEAN:
3251 case Instruction::SGET_BYTE:
3252 case Instruction::SGET_CHAR:
3253 case Instruction::SGET_SHORT: {
Andreas Gampe3db70682018-12-26 15:12:03 -08003254 BuildStaticFieldAccess(instruction, dex_pc, /* is_put= */ false);
David Brazdildee58d62016-04-07 09:54:26 +00003255 break;
3256 }
3257
3258 case Instruction::SPUT:
3259 case Instruction::SPUT_WIDE:
3260 case Instruction::SPUT_OBJECT:
3261 case Instruction::SPUT_BOOLEAN:
3262 case Instruction::SPUT_BYTE:
3263 case Instruction::SPUT_CHAR:
3264 case Instruction::SPUT_SHORT: {
Andreas Gampe3db70682018-12-26 15:12:03 -08003265 BuildStaticFieldAccess(instruction, dex_pc, /* is_put= */ true);
David Brazdildee58d62016-04-07 09:54:26 +00003266 break;
3267 }
3268
3269#define ARRAY_XX(kind, anticipated_type) \
3270 case Instruction::AGET##kind: { \
3271 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
3272 break; \
3273 } \
3274 case Instruction::APUT##kind: { \
3275 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
3276 break; \
3277 }
3278
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003279 ARRAY_XX(, DataType::Type::kInt32);
3280 ARRAY_XX(_WIDE, DataType::Type::kInt64);
3281 ARRAY_XX(_OBJECT, DataType::Type::kReference);
3282 ARRAY_XX(_BOOLEAN, DataType::Type::kBool);
3283 ARRAY_XX(_BYTE, DataType::Type::kInt8);
3284 ARRAY_XX(_CHAR, DataType::Type::kUint16);
3285 ARRAY_XX(_SHORT, DataType::Type::kInt16);
David Brazdildee58d62016-04-07 09:54:26 +00003286
3287 case Instruction::ARRAY_LENGTH: {
David Brazdilc120bbe2016-04-22 16:57:00 +01003288 HInstruction* object = LoadNullCheckedLocal(instruction.VRegB_12x(), dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01003289 AppendInstruction(new (allocator_) HArrayLength(object, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00003290 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
3291 break;
3292 }
3293
3294 case Instruction::CONST_STRING: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08003295 dex::StringIndex string_index(instruction.VRegB_21c());
Vladimir Marko28e012a2017-12-07 11:22:59 +00003296 BuildLoadString(string_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003297 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
3298 break;
3299 }
3300
3301 case Instruction::CONST_STRING_JUMBO: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08003302 dex::StringIndex string_index(instruction.VRegB_31c());
Vladimir Marko28e012a2017-12-07 11:22:59 +00003303 BuildLoadString(string_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003304 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
3305 break;
3306 }
3307
3308 case Instruction::CONST_CLASS: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003309 dex::TypeIndex type_index(instruction.VRegB_21c());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00003310 BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003311 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
3312 break;
3313 }
3314
Orion Hodsondbaa5c72018-05-10 08:22:46 +01003315 case Instruction::CONST_METHOD_HANDLE: {
3316 uint16_t method_handle_idx = instruction.VRegB_21c();
3317 BuildLoadMethodHandle(method_handle_idx, dex_pc);
3318 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
3319 break;
3320 }
3321
Orion Hodson18259d72018-04-12 11:18:23 +01003322 case Instruction::CONST_METHOD_TYPE: {
Orion Hodson06d10a72018-05-14 08:53:38 +01003323 dex::ProtoIndex proto_idx(instruction.VRegB_21c());
Orion Hodson18259d72018-04-12 11:18:23 +01003324 BuildLoadMethodType(proto_idx, dex_pc);
3325 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
3326 break;
3327 }
3328
David Brazdildee58d62016-04-07 09:54:26 +00003329 case Instruction::MOVE_EXCEPTION: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003330 AppendInstruction(new (allocator_) HLoadException(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00003331 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
Vladimir Markoca6fff82017-10-03 14:49:14 +01003332 AppendInstruction(new (allocator_) HClearException(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00003333 break;
3334 }
3335
3336 case Instruction::THROW: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003337 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01003338 AppendInstruction(new (allocator_) HThrow(exception, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00003339 // We finished building this block. Set the current block to null to avoid
3340 // adding dead instructions to it.
3341 current_block_ = nullptr;
3342 break;
3343 }
3344
3345 case Instruction::INSTANCE_OF: {
3346 uint8_t destination = instruction.VRegA_22c();
3347 uint8_t reference = instruction.VRegB_22c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08003348 dex::TypeIndex type_index(instruction.VRegC_22c());
David Brazdildee58d62016-04-07 09:54:26 +00003349 BuildTypeCheck(instruction, destination, reference, type_index, dex_pc);
3350 break;
3351 }
3352
3353 case Instruction::CHECK_CAST: {
3354 uint8_t reference = instruction.VRegA_21c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08003355 dex::TypeIndex type_index(instruction.VRegB_21c());
David Brazdildee58d62016-04-07 09:54:26 +00003356 BuildTypeCheck(instruction, -1, reference, type_index, dex_pc);
3357 break;
3358 }
3359
3360 case Instruction::MONITOR_ENTER: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003361 AppendInstruction(new (allocator_) HMonitorOperation(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003362 LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference),
David Brazdildee58d62016-04-07 09:54:26 +00003363 HMonitorOperation::OperationKind::kEnter,
3364 dex_pc));
Artem Serov2808be82018-12-20 19:15:11 +00003365 graph_->SetHasMonitorOperations(true);
David Brazdildee58d62016-04-07 09:54:26 +00003366 break;
3367 }
3368
3369 case Instruction::MONITOR_EXIT: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003370 AppendInstruction(new (allocator_) HMonitorOperation(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003371 LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference),
David Brazdildee58d62016-04-07 09:54:26 +00003372 HMonitorOperation::OperationKind::kExit,
3373 dex_pc));
Artem Serov2808be82018-12-20 19:15:11 +00003374 graph_->SetHasMonitorOperations(true);
David Brazdildee58d62016-04-07 09:54:26 +00003375 break;
3376 }
3377
3378 case Instruction::SPARSE_SWITCH:
3379 case Instruction::PACKED_SWITCH: {
3380 BuildSwitch(instruction, dex_pc);
3381 break;
3382 }
3383
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003384 case Instruction::UNUSED_3E:
3385 case Instruction::UNUSED_3F:
3386 case Instruction::UNUSED_40:
3387 case Instruction::UNUSED_41:
3388 case Instruction::UNUSED_42:
3389 case Instruction::UNUSED_43:
3390 case Instruction::UNUSED_79:
3391 case Instruction::UNUSED_7A:
3392 case Instruction::UNUSED_F3:
3393 case Instruction::UNUSED_F4:
3394 case Instruction::UNUSED_F5:
3395 case Instruction::UNUSED_F6:
3396 case Instruction::UNUSED_F7:
3397 case Instruction::UNUSED_F8:
3398 case Instruction::UNUSED_F9: {
David Brazdildee58d62016-04-07 09:54:26 +00003399 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07003400 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00003401 << " because of unhandled instruction "
3402 << instruction.Name();
Igor Murashkin1e065a52017-08-09 13:20:34 -07003403 MaybeRecordStat(compilation_stats_,
3404 MethodCompilationStat::kNotCompiledUnhandledInstruction);
David Brazdildee58d62016-04-07 09:54:26 +00003405 return false;
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003406 }
David Brazdildee58d62016-04-07 09:54:26 +00003407 }
3408 return true;
3409} // NOLINT(readability/fn_size)
3410
Vladimir Marko8d6768d2017-03-14 10:13:21 +00003411ObjPtr<mirror::Class> HInstructionBuilder::LookupResolvedType(
3412 dex::TypeIndex type_index,
3413 const DexCompilationUnit& compilation_unit) const {
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003414 return compilation_unit.GetClassLinker()->LookupResolvedType(
Vladimir Marko8d6768d2017-03-14 10:13:21 +00003415 type_index, compilation_unit.GetDexCache().Get(), compilation_unit.GetClassLoader().Get());
3416}
3417
3418ObjPtr<mirror::Class> HInstructionBuilder::LookupReferrerClass() const {
3419 // TODO: Cache the result in a Handle<mirror::Class>.
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003420 const dex::MethodId& method_id =
Vladimir Marko8d6768d2017-03-14 10:13:21 +00003421 dex_compilation_unit_->GetDexFile()->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
3422 return LookupResolvedType(method_id.class_idx_, *dex_compilation_unit_);
3423}
3424
David Brazdildee58d62016-04-07 09:54:26 +00003425} // namespace art