blob: 0f0be209619967dfa12363d3f1ca1c99a975fd8f [file] [log] [blame]
David Brazdildee58d62016-04-07 09:54:26 +00001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "instruction_builder.h"
18
Matthew Gharrity465ecc82016-07-19 21:32:52 +000019#include "art_method-inl.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010020#include "base/arena_bit_vector.h"
21#include "base/bit_vector-inl.h"
22#include "block_builder.h"
David Brazdildee58d62016-04-07 09:54:26 +000023#include "bytecode_utils.h"
24#include "class_linker.h"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010025#include "data_type-inl.h"
Andreas Gampe26de38b2016-07-27 17:53:11 -070026#include "dex_instruction-inl.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010027#include "driver/compiler_driver-inl.h"
28#include "driver/dex_compilation_unit.h"
David Brazdildee58d62016-04-07 09:54:26 +000029#include "driver/compiler_options.h"
Andreas Gampe75a7db62016-09-26 12:04:26 -070030#include "imtable-inl.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010031#include "mirror/dex_cache.h"
32#include "optimizing_compiler_stats.h"
Mathieu Chartierde4b08f2017-07-10 14:13:41 -070033#include "quicken_info.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070034#include "scoped_thread_state_change-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070035#include "sharpening.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010036#include "ssa_builder.h"
Andreas Gampea7c83ac2017-09-11 08:14:23 -070037#include "well_known_classes.h"
David Brazdildee58d62016-04-07 09:54:26 +000038
39namespace art {
40
David Brazdildee58d62016-04-07 09:54:26 +000041HBasicBlock* HInstructionBuilder::FindBlockStartingAt(uint32_t dex_pc) const {
42 return block_builder_->GetBlockAt(dex_pc);
43}
44
Vladimir Marko69d310e2017-10-09 14:12:23 +010045inline ScopedArenaVector<HInstruction*>* HInstructionBuilder::GetLocalsFor(HBasicBlock* block) {
46 ScopedArenaVector<HInstruction*>* locals = &locals_for_[block->GetBlockId()];
David Brazdildee58d62016-04-07 09:54:26 +000047 const size_t vregs = graph_->GetNumberOfVRegs();
Mingyao Yang01b47b02017-02-03 12:09:57 -080048 if (locals->size() == vregs) {
49 return locals;
50 }
51 return GetLocalsForWithAllocation(block, locals, vregs);
52}
David Brazdildee58d62016-04-07 09:54:26 +000053
Vladimir Marko69d310e2017-10-09 14:12:23 +010054ScopedArenaVector<HInstruction*>* HInstructionBuilder::GetLocalsForWithAllocation(
Mingyao Yang01b47b02017-02-03 12:09:57 -080055 HBasicBlock* block,
Vladimir Marko69d310e2017-10-09 14:12:23 +010056 ScopedArenaVector<HInstruction*>* locals,
Mingyao Yang01b47b02017-02-03 12:09:57 -080057 const size_t vregs) {
58 DCHECK_NE(locals->size(), vregs);
59 locals->resize(vregs, nullptr);
60 if (block->IsCatchBlock()) {
61 // We record incoming inputs of catch phis at throwing instructions and
62 // must therefore eagerly create the phis. Phis for undefined vregs will
63 // be deleted when the first throwing instruction with the vreg undefined
64 // is encountered. Unused phis will be removed by dead phi analysis.
65 for (size_t i = 0; i < vregs; ++i) {
66 // No point in creating the catch phi if it is already undefined at
67 // the first throwing instruction.
68 HInstruction* current_local_value = (*current_locals_)[i];
69 if (current_local_value != nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +010070 HPhi* phi = new (allocator_) HPhi(
71 allocator_,
Mingyao Yang01b47b02017-02-03 12:09:57 -080072 i,
73 0,
74 current_local_value->GetType());
75 block->AddPhi(phi);
76 (*locals)[i] = phi;
David Brazdildee58d62016-04-07 09:54:26 +000077 }
78 }
79 }
80 return locals;
81}
82
Mingyao Yang01b47b02017-02-03 12:09:57 -080083inline HInstruction* HInstructionBuilder::ValueOfLocalAt(HBasicBlock* block, size_t local) {
Vladimir Marko69d310e2017-10-09 14:12:23 +010084 ScopedArenaVector<HInstruction*>* locals = GetLocalsFor(block);
David Brazdildee58d62016-04-07 09:54:26 +000085 return (*locals)[local];
86}
87
88void HInstructionBuilder::InitializeBlockLocals() {
89 current_locals_ = GetLocalsFor(current_block_);
90
91 if (current_block_->IsCatchBlock()) {
92 // Catch phis were already created and inputs collected from throwing sites.
93 if (kIsDebugBuild) {
94 // Make sure there was at least one throwing instruction which initialized
95 // locals (guaranteed by HGraphBuilder) and that all try blocks have been
96 // visited already (from HTryBoundary scoping and reverse post order).
97 bool catch_block_visited = false;
Vladimir Marko2c45bc92016-10-25 16:54:12 +010098 for (HBasicBlock* current : graph_->GetReversePostOrder()) {
David Brazdildee58d62016-04-07 09:54:26 +000099 if (current == current_block_) {
100 catch_block_visited = true;
101 } else if (current->IsTryBlock()) {
102 const HTryBoundary& try_entry = current->GetTryCatchInformation()->GetTryEntry();
103 if (try_entry.HasExceptionHandler(*current_block_)) {
104 DCHECK(!catch_block_visited) << "Catch block visited before its try block.";
105 }
106 }
107 }
108 DCHECK_EQ(current_locals_->size(), graph_->GetNumberOfVRegs())
109 << "No instructions throwing into a live catch block.";
110 }
111 } else if (current_block_->IsLoopHeader()) {
112 // If the block is a loop header, we know we only have visited the pre header
113 // because we are visiting in reverse post order. We create phis for all initialized
114 // locals from the pre header. Their inputs will be populated at the end of
115 // the analysis.
116 for (size_t local = 0; local < current_locals_->size(); ++local) {
117 HInstruction* incoming =
118 ValueOfLocalAt(current_block_->GetLoopInformation()->GetPreHeader(), local);
119 if (incoming != nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100120 HPhi* phi = new (allocator_) HPhi(
121 allocator_,
David Brazdildee58d62016-04-07 09:54:26 +0000122 local,
123 0,
124 incoming->GetType());
125 current_block_->AddPhi(phi);
126 (*current_locals_)[local] = phi;
127 }
128 }
129
130 // Save the loop header so that the last phase of the analysis knows which
131 // blocks need to be updated.
132 loop_headers_.push_back(current_block_);
133 } else if (current_block_->GetPredecessors().size() > 0) {
134 // All predecessors have already been visited because we are visiting in reverse post order.
135 // We merge the values of all locals, creating phis if those values differ.
136 for (size_t local = 0; local < current_locals_->size(); ++local) {
137 bool one_predecessor_has_no_value = false;
138 bool is_different = false;
139 HInstruction* value = ValueOfLocalAt(current_block_->GetPredecessors()[0], local);
140
141 for (HBasicBlock* predecessor : current_block_->GetPredecessors()) {
142 HInstruction* current = ValueOfLocalAt(predecessor, local);
143 if (current == nullptr) {
144 one_predecessor_has_no_value = true;
145 break;
146 } else if (current != value) {
147 is_different = true;
148 }
149 }
150
151 if (one_predecessor_has_no_value) {
152 // If one predecessor has no value for this local, we trust the verifier has
153 // successfully checked that there is a store dominating any read after this block.
154 continue;
155 }
156
157 if (is_different) {
158 HInstruction* first_input = ValueOfLocalAt(current_block_->GetPredecessors()[0], local);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100159 HPhi* phi = new (allocator_) HPhi(
160 allocator_,
David Brazdildee58d62016-04-07 09:54:26 +0000161 local,
162 current_block_->GetPredecessors().size(),
163 first_input->GetType());
164 for (size_t i = 0; i < current_block_->GetPredecessors().size(); i++) {
165 HInstruction* pred_value = ValueOfLocalAt(current_block_->GetPredecessors()[i], local);
166 phi->SetRawInputAt(i, pred_value);
167 }
168 current_block_->AddPhi(phi);
169 value = phi;
170 }
171 (*current_locals_)[local] = value;
172 }
173 }
174}
175
176void HInstructionBuilder::PropagateLocalsToCatchBlocks() {
177 const HTryBoundary& try_entry = current_block_->GetTryCatchInformation()->GetTryEntry();
178 for (HBasicBlock* catch_block : try_entry.GetExceptionHandlers()) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100179 ScopedArenaVector<HInstruction*>* handler_locals = GetLocalsFor(catch_block);
David Brazdildee58d62016-04-07 09:54:26 +0000180 DCHECK_EQ(handler_locals->size(), current_locals_->size());
181 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
182 HInstruction* handler_value = (*handler_locals)[vreg];
183 if (handler_value == nullptr) {
184 // Vreg was undefined at a previously encountered throwing instruction
185 // and the catch phi was deleted. Do not record the local value.
186 continue;
187 }
188 DCHECK(handler_value->IsPhi());
189
190 HInstruction* local_value = (*current_locals_)[vreg];
191 if (local_value == nullptr) {
192 // This is the first instruction throwing into `catch_block` where
193 // `vreg` is undefined. Delete the catch phi.
194 catch_block->RemovePhi(handler_value->AsPhi());
195 (*handler_locals)[vreg] = nullptr;
196 } else {
197 // Vreg has been defined at all instructions throwing into `catch_block`
198 // encountered so far. Record the local value in the catch phi.
199 handler_value->AsPhi()->AddInput(local_value);
200 }
201 }
202 }
203}
204
205void HInstructionBuilder::AppendInstruction(HInstruction* instruction) {
206 current_block_->AddInstruction(instruction);
207 InitializeInstruction(instruction);
208}
209
210void HInstructionBuilder::InsertInstructionAtTop(HInstruction* instruction) {
211 if (current_block_->GetInstructions().IsEmpty()) {
212 current_block_->AddInstruction(instruction);
213 } else {
214 current_block_->InsertInstructionBefore(instruction, current_block_->GetFirstInstruction());
215 }
216 InitializeInstruction(instruction);
217}
218
219void HInstructionBuilder::InitializeInstruction(HInstruction* instruction) {
220 if (instruction->NeedsEnvironment()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100221 HEnvironment* environment = new (allocator_) HEnvironment(
222 allocator_,
David Brazdildee58d62016-04-07 09:54:26 +0000223 current_locals_->size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000224 graph_->GetArtMethod(),
David Brazdildee58d62016-04-07 09:54:26 +0000225 instruction->GetDexPc(),
David Brazdildee58d62016-04-07 09:54:26 +0000226 instruction);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100227 environment->CopyFrom(ArrayRef<HInstruction* const>(*current_locals_));
David Brazdildee58d62016-04-07 09:54:26 +0000228 instruction->SetRawEnvironment(environment);
229 }
230}
231
David Brazdilc120bbe2016-04-22 16:57:00 +0100232HInstruction* HInstructionBuilder::LoadNullCheckedLocal(uint32_t register_index, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100233 HInstruction* ref = LoadLocal(register_index, DataType::Type::kReference);
David Brazdilc120bbe2016-04-22 16:57:00 +0100234 if (!ref->CanBeNull()) {
235 return ref;
236 }
237
Vladimir Markoca6fff82017-10-03 14:49:14 +0100238 HNullCheck* null_check = new (allocator_) HNullCheck(ref, dex_pc);
David Brazdilc120bbe2016-04-22 16:57:00 +0100239 AppendInstruction(null_check);
240 return null_check;
241}
242
David Brazdildee58d62016-04-07 09:54:26 +0000243void HInstructionBuilder::SetLoopHeaderPhiInputs() {
244 for (size_t i = loop_headers_.size(); i > 0; --i) {
245 HBasicBlock* block = loop_headers_[i - 1];
246 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
247 HPhi* phi = it.Current()->AsPhi();
248 size_t vreg = phi->GetRegNumber();
249 for (HBasicBlock* predecessor : block->GetPredecessors()) {
250 HInstruction* value = ValueOfLocalAt(predecessor, vreg);
251 if (value == nullptr) {
252 // Vreg is undefined at this predecessor. Mark it dead and leave with
253 // fewer inputs than predecessors. SsaChecker will fail if not removed.
254 phi->SetDead();
255 break;
256 } else {
257 phi->AddInput(value);
258 }
259 }
260 }
261 }
262}
263
264static bool IsBlockPopulated(HBasicBlock* block) {
265 if (block->IsLoopHeader()) {
266 // Suspend checks were inserted into loop headers during building of dominator tree.
267 DCHECK(block->GetFirstInstruction()->IsSuspendCheck());
268 return block->GetFirstInstruction() != block->GetLastInstruction();
269 } else {
270 return !block->GetInstructions().IsEmpty();
271 }
272}
273
274bool HInstructionBuilder::Build() {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100275 locals_for_.resize(
276 graph_->GetBlocks().size(),
277 ScopedArenaVector<HInstruction*>(local_allocator_->Adapter(kArenaAllocGraphBuilder)));
David Brazdildee58d62016-04-07 09:54:26 +0000278
279 // Find locations where we want to generate extra stackmaps for native debugging.
280 // This allows us to generate the info only at interesting points (for example,
281 // at start of java statement) rather than before every dex instruction.
282 const bool native_debuggable = compiler_driver_ != nullptr &&
283 compiler_driver_->GetCompilerOptions().GetNativeDebuggable();
284 ArenaBitVector* native_debug_info_locations = nullptr;
285 if (native_debuggable) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100286 native_debug_info_locations = FindNativeDebugInfoLocations();
David Brazdildee58d62016-04-07 09:54:26 +0000287 }
288
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100289 for (HBasicBlock* block : graph_->GetReversePostOrder()) {
290 current_block_ = block;
David Brazdildee58d62016-04-07 09:54:26 +0000291 uint32_t block_dex_pc = current_block_->GetDexPc();
292
293 InitializeBlockLocals();
294
295 if (current_block_->IsEntryBlock()) {
296 InitializeParameters();
Vladimir Markoca6fff82017-10-03 14:49:14 +0100297 AppendInstruction(new (allocator_) HSuspendCheck(0u));
298 AppendInstruction(new (allocator_) HGoto(0u));
David Brazdildee58d62016-04-07 09:54:26 +0000299 continue;
300 } else if (current_block_->IsExitBlock()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100301 AppendInstruction(new (allocator_) HExit());
David Brazdildee58d62016-04-07 09:54:26 +0000302 continue;
303 } else if (current_block_->IsLoopHeader()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100304 HSuspendCheck* suspend_check = new (allocator_) HSuspendCheck(current_block_->GetDexPc());
David Brazdildee58d62016-04-07 09:54:26 +0000305 current_block_->GetLoopInformation()->SetSuspendCheck(suspend_check);
306 // This is slightly odd because the loop header might not be empty (TryBoundary).
307 // But we're still creating the environment with locals from the top of the block.
308 InsertInstructionAtTop(suspend_check);
309 }
310
311 if (block_dex_pc == kNoDexPc || current_block_ != block_builder_->GetBlockAt(block_dex_pc)) {
312 // Synthetic block that does not need to be populated.
313 DCHECK(IsBlockPopulated(current_block_));
314 continue;
315 }
316
317 DCHECK(!IsBlockPopulated(current_block_));
318
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700319 uint32_t quicken_index = 0;
320 if (CanDecodeQuickenedInfo()) {
321 quicken_index = block_builder_->GetQuickenIndex(block_dex_pc);
322 }
323
David Brazdildee58d62016-04-07 09:54:26 +0000324 for (CodeItemIterator it(code_item_, block_dex_pc); !it.Done(); it.Advance()) {
325 if (current_block_ == nullptr) {
326 // The previous instruction ended this block.
327 break;
328 }
329
330 uint32_t dex_pc = it.CurrentDexPc();
331 if (dex_pc != block_dex_pc && FindBlockStartingAt(dex_pc) != nullptr) {
332 // This dex_pc starts a new basic block.
333 break;
334 }
335
336 if (current_block_->IsTryBlock() && IsThrowingDexInstruction(it.CurrentInstruction())) {
337 PropagateLocalsToCatchBlocks();
338 }
339
340 if (native_debuggable && native_debug_info_locations->IsBitSet(dex_pc)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100341 AppendInstruction(new (allocator_) HNativeDebugInfo(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000342 }
343
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700344 if (!ProcessDexInstruction(it.CurrentInstruction(), dex_pc, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +0000345 return false;
346 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700347
348 if (QuickenInfoTable::NeedsIndexForInstruction(&it.CurrentInstruction())) {
349 ++quicken_index;
350 }
David Brazdildee58d62016-04-07 09:54:26 +0000351 }
352
353 if (current_block_ != nullptr) {
354 // Branching instructions clear current_block, so we know the last
355 // instruction of the current block is not a branching instruction.
356 // We add an unconditional Goto to the next block.
357 DCHECK_EQ(current_block_->GetSuccessors().size(), 1u);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100358 AppendInstruction(new (allocator_) HGoto());
David Brazdildee58d62016-04-07 09:54:26 +0000359 }
360 }
361
362 SetLoopHeaderPhiInputs();
363
364 return true;
365}
366
Vladimir Marko69d310e2017-10-09 14:12:23 +0100367ArenaBitVector* HInstructionBuilder::FindNativeDebugInfoLocations() {
David Brazdildee58d62016-04-07 09:54:26 +0000368 // The callback gets called when the line number changes.
369 // In other words, it marks the start of new java statement.
370 struct Callback {
371 static bool Position(void* ctx, const DexFile::PositionInfo& entry) {
372 static_cast<ArenaBitVector*>(ctx)->SetBit(entry.address_);
373 return false;
374 }
375 };
Vladimir Marko69d310e2017-10-09 14:12:23 +0100376 const uint32_t num_instructions = code_item_.insns_size_in_code_units_;
377 ArenaBitVector* locations = ArenaBitVector::Create(local_allocator_,
378 num_instructions,
379 /* expandable */ false,
380 kArenaAllocGraphBuilder);
381 locations->ClearAllBits();
David Brazdildee58d62016-04-07 09:54:26 +0000382 dex_file_->DecodeDebugPositionInfo(&code_item_, Callback::Position, locations);
383 // Instruction-specific tweaks.
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -0700384 IterationRange<DexInstructionIterator> instructions = code_item_.Instructions();
Mathieu Chartier2b2bef22017-10-26 17:10:19 -0700385 for (DexInstructionIterator it = instructions.begin(); it != instructions.end(); ++it) {
386 switch (it->Opcode()) {
David Brazdildee58d62016-04-07 09:54:26 +0000387 case Instruction::MOVE_EXCEPTION: {
388 // Stop in native debugger after the exception has been moved.
389 // The compiler also expects the move at the start of basic block so
390 // we do not want to interfere by inserting native-debug-info before it.
Mathieu Chartier2b2bef22017-10-26 17:10:19 -0700391 locations->ClearBit(it.DexPc());
392 DexInstructionIterator next = it;
393 ++next;
394 DCHECK(next != it);
395 if (next != instructions.end()) {
396 locations->SetBit(next.DexPc());
David Brazdildee58d62016-04-07 09:54:26 +0000397 }
398 break;
399 }
400 default:
401 break;
402 }
403 }
Vladimir Marko69d310e2017-10-09 14:12:23 +0100404 return locations;
David Brazdildee58d62016-04-07 09:54:26 +0000405}
406
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100407HInstruction* HInstructionBuilder::LoadLocal(uint32_t reg_number, DataType::Type type) const {
David Brazdildee58d62016-04-07 09:54:26 +0000408 HInstruction* value = (*current_locals_)[reg_number];
409 DCHECK(value != nullptr);
410
411 // If the operation requests a specific type, we make sure its input is of that type.
412 if (type != value->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100413 if (DataType::IsFloatingPointType(type)) {
Aart Bik31883642016-06-06 15:02:44 -0700414 value = ssa_builder_->GetFloatOrDoubleEquivalent(value, type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100415 } else if (type == DataType::Type::kReference) {
Aart Bik31883642016-06-06 15:02:44 -0700416 value = ssa_builder_->GetReferenceTypeEquivalent(value);
David Brazdildee58d62016-04-07 09:54:26 +0000417 }
Aart Bik31883642016-06-06 15:02:44 -0700418 DCHECK(value != nullptr);
David Brazdildee58d62016-04-07 09:54:26 +0000419 }
420
421 return value;
422}
423
424void HInstructionBuilder::UpdateLocal(uint32_t reg_number, HInstruction* stored_value) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100425 DataType::Type stored_type = stored_value->GetType();
426 DCHECK_NE(stored_type, DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +0000427
428 // Storing into vreg `reg_number` may implicitly invalidate the surrounding
429 // registers. Consider the following cases:
430 // (1) Storing a wide value must overwrite previous values in both `reg_number`
431 // and `reg_number+1`. We store `nullptr` in `reg_number+1`.
432 // (2) If vreg `reg_number-1` holds a wide value, writing into `reg_number`
433 // must invalidate it. We store `nullptr` in `reg_number-1`.
434 // Consequently, storing a wide value into the high vreg of another wide value
435 // will invalidate both `reg_number-1` and `reg_number+1`.
436
437 if (reg_number != 0) {
438 HInstruction* local_low = (*current_locals_)[reg_number - 1];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100439 if (local_low != nullptr && DataType::Is64BitType(local_low->GetType())) {
David Brazdildee58d62016-04-07 09:54:26 +0000440 // The vreg we are storing into was previously the high vreg of a pair.
441 // We need to invalidate its low vreg.
442 DCHECK((*current_locals_)[reg_number] == nullptr);
443 (*current_locals_)[reg_number - 1] = nullptr;
444 }
445 }
446
447 (*current_locals_)[reg_number] = stored_value;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100448 if (DataType::Is64BitType(stored_type)) {
David Brazdildee58d62016-04-07 09:54:26 +0000449 // We are storing a pair. Invalidate the instruction in the high vreg.
450 (*current_locals_)[reg_number + 1] = nullptr;
451 }
452}
453
454void HInstructionBuilder::InitializeParameters() {
455 DCHECK(current_block_->IsEntryBlock());
456
Vladimir Marko69d310e2017-10-09 14:12:23 +0100457 // outer_compilation_unit_ is null only when unit testing.
458 if (outer_compilation_unit_ == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +0000459 return;
460 }
461
462 const char* shorty = dex_compilation_unit_->GetShorty();
463 uint16_t number_of_parameters = graph_->GetNumberOfInVRegs();
464 uint16_t locals_index = graph_->GetNumberOfLocalVRegs();
465 uint16_t parameter_index = 0;
466
467 const DexFile::MethodId& referrer_method_id =
468 dex_file_->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
469 if (!dex_compilation_unit_->IsStatic()) {
470 // Add the implicit 'this' argument, not expressed in the signature.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100471 HParameterValue* parameter = new (allocator_) HParameterValue(*dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +0000472 referrer_method_id.class_idx_,
473 parameter_index++,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100474 DataType::Type::kReference,
Igor Murashkind01745e2017-04-05 16:40:31 -0700475 /* is_this */ true);
David Brazdildee58d62016-04-07 09:54:26 +0000476 AppendInstruction(parameter);
477 UpdateLocal(locals_index++, parameter);
478 number_of_parameters--;
Igor Murashkind01745e2017-04-05 16:40:31 -0700479 current_this_parameter_ = parameter;
480 } else {
481 DCHECK(current_this_parameter_ == nullptr);
David Brazdildee58d62016-04-07 09:54:26 +0000482 }
483
484 const DexFile::ProtoId& proto = dex_file_->GetMethodPrototype(referrer_method_id);
485 const DexFile::TypeList* arg_types = dex_file_->GetProtoParameters(proto);
486 for (int i = 0, shorty_pos = 1; i < number_of_parameters; i++) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100487 HParameterValue* parameter = new (allocator_) HParameterValue(
David Brazdildee58d62016-04-07 09:54:26 +0000488 *dex_file_,
489 arg_types->GetTypeItem(shorty_pos - 1).type_idx_,
490 parameter_index++,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100491 DataType::FromShorty(shorty[shorty_pos]),
Igor Murashkind01745e2017-04-05 16:40:31 -0700492 /* is_this */ false);
David Brazdildee58d62016-04-07 09:54:26 +0000493 ++shorty_pos;
494 AppendInstruction(parameter);
495 // Store the parameter value in the local that the dex code will use
496 // to reference that parameter.
497 UpdateLocal(locals_index++, parameter);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100498 if (DataType::Is64BitType(parameter->GetType())) {
David Brazdildee58d62016-04-07 09:54:26 +0000499 i++;
500 locals_index++;
501 parameter_index++;
502 }
503 }
504}
505
506template<typename T>
507void HInstructionBuilder::If_22t(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100508 HInstruction* first = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
509 HInstruction* second = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100510 T* comparison = new (allocator_) T(first, second, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000511 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100512 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000513 current_block_ = nullptr;
514}
515
516template<typename T>
517void HInstructionBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100518 HInstruction* value = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100519 T* comparison = new (allocator_) T(value, graph_->GetIntConstant(0, dex_pc), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000520 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100521 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000522 current_block_ = nullptr;
523}
524
525template<typename T>
526void HInstructionBuilder::Unop_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100527 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000528 uint32_t dex_pc) {
529 HInstruction* first = LoadLocal(instruction.VRegB(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100530 AppendInstruction(new (allocator_) T(type, first, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000531 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
532}
533
534void HInstructionBuilder::Conversion_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100535 DataType::Type input_type,
536 DataType::Type result_type,
David Brazdildee58d62016-04-07 09:54:26 +0000537 uint32_t dex_pc) {
538 HInstruction* first = LoadLocal(instruction.VRegB(), input_type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100539 AppendInstruction(new (allocator_) HTypeConversion(result_type, first, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000540 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
541}
542
543template<typename T>
544void HInstructionBuilder::Binop_23x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100545 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000546 uint32_t dex_pc) {
547 HInstruction* first = LoadLocal(instruction.VRegB(), type);
548 HInstruction* second = LoadLocal(instruction.VRegC(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100549 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000550 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
551}
552
553template<typename T>
554void HInstructionBuilder::Binop_23x_shift(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100555 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000556 uint32_t dex_pc) {
557 HInstruction* first = LoadLocal(instruction.VRegB(), type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100558 HInstruction* second = LoadLocal(instruction.VRegC(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100559 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000560 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
561}
562
563void HInstructionBuilder::Binop_23x_cmp(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100564 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000565 ComparisonBias bias,
566 uint32_t dex_pc) {
567 HInstruction* first = LoadLocal(instruction.VRegB(), type);
568 HInstruction* second = LoadLocal(instruction.VRegC(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100569 AppendInstruction(new (allocator_) HCompare(type, first, second, bias, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000570 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
571}
572
573template<typename T>
574void HInstructionBuilder::Binop_12x_shift(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100575 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000576 uint32_t dex_pc) {
577 HInstruction* first = LoadLocal(instruction.VRegA(), type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100578 HInstruction* second = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100579 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000580 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
581}
582
583template<typename T>
584void HInstructionBuilder::Binop_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100585 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000586 uint32_t dex_pc) {
587 HInstruction* first = LoadLocal(instruction.VRegA(), type);
588 HInstruction* second = LoadLocal(instruction.VRegB(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100589 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000590 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
591}
592
593template<typename T>
594void HInstructionBuilder::Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100595 HInstruction* first = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000596 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s(), dex_pc);
597 if (reverse) {
598 std::swap(first, second);
599 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100600 AppendInstruction(new (allocator_) T(DataType::Type::kInt32, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000601 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
602}
603
604template<typename T>
605void HInstructionBuilder::Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100606 HInstruction* first = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000607 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b(), dex_pc);
608 if (reverse) {
609 std::swap(first, second);
610 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100611 AppendInstruction(new (allocator_) T(DataType::Type::kInt32, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000612 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
613}
614
Igor Murashkind01745e2017-04-05 16:40:31 -0700615// Does the method being compiled need any constructor barriers being inserted?
616// (Always 'false' for methods that aren't <init>.)
Mathieu Chartierc4ae9162016-04-07 13:19:19 -0700617static bool RequiresConstructorBarrier(const DexCompilationUnit* cu, CompilerDriver* driver) {
Igor Murashkin032cacd2017-04-06 14:40:08 -0700618 // Can be null in unit tests only.
619 if (UNLIKELY(cu == nullptr)) {
620 return false;
621 }
622
David Brazdildee58d62016-04-07 09:54:26 +0000623 Thread* self = Thread::Current();
624 return cu->IsConstructor()
Igor Murashkind01745e2017-04-05 16:40:31 -0700625 && !cu->IsStatic()
626 // RequiresConstructorBarrier must only be queried for <init> methods;
627 // it's effectively "false" for every other method.
628 //
629 // See CompilerDriver::RequiresConstructBarrier for more explanation.
Mathieu Chartierc4ae9162016-04-07 13:19:19 -0700630 && driver->RequiresConstructorBarrier(self, cu->GetDexFile(), cu->GetClassDefIndex());
David Brazdildee58d62016-04-07 09:54:26 +0000631}
632
633// Returns true if `block` has only one successor which starts at the next
634// dex_pc after `instruction` at `dex_pc`.
635static bool IsFallthroughInstruction(const Instruction& instruction,
636 uint32_t dex_pc,
637 HBasicBlock* block) {
638 uint32_t next_dex_pc = dex_pc + instruction.SizeInCodeUnits();
639 return block->GetSingleSuccessor()->GetDexPc() == next_dex_pc;
640}
641
642void HInstructionBuilder::BuildSwitch(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100643 HInstruction* value = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000644 DexSwitchTable table(instruction, dex_pc);
645
646 if (table.GetNumEntries() == 0) {
647 // Empty Switch. Code falls through to the next block.
648 DCHECK(IsFallthroughInstruction(instruction, dex_pc, current_block_));
Vladimir Markoca6fff82017-10-03 14:49:14 +0100649 AppendInstruction(new (allocator_) HGoto(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000650 } else if (table.ShouldBuildDecisionTree()) {
651 for (DexSwitchTableIterator it(table); !it.Done(); it.Advance()) {
652 HInstruction* case_value = graph_->GetIntConstant(it.CurrentKey(), dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100653 HEqual* comparison = new (allocator_) HEqual(value, case_value, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000654 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100655 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000656
657 if (!it.IsLast()) {
658 current_block_ = FindBlockStartingAt(it.GetDexPcForCurrentIndex());
659 }
660 }
661 } else {
662 AppendInstruction(
Vladimir Markoca6fff82017-10-03 14:49:14 +0100663 new (allocator_) HPackedSwitch(table.GetEntryAt(0), table.GetNumEntries(), value, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000664 }
665
666 current_block_ = nullptr;
667}
668
669void HInstructionBuilder::BuildReturn(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100670 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000671 uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100672 if (type == DataType::Type::kVoid) {
Igor Murashkind01745e2017-04-05 16:40:31 -0700673 // Only <init> (which is a return-void) could possibly have a constructor fence.
Igor Murashkin032cacd2017-04-06 14:40:08 -0700674 // This may insert additional redundant constructor fences from the super constructors.
675 // TODO: remove redundant constructor fences (b/36656456).
676 if (RequiresConstructorBarrier(dex_compilation_unit_, compiler_driver_)) {
Igor Murashkind01745e2017-04-05 16:40:31 -0700677 // Compiling instance constructor.
Vladimir Markoba118822017-06-12 15:41:56 +0100678 DCHECK_STREQ("<init>", graph_->GetMethodName());
Igor Murashkind01745e2017-04-05 16:40:31 -0700679
680 HInstruction* fence_target = current_this_parameter_;
681 DCHECK(fence_target != nullptr);
682
Vladimir Markoca6fff82017-10-03 14:49:14 +0100683 AppendInstruction(new (allocator_) HConstructorFence(fence_target, dex_pc, allocator_));
Igor Murashkin6ef45672017-08-08 13:59:55 -0700684 MaybeRecordStat(
685 compilation_stats_,
686 MethodCompilationStat::kConstructorFenceGeneratedFinal);
David Brazdildee58d62016-04-07 09:54:26 +0000687 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100688 AppendInstruction(new (allocator_) HReturnVoid(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000689 } else {
Igor Murashkind01745e2017-04-05 16:40:31 -0700690 DCHECK(!RequiresConstructorBarrier(dex_compilation_unit_, compiler_driver_));
David Brazdildee58d62016-04-07 09:54:26 +0000691 HInstruction* value = LoadLocal(instruction.VRegA(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100692 AppendInstruction(new (allocator_) HReturn(value, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000693 }
694 current_block_ = nullptr;
695}
696
697static InvokeType GetInvokeTypeFromOpCode(Instruction::Code opcode) {
698 switch (opcode) {
699 case Instruction::INVOKE_STATIC:
700 case Instruction::INVOKE_STATIC_RANGE:
701 return kStatic;
702 case Instruction::INVOKE_DIRECT:
703 case Instruction::INVOKE_DIRECT_RANGE:
704 return kDirect;
705 case Instruction::INVOKE_VIRTUAL:
706 case Instruction::INVOKE_VIRTUAL_QUICK:
707 case Instruction::INVOKE_VIRTUAL_RANGE:
708 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
709 return kVirtual;
710 case Instruction::INVOKE_INTERFACE:
711 case Instruction::INVOKE_INTERFACE_RANGE:
712 return kInterface;
713 case Instruction::INVOKE_SUPER_RANGE:
714 case Instruction::INVOKE_SUPER:
715 return kSuper;
716 default:
717 LOG(FATAL) << "Unexpected invoke opcode: " << opcode;
718 UNREACHABLE();
719 }
720}
721
722ArtMethod* HInstructionBuilder::ResolveMethod(uint16_t method_idx, InvokeType invoke_type) {
723 ScopedObjectAccess soa(Thread::Current());
David Brazdildee58d62016-04-07 09:54:26 +0000724
725 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000726 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100727
Vladimir Markoba118822017-06-12 15:41:56 +0100728 ArtMethod* resolved_method =
729 class_linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
730 *dex_compilation_unit_->GetDexFile(),
731 method_idx,
732 dex_compilation_unit_->GetDexCache(),
733 class_loader,
734 graph_->GetArtMethod(),
735 invoke_type);
David Brazdildee58d62016-04-07 09:54:26 +0000736
737 if (UNLIKELY(resolved_method == nullptr)) {
738 // Clean up any exception left by type resolution.
739 soa.Self()->ClearException();
740 return nullptr;
741 }
742
Vladimir Markoba118822017-06-12 15:41:56 +0100743 // The referrer may be unresolved for AOT if we're compiling a class that cannot be
744 // resolved because, for example, we don't find a superclass in the classpath.
745 if (graph_->GetArtMethod() == nullptr) {
746 // The class linker cannot check access without a referrer, so we have to do it.
747 // Fall back to HInvokeUnresolved if the method isn't public.
David Brazdildee58d62016-04-07 09:54:26 +0000748 if (!resolved_method->IsPublic()) {
749 return nullptr;
750 }
David Brazdildee58d62016-04-07 09:54:26 +0000751 }
752
753 // We have to special case the invoke-super case, as ClassLinker::ResolveMethod does not.
754 // We need to look at the referrer's super class vtable. We need to do this to know if we need to
755 // make this an invoke-unresolved to handle cross-dex invokes or abstract super methods, both of
756 // which require runtime handling.
757 if (invoke_type == kSuper) {
Vladimir Markoba118822017-06-12 15:41:56 +0100758 ObjPtr<mirror::Class> compiling_class = GetCompilingClass();
Andreas Gampefa4333d2017-02-14 11:10:34 -0800759 if (compiling_class == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +0000760 // We could not determine the method's class we need to wait until runtime.
761 DCHECK(Runtime::Current()->IsAotCompiler());
762 return nullptr;
763 }
Vladimir Markoba118822017-06-12 15:41:56 +0100764 ObjPtr<mirror::Class> referenced_class = class_linker->LookupResolvedType(
765 *dex_compilation_unit_->GetDexFile(),
766 dex_compilation_unit_->GetDexFile()->GetMethodId(method_idx).class_idx_,
767 dex_compilation_unit_->GetDexCache().Get(),
768 class_loader.Get());
769 DCHECK(referenced_class != nullptr); // We have already resolved a method from this class.
770 if (!referenced_class->IsAssignableFrom(compiling_class)) {
Aart Bikf663e342016-04-04 17:28:59 -0700771 // We cannot statically determine the target method. The runtime will throw a
772 // NoSuchMethodError on this one.
773 return nullptr;
774 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100775 ArtMethod* actual_method;
Vladimir Markoba118822017-06-12 15:41:56 +0100776 if (referenced_class->IsInterface()) {
777 actual_method = referenced_class->FindVirtualMethodForInterfaceSuper(
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100778 resolved_method, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000779 } else {
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100780 uint16_t vtable_index = resolved_method->GetMethodIndex();
781 actual_method = compiling_class->GetSuperClass()->GetVTableEntry(
782 vtable_index, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000783 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100784 if (actual_method != resolved_method &&
785 !IsSameDexFile(*actual_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
786 // The back-end code generator relies on this check in order to ensure that it will not
787 // attempt to read the dex_cache with a dex_method_index that is not from the correct
788 // dex_file. If we didn't do this check then the dex_method_index will not be updated in the
789 // builder, which means that the code-generator (and compiler driver during sharpening and
790 // inliner, maybe) might invoke an incorrect method.
791 // TODO: The actual method could still be referenced in the current dex file, so we
792 // could try locating it.
793 // TODO: Remove the dex_file restriction.
794 return nullptr;
795 }
796 if (!actual_method->IsInvokable()) {
797 // Fail if the actual method cannot be invoked. Otherwise, the runtime resolution stub
798 // could resolve the callee to the wrong method.
799 return nullptr;
800 }
801 resolved_method = actual_method;
David Brazdildee58d62016-04-07 09:54:26 +0000802 }
803
David Brazdildee58d62016-04-07 09:54:26 +0000804 return resolved_method;
805}
806
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100807static bool IsStringConstructor(ArtMethod* method) {
808 ScopedObjectAccess soa(Thread::Current());
809 return method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
810}
811
David Brazdildee58d62016-04-07 09:54:26 +0000812bool HInstructionBuilder::BuildInvoke(const Instruction& instruction,
813 uint32_t dex_pc,
814 uint32_t method_idx,
815 uint32_t number_of_vreg_arguments,
816 bool is_range,
817 uint32_t* args,
818 uint32_t register_index) {
819 InvokeType invoke_type = GetInvokeTypeFromOpCode(instruction.Opcode());
820 const char* descriptor = dex_file_->GetMethodShorty(method_idx);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100821 DataType::Type return_type = DataType::FromShorty(descriptor[0]);
David Brazdildee58d62016-04-07 09:54:26 +0000822
823 // Remove the return type from the 'proto'.
824 size_t number_of_arguments = strlen(descriptor) - 1;
825 if (invoke_type != kStatic) { // instance call
826 // One extra argument for 'this'.
827 number_of_arguments++;
828 }
829
David Brazdildee58d62016-04-07 09:54:26 +0000830 ArtMethod* resolved_method = ResolveMethod(method_idx, invoke_type);
831
832 if (UNLIKELY(resolved_method == nullptr)) {
Igor Murashkin1e065a52017-08-09 13:20:34 -0700833 MaybeRecordStat(compilation_stats_,
834 MethodCompilationStat::kUnresolvedMethod);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100835 HInvoke* invoke = new (allocator_) HInvokeUnresolved(allocator_,
836 number_of_arguments,
837 return_type,
838 dex_pc,
839 method_idx,
840 invoke_type);
David Brazdildee58d62016-04-07 09:54:26 +0000841 return HandleInvoke(invoke,
842 number_of_vreg_arguments,
843 args,
844 register_index,
845 is_range,
846 descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -0700847 nullptr, /* clinit_check */
848 true /* is_unresolved */);
David Brazdildee58d62016-04-07 09:54:26 +0000849 }
850
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100851 // Replace calls to String.<init> with StringFactory.
852 if (IsStringConstructor(resolved_method)) {
853 uint32_t string_init_entry_point = WellKnownClasses::StringInitToEntryPoint(resolved_method);
854 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
855 HInvokeStaticOrDirect::MethodLoadKind::kStringInit,
856 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000857 dchecked_integral_cast<uint64_t>(string_init_entry_point)
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100858 };
859 MethodReference target_method(dex_file_, method_idx);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100860 HInvoke* invoke = new (allocator_) HInvokeStaticOrDirect(
861 allocator_,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100862 number_of_arguments - 1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100863 DataType::Type::kReference /*return_type */,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100864 dex_pc,
865 method_idx,
866 nullptr,
867 dispatch_info,
868 invoke_type,
869 target_method,
870 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit);
871 return HandleStringInit(invoke,
872 number_of_vreg_arguments,
873 args,
874 register_index,
875 is_range,
876 descriptor);
877 }
878
David Brazdildee58d62016-04-07 09:54:26 +0000879 // Potential class initialization check, in the case of a static method call.
880 HClinitCheck* clinit_check = nullptr;
881 HInvoke* invoke = nullptr;
882 if (invoke_type == kDirect || invoke_type == kStatic || invoke_type == kSuper) {
883 // By default, consider that the called method implicitly requires
884 // an initialization check of its declaring method.
885 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement
886 = HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
887 ScopedObjectAccess soa(Thread::Current());
888 if (invoke_type == kStatic) {
889 clinit_check = ProcessClinitCheckForInvoke(
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000890 dex_pc, resolved_method, &clinit_check_requirement);
David Brazdildee58d62016-04-07 09:54:26 +0000891 } else if (invoke_type == kSuper) {
892 if (IsSameDexFile(*resolved_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100893 // Update the method index to the one resolved. Note that this may be a no-op if
David Brazdildee58d62016-04-07 09:54:26 +0000894 // we resolved to the method referenced by the instruction.
895 method_idx = resolved_method->GetDexMethodIndex();
David Brazdildee58d62016-04-07 09:54:26 +0000896 }
897 }
898
899 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
Vladimir Markoe7197bf2017-06-02 17:00:23 +0100900 HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall,
David Brazdildee58d62016-04-07 09:54:26 +0000901 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000902 0u
David Brazdildee58d62016-04-07 09:54:26 +0000903 };
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100904 MethodReference target_method(resolved_method->GetDexFile(),
905 resolved_method->GetDexMethodIndex());
Vladimir Markoca6fff82017-10-03 14:49:14 +0100906 invoke = new (allocator_) HInvokeStaticOrDirect(allocator_,
907 number_of_arguments,
908 return_type,
909 dex_pc,
910 method_idx,
911 resolved_method,
912 dispatch_info,
913 invoke_type,
914 target_method,
915 clinit_check_requirement);
David Brazdildee58d62016-04-07 09:54:26 +0000916 } else if (invoke_type == kVirtual) {
917 ScopedObjectAccess soa(Thread::Current()); // Needed for the method index
Vladimir Markoca6fff82017-10-03 14:49:14 +0100918 invoke = new (allocator_) HInvokeVirtual(allocator_,
919 number_of_arguments,
920 return_type,
921 dex_pc,
922 method_idx,
923 resolved_method,
924 resolved_method->GetMethodIndex());
David Brazdildee58d62016-04-07 09:54:26 +0000925 } else {
926 DCHECK_EQ(invoke_type, kInterface);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100927 ScopedObjectAccess soa(Thread::Current()); // Needed for the IMT index.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100928 invoke = new (allocator_) HInvokeInterface(allocator_,
929 number_of_arguments,
930 return_type,
931 dex_pc,
932 method_idx,
933 resolved_method,
934 ImTable::GetImtIndex(resolved_method));
David Brazdildee58d62016-04-07 09:54:26 +0000935 }
936
937 return HandleInvoke(invoke,
938 number_of_vreg_arguments,
939 args,
940 register_index,
941 is_range,
942 descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -0700943 clinit_check,
944 false /* is_unresolved */);
David Brazdildee58d62016-04-07 09:54:26 +0000945}
946
Orion Hodsonac141392017-01-13 11:53:47 +0000947bool HInstructionBuilder::BuildInvokePolymorphic(const Instruction& instruction ATTRIBUTE_UNUSED,
948 uint32_t dex_pc,
949 uint32_t method_idx,
950 uint32_t proto_idx,
951 uint32_t number_of_vreg_arguments,
952 bool is_range,
953 uint32_t* args,
954 uint32_t register_index) {
955 const char* descriptor = dex_file_->GetShorty(proto_idx);
956 DCHECK_EQ(1 + ArtMethod::NumArgRegisters(descriptor), number_of_vreg_arguments);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100957 DataType::Type return_type = DataType::FromShorty(descriptor[0]);
Orion Hodsonac141392017-01-13 11:53:47 +0000958 size_t number_of_arguments = strlen(descriptor);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100959 HInvoke* invoke = new (allocator_) HInvokePolymorphic(allocator_,
960 number_of_arguments,
961 return_type,
962 dex_pc,
963 method_idx);
Orion Hodsonac141392017-01-13 11:53:47 +0000964 return HandleInvoke(invoke,
965 number_of_vreg_arguments,
966 args,
967 register_index,
968 is_range,
969 descriptor,
970 nullptr /* clinit_check */,
971 false /* is_unresolved */);
972}
973
Igor Murashkin79d8fa72017-04-18 09:37:23 -0700974HNewInstance* HInstructionBuilder::BuildNewInstance(dex::TypeIndex type_index, uint32_t dex_pc) {
Vladimir Marko3cd50df2016-04-13 19:29:26 +0100975 ScopedObjectAccess soa(Thread::Current());
David Brazdildee58d62016-04-07 09:54:26 +0000976
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000977 HLoadClass* load_class = BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000978
David Brazdildee58d62016-04-07 09:54:26 +0000979 HInstruction* cls = load_class;
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000980 Handle<mirror::Class> klass = load_class->GetClass();
981
982 if (!IsInitialized(klass)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100983 cls = new (allocator_) HClinitCheck(load_class, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000984 AppendInstruction(cls);
985 }
986
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000987 // Only the access check entrypoint handles the finalizable class case. If we
988 // need access checks, then we haven't resolved the method and the class may
989 // again be finalizable.
990 QuickEntrypointEnum entrypoint = kQuickAllocObjectInitialized;
991 if (load_class->NeedsAccessCheck() || klass->IsFinalizable() || !klass->IsInstantiable()) {
992 entrypoint = kQuickAllocObjectWithChecks;
993 }
994
995 // Consider classes we haven't resolved as potentially finalizable.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800996 bool finalizable = (klass == nullptr) || klass->IsFinalizable();
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000997
Vladimir Markoca6fff82017-10-03 14:49:14 +0100998 HNewInstance* new_instance = new (allocator_) HNewInstance(
David Brazdildee58d62016-04-07 09:54:26 +0000999 cls,
David Brazdildee58d62016-04-07 09:54:26 +00001000 dex_pc,
1001 type_index,
1002 *dex_compilation_unit_->GetDexFile(),
David Brazdildee58d62016-04-07 09:54:26 +00001003 finalizable,
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001004 entrypoint);
1005 AppendInstruction(new_instance);
1006
1007 return new_instance;
1008}
1009
1010void HInstructionBuilder::BuildConstructorFenceForAllocation(HInstruction* allocation) {
1011 DCHECK(allocation != nullptr &&
George Burgess IVf2072992017-05-23 15:36:41 -07001012 (allocation->IsNewInstance() ||
1013 allocation->IsNewArray())); // corresponding to "new" keyword in JLS.
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001014
1015 if (allocation->IsNewInstance()) {
1016 // STRING SPECIAL HANDLING:
1017 // -------------------------------
1018 // Strings have a real HNewInstance node but they end up always having 0 uses.
1019 // All uses of a String HNewInstance are always transformed to replace their input
1020 // of the HNewInstance with an input of the invoke to StringFactory.
1021 //
1022 // Do not emit an HConstructorFence here since it can inhibit some String new-instance
1023 // optimizations (to pass checker tests that rely on those optimizations).
1024 HNewInstance* new_inst = allocation->AsNewInstance();
1025 HLoadClass* load_class = new_inst->GetLoadClass();
1026
1027 Thread* self = Thread::Current();
1028 ScopedObjectAccess soa(self);
1029 StackHandleScope<1> hs(self);
1030 Handle<mirror::Class> klass = load_class->GetClass();
1031 if (klass != nullptr && klass->IsStringClass()) {
1032 return;
1033 // Note: Do not use allocation->IsStringAlloc which requires
1034 // a valid ReferenceTypeInfo, but that doesn't get made until after reference type
1035 // propagation (and instruction builder is too early).
1036 }
1037 // (In terms of correctness, the StringFactory needs to provide its own
1038 // default initialization barrier, see below.)
1039 }
1040
1041 // JLS 17.4.5 "Happens-before Order" describes:
1042 //
1043 // The default initialization of any object happens-before any other actions (other than
1044 // default-writes) of a program.
1045 //
1046 // In our implementation the default initialization of an object to type T means
1047 // setting all of its initial data (object[0..size)) to 0, and setting the
1048 // object's class header (i.e. object.getClass() == T.class).
1049 //
1050 // In practice this fence ensures that the writes to the object header
1051 // are visible to other threads if this object escapes the current thread.
1052 // (and in theory the 0-initializing, but that happens automatically
1053 // when new memory pages are mapped in by the OS).
1054 HConstructorFence* ctor_fence =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001055 new (allocator_) HConstructorFence(allocation, allocation->GetDexPc(), allocator_);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001056 AppendInstruction(ctor_fence);
Igor Murashkin6ef45672017-08-08 13:59:55 -07001057 MaybeRecordStat(
1058 compilation_stats_,
1059 MethodCompilationStat::kConstructorFenceGeneratedNew);
David Brazdildee58d62016-04-07 09:54:26 +00001060}
1061
1062static bool IsSubClass(mirror::Class* to_test, mirror::Class* super_class)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001063 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdildee58d62016-04-07 09:54:26 +00001064 return to_test != nullptr && !to_test->IsInterface() && to_test->IsSubClass(super_class);
1065}
1066
1067bool HInstructionBuilder::IsInitialized(Handle<mirror::Class> cls) const {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001068 if (cls == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +00001069 return false;
1070 }
1071
1072 // `CanAssumeClassIsLoaded` will return true if we're JITting, or will
1073 // check whether the class is in an image for the AOT compilation.
1074 if (cls->IsInitialized() &&
1075 compiler_driver_->CanAssumeClassIsLoaded(cls.Get())) {
1076 return true;
1077 }
1078
1079 if (IsSubClass(GetOutermostCompilingClass(), cls.Get())) {
1080 return true;
1081 }
1082
1083 // TODO: We should walk over the inlined methods, but we don't pass
1084 // that information to the builder.
1085 if (IsSubClass(GetCompilingClass(), cls.Get())) {
1086 return true;
1087 }
1088
1089 return false;
1090}
1091
1092HClinitCheck* HInstructionBuilder::ProcessClinitCheckForInvoke(
1093 uint32_t dex_pc,
1094 ArtMethod* resolved_method,
David Brazdildee58d62016-04-07 09:54:26 +00001095 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001096 Handle<mirror::Class> klass = handles_->NewHandle(resolved_method->GetDeclaringClass());
David Brazdildee58d62016-04-07 09:54:26 +00001097
1098 HClinitCheck* clinit_check = nullptr;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001099 if (IsInitialized(klass)) {
David Brazdildee58d62016-04-07 09:54:26 +00001100 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001101 } else {
1102 HLoadClass* cls = BuildLoadClass(klass->GetDexTypeIndex(),
1103 klass->GetDexFile(),
1104 klass,
1105 dex_pc,
1106 /* needs_access_check */ false);
1107 if (cls != nullptr) {
1108 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
Vladimir Markoca6fff82017-10-03 14:49:14 +01001109 clinit_check = new (allocator_) HClinitCheck(cls, dex_pc);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001110 AppendInstruction(clinit_check);
1111 }
David Brazdildee58d62016-04-07 09:54:26 +00001112 }
1113 return clinit_check;
1114}
1115
1116bool HInstructionBuilder::SetupInvokeArguments(HInvoke* invoke,
1117 uint32_t number_of_vreg_arguments,
1118 uint32_t* args,
1119 uint32_t register_index,
1120 bool is_range,
1121 const char* descriptor,
1122 size_t start_index,
1123 size_t* argument_index) {
1124 uint32_t descriptor_index = 1; // Skip the return type.
1125
1126 for (size_t i = start_index;
1127 // Make sure we don't go over the expected arguments or over the number of
1128 // dex registers given. If the instruction was seen as dead by the verifier,
1129 // it hasn't been properly checked.
1130 (i < number_of_vreg_arguments) && (*argument_index < invoke->GetNumberOfArguments());
1131 i++, (*argument_index)++) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001132 DataType::Type type = DataType::FromShorty(descriptor[descriptor_index++]);
1133 bool is_wide = (type == DataType::Type::kInt64) || (type == DataType::Type::kFloat64);
David Brazdildee58d62016-04-07 09:54:26 +00001134 if (!is_range
1135 && is_wide
1136 && ((i + 1 == number_of_vreg_arguments) || (args[i] + 1 != args[i + 1]))) {
1137 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
1138 // reject any class where this is violated. However, the verifier only does these checks
1139 // on non trivially dead instructions, so we just bailout the compilation.
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 non-sequential dex register pair in wide argument";
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 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
1148 invoke->SetArgumentAt(*argument_index, arg);
1149 if (is_wide) {
1150 i++;
1151 }
1152 }
1153
1154 if (*argument_index != invoke->GetNumberOfArguments()) {
1155 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001156 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001157 << " because of wrong number of arguments in invoke instruction";
Igor Murashkin1e065a52017-08-09 13:20:34 -07001158 MaybeRecordStat(compilation_stats_,
1159 MethodCompilationStat::kNotCompiledMalformedOpcode);
David Brazdildee58d62016-04-07 09:54:26 +00001160 return false;
1161 }
1162
1163 if (invoke->IsInvokeStaticOrDirect() &&
1164 HInvokeStaticOrDirect::NeedsCurrentMethodInput(
1165 invoke->AsInvokeStaticOrDirect()->GetMethodLoadKind())) {
1166 invoke->SetArgumentAt(*argument_index, graph_->GetCurrentMethod());
1167 (*argument_index)++;
1168 }
1169
1170 return true;
1171}
1172
1173bool HInstructionBuilder::HandleInvoke(HInvoke* invoke,
1174 uint32_t number_of_vreg_arguments,
1175 uint32_t* args,
1176 uint32_t register_index,
1177 bool is_range,
1178 const char* descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -07001179 HClinitCheck* clinit_check,
1180 bool is_unresolved) {
David Brazdildee58d62016-04-07 09:54:26 +00001181 DCHECK(!invoke->IsInvokeStaticOrDirect() || !invoke->AsInvokeStaticOrDirect()->IsStringInit());
1182
1183 size_t start_index = 0;
1184 size_t argument_index = 0;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001185 if (invoke->GetInvokeType() != InvokeType::kStatic) { // Instance call.
Aart Bik296fbb42016-06-07 13:49:12 -07001186 uint32_t obj_reg = is_range ? register_index : args[0];
1187 HInstruction* arg = is_unresolved
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001188 ? LoadLocal(obj_reg, DataType::Type::kReference)
Aart Bik296fbb42016-06-07 13:49:12 -07001189 : LoadNullCheckedLocal(obj_reg, invoke->GetDexPc());
David Brazdilc120bbe2016-04-22 16:57:00 +01001190 invoke->SetArgumentAt(0, arg);
David Brazdildee58d62016-04-07 09:54:26 +00001191 start_index = 1;
1192 argument_index = 1;
1193 }
1194
1195 if (!SetupInvokeArguments(invoke,
1196 number_of_vreg_arguments,
1197 args,
1198 register_index,
1199 is_range,
1200 descriptor,
1201 start_index,
1202 &argument_index)) {
1203 return false;
1204 }
1205
1206 if (clinit_check != nullptr) {
1207 // Add the class initialization check as last input of `invoke`.
1208 DCHECK(invoke->IsInvokeStaticOrDirect());
1209 DCHECK(invoke->AsInvokeStaticOrDirect()->GetClinitCheckRequirement()
1210 == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit);
1211 invoke->SetArgumentAt(argument_index, clinit_check);
1212 argument_index++;
1213 }
1214
1215 AppendInstruction(invoke);
1216 latest_result_ = invoke;
1217
1218 return true;
1219}
1220
1221bool HInstructionBuilder::HandleStringInit(HInvoke* invoke,
1222 uint32_t number_of_vreg_arguments,
1223 uint32_t* args,
1224 uint32_t register_index,
1225 bool is_range,
1226 const char* descriptor) {
1227 DCHECK(invoke->IsInvokeStaticOrDirect());
1228 DCHECK(invoke->AsInvokeStaticOrDirect()->IsStringInit());
1229
1230 size_t start_index = 1;
1231 size_t argument_index = 0;
1232 if (!SetupInvokeArguments(invoke,
1233 number_of_vreg_arguments,
1234 args,
1235 register_index,
1236 is_range,
1237 descriptor,
1238 start_index,
1239 &argument_index)) {
1240 return false;
1241 }
1242
1243 AppendInstruction(invoke);
1244
1245 // This is a StringFactory call, not an actual String constructor. Its result
1246 // replaces the empty String pre-allocated by NewInstance.
1247 uint32_t orig_this_reg = is_range ? register_index : args[0];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001248 HInstruction* arg_this = LoadLocal(orig_this_reg, DataType::Type::kReference);
David Brazdildee58d62016-04-07 09:54:26 +00001249
1250 // Replacing the NewInstance might render it redundant. Keep a list of these
1251 // to be visited once it is clear whether it is has remaining uses.
1252 if (arg_this->IsNewInstance()) {
1253 ssa_builder_->AddUninitializedString(arg_this->AsNewInstance());
1254 } else {
1255 DCHECK(arg_this->IsPhi());
1256 // NewInstance is not the direct input of the StringFactory call. It might
1257 // be redundant but optimizing this case is not worth the effort.
1258 }
1259
1260 // Walk over all vregs and replace any occurrence of `arg_this` with `invoke`.
1261 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
1262 if ((*current_locals_)[vreg] == arg_this) {
1263 (*current_locals_)[vreg] = invoke;
1264 }
1265 }
1266
1267 return true;
1268}
1269
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001270static DataType::Type GetFieldAccessType(const DexFile& dex_file, uint16_t field_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001271 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
1272 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001273 return DataType::FromShorty(type[0]);
David Brazdildee58d62016-04-07 09:54:26 +00001274}
1275
1276bool HInstructionBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
1277 uint32_t dex_pc,
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001278 bool is_put,
1279 size_t quicken_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001280 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1281 uint32_t obj_reg = instruction.VRegB_22c();
1282 uint16_t field_index;
1283 if (instruction.IsQuickened()) {
1284 if (!CanDecodeQuickenedInfo()) {
1285 return false;
1286 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001287 field_index = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00001288 } else {
1289 field_index = instruction.VRegC_22c();
1290 }
1291
1292 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001293 ArtField* resolved_field = ResolveField(field_index, /* is_static */ false, is_put);
David Brazdildee58d62016-04-07 09:54:26 +00001294
Aart Bik14154132016-06-02 17:53:58 -07001295 // Generate an explicit null check on the reference, unless the field access
1296 // is unresolved. In that case, we rely on the runtime to perform various
1297 // checks first, followed by a null check.
1298 HInstruction* object = (resolved_field == nullptr)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001299 ? LoadLocal(obj_reg, DataType::Type::kReference)
Aart Bik14154132016-06-02 17:53:58 -07001300 : LoadNullCheckedLocal(obj_reg, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001301
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001302 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001303 if (is_put) {
1304 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1305 HInstruction* field_set = nullptr;
1306 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001307 MaybeRecordStat(compilation_stats_,
1308 MethodCompilationStat::kUnresolvedField);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001309 field_set = new (allocator_) HUnresolvedInstanceFieldSet(object,
1310 value,
1311 field_type,
1312 field_index,
1313 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001314 } else {
1315 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Vladimir Markoca6fff82017-10-03 14:49:14 +01001316 field_set = new (allocator_) HInstanceFieldSet(object,
1317 value,
1318 resolved_field,
1319 field_type,
1320 resolved_field->GetOffset(),
1321 resolved_field->IsVolatile(),
1322 field_index,
1323 class_def_index,
1324 *dex_file_,
1325 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001326 }
1327 AppendInstruction(field_set);
1328 } else {
1329 HInstruction* field_get = nullptr;
1330 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001331 MaybeRecordStat(compilation_stats_,
1332 MethodCompilationStat::kUnresolvedField);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001333 field_get = new (allocator_) HUnresolvedInstanceFieldGet(object,
1334 field_type,
1335 field_index,
1336 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001337 } else {
1338 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Vladimir Markoca6fff82017-10-03 14:49:14 +01001339 field_get = new (allocator_) HInstanceFieldGet(object,
1340 resolved_field,
1341 field_type,
1342 resolved_field->GetOffset(),
1343 resolved_field->IsVolatile(),
1344 field_index,
1345 class_def_index,
1346 *dex_file_,
1347 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001348 }
1349 AppendInstruction(field_get);
1350 UpdateLocal(source_or_dest_reg, field_get);
1351 }
1352
1353 return true;
1354}
1355
1356static mirror::Class* GetClassFrom(CompilerDriver* driver,
1357 const DexCompilationUnit& compilation_unit) {
1358 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001359 Handle<mirror::ClassLoader> class_loader = compilation_unit.GetClassLoader();
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001360 Handle<mirror::DexCache> dex_cache = compilation_unit.GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00001361
1362 return driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, &compilation_unit);
1363}
1364
1365mirror::Class* HInstructionBuilder::GetOutermostCompilingClass() const {
1366 return GetClassFrom(compiler_driver_, *outer_compilation_unit_);
1367}
1368
1369mirror::Class* HInstructionBuilder::GetCompilingClass() const {
1370 return GetClassFrom(compiler_driver_, *dex_compilation_unit_);
1371}
1372
Andreas Gampea5b09a62016-11-17 15:21:22 -08001373bool HInstructionBuilder::IsOutermostCompilingClass(dex::TypeIndex type_index) const {
David Brazdildee58d62016-04-07 09:54:26 +00001374 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001375 StackHandleScope<2> hs(soa.Self());
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001376 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001377 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
David Brazdildee58d62016-04-07 09:54:26 +00001378 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
1379 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
1380 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
1381
1382 // GetOutermostCompilingClass returns null when the class is unresolved
1383 // (e.g. if it derives from an unresolved class). This is bogus knowing that
1384 // we are compiling it.
1385 // When this happens we cannot establish a direct relation between the current
1386 // class and the outer class, so we return false.
1387 // (Note that this is only used for optimizing invokes and field accesses)
Andreas Gampefa4333d2017-02-14 11:10:34 -08001388 return (cls != nullptr) && (outer_class.Get() == cls.Get());
David Brazdildee58d62016-04-07 09:54:26 +00001389}
1390
1391void HInstructionBuilder::BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001392 uint32_t dex_pc,
1393 bool is_put,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001394 DataType::Type field_type) {
David Brazdildee58d62016-04-07 09:54:26 +00001395 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1396 uint16_t field_index = instruction.VRegB_21c();
1397
1398 if (is_put) {
1399 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1400 AppendInstruction(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001401 new (allocator_) HUnresolvedStaticFieldSet(value, field_type, field_index, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001402 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001403 AppendInstruction(new (allocator_) HUnresolvedStaticFieldGet(field_type, field_index, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001404 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1405 }
1406}
1407
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001408ArtField* HInstructionBuilder::ResolveField(uint16_t field_idx, bool is_static, bool is_put) {
1409 ScopedObjectAccess soa(Thread::Current());
1410 StackHandleScope<2> hs(soa.Self());
1411
1412 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001413 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001414 Handle<mirror::Class> compiling_class(hs.NewHandle(GetCompilingClass()));
1415
1416 ArtField* resolved_field = class_linker->ResolveField(*dex_compilation_unit_->GetDexFile(),
1417 field_idx,
1418 dex_compilation_unit_->GetDexCache(),
1419 class_loader,
1420 is_static);
1421
1422 if (UNLIKELY(resolved_field == nullptr)) {
1423 // Clean up any exception left by type resolution.
1424 soa.Self()->ClearException();
1425 return nullptr;
1426 }
1427
1428 // Check static/instance. The class linker has a fast path for looking into the dex cache
1429 // and does not check static/instance if it hits it.
1430 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
1431 return nullptr;
1432 }
1433
1434 // Check access.
Andreas Gampefa4333d2017-02-14 11:10:34 -08001435 if (compiling_class == nullptr) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001436 if (!resolved_field->IsPublic()) {
1437 return nullptr;
1438 }
1439 } else if (!compiling_class->CanAccessResolvedField(resolved_field->GetDeclaringClass(),
1440 resolved_field,
1441 dex_compilation_unit_->GetDexCache().Get(),
1442 field_idx)) {
1443 return nullptr;
1444 }
1445
1446 if (is_put &&
1447 resolved_field->IsFinal() &&
1448 (compiling_class.Get() != resolved_field->GetDeclaringClass())) {
1449 // Final fields can only be updated within their own class.
1450 // TODO: Only allow it in constructors. b/34966607.
1451 return nullptr;
1452 }
1453
1454 return resolved_field;
1455}
1456
David Brazdildee58d62016-04-07 09:54:26 +00001457bool HInstructionBuilder::BuildStaticFieldAccess(const Instruction& instruction,
1458 uint32_t dex_pc,
1459 bool is_put) {
1460 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1461 uint16_t field_index = instruction.VRegB_21c();
1462
1463 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001464 ArtField* resolved_field = ResolveField(field_index, /* is_static */ true, is_put);
David Brazdildee58d62016-04-07 09:54:26 +00001465
1466 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001467 MaybeRecordStat(compilation_stats_,
1468 MethodCompilationStat::kUnresolvedField);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001469 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001470 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
1471 return true;
1472 }
1473
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001474 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001475
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001476 Handle<mirror::Class> klass = handles_->NewHandle(resolved_field->GetDeclaringClass());
1477 HLoadClass* constant = BuildLoadClass(klass->GetDexTypeIndex(),
1478 klass->GetDexFile(),
1479 klass,
1480 dex_pc,
1481 /* needs_access_check */ false);
1482
1483 if (constant == nullptr) {
1484 // The class cannot be referenced from this compiled code. Generate
1485 // an unresolved access.
Igor Murashkin1e065a52017-08-09 13:20:34 -07001486 MaybeRecordStat(compilation_stats_,
1487 MethodCompilationStat::kUnresolvedFieldNotAFastAccess);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001488 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
1489 return true;
David Brazdildee58d62016-04-07 09:54:26 +00001490 }
1491
David Brazdildee58d62016-04-07 09:54:26 +00001492 HInstruction* cls = constant;
David Brazdildee58d62016-04-07 09:54:26 +00001493 if (!IsInitialized(klass)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001494 cls = new (allocator_) HClinitCheck(constant, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001495 AppendInstruction(cls);
1496 }
1497
1498 uint16_t class_def_index = klass->GetDexClassDefIndex();
1499 if (is_put) {
1500 // We need to keep the class alive before loading the value.
1501 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1502 DCHECK_EQ(HPhi::ToPhiType(value->GetType()), HPhi::ToPhiType(field_type));
Vladimir Markoca6fff82017-10-03 14:49:14 +01001503 AppendInstruction(new (allocator_) HStaticFieldSet(cls,
1504 value,
1505 resolved_field,
1506 field_type,
1507 resolved_field->GetOffset(),
1508 resolved_field->IsVolatile(),
1509 field_index,
1510 class_def_index,
1511 *dex_file_,
1512 dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001513 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001514 AppendInstruction(new (allocator_) HStaticFieldGet(cls,
1515 resolved_field,
1516 field_type,
1517 resolved_field->GetOffset(),
1518 resolved_field->IsVolatile(),
1519 field_index,
1520 class_def_index,
1521 *dex_file_,
1522 dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001523 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1524 }
1525 return true;
1526}
1527
1528void HInstructionBuilder::BuildCheckedDivRem(uint16_t out_vreg,
Vladimir Markoca6fff82017-10-03 14:49:14 +01001529 uint16_t first_vreg,
1530 int64_t second_vreg_or_constant,
1531 uint32_t dex_pc,
1532 DataType::Type type,
1533 bool second_is_constant,
1534 bool isDiv) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001535 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
David Brazdildee58d62016-04-07 09:54:26 +00001536
1537 HInstruction* first = LoadLocal(first_vreg, type);
1538 HInstruction* second = nullptr;
1539 if (second_is_constant) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001540 if (type == DataType::Type::kInt32) {
David Brazdildee58d62016-04-07 09:54:26 +00001541 second = graph_->GetIntConstant(second_vreg_or_constant, dex_pc);
1542 } else {
1543 second = graph_->GetLongConstant(second_vreg_or_constant, dex_pc);
1544 }
1545 } else {
1546 second = LoadLocal(second_vreg_or_constant, type);
1547 }
1548
1549 if (!second_is_constant
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001550 || (type == DataType::Type::kInt32 && second->AsIntConstant()->GetValue() == 0)
1551 || (type == DataType::Type::kInt64 && second->AsLongConstant()->GetValue() == 0)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001552 second = new (allocator_) HDivZeroCheck(second, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001553 AppendInstruction(second);
1554 }
1555
1556 if (isDiv) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001557 AppendInstruction(new (allocator_) HDiv(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001558 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001559 AppendInstruction(new (allocator_) HRem(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001560 }
1561 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
1562}
1563
1564void HInstructionBuilder::BuildArrayAccess(const Instruction& instruction,
1565 uint32_t dex_pc,
1566 bool is_put,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001567 DataType::Type anticipated_type) {
David Brazdildee58d62016-04-07 09:54:26 +00001568 uint8_t source_or_dest_reg = instruction.VRegA_23x();
1569 uint8_t array_reg = instruction.VRegB_23x();
1570 uint8_t index_reg = instruction.VRegC_23x();
1571
David Brazdilc120bbe2016-04-22 16:57:00 +01001572 HInstruction* object = LoadNullCheckedLocal(array_reg, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001573 HInstruction* length = new (allocator_) HArrayLength(object, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001574 AppendInstruction(length);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001575 HInstruction* index = LoadLocal(index_reg, DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001576 index = new (allocator_) HBoundsCheck(index, length, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001577 AppendInstruction(index);
1578 if (is_put) {
1579 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
1580 // TODO: Insert a type check node if the type is Object.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001581 HArraySet* aset = new (allocator_) HArraySet(object, index, value, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001582 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1583 AppendInstruction(aset);
1584 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001585 HArrayGet* aget = new (allocator_) HArrayGet(object, index, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001586 ssa_builder_->MaybeAddAmbiguousArrayGet(aget);
1587 AppendInstruction(aget);
1588 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1589 }
1590 graph_->SetHasBoundsChecks(true);
1591}
1592
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001593HNewArray* HInstructionBuilder::BuildFilledNewArray(uint32_t dex_pc,
1594 dex::TypeIndex type_index,
1595 uint32_t number_of_vreg_arguments,
1596 bool is_range,
1597 uint32_t* args,
1598 uint32_t register_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001599 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments, dex_pc);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001600 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001601 HNewArray* const object = new (allocator_) HNewArray(cls, length, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001602 AppendInstruction(object);
1603
1604 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1605 DCHECK_EQ(descriptor[0], '[') << descriptor;
1606 char primitive = descriptor[1];
1607 DCHECK(primitive == 'I'
1608 || primitive == 'L'
1609 || primitive == '[') << descriptor;
1610 bool is_reference_array = (primitive == 'L') || (primitive == '[');
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001611 DataType::Type type = is_reference_array ? DataType::Type::kReference : DataType::Type::kInt32;
David Brazdildee58d62016-04-07 09:54:26 +00001612
1613 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
1614 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
1615 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001616 HArraySet* aset = new (allocator_) HArraySet(object, index, value, type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001617 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1618 AppendInstruction(aset);
1619 }
1620 latest_result_ = object;
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001621
1622 return object;
David Brazdildee58d62016-04-07 09:54:26 +00001623}
1624
1625template <typename T>
1626void HInstructionBuilder::BuildFillArrayData(HInstruction* object,
1627 const T* data,
1628 uint32_t element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001629 DataType::Type anticipated_type,
David Brazdildee58d62016-04-07 09:54:26 +00001630 uint32_t dex_pc) {
1631 for (uint32_t i = 0; i < element_count; ++i) {
1632 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1633 HInstruction* value = graph_->GetIntConstant(data[i], dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001634 HArraySet* aset = new (allocator_) HArraySet(object, index, value, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001635 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1636 AppendInstruction(aset);
1637 }
1638}
1639
1640void HInstructionBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
David Brazdilc120bbe2016-04-22 16:57:00 +01001641 HInstruction* array = LoadNullCheckedLocal(instruction.VRegA_31t(), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001642
1643 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
1644 const Instruction::ArrayDataPayload* payload =
1645 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_item_.insns_ + payload_offset);
1646 const uint8_t* data = payload->data;
1647 uint32_t element_count = payload->element_count;
1648
Vladimir Markoc69fba22016-09-06 16:49:15 +01001649 if (element_count == 0u) {
1650 // For empty payload we emit only the null check above.
1651 return;
1652 }
1653
Vladimir Markoca6fff82017-10-03 14:49:14 +01001654 HInstruction* length = new (allocator_) HArrayLength(array, dex_pc);
Vladimir Markoc69fba22016-09-06 16:49:15 +01001655 AppendInstruction(length);
1656
David Brazdildee58d62016-04-07 09:54:26 +00001657 // Implementation of this DEX instruction seems to be that the bounds check is
1658 // done before doing any stores.
1659 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001660 AppendInstruction(new (allocator_) HBoundsCheck(last_index, length, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001661
1662 switch (payload->element_width) {
1663 case 1:
David Brazdilc120bbe2016-04-22 16:57:00 +01001664 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001665 reinterpret_cast<const int8_t*>(data),
1666 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001667 DataType::Type::kInt8,
David Brazdildee58d62016-04-07 09:54:26 +00001668 dex_pc);
1669 break;
1670 case 2:
David Brazdilc120bbe2016-04-22 16:57:00 +01001671 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001672 reinterpret_cast<const int16_t*>(data),
1673 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001674 DataType::Type::kInt16,
David Brazdildee58d62016-04-07 09:54:26 +00001675 dex_pc);
1676 break;
1677 case 4:
David Brazdilc120bbe2016-04-22 16:57:00 +01001678 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001679 reinterpret_cast<const int32_t*>(data),
1680 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001681 DataType::Type::kInt32,
David Brazdildee58d62016-04-07 09:54:26 +00001682 dex_pc);
1683 break;
1684 case 8:
David Brazdilc120bbe2016-04-22 16:57:00 +01001685 BuildFillWideArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001686 reinterpret_cast<const int64_t*>(data),
1687 element_count,
1688 dex_pc);
1689 break;
1690 default:
1691 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1692 }
1693 graph_->SetHasBoundsChecks(true);
1694}
1695
1696void HInstructionBuilder::BuildFillWideArrayData(HInstruction* object,
1697 const int64_t* data,
1698 uint32_t element_count,
1699 uint32_t dex_pc) {
1700 for (uint32_t i = 0; i < element_count; ++i) {
1701 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1702 HInstruction* value = graph_->GetLongConstant(data[i], dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001703 HArraySet* aset =
1704 new (allocator_) HArraySet(object, index, value, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001705 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1706 AppendInstruction(aset);
1707 }
1708}
1709
1710static TypeCheckKind ComputeTypeCheckKind(Handle<mirror::Class> cls)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001711 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001712 if (cls == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +00001713 return TypeCheckKind::kUnresolvedCheck;
1714 } else if (cls->IsInterface()) {
1715 return TypeCheckKind::kInterfaceCheck;
1716 } else if (cls->IsArrayClass()) {
1717 if (cls->GetComponentType()->IsObjectClass()) {
1718 return TypeCheckKind::kArrayObjectCheck;
1719 } else if (cls->CannotBeAssignedFromOtherTypes()) {
1720 return TypeCheckKind::kExactCheck;
1721 } else {
1722 return TypeCheckKind::kArrayCheck;
1723 }
1724 } else if (cls->IsFinal()) {
1725 return TypeCheckKind::kExactCheck;
1726 } else if (cls->IsAbstract()) {
1727 return TypeCheckKind::kAbstractClassCheck;
1728 } else {
1729 return TypeCheckKind::kClassHierarchyCheck;
1730 }
1731}
1732
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001733HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index, uint32_t dex_pc) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001734 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001735 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001736 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00001737 Handle<mirror::Class> klass = handles_->NewHandle(compiler_driver_->ResolveClass(
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001738 soa, dex_compilation_unit_->GetDexCache(), class_loader, type_index, dex_compilation_unit_));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00001739
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001740 bool needs_access_check = true;
Andreas Gampefa4333d2017-02-14 11:10:34 -08001741 if (klass != nullptr) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001742 if (klass->IsPublic()) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001743 needs_access_check = false;
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001744 } else {
1745 mirror::Class* compiling_class = GetCompilingClass();
1746 if (compiling_class != nullptr && compiling_class->CanAccess(klass.Get())) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001747 needs_access_check = false;
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001748 }
1749 }
1750 }
1751
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001752 return BuildLoadClass(type_index, dex_file, klass, dex_pc, needs_access_check);
1753}
1754
1755HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index,
1756 const DexFile& dex_file,
1757 Handle<mirror::Class> klass,
1758 uint32_t dex_pc,
1759 bool needs_access_check) {
1760 // Try to find a reference in the compiling dex file.
1761 const DexFile* actual_dex_file = &dex_file;
1762 if (!IsSameDexFile(dex_file, *dex_compilation_unit_->GetDexFile())) {
1763 dex::TypeIndex local_type_index =
1764 klass->FindTypeIndexInOtherDexFile(*dex_compilation_unit_->GetDexFile());
1765 if (local_type_index.IsValid()) {
1766 type_index = local_type_index;
1767 actual_dex_file = dex_compilation_unit_->GetDexFile();
1768 }
1769 }
1770
1771 // Note: `klass` must be from `handles_`.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001772 HLoadClass* load_class = new (allocator_) HLoadClass(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001773 graph_->GetCurrentMethod(),
1774 type_index,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001775 *actual_dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001776 klass,
Andreas Gampefa4333d2017-02-14 11:10:34 -08001777 klass != nullptr && (klass.Get() == GetOutermostCompilingClass()),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001778 dex_pc,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001779 needs_access_check);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001780
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00001781 HLoadClass::LoadKind load_kind = HSharpening::ComputeLoadClassKind(load_class,
1782 code_generator_,
1783 compiler_driver_,
1784 *dex_compilation_unit_);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001785
1786 if (load_kind == HLoadClass::LoadKind::kInvalid) {
1787 // We actually cannot reference this class, we're forced to bail.
1788 return nullptr;
1789 }
1790 // Append the instruction first, as setting the load kind affects the inputs.
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001791 AppendInstruction(load_class);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001792 load_class->SetLoadKind(load_kind);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001793 return load_class;
1794}
1795
David Brazdildee58d62016-04-07 09:54:26 +00001796void HInstructionBuilder::BuildTypeCheck(const Instruction& instruction,
1797 uint8_t destination,
1798 uint8_t reference,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001799 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +00001800 uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001801 HInstruction* object = LoadLocal(reference, DataType::Type::kReference);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001802 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001803
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001804 ScopedObjectAccess soa(Thread::Current());
1805 TypeCheckKind check_kind = ComputeTypeCheckKind(cls->GetClass());
David Brazdildee58d62016-04-07 09:54:26 +00001806 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001807 AppendInstruction(new (allocator_) HInstanceOf(object, cls, check_kind, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001808 UpdateLocal(destination, current_block_->GetLastInstruction());
1809 } else {
1810 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
1811 // We emit a CheckCast followed by a BoundType. CheckCast is a statement
1812 // which may throw. If it succeeds BoundType sets the new type of `object`
1813 // for all subsequent uses.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001814 AppendInstruction(new (allocator_) HCheckCast(object, cls, check_kind, dex_pc));
1815 AppendInstruction(new (allocator_) HBoundType(object, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001816 UpdateLocal(reference, current_block_->GetLastInstruction());
1817 }
1818}
1819
Vladimir Marko0b66d612017-03-13 14:50:04 +00001820bool HInstructionBuilder::NeedsAccessCheck(dex::TypeIndex type_index, bool* finalizable) const {
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001821 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
1822 LookupReferrerClass(), LookupResolvedType(type_index, *dex_compilation_unit_), finalizable);
David Brazdildee58d62016-04-07 09:54:26 +00001823}
1824
1825bool HInstructionBuilder::CanDecodeQuickenedInfo() const {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001826 return !quicken_info_.IsNull();
David Brazdildee58d62016-04-07 09:54:26 +00001827}
1828
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001829uint16_t HInstructionBuilder::LookupQuickenedInfo(uint32_t quicken_index) {
1830 DCHECK(CanDecodeQuickenedInfo());
1831 return quicken_info_.GetData(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00001832}
1833
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001834bool HInstructionBuilder::ProcessDexInstruction(const Instruction& instruction,
1835 uint32_t dex_pc,
1836 size_t quicken_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001837 switch (instruction.Opcode()) {
1838 case Instruction::CONST_4: {
1839 int32_t register_index = instruction.VRegA();
1840 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n(), dex_pc);
1841 UpdateLocal(register_index, constant);
1842 break;
1843 }
1844
1845 case Instruction::CONST_16: {
1846 int32_t register_index = instruction.VRegA();
1847 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s(), dex_pc);
1848 UpdateLocal(register_index, constant);
1849 break;
1850 }
1851
1852 case Instruction::CONST: {
1853 int32_t register_index = instruction.VRegA();
1854 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i(), dex_pc);
1855 UpdateLocal(register_index, constant);
1856 break;
1857 }
1858
1859 case Instruction::CONST_HIGH16: {
1860 int32_t register_index = instruction.VRegA();
1861 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16, dex_pc);
1862 UpdateLocal(register_index, constant);
1863 break;
1864 }
1865
1866 case Instruction::CONST_WIDE_16: {
1867 int32_t register_index = instruction.VRegA();
1868 // Get 16 bits of constant value, sign extended to 64 bits.
1869 int64_t value = instruction.VRegB_21s();
1870 value <<= 48;
1871 value >>= 48;
1872 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1873 UpdateLocal(register_index, constant);
1874 break;
1875 }
1876
1877 case Instruction::CONST_WIDE_32: {
1878 int32_t register_index = instruction.VRegA();
1879 // Get 32 bits of constant value, sign extended to 64 bits.
1880 int64_t value = instruction.VRegB_31i();
1881 value <<= 32;
1882 value >>= 32;
1883 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1884 UpdateLocal(register_index, constant);
1885 break;
1886 }
1887
1888 case Instruction::CONST_WIDE: {
1889 int32_t register_index = instruction.VRegA();
1890 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l(), dex_pc);
1891 UpdateLocal(register_index, constant);
1892 break;
1893 }
1894
1895 case Instruction::CONST_WIDE_HIGH16: {
1896 int32_t register_index = instruction.VRegA();
1897 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
1898 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1899 UpdateLocal(register_index, constant);
1900 break;
1901 }
1902
1903 // Note that the SSA building will refine the types.
1904 case Instruction::MOVE:
1905 case Instruction::MOVE_FROM16:
1906 case Instruction::MOVE_16: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001907 HInstruction* value = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +00001908 UpdateLocal(instruction.VRegA(), value);
1909 break;
1910 }
1911
1912 // Note that the SSA building will refine the types.
1913 case Instruction::MOVE_WIDE:
1914 case Instruction::MOVE_WIDE_FROM16:
1915 case Instruction::MOVE_WIDE_16: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001916 HInstruction* value = LoadLocal(instruction.VRegB(), DataType::Type::kInt64);
David Brazdildee58d62016-04-07 09:54:26 +00001917 UpdateLocal(instruction.VRegA(), value);
1918 break;
1919 }
1920
1921 case Instruction::MOVE_OBJECT:
1922 case Instruction::MOVE_OBJECT_16:
1923 case Instruction::MOVE_OBJECT_FROM16: {
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01001924 // The verifier has no notion of a null type, so a move-object of constant 0
1925 // will lead to the same constant 0 in the destination register. To mimic
1926 // this behavior, we just pretend we haven't seen a type change (int to reference)
1927 // for the 0 constant and phis. We rely on our type propagation to eventually get the
1928 // types correct.
1929 uint32_t reg_number = instruction.VRegB();
1930 HInstruction* value = (*current_locals_)[reg_number];
1931 if (value->IsIntConstant()) {
1932 DCHECK_EQ(value->AsIntConstant()->GetValue(), 0);
1933 } else if (value->IsPhi()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001934 DCHECK(value->GetType() == DataType::Type::kInt32 ||
1935 value->GetType() == DataType::Type::kReference);
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01001936 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001937 value = LoadLocal(reg_number, DataType::Type::kReference);
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01001938 }
David Brazdildee58d62016-04-07 09:54:26 +00001939 UpdateLocal(instruction.VRegA(), value);
1940 break;
1941 }
1942
1943 case Instruction::RETURN_VOID_NO_BARRIER:
1944 case Instruction::RETURN_VOID: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001945 BuildReturn(instruction, DataType::Type::kVoid, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001946 break;
1947 }
1948
1949#define IF_XX(comparison, cond) \
1950 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
1951 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
1952
1953 IF_XX(HEqual, EQ);
1954 IF_XX(HNotEqual, NE);
1955 IF_XX(HLessThan, LT);
1956 IF_XX(HLessThanOrEqual, LE);
1957 IF_XX(HGreaterThan, GT);
1958 IF_XX(HGreaterThanOrEqual, GE);
1959
1960 case Instruction::GOTO:
1961 case Instruction::GOTO_16:
1962 case Instruction::GOTO_32: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001963 AppendInstruction(new (allocator_) HGoto(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001964 current_block_ = nullptr;
1965 break;
1966 }
1967
1968 case Instruction::RETURN: {
1969 BuildReturn(instruction, return_type_, dex_pc);
1970 break;
1971 }
1972
1973 case Instruction::RETURN_OBJECT: {
1974 BuildReturn(instruction, return_type_, dex_pc);
1975 break;
1976 }
1977
1978 case Instruction::RETURN_WIDE: {
1979 BuildReturn(instruction, return_type_, dex_pc);
1980 break;
1981 }
1982
1983 case Instruction::INVOKE_DIRECT:
1984 case Instruction::INVOKE_INTERFACE:
1985 case Instruction::INVOKE_STATIC:
1986 case Instruction::INVOKE_SUPER:
1987 case Instruction::INVOKE_VIRTUAL:
1988 case Instruction::INVOKE_VIRTUAL_QUICK: {
1989 uint16_t method_idx;
1990 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_QUICK) {
1991 if (!CanDecodeQuickenedInfo()) {
1992 return false;
1993 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001994 method_idx = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00001995 } else {
1996 method_idx = instruction.VRegB_35c();
1997 }
1998 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
1999 uint32_t args[5];
2000 instruction.GetVarArgs(args);
2001 if (!BuildInvoke(instruction, dex_pc, method_idx,
2002 number_of_vreg_arguments, false, args, -1)) {
2003 return false;
2004 }
2005 break;
2006 }
2007
2008 case Instruction::INVOKE_DIRECT_RANGE:
2009 case Instruction::INVOKE_INTERFACE_RANGE:
2010 case Instruction::INVOKE_STATIC_RANGE:
2011 case Instruction::INVOKE_SUPER_RANGE:
2012 case Instruction::INVOKE_VIRTUAL_RANGE:
2013 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2014 uint16_t method_idx;
2015 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK) {
2016 if (!CanDecodeQuickenedInfo()) {
2017 return false;
2018 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002019 method_idx = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00002020 } else {
2021 method_idx = instruction.VRegB_3rc();
2022 }
2023 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
2024 uint32_t register_index = instruction.VRegC();
2025 if (!BuildInvoke(instruction, dex_pc, method_idx,
2026 number_of_vreg_arguments, true, nullptr, register_index)) {
2027 return false;
2028 }
2029 break;
2030 }
2031
Orion Hodsonac141392017-01-13 11:53:47 +00002032 case Instruction::INVOKE_POLYMORPHIC: {
2033 uint16_t method_idx = instruction.VRegB_45cc();
2034 uint16_t proto_idx = instruction.VRegH_45cc();
2035 uint32_t number_of_vreg_arguments = instruction.VRegA_45cc();
2036 uint32_t args[5];
2037 instruction.GetVarArgs(args);
2038 return BuildInvokePolymorphic(instruction,
2039 dex_pc,
2040 method_idx,
2041 proto_idx,
2042 number_of_vreg_arguments,
2043 false,
2044 args,
2045 -1);
2046 }
2047
2048 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
2049 uint16_t method_idx = instruction.VRegB_4rcc();
2050 uint16_t proto_idx = instruction.VRegH_4rcc();
2051 uint32_t number_of_vreg_arguments = instruction.VRegA_4rcc();
2052 uint32_t register_index = instruction.VRegC_4rcc();
2053 return BuildInvokePolymorphic(instruction,
2054 dex_pc,
2055 method_idx,
2056 proto_idx,
2057 number_of_vreg_arguments,
2058 true,
2059 nullptr,
2060 register_index);
2061 }
2062
David Brazdildee58d62016-04-07 09:54:26 +00002063 case Instruction::NEG_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002064 Unop_12x<HNeg>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002065 break;
2066 }
2067
2068 case Instruction::NEG_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002069 Unop_12x<HNeg>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002070 break;
2071 }
2072
2073 case Instruction::NEG_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002074 Unop_12x<HNeg>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002075 break;
2076 }
2077
2078 case Instruction::NEG_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002079 Unop_12x<HNeg>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002080 break;
2081 }
2082
2083 case Instruction::NOT_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002084 Unop_12x<HNot>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002085 break;
2086 }
2087
2088 case Instruction::NOT_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002089 Unop_12x<HNot>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002090 break;
2091 }
2092
2093 case Instruction::INT_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002094 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002095 break;
2096 }
2097
2098 case Instruction::INT_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002099 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002100 break;
2101 }
2102
2103 case Instruction::INT_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002104 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002105 break;
2106 }
2107
2108 case Instruction::LONG_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002109 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002110 break;
2111 }
2112
2113 case Instruction::LONG_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002114 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002115 break;
2116 }
2117
2118 case Instruction::LONG_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002119 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002120 break;
2121 }
2122
2123 case Instruction::FLOAT_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002124 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002125 break;
2126 }
2127
2128 case Instruction::FLOAT_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002129 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002130 break;
2131 }
2132
2133 case Instruction::FLOAT_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002134 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002135 break;
2136 }
2137
2138 case Instruction::DOUBLE_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002139 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002140 break;
2141 }
2142
2143 case Instruction::DOUBLE_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002144 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002145 break;
2146 }
2147
2148 case Instruction::DOUBLE_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002149 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002150 break;
2151 }
2152
2153 case Instruction::INT_TO_BYTE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002154 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt8, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002155 break;
2156 }
2157
2158 case Instruction::INT_TO_SHORT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002159 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt16, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002160 break;
2161 }
2162
2163 case Instruction::INT_TO_CHAR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002164 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kUint16, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002165 break;
2166 }
2167
2168 case Instruction::ADD_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002169 Binop_23x<HAdd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002170 break;
2171 }
2172
2173 case Instruction::ADD_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002174 Binop_23x<HAdd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002175 break;
2176 }
2177
2178 case Instruction::ADD_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002179 Binop_23x<HAdd>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002180 break;
2181 }
2182
2183 case Instruction::ADD_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002184 Binop_23x<HAdd>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002185 break;
2186 }
2187
2188 case Instruction::SUB_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002189 Binop_23x<HSub>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002190 break;
2191 }
2192
2193 case Instruction::SUB_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002194 Binop_23x<HSub>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002195 break;
2196 }
2197
2198 case Instruction::SUB_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002199 Binop_23x<HSub>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002200 break;
2201 }
2202
2203 case Instruction::SUB_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002204 Binop_23x<HSub>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002205 break;
2206 }
2207
2208 case Instruction::ADD_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002209 Binop_12x<HAdd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002210 break;
2211 }
2212
2213 case Instruction::MUL_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002214 Binop_23x<HMul>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002215 break;
2216 }
2217
2218 case Instruction::MUL_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002219 Binop_23x<HMul>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002220 break;
2221 }
2222
2223 case Instruction::MUL_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002224 Binop_23x<HMul>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002225 break;
2226 }
2227
2228 case Instruction::MUL_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002229 Binop_23x<HMul>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002230 break;
2231 }
2232
2233 case Instruction::DIV_INT: {
2234 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002235 dex_pc, DataType::Type::kInt32, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002236 break;
2237 }
2238
2239 case Instruction::DIV_LONG: {
2240 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002241 dex_pc, DataType::Type::kInt64, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002242 break;
2243 }
2244
2245 case Instruction::DIV_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002246 Binop_23x<HDiv>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002247 break;
2248 }
2249
2250 case Instruction::DIV_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002251 Binop_23x<HDiv>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002252 break;
2253 }
2254
2255 case Instruction::REM_INT: {
2256 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002257 dex_pc, DataType::Type::kInt32, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002258 break;
2259 }
2260
2261 case Instruction::REM_LONG: {
2262 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002263 dex_pc, DataType::Type::kInt64, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002264 break;
2265 }
2266
2267 case Instruction::REM_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002268 Binop_23x<HRem>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002269 break;
2270 }
2271
2272 case Instruction::REM_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002273 Binop_23x<HRem>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002274 break;
2275 }
2276
2277 case Instruction::AND_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002278 Binop_23x<HAnd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002279 break;
2280 }
2281
2282 case Instruction::AND_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002283 Binop_23x<HAnd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002284 break;
2285 }
2286
2287 case Instruction::SHL_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002288 Binop_23x_shift<HShl>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002289 break;
2290 }
2291
2292 case Instruction::SHL_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002293 Binop_23x_shift<HShl>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002294 break;
2295 }
2296
2297 case Instruction::SHR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002298 Binop_23x_shift<HShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002299 break;
2300 }
2301
2302 case Instruction::SHR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002303 Binop_23x_shift<HShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002304 break;
2305 }
2306
2307 case Instruction::USHR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002308 Binop_23x_shift<HUShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002309 break;
2310 }
2311
2312 case Instruction::USHR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002313 Binop_23x_shift<HUShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002314 break;
2315 }
2316
2317 case Instruction::OR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002318 Binop_23x<HOr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002319 break;
2320 }
2321
2322 case Instruction::OR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002323 Binop_23x<HOr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002324 break;
2325 }
2326
2327 case Instruction::XOR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002328 Binop_23x<HXor>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002329 break;
2330 }
2331
2332 case Instruction::XOR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002333 Binop_23x<HXor>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002334 break;
2335 }
2336
2337 case Instruction::ADD_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002338 Binop_12x<HAdd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002339 break;
2340 }
2341
2342 case Instruction::ADD_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002343 Binop_12x<HAdd>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002344 break;
2345 }
2346
2347 case Instruction::ADD_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002348 Binop_12x<HAdd>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002349 break;
2350 }
2351
2352 case Instruction::SUB_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002353 Binop_12x<HSub>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002354 break;
2355 }
2356
2357 case Instruction::SUB_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002358 Binop_12x<HSub>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002359 break;
2360 }
2361
2362 case Instruction::SUB_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002363 Binop_12x<HSub>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002364 break;
2365 }
2366
2367 case Instruction::SUB_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002368 Binop_12x<HSub>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002369 break;
2370 }
2371
2372 case Instruction::MUL_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002373 Binop_12x<HMul>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002374 break;
2375 }
2376
2377 case Instruction::MUL_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002378 Binop_12x<HMul>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002379 break;
2380 }
2381
2382 case Instruction::MUL_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002383 Binop_12x<HMul>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002384 break;
2385 }
2386
2387 case Instruction::MUL_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002388 Binop_12x<HMul>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002389 break;
2390 }
2391
2392 case Instruction::DIV_INT_2ADDR: {
2393 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002394 dex_pc, DataType::Type::kInt32, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002395 break;
2396 }
2397
2398 case Instruction::DIV_LONG_2ADDR: {
2399 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002400 dex_pc, DataType::Type::kInt64, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002401 break;
2402 }
2403
2404 case Instruction::REM_INT_2ADDR: {
2405 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002406 dex_pc, DataType::Type::kInt32, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002407 break;
2408 }
2409
2410 case Instruction::REM_LONG_2ADDR: {
2411 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002412 dex_pc, DataType::Type::kInt64, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002413 break;
2414 }
2415
2416 case Instruction::REM_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002417 Binop_12x<HRem>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002418 break;
2419 }
2420
2421 case Instruction::REM_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002422 Binop_12x<HRem>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002423 break;
2424 }
2425
2426 case Instruction::SHL_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002427 Binop_12x_shift<HShl>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002428 break;
2429 }
2430
2431 case Instruction::SHL_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002432 Binop_12x_shift<HShl>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002433 break;
2434 }
2435
2436 case Instruction::SHR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002437 Binop_12x_shift<HShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002438 break;
2439 }
2440
2441 case Instruction::SHR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002442 Binop_12x_shift<HShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002443 break;
2444 }
2445
2446 case Instruction::USHR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002447 Binop_12x_shift<HUShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002448 break;
2449 }
2450
2451 case Instruction::USHR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002452 Binop_12x_shift<HUShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002453 break;
2454 }
2455
2456 case Instruction::DIV_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002457 Binop_12x<HDiv>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002458 break;
2459 }
2460
2461 case Instruction::DIV_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002462 Binop_12x<HDiv>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002463 break;
2464 }
2465
2466 case Instruction::AND_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002467 Binop_12x<HAnd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002468 break;
2469 }
2470
2471 case Instruction::AND_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002472 Binop_12x<HAnd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002473 break;
2474 }
2475
2476 case Instruction::OR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002477 Binop_12x<HOr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002478 break;
2479 }
2480
2481 case Instruction::OR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002482 Binop_12x<HOr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002483 break;
2484 }
2485
2486 case Instruction::XOR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002487 Binop_12x<HXor>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002488 break;
2489 }
2490
2491 case Instruction::XOR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002492 Binop_12x<HXor>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002493 break;
2494 }
2495
2496 case Instruction::ADD_INT_LIT16: {
2497 Binop_22s<HAdd>(instruction, false, dex_pc);
2498 break;
2499 }
2500
2501 case Instruction::AND_INT_LIT16: {
2502 Binop_22s<HAnd>(instruction, false, dex_pc);
2503 break;
2504 }
2505
2506 case Instruction::OR_INT_LIT16: {
2507 Binop_22s<HOr>(instruction, false, dex_pc);
2508 break;
2509 }
2510
2511 case Instruction::XOR_INT_LIT16: {
2512 Binop_22s<HXor>(instruction, false, dex_pc);
2513 break;
2514 }
2515
2516 case Instruction::RSUB_INT: {
2517 Binop_22s<HSub>(instruction, true, dex_pc);
2518 break;
2519 }
2520
2521 case Instruction::MUL_INT_LIT16: {
2522 Binop_22s<HMul>(instruction, false, dex_pc);
2523 break;
2524 }
2525
2526 case Instruction::ADD_INT_LIT8: {
2527 Binop_22b<HAdd>(instruction, false, dex_pc);
2528 break;
2529 }
2530
2531 case Instruction::AND_INT_LIT8: {
2532 Binop_22b<HAnd>(instruction, false, dex_pc);
2533 break;
2534 }
2535
2536 case Instruction::OR_INT_LIT8: {
2537 Binop_22b<HOr>(instruction, false, dex_pc);
2538 break;
2539 }
2540
2541 case Instruction::XOR_INT_LIT8: {
2542 Binop_22b<HXor>(instruction, false, dex_pc);
2543 break;
2544 }
2545
2546 case Instruction::RSUB_INT_LIT8: {
2547 Binop_22b<HSub>(instruction, true, dex_pc);
2548 break;
2549 }
2550
2551 case Instruction::MUL_INT_LIT8: {
2552 Binop_22b<HMul>(instruction, false, dex_pc);
2553 break;
2554 }
2555
2556 case Instruction::DIV_INT_LIT16:
2557 case Instruction::DIV_INT_LIT8: {
2558 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002559 dex_pc, DataType::Type::kInt32, true, true);
David Brazdildee58d62016-04-07 09:54:26 +00002560 break;
2561 }
2562
2563 case Instruction::REM_INT_LIT16:
2564 case Instruction::REM_INT_LIT8: {
2565 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002566 dex_pc, DataType::Type::kInt32, true, false);
David Brazdildee58d62016-04-07 09:54:26 +00002567 break;
2568 }
2569
2570 case Instruction::SHL_INT_LIT8: {
2571 Binop_22b<HShl>(instruction, false, dex_pc);
2572 break;
2573 }
2574
2575 case Instruction::SHR_INT_LIT8: {
2576 Binop_22b<HShr>(instruction, false, dex_pc);
2577 break;
2578 }
2579
2580 case Instruction::USHR_INT_LIT8: {
2581 Binop_22b<HUShr>(instruction, false, dex_pc);
2582 break;
2583 }
2584
2585 case Instruction::NEW_INSTANCE: {
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002586 HNewInstance* new_instance =
2587 BuildNewInstance(dex::TypeIndex(instruction.VRegB_21c()), dex_pc);
2588 DCHECK(new_instance != nullptr);
2589
David Brazdildee58d62016-04-07 09:54:26 +00002590 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002591 BuildConstructorFenceForAllocation(new_instance);
David Brazdildee58d62016-04-07 09:54:26 +00002592 break;
2593 }
2594
2595 case Instruction::NEW_ARRAY: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002596 dex::TypeIndex type_index(instruction.VRegC_22c());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002597 HInstruction* length = LoadLocal(instruction.VRegB_22c(), DataType::Type::kInt32);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002598 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002599
Vladimir Markoca6fff82017-10-03 14:49:14 +01002600 HNewArray* new_array = new (allocator_) HNewArray(cls, length, dex_pc);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002601 AppendInstruction(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002602 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002603 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002604 break;
2605 }
2606
2607 case Instruction::FILLED_NEW_ARRAY: {
2608 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002609 dex::TypeIndex type_index(instruction.VRegB_35c());
David Brazdildee58d62016-04-07 09:54:26 +00002610 uint32_t args[5];
2611 instruction.GetVarArgs(args);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002612 HNewArray* new_array = BuildFilledNewArray(dex_pc,
2613 type_index,
2614 number_of_vreg_arguments,
2615 /* is_range */ false,
2616 args,
2617 /* register_index */ 0);
2618 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002619 break;
2620 }
2621
2622 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2623 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002624 dex::TypeIndex type_index(instruction.VRegB_3rc());
David Brazdildee58d62016-04-07 09:54:26 +00002625 uint32_t register_index = instruction.VRegC_3rc();
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002626 HNewArray* new_array = BuildFilledNewArray(dex_pc,
2627 type_index,
2628 number_of_vreg_arguments,
2629 /* is_range */ true,
2630 /* args*/ nullptr,
2631 register_index);
2632 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002633 break;
2634 }
2635
2636 case Instruction::FILL_ARRAY_DATA: {
2637 BuildFillArrayData(instruction, dex_pc);
2638 break;
2639 }
2640
2641 case Instruction::MOVE_RESULT:
2642 case Instruction::MOVE_RESULT_WIDE:
2643 case Instruction::MOVE_RESULT_OBJECT: {
2644 DCHECK(latest_result_ != nullptr);
2645 UpdateLocal(instruction.VRegA(), latest_result_);
2646 latest_result_ = nullptr;
2647 break;
2648 }
2649
2650 case Instruction::CMP_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002651 Binop_23x_cmp(instruction, DataType::Type::kInt64, ComparisonBias::kNoBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002652 break;
2653 }
2654
2655 case Instruction::CMPG_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002656 Binop_23x_cmp(instruction, DataType::Type::kFloat32, ComparisonBias::kGtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002657 break;
2658 }
2659
2660 case Instruction::CMPG_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002661 Binop_23x_cmp(instruction, DataType::Type::kFloat64, ComparisonBias::kGtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002662 break;
2663 }
2664
2665 case Instruction::CMPL_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002666 Binop_23x_cmp(instruction, DataType::Type::kFloat32, ComparisonBias::kLtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002667 break;
2668 }
2669
2670 case Instruction::CMPL_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002671 Binop_23x_cmp(instruction, DataType::Type::kFloat64, ComparisonBias::kLtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002672 break;
2673 }
2674
2675 case Instruction::NOP:
2676 break;
2677
2678 case Instruction::IGET:
2679 case Instruction::IGET_QUICK:
2680 case Instruction::IGET_WIDE:
2681 case Instruction::IGET_WIDE_QUICK:
2682 case Instruction::IGET_OBJECT:
2683 case Instruction::IGET_OBJECT_QUICK:
2684 case Instruction::IGET_BOOLEAN:
2685 case Instruction::IGET_BOOLEAN_QUICK:
2686 case Instruction::IGET_BYTE:
2687 case Instruction::IGET_BYTE_QUICK:
2688 case Instruction::IGET_CHAR:
2689 case Instruction::IGET_CHAR_QUICK:
2690 case Instruction::IGET_SHORT:
2691 case Instruction::IGET_SHORT_QUICK: {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002692 if (!BuildInstanceFieldAccess(instruction, dex_pc, false, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00002693 return false;
2694 }
2695 break;
2696 }
2697
2698 case Instruction::IPUT:
2699 case Instruction::IPUT_QUICK:
2700 case Instruction::IPUT_WIDE:
2701 case Instruction::IPUT_WIDE_QUICK:
2702 case Instruction::IPUT_OBJECT:
2703 case Instruction::IPUT_OBJECT_QUICK:
2704 case Instruction::IPUT_BOOLEAN:
2705 case Instruction::IPUT_BOOLEAN_QUICK:
2706 case Instruction::IPUT_BYTE:
2707 case Instruction::IPUT_BYTE_QUICK:
2708 case Instruction::IPUT_CHAR:
2709 case Instruction::IPUT_CHAR_QUICK:
2710 case Instruction::IPUT_SHORT:
2711 case Instruction::IPUT_SHORT_QUICK: {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002712 if (!BuildInstanceFieldAccess(instruction, dex_pc, true, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00002713 return false;
2714 }
2715 break;
2716 }
2717
2718 case Instruction::SGET:
2719 case Instruction::SGET_WIDE:
2720 case Instruction::SGET_OBJECT:
2721 case Instruction::SGET_BOOLEAN:
2722 case Instruction::SGET_BYTE:
2723 case Instruction::SGET_CHAR:
2724 case Instruction::SGET_SHORT: {
2725 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
2726 return false;
2727 }
2728 break;
2729 }
2730
2731 case Instruction::SPUT:
2732 case Instruction::SPUT_WIDE:
2733 case Instruction::SPUT_OBJECT:
2734 case Instruction::SPUT_BOOLEAN:
2735 case Instruction::SPUT_BYTE:
2736 case Instruction::SPUT_CHAR:
2737 case Instruction::SPUT_SHORT: {
2738 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
2739 return false;
2740 }
2741 break;
2742 }
2743
2744#define ARRAY_XX(kind, anticipated_type) \
2745 case Instruction::AGET##kind: { \
2746 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
2747 break; \
2748 } \
2749 case Instruction::APUT##kind: { \
2750 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
2751 break; \
2752 }
2753
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002754 ARRAY_XX(, DataType::Type::kInt32);
2755 ARRAY_XX(_WIDE, DataType::Type::kInt64);
2756 ARRAY_XX(_OBJECT, DataType::Type::kReference);
2757 ARRAY_XX(_BOOLEAN, DataType::Type::kBool);
2758 ARRAY_XX(_BYTE, DataType::Type::kInt8);
2759 ARRAY_XX(_CHAR, DataType::Type::kUint16);
2760 ARRAY_XX(_SHORT, DataType::Type::kInt16);
David Brazdildee58d62016-04-07 09:54:26 +00002761
2762 case Instruction::ARRAY_LENGTH: {
David Brazdilc120bbe2016-04-22 16:57:00 +01002763 HInstruction* object = LoadNullCheckedLocal(instruction.VRegB_12x(), dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002764 AppendInstruction(new (allocator_) HArrayLength(object, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002765 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
2766 break;
2767 }
2768
2769 case Instruction::CONST_STRING: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08002770 dex::StringIndex string_index(instruction.VRegB_21c());
Vladimir Markoca6fff82017-10-03 14:49:14 +01002771 AppendInstruction(new (allocator_) HLoadString(graph_->GetCurrentMethod(),
2772 string_index,
2773 *dex_file_,
2774 dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002775 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2776 break;
2777 }
2778
2779 case Instruction::CONST_STRING_JUMBO: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08002780 dex::StringIndex string_index(instruction.VRegB_31c());
Vladimir Markoca6fff82017-10-03 14:49:14 +01002781 AppendInstruction(new (allocator_) HLoadString(graph_->GetCurrentMethod(),
2782 string_index,
2783 *dex_file_,
2784 dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002785 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
2786 break;
2787 }
2788
2789 case Instruction::CONST_CLASS: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002790 dex::TypeIndex type_index(instruction.VRegB_21c());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002791 BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002792 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2793 break;
2794 }
2795
2796 case Instruction::MOVE_EXCEPTION: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002797 AppendInstruction(new (allocator_) HLoadException(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002798 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
Vladimir Markoca6fff82017-10-03 14:49:14 +01002799 AppendInstruction(new (allocator_) HClearException(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002800 break;
2801 }
2802
2803 case Instruction::THROW: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002804 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002805 AppendInstruction(new (allocator_) HThrow(exception, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002806 // We finished building this block. Set the current block to null to avoid
2807 // adding dead instructions to it.
2808 current_block_ = nullptr;
2809 break;
2810 }
2811
2812 case Instruction::INSTANCE_OF: {
2813 uint8_t destination = instruction.VRegA_22c();
2814 uint8_t reference = instruction.VRegB_22c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002815 dex::TypeIndex type_index(instruction.VRegC_22c());
David Brazdildee58d62016-04-07 09:54:26 +00002816 BuildTypeCheck(instruction, destination, reference, type_index, dex_pc);
2817 break;
2818 }
2819
2820 case Instruction::CHECK_CAST: {
2821 uint8_t reference = instruction.VRegA_21c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002822 dex::TypeIndex type_index(instruction.VRegB_21c());
David Brazdildee58d62016-04-07 09:54:26 +00002823 BuildTypeCheck(instruction, -1, reference, type_index, dex_pc);
2824 break;
2825 }
2826
2827 case Instruction::MONITOR_ENTER: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002828 AppendInstruction(new (allocator_) HMonitorOperation(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002829 LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference),
David Brazdildee58d62016-04-07 09:54:26 +00002830 HMonitorOperation::OperationKind::kEnter,
2831 dex_pc));
2832 break;
2833 }
2834
2835 case Instruction::MONITOR_EXIT: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002836 AppendInstruction(new (allocator_) HMonitorOperation(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002837 LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference),
David Brazdildee58d62016-04-07 09:54:26 +00002838 HMonitorOperation::OperationKind::kExit,
2839 dex_pc));
2840 break;
2841 }
2842
2843 case Instruction::SPARSE_SWITCH:
2844 case Instruction::PACKED_SWITCH: {
2845 BuildSwitch(instruction, dex_pc);
2846 break;
2847 }
2848
2849 default:
2850 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07002851 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00002852 << " because of unhandled instruction "
2853 << instruction.Name();
Igor Murashkin1e065a52017-08-09 13:20:34 -07002854 MaybeRecordStat(compilation_stats_,
2855 MethodCompilationStat::kNotCompiledUnhandledInstruction);
David Brazdildee58d62016-04-07 09:54:26 +00002856 return false;
2857 }
2858 return true;
2859} // NOLINT(readability/fn_size)
2860
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002861ObjPtr<mirror::Class> HInstructionBuilder::LookupResolvedType(
2862 dex::TypeIndex type_index,
2863 const DexCompilationUnit& compilation_unit) const {
2864 return ClassLinker::LookupResolvedType(
2865 type_index, compilation_unit.GetDexCache().Get(), compilation_unit.GetClassLoader().Get());
2866}
2867
2868ObjPtr<mirror::Class> HInstructionBuilder::LookupReferrerClass() const {
2869 // TODO: Cache the result in a Handle<mirror::Class>.
2870 const DexFile::MethodId& method_id =
2871 dex_compilation_unit_->GetDexFile()->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
2872 return LookupResolvedType(method_id.class_idx_, *dex_compilation_unit_);
2873}
2874
David Brazdildee58d62016-04-07 09:54:26 +00002875} // namespace art