blob: b06d91c82361a998802faf4504334543fd034c36 [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"
David Brazdildee58d62016-04-07 09:54:26 +000020#include "bytecode_utils.h"
21#include "class_linker.h"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010022#include "data_type-inl.h"
Andreas Gampe26de38b2016-07-27 17:53:11 -070023#include "dex_instruction-inl.h"
David Brazdildee58d62016-04-07 09:54:26 +000024#include "driver/compiler_options.h"
Andreas Gampe75a7db62016-09-26 12:04:26 -070025#include "imtable-inl.h"
Mathieu Chartierde4b08f2017-07-10 14:13:41 -070026#include "quicken_info.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070027#include "scoped_thread_state_change-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070028#include "sharpening.h"
Andreas Gampea7c83ac2017-09-11 08:14:23 -070029#include "well_known_classes.h"
David Brazdildee58d62016-04-07 09:54:26 +000030
31namespace art {
32
David Brazdildee58d62016-04-07 09:54:26 +000033HBasicBlock* HInstructionBuilder::FindBlockStartingAt(uint32_t dex_pc) const {
34 return block_builder_->GetBlockAt(dex_pc);
35}
36
Mingyao Yang01b47b02017-02-03 12:09:57 -080037inline ArenaVector<HInstruction*>* HInstructionBuilder::GetLocalsFor(HBasicBlock* block) {
David Brazdildee58d62016-04-07 09:54:26 +000038 ArenaVector<HInstruction*>* locals = &locals_for_[block->GetBlockId()];
39 const size_t vregs = graph_->GetNumberOfVRegs();
Mingyao Yang01b47b02017-02-03 12:09:57 -080040 if (locals->size() == vregs) {
41 return locals;
42 }
43 return GetLocalsForWithAllocation(block, locals, vregs);
44}
David Brazdildee58d62016-04-07 09:54:26 +000045
Mingyao Yang01b47b02017-02-03 12:09:57 -080046ArenaVector<HInstruction*>* HInstructionBuilder::GetLocalsForWithAllocation(
47 HBasicBlock* block,
48 ArenaVector<HInstruction*>* locals,
49 const size_t vregs) {
50 DCHECK_NE(locals->size(), vregs);
51 locals->resize(vregs, nullptr);
52 if (block->IsCatchBlock()) {
53 // We record incoming inputs of catch phis at throwing instructions and
54 // must therefore eagerly create the phis. Phis for undefined vregs will
55 // be deleted when the first throwing instruction with the vreg undefined
56 // is encountered. Unused phis will be removed by dead phi analysis.
57 for (size_t i = 0; i < vregs; ++i) {
58 // No point in creating the catch phi if it is already undefined at
59 // the first throwing instruction.
60 HInstruction* current_local_value = (*current_locals_)[i];
61 if (current_local_value != nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +010062 HPhi* phi = new (allocator_) HPhi(
63 allocator_,
Mingyao Yang01b47b02017-02-03 12:09:57 -080064 i,
65 0,
66 current_local_value->GetType());
67 block->AddPhi(phi);
68 (*locals)[i] = phi;
David Brazdildee58d62016-04-07 09:54:26 +000069 }
70 }
71 }
72 return locals;
73}
74
Mingyao Yang01b47b02017-02-03 12:09:57 -080075inline HInstruction* HInstructionBuilder::ValueOfLocalAt(HBasicBlock* block, size_t local) {
David Brazdildee58d62016-04-07 09:54:26 +000076 ArenaVector<HInstruction*>* locals = GetLocalsFor(block);
77 return (*locals)[local];
78}
79
80void HInstructionBuilder::InitializeBlockLocals() {
81 current_locals_ = GetLocalsFor(current_block_);
82
83 if (current_block_->IsCatchBlock()) {
84 // Catch phis were already created and inputs collected from throwing sites.
85 if (kIsDebugBuild) {
86 // Make sure there was at least one throwing instruction which initialized
87 // locals (guaranteed by HGraphBuilder) and that all try blocks have been
88 // visited already (from HTryBoundary scoping and reverse post order).
89 bool catch_block_visited = false;
Vladimir Marko2c45bc92016-10-25 16:54:12 +010090 for (HBasicBlock* current : graph_->GetReversePostOrder()) {
David Brazdildee58d62016-04-07 09:54:26 +000091 if (current == current_block_) {
92 catch_block_visited = true;
93 } else if (current->IsTryBlock()) {
94 const HTryBoundary& try_entry = current->GetTryCatchInformation()->GetTryEntry();
95 if (try_entry.HasExceptionHandler(*current_block_)) {
96 DCHECK(!catch_block_visited) << "Catch block visited before its try block.";
97 }
98 }
99 }
100 DCHECK_EQ(current_locals_->size(), graph_->GetNumberOfVRegs())
101 << "No instructions throwing into a live catch block.";
102 }
103 } else if (current_block_->IsLoopHeader()) {
104 // If the block is a loop header, we know we only have visited the pre header
105 // because we are visiting in reverse post order. We create phis for all initialized
106 // locals from the pre header. Their inputs will be populated at the end of
107 // the analysis.
108 for (size_t local = 0; local < current_locals_->size(); ++local) {
109 HInstruction* incoming =
110 ValueOfLocalAt(current_block_->GetLoopInformation()->GetPreHeader(), local);
111 if (incoming != nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100112 HPhi* phi = new (allocator_) HPhi(
113 allocator_,
David Brazdildee58d62016-04-07 09:54:26 +0000114 local,
115 0,
116 incoming->GetType());
117 current_block_->AddPhi(phi);
118 (*current_locals_)[local] = phi;
119 }
120 }
121
122 // Save the loop header so that the last phase of the analysis knows which
123 // blocks need to be updated.
124 loop_headers_.push_back(current_block_);
125 } else if (current_block_->GetPredecessors().size() > 0) {
126 // All predecessors have already been visited because we are visiting in reverse post order.
127 // We merge the values of all locals, creating phis if those values differ.
128 for (size_t local = 0; local < current_locals_->size(); ++local) {
129 bool one_predecessor_has_no_value = false;
130 bool is_different = false;
131 HInstruction* value = ValueOfLocalAt(current_block_->GetPredecessors()[0], local);
132
133 for (HBasicBlock* predecessor : current_block_->GetPredecessors()) {
134 HInstruction* current = ValueOfLocalAt(predecessor, local);
135 if (current == nullptr) {
136 one_predecessor_has_no_value = true;
137 break;
138 } else if (current != value) {
139 is_different = true;
140 }
141 }
142
143 if (one_predecessor_has_no_value) {
144 // If one predecessor has no value for this local, we trust the verifier has
145 // successfully checked that there is a store dominating any read after this block.
146 continue;
147 }
148
149 if (is_different) {
150 HInstruction* first_input = ValueOfLocalAt(current_block_->GetPredecessors()[0], local);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100151 HPhi* phi = new (allocator_) HPhi(
152 allocator_,
David Brazdildee58d62016-04-07 09:54:26 +0000153 local,
154 current_block_->GetPredecessors().size(),
155 first_input->GetType());
156 for (size_t i = 0; i < current_block_->GetPredecessors().size(); i++) {
157 HInstruction* pred_value = ValueOfLocalAt(current_block_->GetPredecessors()[i], local);
158 phi->SetRawInputAt(i, pred_value);
159 }
160 current_block_->AddPhi(phi);
161 value = phi;
162 }
163 (*current_locals_)[local] = value;
164 }
165 }
166}
167
168void HInstructionBuilder::PropagateLocalsToCatchBlocks() {
169 const HTryBoundary& try_entry = current_block_->GetTryCatchInformation()->GetTryEntry();
170 for (HBasicBlock* catch_block : try_entry.GetExceptionHandlers()) {
171 ArenaVector<HInstruction*>* handler_locals = GetLocalsFor(catch_block);
172 DCHECK_EQ(handler_locals->size(), current_locals_->size());
173 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
174 HInstruction* handler_value = (*handler_locals)[vreg];
175 if (handler_value == nullptr) {
176 // Vreg was undefined at a previously encountered throwing instruction
177 // and the catch phi was deleted. Do not record the local value.
178 continue;
179 }
180 DCHECK(handler_value->IsPhi());
181
182 HInstruction* local_value = (*current_locals_)[vreg];
183 if (local_value == nullptr) {
184 // This is the first instruction throwing into `catch_block` where
185 // `vreg` is undefined. Delete the catch phi.
186 catch_block->RemovePhi(handler_value->AsPhi());
187 (*handler_locals)[vreg] = nullptr;
188 } else {
189 // Vreg has been defined at all instructions throwing into `catch_block`
190 // encountered so far. Record the local value in the catch phi.
191 handler_value->AsPhi()->AddInput(local_value);
192 }
193 }
194 }
195}
196
197void HInstructionBuilder::AppendInstruction(HInstruction* instruction) {
198 current_block_->AddInstruction(instruction);
199 InitializeInstruction(instruction);
200}
201
202void HInstructionBuilder::InsertInstructionAtTop(HInstruction* instruction) {
203 if (current_block_->GetInstructions().IsEmpty()) {
204 current_block_->AddInstruction(instruction);
205 } else {
206 current_block_->InsertInstructionBefore(instruction, current_block_->GetFirstInstruction());
207 }
208 InitializeInstruction(instruction);
209}
210
211void HInstructionBuilder::InitializeInstruction(HInstruction* instruction) {
212 if (instruction->NeedsEnvironment()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100213 HEnvironment* environment = new (allocator_) HEnvironment(
214 allocator_,
David Brazdildee58d62016-04-07 09:54:26 +0000215 current_locals_->size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000216 graph_->GetArtMethod(),
David Brazdildee58d62016-04-07 09:54:26 +0000217 instruction->GetDexPc(),
David Brazdildee58d62016-04-07 09:54:26 +0000218 instruction);
219 environment->CopyFrom(*current_locals_);
220 instruction->SetRawEnvironment(environment);
221 }
222}
223
David Brazdilc120bbe2016-04-22 16:57:00 +0100224HInstruction* HInstructionBuilder::LoadNullCheckedLocal(uint32_t register_index, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100225 HInstruction* ref = LoadLocal(register_index, DataType::Type::kReference);
David Brazdilc120bbe2016-04-22 16:57:00 +0100226 if (!ref->CanBeNull()) {
227 return ref;
228 }
229
Vladimir Markoca6fff82017-10-03 14:49:14 +0100230 HNullCheck* null_check = new (allocator_) HNullCheck(ref, dex_pc);
David Brazdilc120bbe2016-04-22 16:57:00 +0100231 AppendInstruction(null_check);
232 return null_check;
233}
234
David Brazdildee58d62016-04-07 09:54:26 +0000235void HInstructionBuilder::SetLoopHeaderPhiInputs() {
236 for (size_t i = loop_headers_.size(); i > 0; --i) {
237 HBasicBlock* block = loop_headers_[i - 1];
238 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
239 HPhi* phi = it.Current()->AsPhi();
240 size_t vreg = phi->GetRegNumber();
241 for (HBasicBlock* predecessor : block->GetPredecessors()) {
242 HInstruction* value = ValueOfLocalAt(predecessor, vreg);
243 if (value == nullptr) {
244 // Vreg is undefined at this predecessor. Mark it dead and leave with
245 // fewer inputs than predecessors. SsaChecker will fail if not removed.
246 phi->SetDead();
247 break;
248 } else {
249 phi->AddInput(value);
250 }
251 }
252 }
253 }
254}
255
256static bool IsBlockPopulated(HBasicBlock* block) {
257 if (block->IsLoopHeader()) {
258 // Suspend checks were inserted into loop headers during building of dominator tree.
259 DCHECK(block->GetFirstInstruction()->IsSuspendCheck());
260 return block->GetFirstInstruction() != block->GetLastInstruction();
261 } else {
262 return !block->GetInstructions().IsEmpty();
263 }
264}
265
266bool HInstructionBuilder::Build() {
267 locals_for_.resize(graph_->GetBlocks().size(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100268 ArenaVector<HInstruction*>(allocator_->Adapter(kArenaAllocGraphBuilder)));
David Brazdildee58d62016-04-07 09:54:26 +0000269
270 // Find locations where we want to generate extra stackmaps for native debugging.
271 // This allows us to generate the info only at interesting points (for example,
272 // at start of java statement) rather than before every dex instruction.
273 const bool native_debuggable = compiler_driver_ != nullptr &&
274 compiler_driver_->GetCompilerOptions().GetNativeDebuggable();
275 ArenaBitVector* native_debug_info_locations = nullptr;
276 if (native_debuggable) {
277 const uint32_t num_instructions = code_item_.insns_size_in_code_units_;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100278 native_debug_info_locations =
279 new (allocator_) ArenaBitVector (allocator_, num_instructions, false);
David Brazdildee58d62016-04-07 09:54:26 +0000280 FindNativeDebugInfoLocations(native_debug_info_locations);
281 }
282
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100283 for (HBasicBlock* block : graph_->GetReversePostOrder()) {
284 current_block_ = block;
David Brazdildee58d62016-04-07 09:54:26 +0000285 uint32_t block_dex_pc = current_block_->GetDexPc();
286
287 InitializeBlockLocals();
288
289 if (current_block_->IsEntryBlock()) {
290 InitializeParameters();
Vladimir Markoca6fff82017-10-03 14:49:14 +0100291 AppendInstruction(new (allocator_) HSuspendCheck(0u));
292 AppendInstruction(new (allocator_) HGoto(0u));
David Brazdildee58d62016-04-07 09:54:26 +0000293 continue;
294 } else if (current_block_->IsExitBlock()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100295 AppendInstruction(new (allocator_) HExit());
David Brazdildee58d62016-04-07 09:54:26 +0000296 continue;
297 } else if (current_block_->IsLoopHeader()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100298 HSuspendCheck* suspend_check = new (allocator_) HSuspendCheck(current_block_->GetDexPc());
David Brazdildee58d62016-04-07 09:54:26 +0000299 current_block_->GetLoopInformation()->SetSuspendCheck(suspend_check);
300 // This is slightly odd because the loop header might not be empty (TryBoundary).
301 // But we're still creating the environment with locals from the top of the block.
302 InsertInstructionAtTop(suspend_check);
303 }
304
305 if (block_dex_pc == kNoDexPc || current_block_ != block_builder_->GetBlockAt(block_dex_pc)) {
306 // Synthetic block that does not need to be populated.
307 DCHECK(IsBlockPopulated(current_block_));
308 continue;
309 }
310
311 DCHECK(!IsBlockPopulated(current_block_));
312
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700313 uint32_t quicken_index = 0;
314 if (CanDecodeQuickenedInfo()) {
315 quicken_index = block_builder_->GetQuickenIndex(block_dex_pc);
316 }
317
David Brazdildee58d62016-04-07 09:54:26 +0000318 for (CodeItemIterator it(code_item_, block_dex_pc); !it.Done(); it.Advance()) {
319 if (current_block_ == nullptr) {
320 // The previous instruction ended this block.
321 break;
322 }
323
324 uint32_t dex_pc = it.CurrentDexPc();
325 if (dex_pc != block_dex_pc && FindBlockStartingAt(dex_pc) != nullptr) {
326 // This dex_pc starts a new basic block.
327 break;
328 }
329
330 if (current_block_->IsTryBlock() && IsThrowingDexInstruction(it.CurrentInstruction())) {
331 PropagateLocalsToCatchBlocks();
332 }
333
334 if (native_debuggable && native_debug_info_locations->IsBitSet(dex_pc)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100335 AppendInstruction(new (allocator_) HNativeDebugInfo(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000336 }
337
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700338 if (!ProcessDexInstruction(it.CurrentInstruction(), dex_pc, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +0000339 return false;
340 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700341
342 if (QuickenInfoTable::NeedsIndexForInstruction(&it.CurrentInstruction())) {
343 ++quicken_index;
344 }
David Brazdildee58d62016-04-07 09:54:26 +0000345 }
346
347 if (current_block_ != nullptr) {
348 // Branching instructions clear current_block, so we know the last
349 // instruction of the current block is not a branching instruction.
350 // We add an unconditional Goto to the next block.
351 DCHECK_EQ(current_block_->GetSuccessors().size(), 1u);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100352 AppendInstruction(new (allocator_) HGoto());
David Brazdildee58d62016-04-07 09:54:26 +0000353 }
354 }
355
356 SetLoopHeaderPhiInputs();
357
358 return true;
359}
360
361void HInstructionBuilder::FindNativeDebugInfoLocations(ArenaBitVector* locations) {
362 // The callback gets called when the line number changes.
363 // In other words, it marks the start of new java statement.
364 struct Callback {
365 static bool Position(void* ctx, const DexFile::PositionInfo& entry) {
366 static_cast<ArenaBitVector*>(ctx)->SetBit(entry.address_);
367 return false;
368 }
369 };
370 dex_file_->DecodeDebugPositionInfo(&code_item_, Callback::Position, locations);
371 // Instruction-specific tweaks.
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -0700372 IterationRange<DexInstructionIterator> instructions = code_item_.Instructions();
373 for (const Instruction& inst : instructions) {
374 switch (inst.Opcode()) {
David Brazdildee58d62016-04-07 09:54:26 +0000375 case Instruction::MOVE_EXCEPTION: {
376 // Stop in native debugger after the exception has been moved.
377 // The compiler also expects the move at the start of basic block so
378 // we do not want to interfere by inserting native-debug-info before it.
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -0700379 locations->ClearBit(inst.GetDexPc(code_item_.insns_));
380 const Instruction* next = inst.Next();
381 if (DexInstructionIterator(next) != instructions.end()) {
David Brazdildee58d62016-04-07 09:54:26 +0000382 locations->SetBit(next->GetDexPc(code_item_.insns_));
383 }
384 break;
385 }
386 default:
387 break;
388 }
389 }
390}
391
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100392HInstruction* HInstructionBuilder::LoadLocal(uint32_t reg_number, DataType::Type type) const {
David Brazdildee58d62016-04-07 09:54:26 +0000393 HInstruction* value = (*current_locals_)[reg_number];
394 DCHECK(value != nullptr);
395
396 // If the operation requests a specific type, we make sure its input is of that type.
397 if (type != value->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100398 if (DataType::IsFloatingPointType(type)) {
Aart Bik31883642016-06-06 15:02:44 -0700399 value = ssa_builder_->GetFloatOrDoubleEquivalent(value, type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100400 } else if (type == DataType::Type::kReference) {
Aart Bik31883642016-06-06 15:02:44 -0700401 value = ssa_builder_->GetReferenceTypeEquivalent(value);
David Brazdildee58d62016-04-07 09:54:26 +0000402 }
Aart Bik31883642016-06-06 15:02:44 -0700403 DCHECK(value != nullptr);
David Brazdildee58d62016-04-07 09:54:26 +0000404 }
405
406 return value;
407}
408
409void HInstructionBuilder::UpdateLocal(uint32_t reg_number, HInstruction* stored_value) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100410 DataType::Type stored_type = stored_value->GetType();
411 DCHECK_NE(stored_type, DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +0000412
413 // Storing into vreg `reg_number` may implicitly invalidate the surrounding
414 // registers. Consider the following cases:
415 // (1) Storing a wide value must overwrite previous values in both `reg_number`
416 // and `reg_number+1`. We store `nullptr` in `reg_number+1`.
417 // (2) If vreg `reg_number-1` holds a wide value, writing into `reg_number`
418 // must invalidate it. We store `nullptr` in `reg_number-1`.
419 // Consequently, storing a wide value into the high vreg of another wide value
420 // will invalidate both `reg_number-1` and `reg_number+1`.
421
422 if (reg_number != 0) {
423 HInstruction* local_low = (*current_locals_)[reg_number - 1];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100424 if (local_low != nullptr && DataType::Is64BitType(local_low->GetType())) {
David Brazdildee58d62016-04-07 09:54:26 +0000425 // The vreg we are storing into was previously the high vreg of a pair.
426 // We need to invalidate its low vreg.
427 DCHECK((*current_locals_)[reg_number] == nullptr);
428 (*current_locals_)[reg_number - 1] = nullptr;
429 }
430 }
431
432 (*current_locals_)[reg_number] = stored_value;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100433 if (DataType::Is64BitType(stored_type)) {
David Brazdildee58d62016-04-07 09:54:26 +0000434 // We are storing a pair. Invalidate the instruction in the high vreg.
435 (*current_locals_)[reg_number + 1] = nullptr;
436 }
437}
438
439void HInstructionBuilder::InitializeParameters() {
440 DCHECK(current_block_->IsEntryBlock());
441
442 // dex_compilation_unit_ is null only when unit testing.
443 if (dex_compilation_unit_ == nullptr) {
444 return;
445 }
446
447 const char* shorty = dex_compilation_unit_->GetShorty();
448 uint16_t number_of_parameters = graph_->GetNumberOfInVRegs();
449 uint16_t locals_index = graph_->GetNumberOfLocalVRegs();
450 uint16_t parameter_index = 0;
451
452 const DexFile::MethodId& referrer_method_id =
453 dex_file_->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
454 if (!dex_compilation_unit_->IsStatic()) {
455 // Add the implicit 'this' argument, not expressed in the signature.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100456 HParameterValue* parameter = new (allocator_) HParameterValue(*dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +0000457 referrer_method_id.class_idx_,
458 parameter_index++,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100459 DataType::Type::kReference,
Igor Murashkind01745e2017-04-05 16:40:31 -0700460 /* is_this */ true);
David Brazdildee58d62016-04-07 09:54:26 +0000461 AppendInstruction(parameter);
462 UpdateLocal(locals_index++, parameter);
463 number_of_parameters--;
Igor Murashkind01745e2017-04-05 16:40:31 -0700464 current_this_parameter_ = parameter;
465 } else {
466 DCHECK(current_this_parameter_ == nullptr);
David Brazdildee58d62016-04-07 09:54:26 +0000467 }
468
469 const DexFile::ProtoId& proto = dex_file_->GetMethodPrototype(referrer_method_id);
470 const DexFile::TypeList* arg_types = dex_file_->GetProtoParameters(proto);
471 for (int i = 0, shorty_pos = 1; i < number_of_parameters; i++) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100472 HParameterValue* parameter = new (allocator_) HParameterValue(
David Brazdildee58d62016-04-07 09:54:26 +0000473 *dex_file_,
474 arg_types->GetTypeItem(shorty_pos - 1).type_idx_,
475 parameter_index++,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100476 DataType::FromShorty(shorty[shorty_pos]),
Igor Murashkind01745e2017-04-05 16:40:31 -0700477 /* is_this */ false);
David Brazdildee58d62016-04-07 09:54:26 +0000478 ++shorty_pos;
479 AppendInstruction(parameter);
480 // Store the parameter value in the local that the dex code will use
481 // to reference that parameter.
482 UpdateLocal(locals_index++, parameter);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100483 if (DataType::Is64BitType(parameter->GetType())) {
David Brazdildee58d62016-04-07 09:54:26 +0000484 i++;
485 locals_index++;
486 parameter_index++;
487 }
488 }
489}
490
491template<typename T>
492void HInstructionBuilder::If_22t(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100493 HInstruction* first = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
494 HInstruction* second = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100495 T* comparison = new (allocator_) T(first, second, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000496 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100497 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000498 current_block_ = nullptr;
499}
500
501template<typename T>
502void HInstructionBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100503 HInstruction* value = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100504 T* comparison = new (allocator_) T(value, graph_->GetIntConstant(0, dex_pc), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000505 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100506 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000507 current_block_ = nullptr;
508}
509
510template<typename T>
511void HInstructionBuilder::Unop_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100512 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000513 uint32_t dex_pc) {
514 HInstruction* first = LoadLocal(instruction.VRegB(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100515 AppendInstruction(new (allocator_) T(type, first, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000516 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
517}
518
519void HInstructionBuilder::Conversion_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100520 DataType::Type input_type,
521 DataType::Type result_type,
David Brazdildee58d62016-04-07 09:54:26 +0000522 uint32_t dex_pc) {
523 HInstruction* first = LoadLocal(instruction.VRegB(), input_type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100524 AppendInstruction(new (allocator_) HTypeConversion(result_type, first, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000525 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
526}
527
528template<typename T>
529void HInstructionBuilder::Binop_23x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100530 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000531 uint32_t dex_pc) {
532 HInstruction* first = LoadLocal(instruction.VRegB(), type);
533 HInstruction* second = LoadLocal(instruction.VRegC(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100534 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000535 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
536}
537
538template<typename T>
539void HInstructionBuilder::Binop_23x_shift(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100540 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000541 uint32_t dex_pc) {
542 HInstruction* first = LoadLocal(instruction.VRegB(), type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100543 HInstruction* second = LoadLocal(instruction.VRegC(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100544 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000545 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
546}
547
548void HInstructionBuilder::Binop_23x_cmp(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100549 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000550 ComparisonBias bias,
551 uint32_t dex_pc) {
552 HInstruction* first = LoadLocal(instruction.VRegB(), type);
553 HInstruction* second = LoadLocal(instruction.VRegC(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100554 AppendInstruction(new (allocator_) HCompare(type, first, second, bias, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000555 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
556}
557
558template<typename T>
559void HInstructionBuilder::Binop_12x_shift(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100560 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000561 uint32_t dex_pc) {
562 HInstruction* first = LoadLocal(instruction.VRegA(), type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100563 HInstruction* second = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100564 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000565 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
566}
567
568template<typename T>
569void HInstructionBuilder::Binop_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100570 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000571 uint32_t dex_pc) {
572 HInstruction* first = LoadLocal(instruction.VRegA(), type);
573 HInstruction* second = LoadLocal(instruction.VRegB(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100574 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000575 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
576}
577
578template<typename T>
579void HInstructionBuilder::Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100580 HInstruction* first = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000581 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s(), dex_pc);
582 if (reverse) {
583 std::swap(first, second);
584 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100585 AppendInstruction(new (allocator_) T(DataType::Type::kInt32, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000586 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
587}
588
589template<typename T>
590void HInstructionBuilder::Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100591 HInstruction* first = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000592 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b(), dex_pc);
593 if (reverse) {
594 std::swap(first, second);
595 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100596 AppendInstruction(new (allocator_) T(DataType::Type::kInt32, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000597 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
598}
599
Igor Murashkind01745e2017-04-05 16:40:31 -0700600// Does the method being compiled need any constructor barriers being inserted?
601// (Always 'false' for methods that aren't <init>.)
Mathieu Chartierc4ae9162016-04-07 13:19:19 -0700602static bool RequiresConstructorBarrier(const DexCompilationUnit* cu, CompilerDriver* driver) {
Igor Murashkin032cacd2017-04-06 14:40:08 -0700603 // Can be null in unit tests only.
604 if (UNLIKELY(cu == nullptr)) {
605 return false;
606 }
607
David Brazdildee58d62016-04-07 09:54:26 +0000608 Thread* self = Thread::Current();
609 return cu->IsConstructor()
Igor Murashkind01745e2017-04-05 16:40:31 -0700610 && !cu->IsStatic()
611 // RequiresConstructorBarrier must only be queried for <init> methods;
612 // it's effectively "false" for every other method.
613 //
614 // See CompilerDriver::RequiresConstructBarrier for more explanation.
Mathieu Chartierc4ae9162016-04-07 13:19:19 -0700615 && driver->RequiresConstructorBarrier(self, cu->GetDexFile(), cu->GetClassDefIndex());
David Brazdildee58d62016-04-07 09:54:26 +0000616}
617
618// Returns true if `block` has only one successor which starts at the next
619// dex_pc after `instruction` at `dex_pc`.
620static bool IsFallthroughInstruction(const Instruction& instruction,
621 uint32_t dex_pc,
622 HBasicBlock* block) {
623 uint32_t next_dex_pc = dex_pc + instruction.SizeInCodeUnits();
624 return block->GetSingleSuccessor()->GetDexPc() == next_dex_pc;
625}
626
627void HInstructionBuilder::BuildSwitch(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100628 HInstruction* value = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000629 DexSwitchTable table(instruction, dex_pc);
630
631 if (table.GetNumEntries() == 0) {
632 // Empty Switch. Code falls through to the next block.
633 DCHECK(IsFallthroughInstruction(instruction, dex_pc, current_block_));
Vladimir Markoca6fff82017-10-03 14:49:14 +0100634 AppendInstruction(new (allocator_) HGoto(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000635 } else if (table.ShouldBuildDecisionTree()) {
636 for (DexSwitchTableIterator it(table); !it.Done(); it.Advance()) {
637 HInstruction* case_value = graph_->GetIntConstant(it.CurrentKey(), dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100638 HEqual* comparison = new (allocator_) HEqual(value, case_value, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000639 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100640 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000641
642 if (!it.IsLast()) {
643 current_block_ = FindBlockStartingAt(it.GetDexPcForCurrentIndex());
644 }
645 }
646 } else {
647 AppendInstruction(
Vladimir Markoca6fff82017-10-03 14:49:14 +0100648 new (allocator_) HPackedSwitch(table.GetEntryAt(0), table.GetNumEntries(), value, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000649 }
650
651 current_block_ = nullptr;
652}
653
654void HInstructionBuilder::BuildReturn(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100655 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000656 uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100657 if (type == DataType::Type::kVoid) {
Igor Murashkind01745e2017-04-05 16:40:31 -0700658 // Only <init> (which is a return-void) could possibly have a constructor fence.
Igor Murashkin032cacd2017-04-06 14:40:08 -0700659 // This may insert additional redundant constructor fences from the super constructors.
660 // TODO: remove redundant constructor fences (b/36656456).
661 if (RequiresConstructorBarrier(dex_compilation_unit_, compiler_driver_)) {
Igor Murashkind01745e2017-04-05 16:40:31 -0700662 // Compiling instance constructor.
Vladimir Markoba118822017-06-12 15:41:56 +0100663 DCHECK_STREQ("<init>", graph_->GetMethodName());
Igor Murashkind01745e2017-04-05 16:40:31 -0700664
665 HInstruction* fence_target = current_this_parameter_;
666 DCHECK(fence_target != nullptr);
667
Vladimir Markoca6fff82017-10-03 14:49:14 +0100668 AppendInstruction(new (allocator_) HConstructorFence(fence_target, dex_pc, allocator_));
Igor Murashkin6ef45672017-08-08 13:59:55 -0700669 MaybeRecordStat(
670 compilation_stats_,
671 MethodCompilationStat::kConstructorFenceGeneratedFinal);
David Brazdildee58d62016-04-07 09:54:26 +0000672 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100673 AppendInstruction(new (allocator_) HReturnVoid(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000674 } else {
Igor Murashkind01745e2017-04-05 16:40:31 -0700675 DCHECK(!RequiresConstructorBarrier(dex_compilation_unit_, compiler_driver_));
David Brazdildee58d62016-04-07 09:54:26 +0000676 HInstruction* value = LoadLocal(instruction.VRegA(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100677 AppendInstruction(new (allocator_) HReturn(value, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000678 }
679 current_block_ = nullptr;
680}
681
682static InvokeType GetInvokeTypeFromOpCode(Instruction::Code opcode) {
683 switch (opcode) {
684 case Instruction::INVOKE_STATIC:
685 case Instruction::INVOKE_STATIC_RANGE:
686 return kStatic;
687 case Instruction::INVOKE_DIRECT:
688 case Instruction::INVOKE_DIRECT_RANGE:
689 return kDirect;
690 case Instruction::INVOKE_VIRTUAL:
691 case Instruction::INVOKE_VIRTUAL_QUICK:
692 case Instruction::INVOKE_VIRTUAL_RANGE:
693 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
694 return kVirtual;
695 case Instruction::INVOKE_INTERFACE:
696 case Instruction::INVOKE_INTERFACE_RANGE:
697 return kInterface;
698 case Instruction::INVOKE_SUPER_RANGE:
699 case Instruction::INVOKE_SUPER:
700 return kSuper;
701 default:
702 LOG(FATAL) << "Unexpected invoke opcode: " << opcode;
703 UNREACHABLE();
704 }
705}
706
707ArtMethod* HInstructionBuilder::ResolveMethod(uint16_t method_idx, InvokeType invoke_type) {
708 ScopedObjectAccess soa(Thread::Current());
David Brazdildee58d62016-04-07 09:54:26 +0000709
710 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000711 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100712
Vladimir Markoba118822017-06-12 15:41:56 +0100713 ArtMethod* resolved_method =
714 class_linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
715 *dex_compilation_unit_->GetDexFile(),
716 method_idx,
717 dex_compilation_unit_->GetDexCache(),
718 class_loader,
719 graph_->GetArtMethod(),
720 invoke_type);
David Brazdildee58d62016-04-07 09:54:26 +0000721
722 if (UNLIKELY(resolved_method == nullptr)) {
723 // Clean up any exception left by type resolution.
724 soa.Self()->ClearException();
725 return nullptr;
726 }
727
Vladimir Markoba118822017-06-12 15:41:56 +0100728 // The referrer may be unresolved for AOT if we're compiling a class that cannot be
729 // resolved because, for example, we don't find a superclass in the classpath.
730 if (graph_->GetArtMethod() == nullptr) {
731 // The class linker cannot check access without a referrer, so we have to do it.
732 // Fall back to HInvokeUnresolved if the method isn't public.
David Brazdildee58d62016-04-07 09:54:26 +0000733 if (!resolved_method->IsPublic()) {
734 return nullptr;
735 }
David Brazdildee58d62016-04-07 09:54:26 +0000736 }
737
738 // We have to special case the invoke-super case, as ClassLinker::ResolveMethod does not.
739 // We need to look at the referrer's super class vtable. We need to do this to know if we need to
740 // make this an invoke-unresolved to handle cross-dex invokes or abstract super methods, both of
741 // which require runtime handling.
742 if (invoke_type == kSuper) {
Vladimir Markoba118822017-06-12 15:41:56 +0100743 ObjPtr<mirror::Class> compiling_class = GetCompilingClass();
Andreas Gampefa4333d2017-02-14 11:10:34 -0800744 if (compiling_class == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +0000745 // We could not determine the method's class we need to wait until runtime.
746 DCHECK(Runtime::Current()->IsAotCompiler());
747 return nullptr;
748 }
Vladimir Markoba118822017-06-12 15:41:56 +0100749 ObjPtr<mirror::Class> referenced_class = class_linker->LookupResolvedType(
750 *dex_compilation_unit_->GetDexFile(),
751 dex_compilation_unit_->GetDexFile()->GetMethodId(method_idx).class_idx_,
752 dex_compilation_unit_->GetDexCache().Get(),
753 class_loader.Get());
754 DCHECK(referenced_class != nullptr); // We have already resolved a method from this class.
755 if (!referenced_class->IsAssignableFrom(compiling_class)) {
Aart Bikf663e342016-04-04 17:28:59 -0700756 // We cannot statically determine the target method. The runtime will throw a
757 // NoSuchMethodError on this one.
758 return nullptr;
759 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100760 ArtMethod* actual_method;
Vladimir Markoba118822017-06-12 15:41:56 +0100761 if (referenced_class->IsInterface()) {
762 actual_method = referenced_class->FindVirtualMethodForInterfaceSuper(
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100763 resolved_method, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000764 } else {
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100765 uint16_t vtable_index = resolved_method->GetMethodIndex();
766 actual_method = compiling_class->GetSuperClass()->GetVTableEntry(
767 vtable_index, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000768 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100769 if (actual_method != resolved_method &&
770 !IsSameDexFile(*actual_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
771 // The back-end code generator relies on this check in order to ensure that it will not
772 // attempt to read the dex_cache with a dex_method_index that is not from the correct
773 // dex_file. If we didn't do this check then the dex_method_index will not be updated in the
774 // builder, which means that the code-generator (and compiler driver during sharpening and
775 // inliner, maybe) might invoke an incorrect method.
776 // TODO: The actual method could still be referenced in the current dex file, so we
777 // could try locating it.
778 // TODO: Remove the dex_file restriction.
779 return nullptr;
780 }
781 if (!actual_method->IsInvokable()) {
782 // Fail if the actual method cannot be invoked. Otherwise, the runtime resolution stub
783 // could resolve the callee to the wrong method.
784 return nullptr;
785 }
786 resolved_method = actual_method;
David Brazdildee58d62016-04-07 09:54:26 +0000787 }
788
David Brazdildee58d62016-04-07 09:54:26 +0000789 return resolved_method;
790}
791
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100792static bool IsStringConstructor(ArtMethod* method) {
793 ScopedObjectAccess soa(Thread::Current());
794 return method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
795}
796
David Brazdildee58d62016-04-07 09:54:26 +0000797bool HInstructionBuilder::BuildInvoke(const Instruction& instruction,
798 uint32_t dex_pc,
799 uint32_t method_idx,
800 uint32_t number_of_vreg_arguments,
801 bool is_range,
802 uint32_t* args,
803 uint32_t register_index) {
804 InvokeType invoke_type = GetInvokeTypeFromOpCode(instruction.Opcode());
805 const char* descriptor = dex_file_->GetMethodShorty(method_idx);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100806 DataType::Type return_type = DataType::FromShorty(descriptor[0]);
David Brazdildee58d62016-04-07 09:54:26 +0000807
808 // Remove the return type from the 'proto'.
809 size_t number_of_arguments = strlen(descriptor) - 1;
810 if (invoke_type != kStatic) { // instance call
811 // One extra argument for 'this'.
812 number_of_arguments++;
813 }
814
David Brazdildee58d62016-04-07 09:54:26 +0000815 ArtMethod* resolved_method = ResolveMethod(method_idx, invoke_type);
816
817 if (UNLIKELY(resolved_method == nullptr)) {
Igor Murashkin1e065a52017-08-09 13:20:34 -0700818 MaybeRecordStat(compilation_stats_,
819 MethodCompilationStat::kUnresolvedMethod);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100820 HInvoke* invoke = new (allocator_) HInvokeUnresolved(allocator_,
821 number_of_arguments,
822 return_type,
823 dex_pc,
824 method_idx,
825 invoke_type);
David Brazdildee58d62016-04-07 09:54:26 +0000826 return HandleInvoke(invoke,
827 number_of_vreg_arguments,
828 args,
829 register_index,
830 is_range,
831 descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -0700832 nullptr, /* clinit_check */
833 true /* is_unresolved */);
David Brazdildee58d62016-04-07 09:54:26 +0000834 }
835
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100836 // Replace calls to String.<init> with StringFactory.
837 if (IsStringConstructor(resolved_method)) {
838 uint32_t string_init_entry_point = WellKnownClasses::StringInitToEntryPoint(resolved_method);
839 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
840 HInvokeStaticOrDirect::MethodLoadKind::kStringInit,
841 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000842 dchecked_integral_cast<uint64_t>(string_init_entry_point)
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100843 };
844 MethodReference target_method(dex_file_, method_idx);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100845 HInvoke* invoke = new (allocator_) HInvokeStaticOrDirect(
846 allocator_,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100847 number_of_arguments - 1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100848 DataType::Type::kReference /*return_type */,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100849 dex_pc,
850 method_idx,
851 nullptr,
852 dispatch_info,
853 invoke_type,
854 target_method,
855 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit);
856 return HandleStringInit(invoke,
857 number_of_vreg_arguments,
858 args,
859 register_index,
860 is_range,
861 descriptor);
862 }
863
David Brazdildee58d62016-04-07 09:54:26 +0000864 // Potential class initialization check, in the case of a static method call.
865 HClinitCheck* clinit_check = nullptr;
866 HInvoke* invoke = nullptr;
867 if (invoke_type == kDirect || invoke_type == kStatic || invoke_type == kSuper) {
868 // By default, consider that the called method implicitly requires
869 // an initialization check of its declaring method.
870 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement
871 = HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
872 ScopedObjectAccess soa(Thread::Current());
873 if (invoke_type == kStatic) {
874 clinit_check = ProcessClinitCheckForInvoke(
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000875 dex_pc, resolved_method, &clinit_check_requirement);
David Brazdildee58d62016-04-07 09:54:26 +0000876 } else if (invoke_type == kSuper) {
877 if (IsSameDexFile(*resolved_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100878 // Update the method index to the one resolved. Note that this may be a no-op if
David Brazdildee58d62016-04-07 09:54:26 +0000879 // we resolved to the method referenced by the instruction.
880 method_idx = resolved_method->GetDexMethodIndex();
David Brazdildee58d62016-04-07 09:54:26 +0000881 }
882 }
883
884 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
Vladimir Markoe7197bf2017-06-02 17:00:23 +0100885 HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall,
David Brazdildee58d62016-04-07 09:54:26 +0000886 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000887 0u
David Brazdildee58d62016-04-07 09:54:26 +0000888 };
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100889 MethodReference target_method(resolved_method->GetDexFile(),
890 resolved_method->GetDexMethodIndex());
Vladimir Markoca6fff82017-10-03 14:49:14 +0100891 invoke = new (allocator_) HInvokeStaticOrDirect(allocator_,
892 number_of_arguments,
893 return_type,
894 dex_pc,
895 method_idx,
896 resolved_method,
897 dispatch_info,
898 invoke_type,
899 target_method,
900 clinit_check_requirement);
David Brazdildee58d62016-04-07 09:54:26 +0000901 } else if (invoke_type == kVirtual) {
902 ScopedObjectAccess soa(Thread::Current()); // Needed for the method index
Vladimir Markoca6fff82017-10-03 14:49:14 +0100903 invoke = new (allocator_) HInvokeVirtual(allocator_,
904 number_of_arguments,
905 return_type,
906 dex_pc,
907 method_idx,
908 resolved_method,
909 resolved_method->GetMethodIndex());
David Brazdildee58d62016-04-07 09:54:26 +0000910 } else {
911 DCHECK_EQ(invoke_type, kInterface);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100912 ScopedObjectAccess soa(Thread::Current()); // Needed for the IMT index.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100913 invoke = new (allocator_) HInvokeInterface(allocator_,
914 number_of_arguments,
915 return_type,
916 dex_pc,
917 method_idx,
918 resolved_method,
919 ImTable::GetImtIndex(resolved_method));
David Brazdildee58d62016-04-07 09:54:26 +0000920 }
921
922 return HandleInvoke(invoke,
923 number_of_vreg_arguments,
924 args,
925 register_index,
926 is_range,
927 descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -0700928 clinit_check,
929 false /* is_unresolved */);
David Brazdildee58d62016-04-07 09:54:26 +0000930}
931
Orion Hodsonac141392017-01-13 11:53:47 +0000932bool HInstructionBuilder::BuildInvokePolymorphic(const Instruction& instruction ATTRIBUTE_UNUSED,
933 uint32_t dex_pc,
934 uint32_t method_idx,
935 uint32_t proto_idx,
936 uint32_t number_of_vreg_arguments,
937 bool is_range,
938 uint32_t* args,
939 uint32_t register_index) {
940 const char* descriptor = dex_file_->GetShorty(proto_idx);
941 DCHECK_EQ(1 + ArtMethod::NumArgRegisters(descriptor), number_of_vreg_arguments);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100942 DataType::Type return_type = DataType::FromShorty(descriptor[0]);
Orion Hodsonac141392017-01-13 11:53:47 +0000943 size_t number_of_arguments = strlen(descriptor);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100944 HInvoke* invoke = new (allocator_) HInvokePolymorphic(allocator_,
945 number_of_arguments,
946 return_type,
947 dex_pc,
948 method_idx);
Orion Hodsonac141392017-01-13 11:53:47 +0000949 return HandleInvoke(invoke,
950 number_of_vreg_arguments,
951 args,
952 register_index,
953 is_range,
954 descriptor,
955 nullptr /* clinit_check */,
956 false /* is_unresolved */);
957}
958
Igor Murashkin79d8fa72017-04-18 09:37:23 -0700959HNewInstance* HInstructionBuilder::BuildNewInstance(dex::TypeIndex type_index, uint32_t dex_pc) {
Vladimir Marko3cd50df2016-04-13 19:29:26 +0100960 ScopedObjectAccess soa(Thread::Current());
David Brazdildee58d62016-04-07 09:54:26 +0000961
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000962 HLoadClass* load_class = BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000963
David Brazdildee58d62016-04-07 09:54:26 +0000964 HInstruction* cls = load_class;
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000965 Handle<mirror::Class> klass = load_class->GetClass();
966
967 if (!IsInitialized(klass)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100968 cls = new (allocator_) HClinitCheck(load_class, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000969 AppendInstruction(cls);
970 }
971
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000972 // Only the access check entrypoint handles the finalizable class case. If we
973 // need access checks, then we haven't resolved the method and the class may
974 // again be finalizable.
975 QuickEntrypointEnum entrypoint = kQuickAllocObjectInitialized;
976 if (load_class->NeedsAccessCheck() || klass->IsFinalizable() || !klass->IsInstantiable()) {
977 entrypoint = kQuickAllocObjectWithChecks;
978 }
979
980 // Consider classes we haven't resolved as potentially finalizable.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800981 bool finalizable = (klass == nullptr) || klass->IsFinalizable();
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000982
Vladimir Markoca6fff82017-10-03 14:49:14 +0100983 HNewInstance* new_instance = new (allocator_) HNewInstance(
David Brazdildee58d62016-04-07 09:54:26 +0000984 cls,
David Brazdildee58d62016-04-07 09:54:26 +0000985 dex_pc,
986 type_index,
987 *dex_compilation_unit_->GetDexFile(),
David Brazdildee58d62016-04-07 09:54:26 +0000988 finalizable,
Igor Murashkin79d8fa72017-04-18 09:37:23 -0700989 entrypoint);
990 AppendInstruction(new_instance);
991
992 return new_instance;
993}
994
995void HInstructionBuilder::BuildConstructorFenceForAllocation(HInstruction* allocation) {
996 DCHECK(allocation != nullptr &&
George Burgess IVf2072992017-05-23 15:36:41 -0700997 (allocation->IsNewInstance() ||
998 allocation->IsNewArray())); // corresponding to "new" keyword in JLS.
Igor Murashkin79d8fa72017-04-18 09:37:23 -0700999
1000 if (allocation->IsNewInstance()) {
1001 // STRING SPECIAL HANDLING:
1002 // -------------------------------
1003 // Strings have a real HNewInstance node but they end up always having 0 uses.
1004 // All uses of a String HNewInstance are always transformed to replace their input
1005 // of the HNewInstance with an input of the invoke to StringFactory.
1006 //
1007 // Do not emit an HConstructorFence here since it can inhibit some String new-instance
1008 // optimizations (to pass checker tests that rely on those optimizations).
1009 HNewInstance* new_inst = allocation->AsNewInstance();
1010 HLoadClass* load_class = new_inst->GetLoadClass();
1011
1012 Thread* self = Thread::Current();
1013 ScopedObjectAccess soa(self);
1014 StackHandleScope<1> hs(self);
1015 Handle<mirror::Class> klass = load_class->GetClass();
1016 if (klass != nullptr && klass->IsStringClass()) {
1017 return;
1018 // Note: Do not use allocation->IsStringAlloc which requires
1019 // a valid ReferenceTypeInfo, but that doesn't get made until after reference type
1020 // propagation (and instruction builder is too early).
1021 }
1022 // (In terms of correctness, the StringFactory needs to provide its own
1023 // default initialization barrier, see below.)
1024 }
1025
1026 // JLS 17.4.5 "Happens-before Order" describes:
1027 //
1028 // The default initialization of any object happens-before any other actions (other than
1029 // default-writes) of a program.
1030 //
1031 // In our implementation the default initialization of an object to type T means
1032 // setting all of its initial data (object[0..size)) to 0, and setting the
1033 // object's class header (i.e. object.getClass() == T.class).
1034 //
1035 // In practice this fence ensures that the writes to the object header
1036 // are visible to other threads if this object escapes the current thread.
1037 // (and in theory the 0-initializing, but that happens automatically
1038 // when new memory pages are mapped in by the OS).
1039 HConstructorFence* ctor_fence =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001040 new (allocator_) HConstructorFence(allocation, allocation->GetDexPc(), allocator_);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001041 AppendInstruction(ctor_fence);
Igor Murashkin6ef45672017-08-08 13:59:55 -07001042 MaybeRecordStat(
1043 compilation_stats_,
1044 MethodCompilationStat::kConstructorFenceGeneratedNew);
David Brazdildee58d62016-04-07 09:54:26 +00001045}
1046
1047static bool IsSubClass(mirror::Class* to_test, mirror::Class* super_class)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001048 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdildee58d62016-04-07 09:54:26 +00001049 return to_test != nullptr && !to_test->IsInterface() && to_test->IsSubClass(super_class);
1050}
1051
1052bool HInstructionBuilder::IsInitialized(Handle<mirror::Class> cls) const {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001053 if (cls == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +00001054 return false;
1055 }
1056
1057 // `CanAssumeClassIsLoaded` will return true if we're JITting, or will
1058 // check whether the class is in an image for the AOT compilation.
1059 if (cls->IsInitialized() &&
1060 compiler_driver_->CanAssumeClassIsLoaded(cls.Get())) {
1061 return true;
1062 }
1063
1064 if (IsSubClass(GetOutermostCompilingClass(), cls.Get())) {
1065 return true;
1066 }
1067
1068 // TODO: We should walk over the inlined methods, but we don't pass
1069 // that information to the builder.
1070 if (IsSubClass(GetCompilingClass(), cls.Get())) {
1071 return true;
1072 }
1073
1074 return false;
1075}
1076
1077HClinitCheck* HInstructionBuilder::ProcessClinitCheckForInvoke(
1078 uint32_t dex_pc,
1079 ArtMethod* resolved_method,
David Brazdildee58d62016-04-07 09:54:26 +00001080 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001081 Handle<mirror::Class> klass = handles_->NewHandle(resolved_method->GetDeclaringClass());
David Brazdildee58d62016-04-07 09:54:26 +00001082
1083 HClinitCheck* clinit_check = nullptr;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001084 if (IsInitialized(klass)) {
David Brazdildee58d62016-04-07 09:54:26 +00001085 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001086 } else {
1087 HLoadClass* cls = BuildLoadClass(klass->GetDexTypeIndex(),
1088 klass->GetDexFile(),
1089 klass,
1090 dex_pc,
1091 /* needs_access_check */ false);
1092 if (cls != nullptr) {
1093 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
Vladimir Markoca6fff82017-10-03 14:49:14 +01001094 clinit_check = new (allocator_) HClinitCheck(cls, dex_pc);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001095 AppendInstruction(clinit_check);
1096 }
David Brazdildee58d62016-04-07 09:54:26 +00001097 }
1098 return clinit_check;
1099}
1100
1101bool HInstructionBuilder::SetupInvokeArguments(HInvoke* invoke,
1102 uint32_t number_of_vreg_arguments,
1103 uint32_t* args,
1104 uint32_t register_index,
1105 bool is_range,
1106 const char* descriptor,
1107 size_t start_index,
1108 size_t* argument_index) {
1109 uint32_t descriptor_index = 1; // Skip the return type.
1110
1111 for (size_t i = start_index;
1112 // Make sure we don't go over the expected arguments or over the number of
1113 // dex registers given. If the instruction was seen as dead by the verifier,
1114 // it hasn't been properly checked.
1115 (i < number_of_vreg_arguments) && (*argument_index < invoke->GetNumberOfArguments());
1116 i++, (*argument_index)++) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001117 DataType::Type type = DataType::FromShorty(descriptor[descriptor_index++]);
1118 bool is_wide = (type == DataType::Type::kInt64) || (type == DataType::Type::kFloat64);
David Brazdildee58d62016-04-07 09:54:26 +00001119 if (!is_range
1120 && is_wide
1121 && ((i + 1 == number_of_vreg_arguments) || (args[i] + 1 != args[i + 1]))) {
1122 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
1123 // reject any class where this is violated. However, the verifier only does these checks
1124 // on non trivially dead instructions, so we just bailout the compilation.
1125 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001126 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001127 << " because of non-sequential dex register pair in wide argument";
Igor Murashkin1e065a52017-08-09 13:20:34 -07001128 MaybeRecordStat(compilation_stats_,
1129 MethodCompilationStat::kNotCompiledMalformedOpcode);
David Brazdildee58d62016-04-07 09:54:26 +00001130 return false;
1131 }
1132 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
1133 invoke->SetArgumentAt(*argument_index, arg);
1134 if (is_wide) {
1135 i++;
1136 }
1137 }
1138
1139 if (*argument_index != invoke->GetNumberOfArguments()) {
1140 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001141 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001142 << " because of wrong number of arguments in invoke instruction";
Igor Murashkin1e065a52017-08-09 13:20:34 -07001143 MaybeRecordStat(compilation_stats_,
1144 MethodCompilationStat::kNotCompiledMalformedOpcode);
David Brazdildee58d62016-04-07 09:54:26 +00001145 return false;
1146 }
1147
1148 if (invoke->IsInvokeStaticOrDirect() &&
1149 HInvokeStaticOrDirect::NeedsCurrentMethodInput(
1150 invoke->AsInvokeStaticOrDirect()->GetMethodLoadKind())) {
1151 invoke->SetArgumentAt(*argument_index, graph_->GetCurrentMethod());
1152 (*argument_index)++;
1153 }
1154
1155 return true;
1156}
1157
1158bool HInstructionBuilder::HandleInvoke(HInvoke* invoke,
1159 uint32_t number_of_vreg_arguments,
1160 uint32_t* args,
1161 uint32_t register_index,
1162 bool is_range,
1163 const char* descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -07001164 HClinitCheck* clinit_check,
1165 bool is_unresolved) {
David Brazdildee58d62016-04-07 09:54:26 +00001166 DCHECK(!invoke->IsInvokeStaticOrDirect() || !invoke->AsInvokeStaticOrDirect()->IsStringInit());
1167
1168 size_t start_index = 0;
1169 size_t argument_index = 0;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001170 if (invoke->GetInvokeType() != InvokeType::kStatic) { // Instance call.
Aart Bik296fbb42016-06-07 13:49:12 -07001171 uint32_t obj_reg = is_range ? register_index : args[0];
1172 HInstruction* arg = is_unresolved
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001173 ? LoadLocal(obj_reg, DataType::Type::kReference)
Aart Bik296fbb42016-06-07 13:49:12 -07001174 : LoadNullCheckedLocal(obj_reg, invoke->GetDexPc());
David Brazdilc120bbe2016-04-22 16:57:00 +01001175 invoke->SetArgumentAt(0, arg);
David Brazdildee58d62016-04-07 09:54:26 +00001176 start_index = 1;
1177 argument_index = 1;
1178 }
1179
1180 if (!SetupInvokeArguments(invoke,
1181 number_of_vreg_arguments,
1182 args,
1183 register_index,
1184 is_range,
1185 descriptor,
1186 start_index,
1187 &argument_index)) {
1188 return false;
1189 }
1190
1191 if (clinit_check != nullptr) {
1192 // Add the class initialization check as last input of `invoke`.
1193 DCHECK(invoke->IsInvokeStaticOrDirect());
1194 DCHECK(invoke->AsInvokeStaticOrDirect()->GetClinitCheckRequirement()
1195 == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit);
1196 invoke->SetArgumentAt(argument_index, clinit_check);
1197 argument_index++;
1198 }
1199
1200 AppendInstruction(invoke);
1201 latest_result_ = invoke;
1202
1203 return true;
1204}
1205
1206bool HInstructionBuilder::HandleStringInit(HInvoke* invoke,
1207 uint32_t number_of_vreg_arguments,
1208 uint32_t* args,
1209 uint32_t register_index,
1210 bool is_range,
1211 const char* descriptor) {
1212 DCHECK(invoke->IsInvokeStaticOrDirect());
1213 DCHECK(invoke->AsInvokeStaticOrDirect()->IsStringInit());
1214
1215 size_t start_index = 1;
1216 size_t argument_index = 0;
1217 if (!SetupInvokeArguments(invoke,
1218 number_of_vreg_arguments,
1219 args,
1220 register_index,
1221 is_range,
1222 descriptor,
1223 start_index,
1224 &argument_index)) {
1225 return false;
1226 }
1227
1228 AppendInstruction(invoke);
1229
1230 // This is a StringFactory call, not an actual String constructor. Its result
1231 // replaces the empty String pre-allocated by NewInstance.
1232 uint32_t orig_this_reg = is_range ? register_index : args[0];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001233 HInstruction* arg_this = LoadLocal(orig_this_reg, DataType::Type::kReference);
David Brazdildee58d62016-04-07 09:54:26 +00001234
1235 // Replacing the NewInstance might render it redundant. Keep a list of these
1236 // to be visited once it is clear whether it is has remaining uses.
1237 if (arg_this->IsNewInstance()) {
1238 ssa_builder_->AddUninitializedString(arg_this->AsNewInstance());
1239 } else {
1240 DCHECK(arg_this->IsPhi());
1241 // NewInstance is not the direct input of the StringFactory call. It might
1242 // be redundant but optimizing this case is not worth the effort.
1243 }
1244
1245 // Walk over all vregs and replace any occurrence of `arg_this` with `invoke`.
1246 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
1247 if ((*current_locals_)[vreg] == arg_this) {
1248 (*current_locals_)[vreg] = invoke;
1249 }
1250 }
1251
1252 return true;
1253}
1254
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001255static DataType::Type GetFieldAccessType(const DexFile& dex_file, uint16_t field_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001256 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
1257 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001258 return DataType::FromShorty(type[0]);
David Brazdildee58d62016-04-07 09:54:26 +00001259}
1260
1261bool HInstructionBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
1262 uint32_t dex_pc,
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001263 bool is_put,
1264 size_t quicken_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001265 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1266 uint32_t obj_reg = instruction.VRegB_22c();
1267 uint16_t field_index;
1268 if (instruction.IsQuickened()) {
1269 if (!CanDecodeQuickenedInfo()) {
1270 return false;
1271 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001272 field_index = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00001273 } else {
1274 field_index = instruction.VRegC_22c();
1275 }
1276
1277 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001278 ArtField* resolved_field = ResolveField(field_index, /* is_static */ false, is_put);
David Brazdildee58d62016-04-07 09:54:26 +00001279
Aart Bik14154132016-06-02 17:53:58 -07001280 // Generate an explicit null check on the reference, unless the field access
1281 // is unresolved. In that case, we rely on the runtime to perform various
1282 // checks first, followed by a null check.
1283 HInstruction* object = (resolved_field == nullptr)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001284 ? LoadLocal(obj_reg, DataType::Type::kReference)
Aart Bik14154132016-06-02 17:53:58 -07001285 : LoadNullCheckedLocal(obj_reg, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001286
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001287 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001288 if (is_put) {
1289 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1290 HInstruction* field_set = nullptr;
1291 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001292 MaybeRecordStat(compilation_stats_,
1293 MethodCompilationStat::kUnresolvedField);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001294 field_set = new (allocator_) HUnresolvedInstanceFieldSet(object,
1295 value,
1296 field_type,
1297 field_index,
1298 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001299 } else {
1300 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Vladimir Markoca6fff82017-10-03 14:49:14 +01001301 field_set = new (allocator_) HInstanceFieldSet(object,
1302 value,
1303 resolved_field,
1304 field_type,
1305 resolved_field->GetOffset(),
1306 resolved_field->IsVolatile(),
1307 field_index,
1308 class_def_index,
1309 *dex_file_,
1310 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001311 }
1312 AppendInstruction(field_set);
1313 } else {
1314 HInstruction* field_get = nullptr;
1315 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001316 MaybeRecordStat(compilation_stats_,
1317 MethodCompilationStat::kUnresolvedField);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001318 field_get = new (allocator_) HUnresolvedInstanceFieldGet(object,
1319 field_type,
1320 field_index,
1321 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001322 } else {
1323 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Vladimir Markoca6fff82017-10-03 14:49:14 +01001324 field_get = new (allocator_) HInstanceFieldGet(object,
1325 resolved_field,
1326 field_type,
1327 resolved_field->GetOffset(),
1328 resolved_field->IsVolatile(),
1329 field_index,
1330 class_def_index,
1331 *dex_file_,
1332 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001333 }
1334 AppendInstruction(field_get);
1335 UpdateLocal(source_or_dest_reg, field_get);
1336 }
1337
1338 return true;
1339}
1340
1341static mirror::Class* GetClassFrom(CompilerDriver* driver,
1342 const DexCompilationUnit& compilation_unit) {
1343 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001344 Handle<mirror::ClassLoader> class_loader = compilation_unit.GetClassLoader();
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001345 Handle<mirror::DexCache> dex_cache = compilation_unit.GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00001346
1347 return driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, &compilation_unit);
1348}
1349
1350mirror::Class* HInstructionBuilder::GetOutermostCompilingClass() const {
1351 return GetClassFrom(compiler_driver_, *outer_compilation_unit_);
1352}
1353
1354mirror::Class* HInstructionBuilder::GetCompilingClass() const {
1355 return GetClassFrom(compiler_driver_, *dex_compilation_unit_);
1356}
1357
Andreas Gampea5b09a62016-11-17 15:21:22 -08001358bool HInstructionBuilder::IsOutermostCompilingClass(dex::TypeIndex type_index) const {
David Brazdildee58d62016-04-07 09:54:26 +00001359 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001360 StackHandleScope<2> hs(soa.Self());
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001361 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001362 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
David Brazdildee58d62016-04-07 09:54:26 +00001363 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
1364 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
1365 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
1366
1367 // GetOutermostCompilingClass returns null when the class is unresolved
1368 // (e.g. if it derives from an unresolved class). This is bogus knowing that
1369 // we are compiling it.
1370 // When this happens we cannot establish a direct relation between the current
1371 // class and the outer class, so we return false.
1372 // (Note that this is only used for optimizing invokes and field accesses)
Andreas Gampefa4333d2017-02-14 11:10:34 -08001373 return (cls != nullptr) && (outer_class.Get() == cls.Get());
David Brazdildee58d62016-04-07 09:54:26 +00001374}
1375
1376void HInstructionBuilder::BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001377 uint32_t dex_pc,
1378 bool is_put,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001379 DataType::Type field_type) {
David Brazdildee58d62016-04-07 09:54:26 +00001380 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1381 uint16_t field_index = instruction.VRegB_21c();
1382
1383 if (is_put) {
1384 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1385 AppendInstruction(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001386 new (allocator_) HUnresolvedStaticFieldSet(value, field_type, field_index, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001387 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001388 AppendInstruction(new (allocator_) HUnresolvedStaticFieldGet(field_type, field_index, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001389 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1390 }
1391}
1392
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001393ArtField* HInstructionBuilder::ResolveField(uint16_t field_idx, bool is_static, bool is_put) {
1394 ScopedObjectAccess soa(Thread::Current());
1395 StackHandleScope<2> hs(soa.Self());
1396
1397 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001398 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001399 Handle<mirror::Class> compiling_class(hs.NewHandle(GetCompilingClass()));
1400
1401 ArtField* resolved_field = class_linker->ResolveField(*dex_compilation_unit_->GetDexFile(),
1402 field_idx,
1403 dex_compilation_unit_->GetDexCache(),
1404 class_loader,
1405 is_static);
1406
1407 if (UNLIKELY(resolved_field == nullptr)) {
1408 // Clean up any exception left by type resolution.
1409 soa.Self()->ClearException();
1410 return nullptr;
1411 }
1412
1413 // Check static/instance. The class linker has a fast path for looking into the dex cache
1414 // and does not check static/instance if it hits it.
1415 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
1416 return nullptr;
1417 }
1418
1419 // Check access.
Andreas Gampefa4333d2017-02-14 11:10:34 -08001420 if (compiling_class == nullptr) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001421 if (!resolved_field->IsPublic()) {
1422 return nullptr;
1423 }
1424 } else if (!compiling_class->CanAccessResolvedField(resolved_field->GetDeclaringClass(),
1425 resolved_field,
1426 dex_compilation_unit_->GetDexCache().Get(),
1427 field_idx)) {
1428 return nullptr;
1429 }
1430
1431 if (is_put &&
1432 resolved_field->IsFinal() &&
1433 (compiling_class.Get() != resolved_field->GetDeclaringClass())) {
1434 // Final fields can only be updated within their own class.
1435 // TODO: Only allow it in constructors. b/34966607.
1436 return nullptr;
1437 }
1438
1439 return resolved_field;
1440}
1441
David Brazdildee58d62016-04-07 09:54:26 +00001442bool HInstructionBuilder::BuildStaticFieldAccess(const Instruction& instruction,
1443 uint32_t dex_pc,
1444 bool is_put) {
1445 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1446 uint16_t field_index = instruction.VRegB_21c();
1447
1448 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001449 ArtField* resolved_field = ResolveField(field_index, /* is_static */ true, is_put);
David Brazdildee58d62016-04-07 09:54:26 +00001450
1451 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001452 MaybeRecordStat(compilation_stats_,
1453 MethodCompilationStat::kUnresolvedField);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001454 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001455 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
1456 return true;
1457 }
1458
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001459 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001460
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001461 Handle<mirror::Class> klass = handles_->NewHandle(resolved_field->GetDeclaringClass());
1462 HLoadClass* constant = BuildLoadClass(klass->GetDexTypeIndex(),
1463 klass->GetDexFile(),
1464 klass,
1465 dex_pc,
1466 /* needs_access_check */ false);
1467
1468 if (constant == nullptr) {
1469 // The class cannot be referenced from this compiled code. Generate
1470 // an unresolved access.
Igor Murashkin1e065a52017-08-09 13:20:34 -07001471 MaybeRecordStat(compilation_stats_,
1472 MethodCompilationStat::kUnresolvedFieldNotAFastAccess);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001473 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
1474 return true;
David Brazdildee58d62016-04-07 09:54:26 +00001475 }
1476
David Brazdildee58d62016-04-07 09:54:26 +00001477 HInstruction* cls = constant;
David Brazdildee58d62016-04-07 09:54:26 +00001478 if (!IsInitialized(klass)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001479 cls = new (allocator_) HClinitCheck(constant, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001480 AppendInstruction(cls);
1481 }
1482
1483 uint16_t class_def_index = klass->GetDexClassDefIndex();
1484 if (is_put) {
1485 // We need to keep the class alive before loading the value.
1486 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1487 DCHECK_EQ(HPhi::ToPhiType(value->GetType()), HPhi::ToPhiType(field_type));
Vladimir Markoca6fff82017-10-03 14:49:14 +01001488 AppendInstruction(new (allocator_) HStaticFieldSet(cls,
1489 value,
1490 resolved_field,
1491 field_type,
1492 resolved_field->GetOffset(),
1493 resolved_field->IsVolatile(),
1494 field_index,
1495 class_def_index,
1496 *dex_file_,
1497 dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001498 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001499 AppendInstruction(new (allocator_) HStaticFieldGet(cls,
1500 resolved_field,
1501 field_type,
1502 resolved_field->GetOffset(),
1503 resolved_field->IsVolatile(),
1504 field_index,
1505 class_def_index,
1506 *dex_file_,
1507 dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001508 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1509 }
1510 return true;
1511}
1512
1513void HInstructionBuilder::BuildCheckedDivRem(uint16_t out_vreg,
Vladimir Markoca6fff82017-10-03 14:49:14 +01001514 uint16_t first_vreg,
1515 int64_t second_vreg_or_constant,
1516 uint32_t dex_pc,
1517 DataType::Type type,
1518 bool second_is_constant,
1519 bool isDiv) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001520 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
David Brazdildee58d62016-04-07 09:54:26 +00001521
1522 HInstruction* first = LoadLocal(first_vreg, type);
1523 HInstruction* second = nullptr;
1524 if (second_is_constant) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001525 if (type == DataType::Type::kInt32) {
David Brazdildee58d62016-04-07 09:54:26 +00001526 second = graph_->GetIntConstant(second_vreg_or_constant, dex_pc);
1527 } else {
1528 second = graph_->GetLongConstant(second_vreg_or_constant, dex_pc);
1529 }
1530 } else {
1531 second = LoadLocal(second_vreg_or_constant, type);
1532 }
1533
1534 if (!second_is_constant
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001535 || (type == DataType::Type::kInt32 && second->AsIntConstant()->GetValue() == 0)
1536 || (type == DataType::Type::kInt64 && second->AsLongConstant()->GetValue() == 0)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001537 second = new (allocator_) HDivZeroCheck(second, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001538 AppendInstruction(second);
1539 }
1540
1541 if (isDiv) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001542 AppendInstruction(new (allocator_) HDiv(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001543 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001544 AppendInstruction(new (allocator_) HRem(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001545 }
1546 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
1547}
1548
1549void HInstructionBuilder::BuildArrayAccess(const Instruction& instruction,
1550 uint32_t dex_pc,
1551 bool is_put,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001552 DataType::Type anticipated_type) {
David Brazdildee58d62016-04-07 09:54:26 +00001553 uint8_t source_or_dest_reg = instruction.VRegA_23x();
1554 uint8_t array_reg = instruction.VRegB_23x();
1555 uint8_t index_reg = instruction.VRegC_23x();
1556
David Brazdilc120bbe2016-04-22 16:57:00 +01001557 HInstruction* object = LoadNullCheckedLocal(array_reg, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001558 HInstruction* length = new (allocator_) HArrayLength(object, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001559 AppendInstruction(length);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001560 HInstruction* index = LoadLocal(index_reg, DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001561 index = new (allocator_) HBoundsCheck(index, length, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001562 AppendInstruction(index);
1563 if (is_put) {
1564 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
1565 // TODO: Insert a type check node if the type is Object.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001566 HArraySet* aset = new (allocator_) HArraySet(object, index, value, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001567 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1568 AppendInstruction(aset);
1569 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001570 HArrayGet* aget = new (allocator_) HArrayGet(object, index, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001571 ssa_builder_->MaybeAddAmbiguousArrayGet(aget);
1572 AppendInstruction(aget);
1573 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1574 }
1575 graph_->SetHasBoundsChecks(true);
1576}
1577
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001578HNewArray* HInstructionBuilder::BuildFilledNewArray(uint32_t dex_pc,
1579 dex::TypeIndex type_index,
1580 uint32_t number_of_vreg_arguments,
1581 bool is_range,
1582 uint32_t* args,
1583 uint32_t register_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001584 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments, dex_pc);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001585 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001586 HNewArray* const object = new (allocator_) HNewArray(cls, length, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001587 AppendInstruction(object);
1588
1589 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1590 DCHECK_EQ(descriptor[0], '[') << descriptor;
1591 char primitive = descriptor[1];
1592 DCHECK(primitive == 'I'
1593 || primitive == 'L'
1594 || primitive == '[') << descriptor;
1595 bool is_reference_array = (primitive == 'L') || (primitive == '[');
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001596 DataType::Type type = is_reference_array ? DataType::Type::kReference : DataType::Type::kInt32;
David Brazdildee58d62016-04-07 09:54:26 +00001597
1598 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
1599 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
1600 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001601 HArraySet* aset = new (allocator_) HArraySet(object, index, value, type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001602 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1603 AppendInstruction(aset);
1604 }
1605 latest_result_ = object;
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001606
1607 return object;
David Brazdildee58d62016-04-07 09:54:26 +00001608}
1609
1610template <typename T>
1611void HInstructionBuilder::BuildFillArrayData(HInstruction* object,
1612 const T* data,
1613 uint32_t element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001614 DataType::Type anticipated_type,
David Brazdildee58d62016-04-07 09:54:26 +00001615 uint32_t dex_pc) {
1616 for (uint32_t i = 0; i < element_count; ++i) {
1617 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1618 HInstruction* value = graph_->GetIntConstant(data[i], dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001619 HArraySet* aset = new (allocator_) HArraySet(object, index, value, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001620 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1621 AppendInstruction(aset);
1622 }
1623}
1624
1625void HInstructionBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
David Brazdilc120bbe2016-04-22 16:57:00 +01001626 HInstruction* array = LoadNullCheckedLocal(instruction.VRegA_31t(), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001627
1628 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
1629 const Instruction::ArrayDataPayload* payload =
1630 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_item_.insns_ + payload_offset);
1631 const uint8_t* data = payload->data;
1632 uint32_t element_count = payload->element_count;
1633
Vladimir Markoc69fba22016-09-06 16:49:15 +01001634 if (element_count == 0u) {
1635 // For empty payload we emit only the null check above.
1636 return;
1637 }
1638
Vladimir Markoca6fff82017-10-03 14:49:14 +01001639 HInstruction* length = new (allocator_) HArrayLength(array, dex_pc);
Vladimir Markoc69fba22016-09-06 16:49:15 +01001640 AppendInstruction(length);
1641
David Brazdildee58d62016-04-07 09:54:26 +00001642 // Implementation of this DEX instruction seems to be that the bounds check is
1643 // done before doing any stores.
1644 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001645 AppendInstruction(new (allocator_) HBoundsCheck(last_index, length, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001646
1647 switch (payload->element_width) {
1648 case 1:
David Brazdilc120bbe2016-04-22 16:57:00 +01001649 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001650 reinterpret_cast<const int8_t*>(data),
1651 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001652 DataType::Type::kInt8,
David Brazdildee58d62016-04-07 09:54:26 +00001653 dex_pc);
1654 break;
1655 case 2:
David Brazdilc120bbe2016-04-22 16:57:00 +01001656 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001657 reinterpret_cast<const int16_t*>(data),
1658 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001659 DataType::Type::kInt16,
David Brazdildee58d62016-04-07 09:54:26 +00001660 dex_pc);
1661 break;
1662 case 4:
David Brazdilc120bbe2016-04-22 16:57:00 +01001663 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001664 reinterpret_cast<const int32_t*>(data),
1665 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001666 DataType::Type::kInt32,
David Brazdildee58d62016-04-07 09:54:26 +00001667 dex_pc);
1668 break;
1669 case 8:
David Brazdilc120bbe2016-04-22 16:57:00 +01001670 BuildFillWideArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001671 reinterpret_cast<const int64_t*>(data),
1672 element_count,
1673 dex_pc);
1674 break;
1675 default:
1676 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1677 }
1678 graph_->SetHasBoundsChecks(true);
1679}
1680
1681void HInstructionBuilder::BuildFillWideArrayData(HInstruction* object,
1682 const int64_t* data,
1683 uint32_t element_count,
1684 uint32_t dex_pc) {
1685 for (uint32_t i = 0; i < element_count; ++i) {
1686 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1687 HInstruction* value = graph_->GetLongConstant(data[i], dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001688 HArraySet* aset =
1689 new (allocator_) HArraySet(object, index, value, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001690 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1691 AppendInstruction(aset);
1692 }
1693}
1694
1695static TypeCheckKind ComputeTypeCheckKind(Handle<mirror::Class> cls)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001696 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001697 if (cls == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +00001698 return TypeCheckKind::kUnresolvedCheck;
1699 } else if (cls->IsInterface()) {
1700 return TypeCheckKind::kInterfaceCheck;
1701 } else if (cls->IsArrayClass()) {
1702 if (cls->GetComponentType()->IsObjectClass()) {
1703 return TypeCheckKind::kArrayObjectCheck;
1704 } else if (cls->CannotBeAssignedFromOtherTypes()) {
1705 return TypeCheckKind::kExactCheck;
1706 } else {
1707 return TypeCheckKind::kArrayCheck;
1708 }
1709 } else if (cls->IsFinal()) {
1710 return TypeCheckKind::kExactCheck;
1711 } else if (cls->IsAbstract()) {
1712 return TypeCheckKind::kAbstractClassCheck;
1713 } else {
1714 return TypeCheckKind::kClassHierarchyCheck;
1715 }
1716}
1717
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001718HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index, uint32_t dex_pc) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001719 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001720 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001721 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00001722 Handle<mirror::Class> klass = handles_->NewHandle(compiler_driver_->ResolveClass(
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001723 soa, dex_compilation_unit_->GetDexCache(), class_loader, type_index, dex_compilation_unit_));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00001724
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001725 bool needs_access_check = true;
Andreas Gampefa4333d2017-02-14 11:10:34 -08001726 if (klass != nullptr) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001727 if (klass->IsPublic()) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001728 needs_access_check = false;
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001729 } else {
1730 mirror::Class* compiling_class = GetCompilingClass();
1731 if (compiling_class != nullptr && compiling_class->CanAccess(klass.Get())) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001732 needs_access_check = false;
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001733 }
1734 }
1735 }
1736
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001737 return BuildLoadClass(type_index, dex_file, klass, dex_pc, needs_access_check);
1738}
1739
1740HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index,
1741 const DexFile& dex_file,
1742 Handle<mirror::Class> klass,
1743 uint32_t dex_pc,
1744 bool needs_access_check) {
1745 // Try to find a reference in the compiling dex file.
1746 const DexFile* actual_dex_file = &dex_file;
1747 if (!IsSameDexFile(dex_file, *dex_compilation_unit_->GetDexFile())) {
1748 dex::TypeIndex local_type_index =
1749 klass->FindTypeIndexInOtherDexFile(*dex_compilation_unit_->GetDexFile());
1750 if (local_type_index.IsValid()) {
1751 type_index = local_type_index;
1752 actual_dex_file = dex_compilation_unit_->GetDexFile();
1753 }
1754 }
1755
1756 // Note: `klass` must be from `handles_`.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001757 HLoadClass* load_class = new (allocator_) HLoadClass(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001758 graph_->GetCurrentMethod(),
1759 type_index,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001760 *actual_dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001761 klass,
Andreas Gampefa4333d2017-02-14 11:10:34 -08001762 klass != nullptr && (klass.Get() == GetOutermostCompilingClass()),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001763 dex_pc,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001764 needs_access_check);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001765
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00001766 HLoadClass::LoadKind load_kind = HSharpening::ComputeLoadClassKind(load_class,
1767 code_generator_,
1768 compiler_driver_,
1769 *dex_compilation_unit_);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001770
1771 if (load_kind == HLoadClass::LoadKind::kInvalid) {
1772 // We actually cannot reference this class, we're forced to bail.
1773 return nullptr;
1774 }
1775 // Append the instruction first, as setting the load kind affects the inputs.
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001776 AppendInstruction(load_class);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001777 load_class->SetLoadKind(load_kind);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001778 return load_class;
1779}
1780
David Brazdildee58d62016-04-07 09:54:26 +00001781void HInstructionBuilder::BuildTypeCheck(const Instruction& instruction,
1782 uint8_t destination,
1783 uint8_t reference,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001784 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +00001785 uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001786 HInstruction* object = LoadLocal(reference, DataType::Type::kReference);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001787 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001788
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001789 ScopedObjectAccess soa(Thread::Current());
1790 TypeCheckKind check_kind = ComputeTypeCheckKind(cls->GetClass());
David Brazdildee58d62016-04-07 09:54:26 +00001791 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001792 AppendInstruction(new (allocator_) HInstanceOf(object, cls, check_kind, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001793 UpdateLocal(destination, current_block_->GetLastInstruction());
1794 } else {
1795 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
1796 // We emit a CheckCast followed by a BoundType. CheckCast is a statement
1797 // which may throw. If it succeeds BoundType sets the new type of `object`
1798 // for all subsequent uses.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001799 AppendInstruction(new (allocator_) HCheckCast(object, cls, check_kind, dex_pc));
1800 AppendInstruction(new (allocator_) HBoundType(object, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001801 UpdateLocal(reference, current_block_->GetLastInstruction());
1802 }
1803}
1804
Vladimir Marko0b66d612017-03-13 14:50:04 +00001805bool HInstructionBuilder::NeedsAccessCheck(dex::TypeIndex type_index, bool* finalizable) const {
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001806 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
1807 LookupReferrerClass(), LookupResolvedType(type_index, *dex_compilation_unit_), finalizable);
David Brazdildee58d62016-04-07 09:54:26 +00001808}
1809
1810bool HInstructionBuilder::CanDecodeQuickenedInfo() const {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001811 return !quicken_info_.IsNull();
David Brazdildee58d62016-04-07 09:54:26 +00001812}
1813
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001814uint16_t HInstructionBuilder::LookupQuickenedInfo(uint32_t quicken_index) {
1815 DCHECK(CanDecodeQuickenedInfo());
1816 return quicken_info_.GetData(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00001817}
1818
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001819bool HInstructionBuilder::ProcessDexInstruction(const Instruction& instruction,
1820 uint32_t dex_pc,
1821 size_t quicken_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001822 switch (instruction.Opcode()) {
1823 case Instruction::CONST_4: {
1824 int32_t register_index = instruction.VRegA();
1825 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n(), dex_pc);
1826 UpdateLocal(register_index, constant);
1827 break;
1828 }
1829
1830 case Instruction::CONST_16: {
1831 int32_t register_index = instruction.VRegA();
1832 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s(), dex_pc);
1833 UpdateLocal(register_index, constant);
1834 break;
1835 }
1836
1837 case Instruction::CONST: {
1838 int32_t register_index = instruction.VRegA();
1839 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i(), dex_pc);
1840 UpdateLocal(register_index, constant);
1841 break;
1842 }
1843
1844 case Instruction::CONST_HIGH16: {
1845 int32_t register_index = instruction.VRegA();
1846 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16, dex_pc);
1847 UpdateLocal(register_index, constant);
1848 break;
1849 }
1850
1851 case Instruction::CONST_WIDE_16: {
1852 int32_t register_index = instruction.VRegA();
1853 // Get 16 bits of constant value, sign extended to 64 bits.
1854 int64_t value = instruction.VRegB_21s();
1855 value <<= 48;
1856 value >>= 48;
1857 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1858 UpdateLocal(register_index, constant);
1859 break;
1860 }
1861
1862 case Instruction::CONST_WIDE_32: {
1863 int32_t register_index = instruction.VRegA();
1864 // Get 32 bits of constant value, sign extended to 64 bits.
1865 int64_t value = instruction.VRegB_31i();
1866 value <<= 32;
1867 value >>= 32;
1868 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1869 UpdateLocal(register_index, constant);
1870 break;
1871 }
1872
1873 case Instruction::CONST_WIDE: {
1874 int32_t register_index = instruction.VRegA();
1875 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l(), dex_pc);
1876 UpdateLocal(register_index, constant);
1877 break;
1878 }
1879
1880 case Instruction::CONST_WIDE_HIGH16: {
1881 int32_t register_index = instruction.VRegA();
1882 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
1883 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1884 UpdateLocal(register_index, constant);
1885 break;
1886 }
1887
1888 // Note that the SSA building will refine the types.
1889 case Instruction::MOVE:
1890 case Instruction::MOVE_FROM16:
1891 case Instruction::MOVE_16: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001892 HInstruction* value = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +00001893 UpdateLocal(instruction.VRegA(), value);
1894 break;
1895 }
1896
1897 // Note that the SSA building will refine the types.
1898 case Instruction::MOVE_WIDE:
1899 case Instruction::MOVE_WIDE_FROM16:
1900 case Instruction::MOVE_WIDE_16: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001901 HInstruction* value = LoadLocal(instruction.VRegB(), DataType::Type::kInt64);
David Brazdildee58d62016-04-07 09:54:26 +00001902 UpdateLocal(instruction.VRegA(), value);
1903 break;
1904 }
1905
1906 case Instruction::MOVE_OBJECT:
1907 case Instruction::MOVE_OBJECT_16:
1908 case Instruction::MOVE_OBJECT_FROM16: {
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01001909 // The verifier has no notion of a null type, so a move-object of constant 0
1910 // will lead to the same constant 0 in the destination register. To mimic
1911 // this behavior, we just pretend we haven't seen a type change (int to reference)
1912 // for the 0 constant and phis. We rely on our type propagation to eventually get the
1913 // types correct.
1914 uint32_t reg_number = instruction.VRegB();
1915 HInstruction* value = (*current_locals_)[reg_number];
1916 if (value->IsIntConstant()) {
1917 DCHECK_EQ(value->AsIntConstant()->GetValue(), 0);
1918 } else if (value->IsPhi()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001919 DCHECK(value->GetType() == DataType::Type::kInt32 ||
1920 value->GetType() == DataType::Type::kReference);
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01001921 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001922 value = LoadLocal(reg_number, DataType::Type::kReference);
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01001923 }
David Brazdildee58d62016-04-07 09:54:26 +00001924 UpdateLocal(instruction.VRegA(), value);
1925 break;
1926 }
1927
1928 case Instruction::RETURN_VOID_NO_BARRIER:
1929 case Instruction::RETURN_VOID: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001930 BuildReturn(instruction, DataType::Type::kVoid, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001931 break;
1932 }
1933
1934#define IF_XX(comparison, cond) \
1935 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
1936 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
1937
1938 IF_XX(HEqual, EQ);
1939 IF_XX(HNotEqual, NE);
1940 IF_XX(HLessThan, LT);
1941 IF_XX(HLessThanOrEqual, LE);
1942 IF_XX(HGreaterThan, GT);
1943 IF_XX(HGreaterThanOrEqual, GE);
1944
1945 case Instruction::GOTO:
1946 case Instruction::GOTO_16:
1947 case Instruction::GOTO_32: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001948 AppendInstruction(new (allocator_) HGoto(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001949 current_block_ = nullptr;
1950 break;
1951 }
1952
1953 case Instruction::RETURN: {
1954 BuildReturn(instruction, return_type_, dex_pc);
1955 break;
1956 }
1957
1958 case Instruction::RETURN_OBJECT: {
1959 BuildReturn(instruction, return_type_, dex_pc);
1960 break;
1961 }
1962
1963 case Instruction::RETURN_WIDE: {
1964 BuildReturn(instruction, return_type_, dex_pc);
1965 break;
1966 }
1967
1968 case Instruction::INVOKE_DIRECT:
1969 case Instruction::INVOKE_INTERFACE:
1970 case Instruction::INVOKE_STATIC:
1971 case Instruction::INVOKE_SUPER:
1972 case Instruction::INVOKE_VIRTUAL:
1973 case Instruction::INVOKE_VIRTUAL_QUICK: {
1974 uint16_t method_idx;
1975 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_QUICK) {
1976 if (!CanDecodeQuickenedInfo()) {
1977 return false;
1978 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001979 method_idx = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00001980 } else {
1981 method_idx = instruction.VRegB_35c();
1982 }
1983 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
1984 uint32_t args[5];
1985 instruction.GetVarArgs(args);
1986 if (!BuildInvoke(instruction, dex_pc, method_idx,
1987 number_of_vreg_arguments, false, args, -1)) {
1988 return false;
1989 }
1990 break;
1991 }
1992
1993 case Instruction::INVOKE_DIRECT_RANGE:
1994 case Instruction::INVOKE_INTERFACE_RANGE:
1995 case Instruction::INVOKE_STATIC_RANGE:
1996 case Instruction::INVOKE_SUPER_RANGE:
1997 case Instruction::INVOKE_VIRTUAL_RANGE:
1998 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
1999 uint16_t method_idx;
2000 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK) {
2001 if (!CanDecodeQuickenedInfo()) {
2002 return false;
2003 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002004 method_idx = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00002005 } else {
2006 method_idx = instruction.VRegB_3rc();
2007 }
2008 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
2009 uint32_t register_index = instruction.VRegC();
2010 if (!BuildInvoke(instruction, dex_pc, method_idx,
2011 number_of_vreg_arguments, true, nullptr, register_index)) {
2012 return false;
2013 }
2014 break;
2015 }
2016
Orion Hodsonac141392017-01-13 11:53:47 +00002017 case Instruction::INVOKE_POLYMORPHIC: {
2018 uint16_t method_idx = instruction.VRegB_45cc();
2019 uint16_t proto_idx = instruction.VRegH_45cc();
2020 uint32_t number_of_vreg_arguments = instruction.VRegA_45cc();
2021 uint32_t args[5];
2022 instruction.GetVarArgs(args);
2023 return BuildInvokePolymorphic(instruction,
2024 dex_pc,
2025 method_idx,
2026 proto_idx,
2027 number_of_vreg_arguments,
2028 false,
2029 args,
2030 -1);
2031 }
2032
2033 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
2034 uint16_t method_idx = instruction.VRegB_4rcc();
2035 uint16_t proto_idx = instruction.VRegH_4rcc();
2036 uint32_t number_of_vreg_arguments = instruction.VRegA_4rcc();
2037 uint32_t register_index = instruction.VRegC_4rcc();
2038 return BuildInvokePolymorphic(instruction,
2039 dex_pc,
2040 method_idx,
2041 proto_idx,
2042 number_of_vreg_arguments,
2043 true,
2044 nullptr,
2045 register_index);
2046 }
2047
David Brazdildee58d62016-04-07 09:54:26 +00002048 case Instruction::NEG_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002049 Unop_12x<HNeg>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002050 break;
2051 }
2052
2053 case Instruction::NEG_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002054 Unop_12x<HNeg>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002055 break;
2056 }
2057
2058 case Instruction::NEG_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002059 Unop_12x<HNeg>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002060 break;
2061 }
2062
2063 case Instruction::NEG_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002064 Unop_12x<HNeg>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002065 break;
2066 }
2067
2068 case Instruction::NOT_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002069 Unop_12x<HNot>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002070 break;
2071 }
2072
2073 case Instruction::NOT_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002074 Unop_12x<HNot>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002075 break;
2076 }
2077
2078 case Instruction::INT_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002079 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002080 break;
2081 }
2082
2083 case Instruction::INT_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002084 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002085 break;
2086 }
2087
2088 case Instruction::INT_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002089 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002090 break;
2091 }
2092
2093 case Instruction::LONG_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002094 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002095 break;
2096 }
2097
2098 case Instruction::LONG_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002099 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002100 break;
2101 }
2102
2103 case Instruction::LONG_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002104 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002105 break;
2106 }
2107
2108 case Instruction::FLOAT_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002109 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002110 break;
2111 }
2112
2113 case Instruction::FLOAT_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002114 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002115 break;
2116 }
2117
2118 case Instruction::FLOAT_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002119 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002120 break;
2121 }
2122
2123 case Instruction::DOUBLE_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002124 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002125 break;
2126 }
2127
2128 case Instruction::DOUBLE_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002129 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002130 break;
2131 }
2132
2133 case Instruction::DOUBLE_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002134 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002135 break;
2136 }
2137
2138 case Instruction::INT_TO_BYTE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002139 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt8, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002140 break;
2141 }
2142
2143 case Instruction::INT_TO_SHORT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002144 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt16, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002145 break;
2146 }
2147
2148 case Instruction::INT_TO_CHAR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002149 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kUint16, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002150 break;
2151 }
2152
2153 case Instruction::ADD_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002154 Binop_23x<HAdd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002155 break;
2156 }
2157
2158 case Instruction::ADD_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002159 Binop_23x<HAdd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002160 break;
2161 }
2162
2163 case Instruction::ADD_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002164 Binop_23x<HAdd>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002165 break;
2166 }
2167
2168 case Instruction::ADD_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002169 Binop_23x<HAdd>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002170 break;
2171 }
2172
2173 case Instruction::SUB_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002174 Binop_23x<HSub>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002175 break;
2176 }
2177
2178 case Instruction::SUB_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002179 Binop_23x<HSub>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002180 break;
2181 }
2182
2183 case Instruction::SUB_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002184 Binop_23x<HSub>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002185 break;
2186 }
2187
2188 case Instruction::SUB_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002189 Binop_23x<HSub>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002190 break;
2191 }
2192
2193 case Instruction::ADD_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002194 Binop_12x<HAdd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002195 break;
2196 }
2197
2198 case Instruction::MUL_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002199 Binop_23x<HMul>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002200 break;
2201 }
2202
2203 case Instruction::MUL_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002204 Binop_23x<HMul>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002205 break;
2206 }
2207
2208 case Instruction::MUL_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002209 Binop_23x<HMul>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002210 break;
2211 }
2212
2213 case Instruction::MUL_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002214 Binop_23x<HMul>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002215 break;
2216 }
2217
2218 case Instruction::DIV_INT: {
2219 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002220 dex_pc, DataType::Type::kInt32, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002221 break;
2222 }
2223
2224 case Instruction::DIV_LONG: {
2225 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002226 dex_pc, DataType::Type::kInt64, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002227 break;
2228 }
2229
2230 case Instruction::DIV_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002231 Binop_23x<HDiv>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002232 break;
2233 }
2234
2235 case Instruction::DIV_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002236 Binop_23x<HDiv>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002237 break;
2238 }
2239
2240 case Instruction::REM_INT: {
2241 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002242 dex_pc, DataType::Type::kInt32, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002243 break;
2244 }
2245
2246 case Instruction::REM_LONG: {
2247 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002248 dex_pc, DataType::Type::kInt64, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002249 break;
2250 }
2251
2252 case Instruction::REM_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002253 Binop_23x<HRem>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002254 break;
2255 }
2256
2257 case Instruction::REM_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002258 Binop_23x<HRem>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002259 break;
2260 }
2261
2262 case Instruction::AND_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002263 Binop_23x<HAnd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002264 break;
2265 }
2266
2267 case Instruction::AND_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002268 Binop_23x<HAnd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002269 break;
2270 }
2271
2272 case Instruction::SHL_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002273 Binop_23x_shift<HShl>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002274 break;
2275 }
2276
2277 case Instruction::SHL_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002278 Binop_23x_shift<HShl>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002279 break;
2280 }
2281
2282 case Instruction::SHR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002283 Binop_23x_shift<HShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002284 break;
2285 }
2286
2287 case Instruction::SHR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002288 Binop_23x_shift<HShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002289 break;
2290 }
2291
2292 case Instruction::USHR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002293 Binop_23x_shift<HUShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002294 break;
2295 }
2296
2297 case Instruction::USHR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002298 Binop_23x_shift<HUShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002299 break;
2300 }
2301
2302 case Instruction::OR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002303 Binop_23x<HOr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002304 break;
2305 }
2306
2307 case Instruction::OR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002308 Binop_23x<HOr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002309 break;
2310 }
2311
2312 case Instruction::XOR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002313 Binop_23x<HXor>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002314 break;
2315 }
2316
2317 case Instruction::XOR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002318 Binop_23x<HXor>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002319 break;
2320 }
2321
2322 case Instruction::ADD_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002323 Binop_12x<HAdd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002324 break;
2325 }
2326
2327 case Instruction::ADD_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002328 Binop_12x<HAdd>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002329 break;
2330 }
2331
2332 case Instruction::ADD_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002333 Binop_12x<HAdd>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002334 break;
2335 }
2336
2337 case Instruction::SUB_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002338 Binop_12x<HSub>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002339 break;
2340 }
2341
2342 case Instruction::SUB_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002343 Binop_12x<HSub>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002344 break;
2345 }
2346
2347 case Instruction::SUB_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002348 Binop_12x<HSub>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002349 break;
2350 }
2351
2352 case Instruction::SUB_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002353 Binop_12x<HSub>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002354 break;
2355 }
2356
2357 case Instruction::MUL_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002358 Binop_12x<HMul>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002359 break;
2360 }
2361
2362 case Instruction::MUL_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002363 Binop_12x<HMul>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002364 break;
2365 }
2366
2367 case Instruction::MUL_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002368 Binop_12x<HMul>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002369 break;
2370 }
2371
2372 case Instruction::MUL_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002373 Binop_12x<HMul>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002374 break;
2375 }
2376
2377 case Instruction::DIV_INT_2ADDR: {
2378 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002379 dex_pc, DataType::Type::kInt32, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002380 break;
2381 }
2382
2383 case Instruction::DIV_LONG_2ADDR: {
2384 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002385 dex_pc, DataType::Type::kInt64, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002386 break;
2387 }
2388
2389 case Instruction::REM_INT_2ADDR: {
2390 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002391 dex_pc, DataType::Type::kInt32, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002392 break;
2393 }
2394
2395 case Instruction::REM_LONG_2ADDR: {
2396 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002397 dex_pc, DataType::Type::kInt64, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002398 break;
2399 }
2400
2401 case Instruction::REM_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002402 Binop_12x<HRem>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002403 break;
2404 }
2405
2406 case Instruction::REM_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002407 Binop_12x<HRem>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002408 break;
2409 }
2410
2411 case Instruction::SHL_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002412 Binop_12x_shift<HShl>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002413 break;
2414 }
2415
2416 case Instruction::SHL_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002417 Binop_12x_shift<HShl>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002418 break;
2419 }
2420
2421 case Instruction::SHR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002422 Binop_12x_shift<HShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002423 break;
2424 }
2425
2426 case Instruction::SHR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002427 Binop_12x_shift<HShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002428 break;
2429 }
2430
2431 case Instruction::USHR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002432 Binop_12x_shift<HUShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002433 break;
2434 }
2435
2436 case Instruction::USHR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002437 Binop_12x_shift<HUShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002438 break;
2439 }
2440
2441 case Instruction::DIV_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002442 Binop_12x<HDiv>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002443 break;
2444 }
2445
2446 case Instruction::DIV_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002447 Binop_12x<HDiv>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002448 break;
2449 }
2450
2451 case Instruction::AND_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002452 Binop_12x<HAnd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002453 break;
2454 }
2455
2456 case Instruction::AND_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002457 Binop_12x<HAnd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002458 break;
2459 }
2460
2461 case Instruction::OR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002462 Binop_12x<HOr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002463 break;
2464 }
2465
2466 case Instruction::OR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002467 Binop_12x<HOr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002468 break;
2469 }
2470
2471 case Instruction::XOR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002472 Binop_12x<HXor>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002473 break;
2474 }
2475
2476 case Instruction::XOR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002477 Binop_12x<HXor>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002478 break;
2479 }
2480
2481 case Instruction::ADD_INT_LIT16: {
2482 Binop_22s<HAdd>(instruction, false, dex_pc);
2483 break;
2484 }
2485
2486 case Instruction::AND_INT_LIT16: {
2487 Binop_22s<HAnd>(instruction, false, dex_pc);
2488 break;
2489 }
2490
2491 case Instruction::OR_INT_LIT16: {
2492 Binop_22s<HOr>(instruction, false, dex_pc);
2493 break;
2494 }
2495
2496 case Instruction::XOR_INT_LIT16: {
2497 Binop_22s<HXor>(instruction, false, dex_pc);
2498 break;
2499 }
2500
2501 case Instruction::RSUB_INT: {
2502 Binop_22s<HSub>(instruction, true, dex_pc);
2503 break;
2504 }
2505
2506 case Instruction::MUL_INT_LIT16: {
2507 Binop_22s<HMul>(instruction, false, dex_pc);
2508 break;
2509 }
2510
2511 case Instruction::ADD_INT_LIT8: {
2512 Binop_22b<HAdd>(instruction, false, dex_pc);
2513 break;
2514 }
2515
2516 case Instruction::AND_INT_LIT8: {
2517 Binop_22b<HAnd>(instruction, false, dex_pc);
2518 break;
2519 }
2520
2521 case Instruction::OR_INT_LIT8: {
2522 Binop_22b<HOr>(instruction, false, dex_pc);
2523 break;
2524 }
2525
2526 case Instruction::XOR_INT_LIT8: {
2527 Binop_22b<HXor>(instruction, false, dex_pc);
2528 break;
2529 }
2530
2531 case Instruction::RSUB_INT_LIT8: {
2532 Binop_22b<HSub>(instruction, true, dex_pc);
2533 break;
2534 }
2535
2536 case Instruction::MUL_INT_LIT8: {
2537 Binop_22b<HMul>(instruction, false, dex_pc);
2538 break;
2539 }
2540
2541 case Instruction::DIV_INT_LIT16:
2542 case Instruction::DIV_INT_LIT8: {
2543 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002544 dex_pc, DataType::Type::kInt32, true, true);
David Brazdildee58d62016-04-07 09:54:26 +00002545 break;
2546 }
2547
2548 case Instruction::REM_INT_LIT16:
2549 case Instruction::REM_INT_LIT8: {
2550 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002551 dex_pc, DataType::Type::kInt32, true, false);
David Brazdildee58d62016-04-07 09:54:26 +00002552 break;
2553 }
2554
2555 case Instruction::SHL_INT_LIT8: {
2556 Binop_22b<HShl>(instruction, false, dex_pc);
2557 break;
2558 }
2559
2560 case Instruction::SHR_INT_LIT8: {
2561 Binop_22b<HShr>(instruction, false, dex_pc);
2562 break;
2563 }
2564
2565 case Instruction::USHR_INT_LIT8: {
2566 Binop_22b<HUShr>(instruction, false, dex_pc);
2567 break;
2568 }
2569
2570 case Instruction::NEW_INSTANCE: {
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002571 HNewInstance* new_instance =
2572 BuildNewInstance(dex::TypeIndex(instruction.VRegB_21c()), dex_pc);
2573 DCHECK(new_instance != nullptr);
2574
David Brazdildee58d62016-04-07 09:54:26 +00002575 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002576 BuildConstructorFenceForAllocation(new_instance);
David Brazdildee58d62016-04-07 09:54:26 +00002577 break;
2578 }
2579
2580 case Instruction::NEW_ARRAY: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002581 dex::TypeIndex type_index(instruction.VRegC_22c());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002582 HInstruction* length = LoadLocal(instruction.VRegB_22c(), DataType::Type::kInt32);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002583 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002584
Vladimir Markoca6fff82017-10-03 14:49:14 +01002585 HNewArray* new_array = new (allocator_) HNewArray(cls, length, dex_pc);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002586 AppendInstruction(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002587 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002588 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002589 break;
2590 }
2591
2592 case Instruction::FILLED_NEW_ARRAY: {
2593 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002594 dex::TypeIndex type_index(instruction.VRegB_35c());
David Brazdildee58d62016-04-07 09:54:26 +00002595 uint32_t args[5];
2596 instruction.GetVarArgs(args);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002597 HNewArray* new_array = BuildFilledNewArray(dex_pc,
2598 type_index,
2599 number_of_vreg_arguments,
2600 /* is_range */ false,
2601 args,
2602 /* register_index */ 0);
2603 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002604 break;
2605 }
2606
2607 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2608 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002609 dex::TypeIndex type_index(instruction.VRegB_3rc());
David Brazdildee58d62016-04-07 09:54:26 +00002610 uint32_t register_index = instruction.VRegC_3rc();
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002611 HNewArray* new_array = BuildFilledNewArray(dex_pc,
2612 type_index,
2613 number_of_vreg_arguments,
2614 /* is_range */ true,
2615 /* args*/ nullptr,
2616 register_index);
2617 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002618 break;
2619 }
2620
2621 case Instruction::FILL_ARRAY_DATA: {
2622 BuildFillArrayData(instruction, dex_pc);
2623 break;
2624 }
2625
2626 case Instruction::MOVE_RESULT:
2627 case Instruction::MOVE_RESULT_WIDE:
2628 case Instruction::MOVE_RESULT_OBJECT: {
2629 DCHECK(latest_result_ != nullptr);
2630 UpdateLocal(instruction.VRegA(), latest_result_);
2631 latest_result_ = nullptr;
2632 break;
2633 }
2634
2635 case Instruction::CMP_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002636 Binop_23x_cmp(instruction, DataType::Type::kInt64, ComparisonBias::kNoBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002637 break;
2638 }
2639
2640 case Instruction::CMPG_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002641 Binop_23x_cmp(instruction, DataType::Type::kFloat32, ComparisonBias::kGtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002642 break;
2643 }
2644
2645 case Instruction::CMPG_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002646 Binop_23x_cmp(instruction, DataType::Type::kFloat64, ComparisonBias::kGtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002647 break;
2648 }
2649
2650 case Instruction::CMPL_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002651 Binop_23x_cmp(instruction, DataType::Type::kFloat32, ComparisonBias::kLtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002652 break;
2653 }
2654
2655 case Instruction::CMPL_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002656 Binop_23x_cmp(instruction, DataType::Type::kFloat64, ComparisonBias::kLtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002657 break;
2658 }
2659
2660 case Instruction::NOP:
2661 break;
2662
2663 case Instruction::IGET:
2664 case Instruction::IGET_QUICK:
2665 case Instruction::IGET_WIDE:
2666 case Instruction::IGET_WIDE_QUICK:
2667 case Instruction::IGET_OBJECT:
2668 case Instruction::IGET_OBJECT_QUICK:
2669 case Instruction::IGET_BOOLEAN:
2670 case Instruction::IGET_BOOLEAN_QUICK:
2671 case Instruction::IGET_BYTE:
2672 case Instruction::IGET_BYTE_QUICK:
2673 case Instruction::IGET_CHAR:
2674 case Instruction::IGET_CHAR_QUICK:
2675 case Instruction::IGET_SHORT:
2676 case Instruction::IGET_SHORT_QUICK: {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002677 if (!BuildInstanceFieldAccess(instruction, dex_pc, false, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00002678 return false;
2679 }
2680 break;
2681 }
2682
2683 case Instruction::IPUT:
2684 case Instruction::IPUT_QUICK:
2685 case Instruction::IPUT_WIDE:
2686 case Instruction::IPUT_WIDE_QUICK:
2687 case Instruction::IPUT_OBJECT:
2688 case Instruction::IPUT_OBJECT_QUICK:
2689 case Instruction::IPUT_BOOLEAN:
2690 case Instruction::IPUT_BOOLEAN_QUICK:
2691 case Instruction::IPUT_BYTE:
2692 case Instruction::IPUT_BYTE_QUICK:
2693 case Instruction::IPUT_CHAR:
2694 case Instruction::IPUT_CHAR_QUICK:
2695 case Instruction::IPUT_SHORT:
2696 case Instruction::IPUT_SHORT_QUICK: {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002697 if (!BuildInstanceFieldAccess(instruction, dex_pc, true, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00002698 return false;
2699 }
2700 break;
2701 }
2702
2703 case Instruction::SGET:
2704 case Instruction::SGET_WIDE:
2705 case Instruction::SGET_OBJECT:
2706 case Instruction::SGET_BOOLEAN:
2707 case Instruction::SGET_BYTE:
2708 case Instruction::SGET_CHAR:
2709 case Instruction::SGET_SHORT: {
2710 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
2711 return false;
2712 }
2713 break;
2714 }
2715
2716 case Instruction::SPUT:
2717 case Instruction::SPUT_WIDE:
2718 case Instruction::SPUT_OBJECT:
2719 case Instruction::SPUT_BOOLEAN:
2720 case Instruction::SPUT_BYTE:
2721 case Instruction::SPUT_CHAR:
2722 case Instruction::SPUT_SHORT: {
2723 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
2724 return false;
2725 }
2726 break;
2727 }
2728
2729#define ARRAY_XX(kind, anticipated_type) \
2730 case Instruction::AGET##kind: { \
2731 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
2732 break; \
2733 } \
2734 case Instruction::APUT##kind: { \
2735 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
2736 break; \
2737 }
2738
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002739 ARRAY_XX(, DataType::Type::kInt32);
2740 ARRAY_XX(_WIDE, DataType::Type::kInt64);
2741 ARRAY_XX(_OBJECT, DataType::Type::kReference);
2742 ARRAY_XX(_BOOLEAN, DataType::Type::kBool);
2743 ARRAY_XX(_BYTE, DataType::Type::kInt8);
2744 ARRAY_XX(_CHAR, DataType::Type::kUint16);
2745 ARRAY_XX(_SHORT, DataType::Type::kInt16);
David Brazdildee58d62016-04-07 09:54:26 +00002746
2747 case Instruction::ARRAY_LENGTH: {
David Brazdilc120bbe2016-04-22 16:57:00 +01002748 HInstruction* object = LoadNullCheckedLocal(instruction.VRegB_12x(), dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002749 AppendInstruction(new (allocator_) HArrayLength(object, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002750 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
2751 break;
2752 }
2753
2754 case Instruction::CONST_STRING: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08002755 dex::StringIndex string_index(instruction.VRegB_21c());
Vladimir Markoca6fff82017-10-03 14:49:14 +01002756 AppendInstruction(new (allocator_) HLoadString(graph_->GetCurrentMethod(),
2757 string_index,
2758 *dex_file_,
2759 dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002760 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2761 break;
2762 }
2763
2764 case Instruction::CONST_STRING_JUMBO: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08002765 dex::StringIndex string_index(instruction.VRegB_31c());
Vladimir Markoca6fff82017-10-03 14:49:14 +01002766 AppendInstruction(new (allocator_) HLoadString(graph_->GetCurrentMethod(),
2767 string_index,
2768 *dex_file_,
2769 dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002770 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
2771 break;
2772 }
2773
2774 case Instruction::CONST_CLASS: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002775 dex::TypeIndex type_index(instruction.VRegB_21c());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002776 BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002777 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2778 break;
2779 }
2780
2781 case Instruction::MOVE_EXCEPTION: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002782 AppendInstruction(new (allocator_) HLoadException(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002783 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
Vladimir Markoca6fff82017-10-03 14:49:14 +01002784 AppendInstruction(new (allocator_) HClearException(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002785 break;
2786 }
2787
2788 case Instruction::THROW: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002789 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002790 AppendInstruction(new (allocator_) HThrow(exception, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002791 // We finished building this block. Set the current block to null to avoid
2792 // adding dead instructions to it.
2793 current_block_ = nullptr;
2794 break;
2795 }
2796
2797 case Instruction::INSTANCE_OF: {
2798 uint8_t destination = instruction.VRegA_22c();
2799 uint8_t reference = instruction.VRegB_22c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002800 dex::TypeIndex type_index(instruction.VRegC_22c());
David Brazdildee58d62016-04-07 09:54:26 +00002801 BuildTypeCheck(instruction, destination, reference, type_index, dex_pc);
2802 break;
2803 }
2804
2805 case Instruction::CHECK_CAST: {
2806 uint8_t reference = instruction.VRegA_21c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002807 dex::TypeIndex type_index(instruction.VRegB_21c());
David Brazdildee58d62016-04-07 09:54:26 +00002808 BuildTypeCheck(instruction, -1, reference, type_index, dex_pc);
2809 break;
2810 }
2811
2812 case Instruction::MONITOR_ENTER: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002813 AppendInstruction(new (allocator_) HMonitorOperation(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002814 LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference),
David Brazdildee58d62016-04-07 09:54:26 +00002815 HMonitorOperation::OperationKind::kEnter,
2816 dex_pc));
2817 break;
2818 }
2819
2820 case Instruction::MONITOR_EXIT: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002821 AppendInstruction(new (allocator_) HMonitorOperation(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002822 LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference),
David Brazdildee58d62016-04-07 09:54:26 +00002823 HMonitorOperation::OperationKind::kExit,
2824 dex_pc));
2825 break;
2826 }
2827
2828 case Instruction::SPARSE_SWITCH:
2829 case Instruction::PACKED_SWITCH: {
2830 BuildSwitch(instruction, dex_pc);
2831 break;
2832 }
2833
2834 default:
2835 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07002836 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00002837 << " because of unhandled instruction "
2838 << instruction.Name();
Igor Murashkin1e065a52017-08-09 13:20:34 -07002839 MaybeRecordStat(compilation_stats_,
2840 MethodCompilationStat::kNotCompiledUnhandledInstruction);
David Brazdildee58d62016-04-07 09:54:26 +00002841 return false;
2842 }
2843 return true;
2844} // NOLINT(readability/fn_size)
2845
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002846ObjPtr<mirror::Class> HInstructionBuilder::LookupResolvedType(
2847 dex::TypeIndex type_index,
2848 const DexCompilationUnit& compilation_unit) const {
2849 return ClassLinker::LookupResolvedType(
2850 type_index, compilation_unit.GetDexCache().Get(), compilation_unit.GetClassLoader().Get());
2851}
2852
2853ObjPtr<mirror::Class> HInstructionBuilder::LookupReferrerClass() const {
2854 // TODO: Cache the result in a Handle<mirror::Class>.
2855 const DexFile::MethodId& method_id =
2856 dex_compilation_unit_->GetDexFile()->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
2857 return LookupResolvedType(method_id.class_idx_, *dex_compilation_unit_);
2858}
2859
David Brazdildee58d62016-04-07 09:54:26 +00002860} // namespace art