blob: 902985e4ee647a1e4971645fa2835448861d68b6 [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();
385 for (const Instruction& inst : instructions) {
386 switch (inst.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 Chartier1d2d4ff2017-09-23 16:11:06 -0700391 locations->ClearBit(inst.GetDexPc(code_item_.insns_));
392 const Instruction* next = inst.Next();
393 if (DexInstructionIterator(next) != instructions.end()) {
David Brazdildee58d62016-04-07 09:54:26 +0000394 locations->SetBit(next->GetDexPc(code_item_.insns_));
395 }
396 break;
397 }
398 default:
399 break;
400 }
401 }
Vladimir Marko69d310e2017-10-09 14:12:23 +0100402 return locations;
David Brazdildee58d62016-04-07 09:54:26 +0000403}
404
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100405HInstruction* HInstructionBuilder::LoadLocal(uint32_t reg_number, DataType::Type type) const {
David Brazdildee58d62016-04-07 09:54:26 +0000406 HInstruction* value = (*current_locals_)[reg_number];
407 DCHECK(value != nullptr);
408
409 // If the operation requests a specific type, we make sure its input is of that type.
410 if (type != value->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100411 if (DataType::IsFloatingPointType(type)) {
Aart Bik31883642016-06-06 15:02:44 -0700412 value = ssa_builder_->GetFloatOrDoubleEquivalent(value, type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100413 } else if (type == DataType::Type::kReference) {
Aart Bik31883642016-06-06 15:02:44 -0700414 value = ssa_builder_->GetReferenceTypeEquivalent(value);
David Brazdildee58d62016-04-07 09:54:26 +0000415 }
Aart Bik31883642016-06-06 15:02:44 -0700416 DCHECK(value != nullptr);
David Brazdildee58d62016-04-07 09:54:26 +0000417 }
418
419 return value;
420}
421
422void HInstructionBuilder::UpdateLocal(uint32_t reg_number, HInstruction* stored_value) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100423 DataType::Type stored_type = stored_value->GetType();
424 DCHECK_NE(stored_type, DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +0000425
426 // Storing into vreg `reg_number` may implicitly invalidate the surrounding
427 // registers. Consider the following cases:
428 // (1) Storing a wide value must overwrite previous values in both `reg_number`
429 // and `reg_number+1`. We store `nullptr` in `reg_number+1`.
430 // (2) If vreg `reg_number-1` holds a wide value, writing into `reg_number`
431 // must invalidate it. We store `nullptr` in `reg_number-1`.
432 // Consequently, storing a wide value into the high vreg of another wide value
433 // will invalidate both `reg_number-1` and `reg_number+1`.
434
435 if (reg_number != 0) {
436 HInstruction* local_low = (*current_locals_)[reg_number - 1];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100437 if (local_low != nullptr && DataType::Is64BitType(local_low->GetType())) {
David Brazdildee58d62016-04-07 09:54:26 +0000438 // The vreg we are storing into was previously the high vreg of a pair.
439 // We need to invalidate its low vreg.
440 DCHECK((*current_locals_)[reg_number] == nullptr);
441 (*current_locals_)[reg_number - 1] = nullptr;
442 }
443 }
444
445 (*current_locals_)[reg_number] = stored_value;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100446 if (DataType::Is64BitType(stored_type)) {
David Brazdildee58d62016-04-07 09:54:26 +0000447 // We are storing a pair. Invalidate the instruction in the high vreg.
448 (*current_locals_)[reg_number + 1] = nullptr;
449 }
450}
451
452void HInstructionBuilder::InitializeParameters() {
453 DCHECK(current_block_->IsEntryBlock());
454
Vladimir Marko69d310e2017-10-09 14:12:23 +0100455 // outer_compilation_unit_ is null only when unit testing.
456 if (outer_compilation_unit_ == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +0000457 return;
458 }
459
460 const char* shorty = dex_compilation_unit_->GetShorty();
461 uint16_t number_of_parameters = graph_->GetNumberOfInVRegs();
462 uint16_t locals_index = graph_->GetNumberOfLocalVRegs();
463 uint16_t parameter_index = 0;
464
465 const DexFile::MethodId& referrer_method_id =
466 dex_file_->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
467 if (!dex_compilation_unit_->IsStatic()) {
468 // Add the implicit 'this' argument, not expressed in the signature.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100469 HParameterValue* parameter = new (allocator_) HParameterValue(*dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +0000470 referrer_method_id.class_idx_,
471 parameter_index++,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100472 DataType::Type::kReference,
Igor Murashkind01745e2017-04-05 16:40:31 -0700473 /* is_this */ true);
David Brazdildee58d62016-04-07 09:54:26 +0000474 AppendInstruction(parameter);
475 UpdateLocal(locals_index++, parameter);
476 number_of_parameters--;
Igor Murashkind01745e2017-04-05 16:40:31 -0700477 current_this_parameter_ = parameter;
478 } else {
479 DCHECK(current_this_parameter_ == nullptr);
David Brazdildee58d62016-04-07 09:54:26 +0000480 }
481
482 const DexFile::ProtoId& proto = dex_file_->GetMethodPrototype(referrer_method_id);
483 const DexFile::TypeList* arg_types = dex_file_->GetProtoParameters(proto);
484 for (int i = 0, shorty_pos = 1; i < number_of_parameters; i++) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100485 HParameterValue* parameter = new (allocator_) HParameterValue(
David Brazdildee58d62016-04-07 09:54:26 +0000486 *dex_file_,
487 arg_types->GetTypeItem(shorty_pos - 1).type_idx_,
488 parameter_index++,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100489 DataType::FromShorty(shorty[shorty_pos]),
Igor Murashkind01745e2017-04-05 16:40:31 -0700490 /* is_this */ false);
David Brazdildee58d62016-04-07 09:54:26 +0000491 ++shorty_pos;
492 AppendInstruction(parameter);
493 // Store the parameter value in the local that the dex code will use
494 // to reference that parameter.
495 UpdateLocal(locals_index++, parameter);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100496 if (DataType::Is64BitType(parameter->GetType())) {
David Brazdildee58d62016-04-07 09:54:26 +0000497 i++;
498 locals_index++;
499 parameter_index++;
500 }
501 }
502}
503
504template<typename T>
505void HInstructionBuilder::If_22t(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100506 HInstruction* first = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
507 HInstruction* second = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100508 T* comparison = new (allocator_) T(first, second, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000509 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100510 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000511 current_block_ = nullptr;
512}
513
514template<typename T>
515void HInstructionBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100516 HInstruction* value = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100517 T* comparison = new (allocator_) T(value, graph_->GetIntConstant(0, dex_pc), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000518 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100519 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000520 current_block_ = nullptr;
521}
522
523template<typename T>
524void HInstructionBuilder::Unop_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100525 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000526 uint32_t dex_pc) {
527 HInstruction* first = LoadLocal(instruction.VRegB(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100528 AppendInstruction(new (allocator_) T(type, first, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000529 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
530}
531
532void HInstructionBuilder::Conversion_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100533 DataType::Type input_type,
534 DataType::Type result_type,
David Brazdildee58d62016-04-07 09:54:26 +0000535 uint32_t dex_pc) {
536 HInstruction* first = LoadLocal(instruction.VRegB(), input_type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100537 AppendInstruction(new (allocator_) HTypeConversion(result_type, first, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000538 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
539}
540
541template<typename T>
542void HInstructionBuilder::Binop_23x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100543 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000544 uint32_t dex_pc) {
545 HInstruction* first = LoadLocal(instruction.VRegB(), type);
546 HInstruction* second = LoadLocal(instruction.VRegC(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100547 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000548 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
549}
550
551template<typename T>
552void HInstructionBuilder::Binop_23x_shift(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100553 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000554 uint32_t dex_pc) {
555 HInstruction* first = LoadLocal(instruction.VRegB(), type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100556 HInstruction* second = LoadLocal(instruction.VRegC(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100557 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000558 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
559}
560
561void HInstructionBuilder::Binop_23x_cmp(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100562 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000563 ComparisonBias bias,
564 uint32_t dex_pc) {
565 HInstruction* first = LoadLocal(instruction.VRegB(), type);
566 HInstruction* second = LoadLocal(instruction.VRegC(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100567 AppendInstruction(new (allocator_) HCompare(type, first, second, bias, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000568 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
569}
570
571template<typename T>
572void HInstructionBuilder::Binop_12x_shift(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100573 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000574 uint32_t dex_pc) {
575 HInstruction* first = LoadLocal(instruction.VRegA(), type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100576 HInstruction* second = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100577 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000578 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
579}
580
581template<typename T>
582void HInstructionBuilder::Binop_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100583 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000584 uint32_t dex_pc) {
585 HInstruction* first = LoadLocal(instruction.VRegA(), type);
586 HInstruction* second = LoadLocal(instruction.VRegB(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100587 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000588 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
589}
590
591template<typename T>
592void HInstructionBuilder::Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100593 HInstruction* first = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000594 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s(), dex_pc);
595 if (reverse) {
596 std::swap(first, second);
597 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100598 AppendInstruction(new (allocator_) T(DataType::Type::kInt32, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000599 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
600}
601
602template<typename T>
603void HInstructionBuilder::Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100604 HInstruction* first = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000605 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b(), dex_pc);
606 if (reverse) {
607 std::swap(first, second);
608 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100609 AppendInstruction(new (allocator_) T(DataType::Type::kInt32, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000610 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
611}
612
Igor Murashkind01745e2017-04-05 16:40:31 -0700613// Does the method being compiled need any constructor barriers being inserted?
614// (Always 'false' for methods that aren't <init>.)
Mathieu Chartierc4ae9162016-04-07 13:19:19 -0700615static bool RequiresConstructorBarrier(const DexCompilationUnit* cu, CompilerDriver* driver) {
Igor Murashkin032cacd2017-04-06 14:40:08 -0700616 // Can be null in unit tests only.
617 if (UNLIKELY(cu == nullptr)) {
618 return false;
619 }
620
David Brazdildee58d62016-04-07 09:54:26 +0000621 Thread* self = Thread::Current();
622 return cu->IsConstructor()
Igor Murashkind01745e2017-04-05 16:40:31 -0700623 && !cu->IsStatic()
624 // RequiresConstructorBarrier must only be queried for <init> methods;
625 // it's effectively "false" for every other method.
626 //
627 // See CompilerDriver::RequiresConstructBarrier for more explanation.
Mathieu Chartierc4ae9162016-04-07 13:19:19 -0700628 && driver->RequiresConstructorBarrier(self, cu->GetDexFile(), cu->GetClassDefIndex());
David Brazdildee58d62016-04-07 09:54:26 +0000629}
630
631// Returns true if `block` has only one successor which starts at the next
632// dex_pc after `instruction` at `dex_pc`.
633static bool IsFallthroughInstruction(const Instruction& instruction,
634 uint32_t dex_pc,
635 HBasicBlock* block) {
636 uint32_t next_dex_pc = dex_pc + instruction.SizeInCodeUnits();
637 return block->GetSingleSuccessor()->GetDexPc() == next_dex_pc;
638}
639
640void HInstructionBuilder::BuildSwitch(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100641 HInstruction* value = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000642 DexSwitchTable table(instruction, dex_pc);
643
644 if (table.GetNumEntries() == 0) {
645 // Empty Switch. Code falls through to the next block.
646 DCHECK(IsFallthroughInstruction(instruction, dex_pc, current_block_));
Vladimir Markoca6fff82017-10-03 14:49:14 +0100647 AppendInstruction(new (allocator_) HGoto(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000648 } else if (table.ShouldBuildDecisionTree()) {
649 for (DexSwitchTableIterator it(table); !it.Done(); it.Advance()) {
650 HInstruction* case_value = graph_->GetIntConstant(it.CurrentKey(), dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100651 HEqual* comparison = new (allocator_) HEqual(value, case_value, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000652 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100653 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000654
655 if (!it.IsLast()) {
656 current_block_ = FindBlockStartingAt(it.GetDexPcForCurrentIndex());
657 }
658 }
659 } else {
660 AppendInstruction(
Vladimir Markoca6fff82017-10-03 14:49:14 +0100661 new (allocator_) HPackedSwitch(table.GetEntryAt(0), table.GetNumEntries(), value, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000662 }
663
664 current_block_ = nullptr;
665}
666
667void HInstructionBuilder::BuildReturn(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100668 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000669 uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100670 if (type == DataType::Type::kVoid) {
Igor Murashkind01745e2017-04-05 16:40:31 -0700671 // Only <init> (which is a return-void) could possibly have a constructor fence.
Igor Murashkin032cacd2017-04-06 14:40:08 -0700672 // This may insert additional redundant constructor fences from the super constructors.
673 // TODO: remove redundant constructor fences (b/36656456).
674 if (RequiresConstructorBarrier(dex_compilation_unit_, compiler_driver_)) {
Igor Murashkind01745e2017-04-05 16:40:31 -0700675 // Compiling instance constructor.
Vladimir Markoba118822017-06-12 15:41:56 +0100676 DCHECK_STREQ("<init>", graph_->GetMethodName());
Igor Murashkind01745e2017-04-05 16:40:31 -0700677
678 HInstruction* fence_target = current_this_parameter_;
679 DCHECK(fence_target != nullptr);
680
Vladimir Markoca6fff82017-10-03 14:49:14 +0100681 AppendInstruction(new (allocator_) HConstructorFence(fence_target, dex_pc, allocator_));
Igor Murashkin6ef45672017-08-08 13:59:55 -0700682 MaybeRecordStat(
683 compilation_stats_,
684 MethodCompilationStat::kConstructorFenceGeneratedFinal);
David Brazdildee58d62016-04-07 09:54:26 +0000685 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100686 AppendInstruction(new (allocator_) HReturnVoid(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000687 } else {
Igor Murashkind01745e2017-04-05 16:40:31 -0700688 DCHECK(!RequiresConstructorBarrier(dex_compilation_unit_, compiler_driver_));
David Brazdildee58d62016-04-07 09:54:26 +0000689 HInstruction* value = LoadLocal(instruction.VRegA(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100690 AppendInstruction(new (allocator_) HReturn(value, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000691 }
692 current_block_ = nullptr;
693}
694
695static InvokeType GetInvokeTypeFromOpCode(Instruction::Code opcode) {
696 switch (opcode) {
697 case Instruction::INVOKE_STATIC:
698 case Instruction::INVOKE_STATIC_RANGE:
699 return kStatic;
700 case Instruction::INVOKE_DIRECT:
701 case Instruction::INVOKE_DIRECT_RANGE:
702 return kDirect;
703 case Instruction::INVOKE_VIRTUAL:
704 case Instruction::INVOKE_VIRTUAL_QUICK:
705 case Instruction::INVOKE_VIRTUAL_RANGE:
706 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
707 return kVirtual;
708 case Instruction::INVOKE_INTERFACE:
709 case Instruction::INVOKE_INTERFACE_RANGE:
710 return kInterface;
711 case Instruction::INVOKE_SUPER_RANGE:
712 case Instruction::INVOKE_SUPER:
713 return kSuper;
714 default:
715 LOG(FATAL) << "Unexpected invoke opcode: " << opcode;
716 UNREACHABLE();
717 }
718}
719
720ArtMethod* HInstructionBuilder::ResolveMethod(uint16_t method_idx, InvokeType invoke_type) {
721 ScopedObjectAccess soa(Thread::Current());
David Brazdildee58d62016-04-07 09:54:26 +0000722
723 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000724 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100725
Vladimir Markoba118822017-06-12 15:41:56 +0100726 ArtMethod* resolved_method =
727 class_linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
728 *dex_compilation_unit_->GetDexFile(),
729 method_idx,
730 dex_compilation_unit_->GetDexCache(),
731 class_loader,
732 graph_->GetArtMethod(),
733 invoke_type);
David Brazdildee58d62016-04-07 09:54:26 +0000734
735 if (UNLIKELY(resolved_method == nullptr)) {
736 // Clean up any exception left by type resolution.
737 soa.Self()->ClearException();
738 return nullptr;
739 }
740
Vladimir Markoba118822017-06-12 15:41:56 +0100741 // The referrer may be unresolved for AOT if we're compiling a class that cannot be
742 // resolved because, for example, we don't find a superclass in the classpath.
743 if (graph_->GetArtMethod() == nullptr) {
744 // The class linker cannot check access without a referrer, so we have to do it.
745 // Fall back to HInvokeUnresolved if the method isn't public.
David Brazdildee58d62016-04-07 09:54:26 +0000746 if (!resolved_method->IsPublic()) {
747 return nullptr;
748 }
David Brazdildee58d62016-04-07 09:54:26 +0000749 }
750
751 // We have to special case the invoke-super case, as ClassLinker::ResolveMethod does not.
752 // We need to look at the referrer's super class vtable. We need to do this to know if we need to
753 // make this an invoke-unresolved to handle cross-dex invokes or abstract super methods, both of
754 // which require runtime handling.
755 if (invoke_type == kSuper) {
Vladimir Markoba118822017-06-12 15:41:56 +0100756 ObjPtr<mirror::Class> compiling_class = GetCompilingClass();
Andreas Gampefa4333d2017-02-14 11:10:34 -0800757 if (compiling_class == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +0000758 // We could not determine the method's class we need to wait until runtime.
759 DCHECK(Runtime::Current()->IsAotCompiler());
760 return nullptr;
761 }
Vladimir Markoba118822017-06-12 15:41:56 +0100762 ObjPtr<mirror::Class> referenced_class = class_linker->LookupResolvedType(
763 *dex_compilation_unit_->GetDexFile(),
764 dex_compilation_unit_->GetDexFile()->GetMethodId(method_idx).class_idx_,
765 dex_compilation_unit_->GetDexCache().Get(),
766 class_loader.Get());
767 DCHECK(referenced_class != nullptr); // We have already resolved a method from this class.
768 if (!referenced_class->IsAssignableFrom(compiling_class)) {
Aart Bikf663e342016-04-04 17:28:59 -0700769 // We cannot statically determine the target method. The runtime will throw a
770 // NoSuchMethodError on this one.
771 return nullptr;
772 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100773 ArtMethod* actual_method;
Vladimir Markoba118822017-06-12 15:41:56 +0100774 if (referenced_class->IsInterface()) {
775 actual_method = referenced_class->FindVirtualMethodForInterfaceSuper(
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100776 resolved_method, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000777 } else {
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100778 uint16_t vtable_index = resolved_method->GetMethodIndex();
779 actual_method = compiling_class->GetSuperClass()->GetVTableEntry(
780 vtable_index, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000781 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100782 if (actual_method != resolved_method &&
783 !IsSameDexFile(*actual_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
784 // The back-end code generator relies on this check in order to ensure that it will not
785 // attempt to read the dex_cache with a dex_method_index that is not from the correct
786 // dex_file. If we didn't do this check then the dex_method_index will not be updated in the
787 // builder, which means that the code-generator (and compiler driver during sharpening and
788 // inliner, maybe) might invoke an incorrect method.
789 // TODO: The actual method could still be referenced in the current dex file, so we
790 // could try locating it.
791 // TODO: Remove the dex_file restriction.
792 return nullptr;
793 }
794 if (!actual_method->IsInvokable()) {
795 // Fail if the actual method cannot be invoked. Otherwise, the runtime resolution stub
796 // could resolve the callee to the wrong method.
797 return nullptr;
798 }
799 resolved_method = actual_method;
David Brazdildee58d62016-04-07 09:54:26 +0000800 }
801
David Brazdildee58d62016-04-07 09:54:26 +0000802 return resolved_method;
803}
804
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100805static bool IsStringConstructor(ArtMethod* method) {
806 ScopedObjectAccess soa(Thread::Current());
807 return method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
808}
809
David Brazdildee58d62016-04-07 09:54:26 +0000810bool HInstructionBuilder::BuildInvoke(const Instruction& instruction,
811 uint32_t dex_pc,
812 uint32_t method_idx,
813 uint32_t number_of_vreg_arguments,
814 bool is_range,
815 uint32_t* args,
816 uint32_t register_index) {
817 InvokeType invoke_type = GetInvokeTypeFromOpCode(instruction.Opcode());
818 const char* descriptor = dex_file_->GetMethodShorty(method_idx);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100819 DataType::Type return_type = DataType::FromShorty(descriptor[0]);
David Brazdildee58d62016-04-07 09:54:26 +0000820
821 // Remove the return type from the 'proto'.
822 size_t number_of_arguments = strlen(descriptor) - 1;
823 if (invoke_type != kStatic) { // instance call
824 // One extra argument for 'this'.
825 number_of_arguments++;
826 }
827
David Brazdildee58d62016-04-07 09:54:26 +0000828 ArtMethod* resolved_method = ResolveMethod(method_idx, invoke_type);
829
830 if (UNLIKELY(resolved_method == nullptr)) {
Igor Murashkin1e065a52017-08-09 13:20:34 -0700831 MaybeRecordStat(compilation_stats_,
832 MethodCompilationStat::kUnresolvedMethod);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100833 HInvoke* invoke = new (allocator_) HInvokeUnresolved(allocator_,
834 number_of_arguments,
835 return_type,
836 dex_pc,
837 method_idx,
838 invoke_type);
David Brazdildee58d62016-04-07 09:54:26 +0000839 return HandleInvoke(invoke,
840 number_of_vreg_arguments,
841 args,
842 register_index,
843 is_range,
844 descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -0700845 nullptr, /* clinit_check */
846 true /* is_unresolved */);
David Brazdildee58d62016-04-07 09:54:26 +0000847 }
848
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100849 // Replace calls to String.<init> with StringFactory.
850 if (IsStringConstructor(resolved_method)) {
851 uint32_t string_init_entry_point = WellKnownClasses::StringInitToEntryPoint(resolved_method);
852 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
853 HInvokeStaticOrDirect::MethodLoadKind::kStringInit,
854 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000855 dchecked_integral_cast<uint64_t>(string_init_entry_point)
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100856 };
857 MethodReference target_method(dex_file_, method_idx);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100858 HInvoke* invoke = new (allocator_) HInvokeStaticOrDirect(
859 allocator_,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100860 number_of_arguments - 1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100861 DataType::Type::kReference /*return_type */,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100862 dex_pc,
863 method_idx,
864 nullptr,
865 dispatch_info,
866 invoke_type,
867 target_method,
868 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit);
869 return HandleStringInit(invoke,
870 number_of_vreg_arguments,
871 args,
872 register_index,
873 is_range,
874 descriptor);
875 }
876
David Brazdildee58d62016-04-07 09:54:26 +0000877 // Potential class initialization check, in the case of a static method call.
878 HClinitCheck* clinit_check = nullptr;
879 HInvoke* invoke = nullptr;
880 if (invoke_type == kDirect || invoke_type == kStatic || invoke_type == kSuper) {
881 // By default, consider that the called method implicitly requires
882 // an initialization check of its declaring method.
883 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement
884 = HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
885 ScopedObjectAccess soa(Thread::Current());
886 if (invoke_type == kStatic) {
887 clinit_check = ProcessClinitCheckForInvoke(
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000888 dex_pc, resolved_method, &clinit_check_requirement);
David Brazdildee58d62016-04-07 09:54:26 +0000889 } else if (invoke_type == kSuper) {
890 if (IsSameDexFile(*resolved_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100891 // Update the method index to the one resolved. Note that this may be a no-op if
David Brazdildee58d62016-04-07 09:54:26 +0000892 // we resolved to the method referenced by the instruction.
893 method_idx = resolved_method->GetDexMethodIndex();
David Brazdildee58d62016-04-07 09:54:26 +0000894 }
895 }
896
897 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
Vladimir Markoe7197bf2017-06-02 17:00:23 +0100898 HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall,
David Brazdildee58d62016-04-07 09:54:26 +0000899 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000900 0u
David Brazdildee58d62016-04-07 09:54:26 +0000901 };
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100902 MethodReference target_method(resolved_method->GetDexFile(),
903 resolved_method->GetDexMethodIndex());
Vladimir Markoca6fff82017-10-03 14:49:14 +0100904 invoke = new (allocator_) HInvokeStaticOrDirect(allocator_,
905 number_of_arguments,
906 return_type,
907 dex_pc,
908 method_idx,
909 resolved_method,
910 dispatch_info,
911 invoke_type,
912 target_method,
913 clinit_check_requirement);
David Brazdildee58d62016-04-07 09:54:26 +0000914 } else if (invoke_type == kVirtual) {
915 ScopedObjectAccess soa(Thread::Current()); // Needed for the method index
Vladimir Markoca6fff82017-10-03 14:49:14 +0100916 invoke = new (allocator_) HInvokeVirtual(allocator_,
917 number_of_arguments,
918 return_type,
919 dex_pc,
920 method_idx,
921 resolved_method,
922 resolved_method->GetMethodIndex());
David Brazdildee58d62016-04-07 09:54:26 +0000923 } else {
924 DCHECK_EQ(invoke_type, kInterface);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100925 ScopedObjectAccess soa(Thread::Current()); // Needed for the IMT index.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100926 invoke = new (allocator_) HInvokeInterface(allocator_,
927 number_of_arguments,
928 return_type,
929 dex_pc,
930 method_idx,
931 resolved_method,
932 ImTable::GetImtIndex(resolved_method));
David Brazdildee58d62016-04-07 09:54:26 +0000933 }
934
935 return HandleInvoke(invoke,
936 number_of_vreg_arguments,
937 args,
938 register_index,
939 is_range,
940 descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -0700941 clinit_check,
942 false /* is_unresolved */);
David Brazdildee58d62016-04-07 09:54:26 +0000943}
944
Orion Hodsonac141392017-01-13 11:53:47 +0000945bool HInstructionBuilder::BuildInvokePolymorphic(const Instruction& instruction ATTRIBUTE_UNUSED,
946 uint32_t dex_pc,
947 uint32_t method_idx,
948 uint32_t proto_idx,
949 uint32_t number_of_vreg_arguments,
950 bool is_range,
951 uint32_t* args,
952 uint32_t register_index) {
953 const char* descriptor = dex_file_->GetShorty(proto_idx);
954 DCHECK_EQ(1 + ArtMethod::NumArgRegisters(descriptor), number_of_vreg_arguments);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100955 DataType::Type return_type = DataType::FromShorty(descriptor[0]);
Orion Hodsonac141392017-01-13 11:53:47 +0000956 size_t number_of_arguments = strlen(descriptor);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100957 HInvoke* invoke = new (allocator_) HInvokePolymorphic(allocator_,
958 number_of_arguments,
959 return_type,
960 dex_pc,
961 method_idx);
Orion Hodsonac141392017-01-13 11:53:47 +0000962 return HandleInvoke(invoke,
963 number_of_vreg_arguments,
964 args,
965 register_index,
966 is_range,
967 descriptor,
968 nullptr /* clinit_check */,
969 false /* is_unresolved */);
970}
971
Igor Murashkin79d8fa72017-04-18 09:37:23 -0700972HNewInstance* HInstructionBuilder::BuildNewInstance(dex::TypeIndex type_index, uint32_t dex_pc) {
Vladimir Marko3cd50df2016-04-13 19:29:26 +0100973 ScopedObjectAccess soa(Thread::Current());
David Brazdildee58d62016-04-07 09:54:26 +0000974
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000975 HLoadClass* load_class = BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000976
David Brazdildee58d62016-04-07 09:54:26 +0000977 HInstruction* cls = load_class;
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000978 Handle<mirror::Class> klass = load_class->GetClass();
979
980 if (!IsInitialized(klass)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100981 cls = new (allocator_) HClinitCheck(load_class, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000982 AppendInstruction(cls);
983 }
984
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000985 // Only the access check entrypoint handles the finalizable class case. If we
986 // need access checks, then we haven't resolved the method and the class may
987 // again be finalizable.
988 QuickEntrypointEnum entrypoint = kQuickAllocObjectInitialized;
989 if (load_class->NeedsAccessCheck() || klass->IsFinalizable() || !klass->IsInstantiable()) {
990 entrypoint = kQuickAllocObjectWithChecks;
991 }
992
993 // Consider classes we haven't resolved as potentially finalizable.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800994 bool finalizable = (klass == nullptr) || klass->IsFinalizable();
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000995
Vladimir Markoca6fff82017-10-03 14:49:14 +0100996 HNewInstance* new_instance = new (allocator_) HNewInstance(
David Brazdildee58d62016-04-07 09:54:26 +0000997 cls,
David Brazdildee58d62016-04-07 09:54:26 +0000998 dex_pc,
999 type_index,
1000 *dex_compilation_unit_->GetDexFile(),
David Brazdildee58d62016-04-07 09:54:26 +00001001 finalizable,
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001002 entrypoint);
1003 AppendInstruction(new_instance);
1004
1005 return new_instance;
1006}
1007
1008void HInstructionBuilder::BuildConstructorFenceForAllocation(HInstruction* allocation) {
1009 DCHECK(allocation != nullptr &&
George Burgess IVf2072992017-05-23 15:36:41 -07001010 (allocation->IsNewInstance() ||
1011 allocation->IsNewArray())); // corresponding to "new" keyword in JLS.
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001012
1013 if (allocation->IsNewInstance()) {
1014 // STRING SPECIAL HANDLING:
1015 // -------------------------------
1016 // Strings have a real HNewInstance node but they end up always having 0 uses.
1017 // All uses of a String HNewInstance are always transformed to replace their input
1018 // of the HNewInstance with an input of the invoke to StringFactory.
1019 //
1020 // Do not emit an HConstructorFence here since it can inhibit some String new-instance
1021 // optimizations (to pass checker tests that rely on those optimizations).
1022 HNewInstance* new_inst = allocation->AsNewInstance();
1023 HLoadClass* load_class = new_inst->GetLoadClass();
1024
1025 Thread* self = Thread::Current();
1026 ScopedObjectAccess soa(self);
1027 StackHandleScope<1> hs(self);
1028 Handle<mirror::Class> klass = load_class->GetClass();
1029 if (klass != nullptr && klass->IsStringClass()) {
1030 return;
1031 // Note: Do not use allocation->IsStringAlloc which requires
1032 // a valid ReferenceTypeInfo, but that doesn't get made until after reference type
1033 // propagation (and instruction builder is too early).
1034 }
1035 // (In terms of correctness, the StringFactory needs to provide its own
1036 // default initialization barrier, see below.)
1037 }
1038
1039 // JLS 17.4.5 "Happens-before Order" describes:
1040 //
1041 // The default initialization of any object happens-before any other actions (other than
1042 // default-writes) of a program.
1043 //
1044 // In our implementation the default initialization of an object to type T means
1045 // setting all of its initial data (object[0..size)) to 0, and setting the
1046 // object's class header (i.e. object.getClass() == T.class).
1047 //
1048 // In practice this fence ensures that the writes to the object header
1049 // are visible to other threads if this object escapes the current thread.
1050 // (and in theory the 0-initializing, but that happens automatically
1051 // when new memory pages are mapped in by the OS).
1052 HConstructorFence* ctor_fence =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001053 new (allocator_) HConstructorFence(allocation, allocation->GetDexPc(), allocator_);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001054 AppendInstruction(ctor_fence);
Igor Murashkin6ef45672017-08-08 13:59:55 -07001055 MaybeRecordStat(
1056 compilation_stats_,
1057 MethodCompilationStat::kConstructorFenceGeneratedNew);
David Brazdildee58d62016-04-07 09:54:26 +00001058}
1059
1060static bool IsSubClass(mirror::Class* to_test, mirror::Class* super_class)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001061 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdildee58d62016-04-07 09:54:26 +00001062 return to_test != nullptr && !to_test->IsInterface() && to_test->IsSubClass(super_class);
1063}
1064
1065bool HInstructionBuilder::IsInitialized(Handle<mirror::Class> cls) const {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001066 if (cls == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +00001067 return false;
1068 }
1069
1070 // `CanAssumeClassIsLoaded` will return true if we're JITting, or will
1071 // check whether the class is in an image for the AOT compilation.
1072 if (cls->IsInitialized() &&
1073 compiler_driver_->CanAssumeClassIsLoaded(cls.Get())) {
1074 return true;
1075 }
1076
1077 if (IsSubClass(GetOutermostCompilingClass(), cls.Get())) {
1078 return true;
1079 }
1080
1081 // TODO: We should walk over the inlined methods, but we don't pass
1082 // that information to the builder.
1083 if (IsSubClass(GetCompilingClass(), cls.Get())) {
1084 return true;
1085 }
1086
1087 return false;
1088}
1089
1090HClinitCheck* HInstructionBuilder::ProcessClinitCheckForInvoke(
1091 uint32_t dex_pc,
1092 ArtMethod* resolved_method,
David Brazdildee58d62016-04-07 09:54:26 +00001093 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001094 Handle<mirror::Class> klass = handles_->NewHandle(resolved_method->GetDeclaringClass());
David Brazdildee58d62016-04-07 09:54:26 +00001095
1096 HClinitCheck* clinit_check = nullptr;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001097 if (IsInitialized(klass)) {
David Brazdildee58d62016-04-07 09:54:26 +00001098 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001099 } else {
1100 HLoadClass* cls = BuildLoadClass(klass->GetDexTypeIndex(),
1101 klass->GetDexFile(),
1102 klass,
1103 dex_pc,
1104 /* needs_access_check */ false);
1105 if (cls != nullptr) {
1106 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
Vladimir Markoca6fff82017-10-03 14:49:14 +01001107 clinit_check = new (allocator_) HClinitCheck(cls, dex_pc);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001108 AppendInstruction(clinit_check);
1109 }
David Brazdildee58d62016-04-07 09:54:26 +00001110 }
1111 return clinit_check;
1112}
1113
1114bool HInstructionBuilder::SetupInvokeArguments(HInvoke* invoke,
1115 uint32_t number_of_vreg_arguments,
1116 uint32_t* args,
1117 uint32_t register_index,
1118 bool is_range,
1119 const char* descriptor,
1120 size_t start_index,
1121 size_t* argument_index) {
1122 uint32_t descriptor_index = 1; // Skip the return type.
1123
1124 for (size_t i = start_index;
1125 // Make sure we don't go over the expected arguments or over the number of
1126 // dex registers given. If the instruction was seen as dead by the verifier,
1127 // it hasn't been properly checked.
1128 (i < number_of_vreg_arguments) && (*argument_index < invoke->GetNumberOfArguments());
1129 i++, (*argument_index)++) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001130 DataType::Type type = DataType::FromShorty(descriptor[descriptor_index++]);
1131 bool is_wide = (type == DataType::Type::kInt64) || (type == DataType::Type::kFloat64);
David Brazdildee58d62016-04-07 09:54:26 +00001132 if (!is_range
1133 && is_wide
1134 && ((i + 1 == number_of_vreg_arguments) || (args[i] + 1 != args[i + 1]))) {
1135 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
1136 // reject any class where this is violated. However, the verifier only does these checks
1137 // on non trivially dead instructions, so we just bailout the compilation.
1138 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001139 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001140 << " because of non-sequential dex register pair in wide argument";
Igor Murashkin1e065a52017-08-09 13:20:34 -07001141 MaybeRecordStat(compilation_stats_,
1142 MethodCompilationStat::kNotCompiledMalformedOpcode);
David Brazdildee58d62016-04-07 09:54:26 +00001143 return false;
1144 }
1145 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
1146 invoke->SetArgumentAt(*argument_index, arg);
1147 if (is_wide) {
1148 i++;
1149 }
1150 }
1151
1152 if (*argument_index != invoke->GetNumberOfArguments()) {
1153 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001154 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001155 << " because of wrong number of arguments in invoke instruction";
Igor Murashkin1e065a52017-08-09 13:20:34 -07001156 MaybeRecordStat(compilation_stats_,
1157 MethodCompilationStat::kNotCompiledMalformedOpcode);
David Brazdildee58d62016-04-07 09:54:26 +00001158 return false;
1159 }
1160
1161 if (invoke->IsInvokeStaticOrDirect() &&
1162 HInvokeStaticOrDirect::NeedsCurrentMethodInput(
1163 invoke->AsInvokeStaticOrDirect()->GetMethodLoadKind())) {
1164 invoke->SetArgumentAt(*argument_index, graph_->GetCurrentMethod());
1165 (*argument_index)++;
1166 }
1167
1168 return true;
1169}
1170
1171bool HInstructionBuilder::HandleInvoke(HInvoke* invoke,
1172 uint32_t number_of_vreg_arguments,
1173 uint32_t* args,
1174 uint32_t register_index,
1175 bool is_range,
1176 const char* descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -07001177 HClinitCheck* clinit_check,
1178 bool is_unresolved) {
David Brazdildee58d62016-04-07 09:54:26 +00001179 DCHECK(!invoke->IsInvokeStaticOrDirect() || !invoke->AsInvokeStaticOrDirect()->IsStringInit());
1180
1181 size_t start_index = 0;
1182 size_t argument_index = 0;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001183 if (invoke->GetInvokeType() != InvokeType::kStatic) { // Instance call.
Aart Bik296fbb42016-06-07 13:49:12 -07001184 uint32_t obj_reg = is_range ? register_index : args[0];
1185 HInstruction* arg = is_unresolved
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001186 ? LoadLocal(obj_reg, DataType::Type::kReference)
Aart Bik296fbb42016-06-07 13:49:12 -07001187 : LoadNullCheckedLocal(obj_reg, invoke->GetDexPc());
David Brazdilc120bbe2016-04-22 16:57:00 +01001188 invoke->SetArgumentAt(0, arg);
David Brazdildee58d62016-04-07 09:54:26 +00001189 start_index = 1;
1190 argument_index = 1;
1191 }
1192
1193 if (!SetupInvokeArguments(invoke,
1194 number_of_vreg_arguments,
1195 args,
1196 register_index,
1197 is_range,
1198 descriptor,
1199 start_index,
1200 &argument_index)) {
1201 return false;
1202 }
1203
1204 if (clinit_check != nullptr) {
1205 // Add the class initialization check as last input of `invoke`.
1206 DCHECK(invoke->IsInvokeStaticOrDirect());
1207 DCHECK(invoke->AsInvokeStaticOrDirect()->GetClinitCheckRequirement()
1208 == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit);
1209 invoke->SetArgumentAt(argument_index, clinit_check);
1210 argument_index++;
1211 }
1212
1213 AppendInstruction(invoke);
1214 latest_result_ = invoke;
1215
1216 return true;
1217}
1218
1219bool HInstructionBuilder::HandleStringInit(HInvoke* invoke,
1220 uint32_t number_of_vreg_arguments,
1221 uint32_t* args,
1222 uint32_t register_index,
1223 bool is_range,
1224 const char* descriptor) {
1225 DCHECK(invoke->IsInvokeStaticOrDirect());
1226 DCHECK(invoke->AsInvokeStaticOrDirect()->IsStringInit());
1227
1228 size_t start_index = 1;
1229 size_t argument_index = 0;
1230 if (!SetupInvokeArguments(invoke,
1231 number_of_vreg_arguments,
1232 args,
1233 register_index,
1234 is_range,
1235 descriptor,
1236 start_index,
1237 &argument_index)) {
1238 return false;
1239 }
1240
1241 AppendInstruction(invoke);
1242
1243 // This is a StringFactory call, not an actual String constructor. Its result
1244 // replaces the empty String pre-allocated by NewInstance.
1245 uint32_t orig_this_reg = is_range ? register_index : args[0];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001246 HInstruction* arg_this = LoadLocal(orig_this_reg, DataType::Type::kReference);
David Brazdildee58d62016-04-07 09:54:26 +00001247
1248 // Replacing the NewInstance might render it redundant. Keep a list of these
1249 // to be visited once it is clear whether it is has remaining uses.
1250 if (arg_this->IsNewInstance()) {
1251 ssa_builder_->AddUninitializedString(arg_this->AsNewInstance());
1252 } else {
1253 DCHECK(arg_this->IsPhi());
1254 // NewInstance is not the direct input of the StringFactory call. It might
1255 // be redundant but optimizing this case is not worth the effort.
1256 }
1257
1258 // Walk over all vregs and replace any occurrence of `arg_this` with `invoke`.
1259 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
1260 if ((*current_locals_)[vreg] == arg_this) {
1261 (*current_locals_)[vreg] = invoke;
1262 }
1263 }
1264
1265 return true;
1266}
1267
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001268static DataType::Type GetFieldAccessType(const DexFile& dex_file, uint16_t field_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001269 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
1270 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001271 return DataType::FromShorty(type[0]);
David Brazdildee58d62016-04-07 09:54:26 +00001272}
1273
1274bool HInstructionBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
1275 uint32_t dex_pc,
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001276 bool is_put,
1277 size_t quicken_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001278 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1279 uint32_t obj_reg = instruction.VRegB_22c();
1280 uint16_t field_index;
1281 if (instruction.IsQuickened()) {
1282 if (!CanDecodeQuickenedInfo()) {
1283 return false;
1284 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001285 field_index = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00001286 } else {
1287 field_index = instruction.VRegC_22c();
1288 }
1289
1290 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001291 ArtField* resolved_field = ResolveField(field_index, /* is_static */ false, is_put);
David Brazdildee58d62016-04-07 09:54:26 +00001292
Aart Bik14154132016-06-02 17:53:58 -07001293 // Generate an explicit null check on the reference, unless the field access
1294 // is unresolved. In that case, we rely on the runtime to perform various
1295 // checks first, followed by a null check.
1296 HInstruction* object = (resolved_field == nullptr)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001297 ? LoadLocal(obj_reg, DataType::Type::kReference)
Aart Bik14154132016-06-02 17:53:58 -07001298 : LoadNullCheckedLocal(obj_reg, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001299
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001300 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001301 if (is_put) {
1302 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1303 HInstruction* field_set = nullptr;
1304 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001305 MaybeRecordStat(compilation_stats_,
1306 MethodCompilationStat::kUnresolvedField);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001307 field_set = new (allocator_) HUnresolvedInstanceFieldSet(object,
1308 value,
1309 field_type,
1310 field_index,
1311 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001312 } else {
1313 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Vladimir Markoca6fff82017-10-03 14:49:14 +01001314 field_set = new (allocator_) HInstanceFieldSet(object,
1315 value,
1316 resolved_field,
1317 field_type,
1318 resolved_field->GetOffset(),
1319 resolved_field->IsVolatile(),
1320 field_index,
1321 class_def_index,
1322 *dex_file_,
1323 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001324 }
1325 AppendInstruction(field_set);
1326 } else {
1327 HInstruction* field_get = nullptr;
1328 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001329 MaybeRecordStat(compilation_stats_,
1330 MethodCompilationStat::kUnresolvedField);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001331 field_get = new (allocator_) HUnresolvedInstanceFieldGet(object,
1332 field_type,
1333 field_index,
1334 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001335 } else {
1336 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Vladimir Markoca6fff82017-10-03 14:49:14 +01001337 field_get = new (allocator_) HInstanceFieldGet(object,
1338 resolved_field,
1339 field_type,
1340 resolved_field->GetOffset(),
1341 resolved_field->IsVolatile(),
1342 field_index,
1343 class_def_index,
1344 *dex_file_,
1345 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001346 }
1347 AppendInstruction(field_get);
1348 UpdateLocal(source_or_dest_reg, field_get);
1349 }
1350
1351 return true;
1352}
1353
1354static mirror::Class* GetClassFrom(CompilerDriver* driver,
1355 const DexCompilationUnit& compilation_unit) {
1356 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001357 Handle<mirror::ClassLoader> class_loader = compilation_unit.GetClassLoader();
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001358 Handle<mirror::DexCache> dex_cache = compilation_unit.GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00001359
1360 return driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, &compilation_unit);
1361}
1362
1363mirror::Class* HInstructionBuilder::GetOutermostCompilingClass() const {
1364 return GetClassFrom(compiler_driver_, *outer_compilation_unit_);
1365}
1366
1367mirror::Class* HInstructionBuilder::GetCompilingClass() const {
1368 return GetClassFrom(compiler_driver_, *dex_compilation_unit_);
1369}
1370
Andreas Gampea5b09a62016-11-17 15:21:22 -08001371bool HInstructionBuilder::IsOutermostCompilingClass(dex::TypeIndex type_index) const {
David Brazdildee58d62016-04-07 09:54:26 +00001372 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001373 StackHandleScope<2> hs(soa.Self());
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001374 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001375 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
David Brazdildee58d62016-04-07 09:54:26 +00001376 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
1377 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
1378 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
1379
1380 // GetOutermostCompilingClass returns null when the class is unresolved
1381 // (e.g. if it derives from an unresolved class). This is bogus knowing that
1382 // we are compiling it.
1383 // When this happens we cannot establish a direct relation between the current
1384 // class and the outer class, so we return false.
1385 // (Note that this is only used for optimizing invokes and field accesses)
Andreas Gampefa4333d2017-02-14 11:10:34 -08001386 return (cls != nullptr) && (outer_class.Get() == cls.Get());
David Brazdildee58d62016-04-07 09:54:26 +00001387}
1388
1389void HInstructionBuilder::BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001390 uint32_t dex_pc,
1391 bool is_put,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001392 DataType::Type field_type) {
David Brazdildee58d62016-04-07 09:54:26 +00001393 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1394 uint16_t field_index = instruction.VRegB_21c();
1395
1396 if (is_put) {
1397 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1398 AppendInstruction(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001399 new (allocator_) HUnresolvedStaticFieldSet(value, field_type, field_index, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001400 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001401 AppendInstruction(new (allocator_) HUnresolvedStaticFieldGet(field_type, field_index, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001402 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1403 }
1404}
1405
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001406ArtField* HInstructionBuilder::ResolveField(uint16_t field_idx, bool is_static, bool is_put) {
1407 ScopedObjectAccess soa(Thread::Current());
1408 StackHandleScope<2> hs(soa.Self());
1409
1410 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001411 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001412 Handle<mirror::Class> compiling_class(hs.NewHandle(GetCompilingClass()));
1413
1414 ArtField* resolved_field = class_linker->ResolveField(*dex_compilation_unit_->GetDexFile(),
1415 field_idx,
1416 dex_compilation_unit_->GetDexCache(),
1417 class_loader,
1418 is_static);
1419
1420 if (UNLIKELY(resolved_field == nullptr)) {
1421 // Clean up any exception left by type resolution.
1422 soa.Self()->ClearException();
1423 return nullptr;
1424 }
1425
1426 // Check static/instance. The class linker has a fast path for looking into the dex cache
1427 // and does not check static/instance if it hits it.
1428 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
1429 return nullptr;
1430 }
1431
1432 // Check access.
Andreas Gampefa4333d2017-02-14 11:10:34 -08001433 if (compiling_class == nullptr) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001434 if (!resolved_field->IsPublic()) {
1435 return nullptr;
1436 }
1437 } else if (!compiling_class->CanAccessResolvedField(resolved_field->GetDeclaringClass(),
1438 resolved_field,
1439 dex_compilation_unit_->GetDexCache().Get(),
1440 field_idx)) {
1441 return nullptr;
1442 }
1443
1444 if (is_put &&
1445 resolved_field->IsFinal() &&
1446 (compiling_class.Get() != resolved_field->GetDeclaringClass())) {
1447 // Final fields can only be updated within their own class.
1448 // TODO: Only allow it in constructors. b/34966607.
1449 return nullptr;
1450 }
1451
1452 return resolved_field;
1453}
1454
David Brazdildee58d62016-04-07 09:54:26 +00001455bool HInstructionBuilder::BuildStaticFieldAccess(const Instruction& instruction,
1456 uint32_t dex_pc,
1457 bool is_put) {
1458 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1459 uint16_t field_index = instruction.VRegB_21c();
1460
1461 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001462 ArtField* resolved_field = ResolveField(field_index, /* is_static */ true, is_put);
David Brazdildee58d62016-04-07 09:54:26 +00001463
1464 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001465 MaybeRecordStat(compilation_stats_,
1466 MethodCompilationStat::kUnresolvedField);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001467 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001468 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
1469 return true;
1470 }
1471
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001472 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001473
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001474 Handle<mirror::Class> klass = handles_->NewHandle(resolved_field->GetDeclaringClass());
1475 HLoadClass* constant = BuildLoadClass(klass->GetDexTypeIndex(),
1476 klass->GetDexFile(),
1477 klass,
1478 dex_pc,
1479 /* needs_access_check */ false);
1480
1481 if (constant == nullptr) {
1482 // The class cannot be referenced from this compiled code. Generate
1483 // an unresolved access.
Igor Murashkin1e065a52017-08-09 13:20:34 -07001484 MaybeRecordStat(compilation_stats_,
1485 MethodCompilationStat::kUnresolvedFieldNotAFastAccess);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001486 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
1487 return true;
David Brazdildee58d62016-04-07 09:54:26 +00001488 }
1489
David Brazdildee58d62016-04-07 09:54:26 +00001490 HInstruction* cls = constant;
David Brazdildee58d62016-04-07 09:54:26 +00001491 if (!IsInitialized(klass)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001492 cls = new (allocator_) HClinitCheck(constant, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001493 AppendInstruction(cls);
1494 }
1495
1496 uint16_t class_def_index = klass->GetDexClassDefIndex();
1497 if (is_put) {
1498 // We need to keep the class alive before loading the value.
1499 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1500 DCHECK_EQ(HPhi::ToPhiType(value->GetType()), HPhi::ToPhiType(field_type));
Vladimir Markoca6fff82017-10-03 14:49:14 +01001501 AppendInstruction(new (allocator_) HStaticFieldSet(cls,
1502 value,
1503 resolved_field,
1504 field_type,
1505 resolved_field->GetOffset(),
1506 resolved_field->IsVolatile(),
1507 field_index,
1508 class_def_index,
1509 *dex_file_,
1510 dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001511 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001512 AppendInstruction(new (allocator_) HStaticFieldGet(cls,
1513 resolved_field,
1514 field_type,
1515 resolved_field->GetOffset(),
1516 resolved_field->IsVolatile(),
1517 field_index,
1518 class_def_index,
1519 *dex_file_,
1520 dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001521 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1522 }
1523 return true;
1524}
1525
1526void HInstructionBuilder::BuildCheckedDivRem(uint16_t out_vreg,
Vladimir Markoca6fff82017-10-03 14:49:14 +01001527 uint16_t first_vreg,
1528 int64_t second_vreg_or_constant,
1529 uint32_t dex_pc,
1530 DataType::Type type,
1531 bool second_is_constant,
1532 bool isDiv) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001533 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
David Brazdildee58d62016-04-07 09:54:26 +00001534
1535 HInstruction* first = LoadLocal(first_vreg, type);
1536 HInstruction* second = nullptr;
1537 if (second_is_constant) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001538 if (type == DataType::Type::kInt32) {
David Brazdildee58d62016-04-07 09:54:26 +00001539 second = graph_->GetIntConstant(second_vreg_or_constant, dex_pc);
1540 } else {
1541 second = graph_->GetLongConstant(second_vreg_or_constant, dex_pc);
1542 }
1543 } else {
1544 second = LoadLocal(second_vreg_or_constant, type);
1545 }
1546
1547 if (!second_is_constant
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001548 || (type == DataType::Type::kInt32 && second->AsIntConstant()->GetValue() == 0)
1549 || (type == DataType::Type::kInt64 && second->AsLongConstant()->GetValue() == 0)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001550 second = new (allocator_) HDivZeroCheck(second, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001551 AppendInstruction(second);
1552 }
1553
1554 if (isDiv) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001555 AppendInstruction(new (allocator_) HDiv(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001556 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001557 AppendInstruction(new (allocator_) HRem(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001558 }
1559 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
1560}
1561
1562void HInstructionBuilder::BuildArrayAccess(const Instruction& instruction,
1563 uint32_t dex_pc,
1564 bool is_put,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001565 DataType::Type anticipated_type) {
David Brazdildee58d62016-04-07 09:54:26 +00001566 uint8_t source_or_dest_reg = instruction.VRegA_23x();
1567 uint8_t array_reg = instruction.VRegB_23x();
1568 uint8_t index_reg = instruction.VRegC_23x();
1569
David Brazdilc120bbe2016-04-22 16:57:00 +01001570 HInstruction* object = LoadNullCheckedLocal(array_reg, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001571 HInstruction* length = new (allocator_) HArrayLength(object, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001572 AppendInstruction(length);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001573 HInstruction* index = LoadLocal(index_reg, DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001574 index = new (allocator_) HBoundsCheck(index, length, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001575 AppendInstruction(index);
1576 if (is_put) {
1577 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
1578 // TODO: Insert a type check node if the type is Object.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001579 HArraySet* aset = new (allocator_) HArraySet(object, index, value, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001580 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1581 AppendInstruction(aset);
1582 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001583 HArrayGet* aget = new (allocator_) HArrayGet(object, index, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001584 ssa_builder_->MaybeAddAmbiguousArrayGet(aget);
1585 AppendInstruction(aget);
1586 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1587 }
1588 graph_->SetHasBoundsChecks(true);
1589}
1590
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001591HNewArray* HInstructionBuilder::BuildFilledNewArray(uint32_t dex_pc,
1592 dex::TypeIndex type_index,
1593 uint32_t number_of_vreg_arguments,
1594 bool is_range,
1595 uint32_t* args,
1596 uint32_t register_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001597 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments, dex_pc);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001598 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001599 HNewArray* const object = new (allocator_) HNewArray(cls, length, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001600 AppendInstruction(object);
1601
1602 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1603 DCHECK_EQ(descriptor[0], '[') << descriptor;
1604 char primitive = descriptor[1];
1605 DCHECK(primitive == 'I'
1606 || primitive == 'L'
1607 || primitive == '[') << descriptor;
1608 bool is_reference_array = (primitive == 'L') || (primitive == '[');
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001609 DataType::Type type = is_reference_array ? DataType::Type::kReference : DataType::Type::kInt32;
David Brazdildee58d62016-04-07 09:54:26 +00001610
1611 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
1612 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
1613 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001614 HArraySet* aset = new (allocator_) HArraySet(object, index, value, type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001615 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1616 AppendInstruction(aset);
1617 }
1618 latest_result_ = object;
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001619
1620 return object;
David Brazdildee58d62016-04-07 09:54:26 +00001621}
1622
1623template <typename T>
1624void HInstructionBuilder::BuildFillArrayData(HInstruction* object,
1625 const T* data,
1626 uint32_t element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001627 DataType::Type anticipated_type,
David Brazdildee58d62016-04-07 09:54:26 +00001628 uint32_t dex_pc) {
1629 for (uint32_t i = 0; i < element_count; ++i) {
1630 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1631 HInstruction* value = graph_->GetIntConstant(data[i], dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001632 HArraySet* aset = new (allocator_) HArraySet(object, index, value, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001633 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1634 AppendInstruction(aset);
1635 }
1636}
1637
1638void HInstructionBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
David Brazdilc120bbe2016-04-22 16:57:00 +01001639 HInstruction* array = LoadNullCheckedLocal(instruction.VRegA_31t(), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001640
1641 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
1642 const Instruction::ArrayDataPayload* payload =
1643 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_item_.insns_ + payload_offset);
1644 const uint8_t* data = payload->data;
1645 uint32_t element_count = payload->element_count;
1646
Vladimir Markoc69fba22016-09-06 16:49:15 +01001647 if (element_count == 0u) {
1648 // For empty payload we emit only the null check above.
1649 return;
1650 }
1651
Vladimir Markoca6fff82017-10-03 14:49:14 +01001652 HInstruction* length = new (allocator_) HArrayLength(array, dex_pc);
Vladimir Markoc69fba22016-09-06 16:49:15 +01001653 AppendInstruction(length);
1654
David Brazdildee58d62016-04-07 09:54:26 +00001655 // Implementation of this DEX instruction seems to be that the bounds check is
1656 // done before doing any stores.
1657 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001658 AppendInstruction(new (allocator_) HBoundsCheck(last_index, length, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001659
1660 switch (payload->element_width) {
1661 case 1:
David Brazdilc120bbe2016-04-22 16:57:00 +01001662 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001663 reinterpret_cast<const int8_t*>(data),
1664 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001665 DataType::Type::kInt8,
David Brazdildee58d62016-04-07 09:54:26 +00001666 dex_pc);
1667 break;
1668 case 2:
David Brazdilc120bbe2016-04-22 16:57:00 +01001669 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001670 reinterpret_cast<const int16_t*>(data),
1671 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001672 DataType::Type::kInt16,
David Brazdildee58d62016-04-07 09:54:26 +00001673 dex_pc);
1674 break;
1675 case 4:
David Brazdilc120bbe2016-04-22 16:57:00 +01001676 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001677 reinterpret_cast<const int32_t*>(data),
1678 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001679 DataType::Type::kInt32,
David Brazdildee58d62016-04-07 09:54:26 +00001680 dex_pc);
1681 break;
1682 case 8:
David Brazdilc120bbe2016-04-22 16:57:00 +01001683 BuildFillWideArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001684 reinterpret_cast<const int64_t*>(data),
1685 element_count,
1686 dex_pc);
1687 break;
1688 default:
1689 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1690 }
1691 graph_->SetHasBoundsChecks(true);
1692}
1693
1694void HInstructionBuilder::BuildFillWideArrayData(HInstruction* object,
1695 const int64_t* data,
1696 uint32_t element_count,
1697 uint32_t dex_pc) {
1698 for (uint32_t i = 0; i < element_count; ++i) {
1699 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1700 HInstruction* value = graph_->GetLongConstant(data[i], dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001701 HArraySet* aset =
1702 new (allocator_) HArraySet(object, index, value, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001703 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1704 AppendInstruction(aset);
1705 }
1706}
1707
1708static TypeCheckKind ComputeTypeCheckKind(Handle<mirror::Class> cls)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001709 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001710 if (cls == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +00001711 return TypeCheckKind::kUnresolvedCheck;
1712 } else if (cls->IsInterface()) {
1713 return TypeCheckKind::kInterfaceCheck;
1714 } else if (cls->IsArrayClass()) {
1715 if (cls->GetComponentType()->IsObjectClass()) {
1716 return TypeCheckKind::kArrayObjectCheck;
1717 } else if (cls->CannotBeAssignedFromOtherTypes()) {
1718 return TypeCheckKind::kExactCheck;
1719 } else {
1720 return TypeCheckKind::kArrayCheck;
1721 }
1722 } else if (cls->IsFinal()) {
1723 return TypeCheckKind::kExactCheck;
1724 } else if (cls->IsAbstract()) {
1725 return TypeCheckKind::kAbstractClassCheck;
1726 } else {
1727 return TypeCheckKind::kClassHierarchyCheck;
1728 }
1729}
1730
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001731HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index, uint32_t dex_pc) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001732 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001733 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001734 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00001735 Handle<mirror::Class> klass = handles_->NewHandle(compiler_driver_->ResolveClass(
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001736 soa, dex_compilation_unit_->GetDexCache(), class_loader, type_index, dex_compilation_unit_));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00001737
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001738 bool needs_access_check = true;
Andreas Gampefa4333d2017-02-14 11:10:34 -08001739 if (klass != nullptr) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001740 if (klass->IsPublic()) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001741 needs_access_check = false;
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001742 } else {
1743 mirror::Class* compiling_class = GetCompilingClass();
1744 if (compiling_class != nullptr && compiling_class->CanAccess(klass.Get())) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001745 needs_access_check = false;
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001746 }
1747 }
1748 }
1749
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001750 return BuildLoadClass(type_index, dex_file, klass, dex_pc, needs_access_check);
1751}
1752
1753HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index,
1754 const DexFile& dex_file,
1755 Handle<mirror::Class> klass,
1756 uint32_t dex_pc,
1757 bool needs_access_check) {
1758 // Try to find a reference in the compiling dex file.
1759 const DexFile* actual_dex_file = &dex_file;
1760 if (!IsSameDexFile(dex_file, *dex_compilation_unit_->GetDexFile())) {
1761 dex::TypeIndex local_type_index =
1762 klass->FindTypeIndexInOtherDexFile(*dex_compilation_unit_->GetDexFile());
1763 if (local_type_index.IsValid()) {
1764 type_index = local_type_index;
1765 actual_dex_file = dex_compilation_unit_->GetDexFile();
1766 }
1767 }
1768
1769 // Note: `klass` must be from `handles_`.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001770 HLoadClass* load_class = new (allocator_) HLoadClass(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001771 graph_->GetCurrentMethod(),
1772 type_index,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001773 *actual_dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001774 klass,
Andreas Gampefa4333d2017-02-14 11:10:34 -08001775 klass != nullptr && (klass.Get() == GetOutermostCompilingClass()),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001776 dex_pc,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001777 needs_access_check);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001778
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00001779 HLoadClass::LoadKind load_kind = HSharpening::ComputeLoadClassKind(load_class,
1780 code_generator_,
1781 compiler_driver_,
1782 *dex_compilation_unit_);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001783
1784 if (load_kind == HLoadClass::LoadKind::kInvalid) {
1785 // We actually cannot reference this class, we're forced to bail.
1786 return nullptr;
1787 }
1788 // Append the instruction first, as setting the load kind affects the inputs.
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001789 AppendInstruction(load_class);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001790 load_class->SetLoadKind(load_kind);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001791 return load_class;
1792}
1793
David Brazdildee58d62016-04-07 09:54:26 +00001794void HInstructionBuilder::BuildTypeCheck(const Instruction& instruction,
1795 uint8_t destination,
1796 uint8_t reference,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001797 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +00001798 uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001799 HInstruction* object = LoadLocal(reference, DataType::Type::kReference);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001800 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001801
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001802 ScopedObjectAccess soa(Thread::Current());
1803 TypeCheckKind check_kind = ComputeTypeCheckKind(cls->GetClass());
David Brazdildee58d62016-04-07 09:54:26 +00001804 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001805 AppendInstruction(new (allocator_) HInstanceOf(object, cls, check_kind, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001806 UpdateLocal(destination, current_block_->GetLastInstruction());
1807 } else {
1808 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
1809 // We emit a CheckCast followed by a BoundType. CheckCast is a statement
1810 // which may throw. If it succeeds BoundType sets the new type of `object`
1811 // for all subsequent uses.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001812 AppendInstruction(new (allocator_) HCheckCast(object, cls, check_kind, dex_pc));
1813 AppendInstruction(new (allocator_) HBoundType(object, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001814 UpdateLocal(reference, current_block_->GetLastInstruction());
1815 }
1816}
1817
Vladimir Marko0b66d612017-03-13 14:50:04 +00001818bool HInstructionBuilder::NeedsAccessCheck(dex::TypeIndex type_index, bool* finalizable) const {
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001819 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
1820 LookupReferrerClass(), LookupResolvedType(type_index, *dex_compilation_unit_), finalizable);
David Brazdildee58d62016-04-07 09:54:26 +00001821}
1822
1823bool HInstructionBuilder::CanDecodeQuickenedInfo() const {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001824 return !quicken_info_.IsNull();
David Brazdildee58d62016-04-07 09:54:26 +00001825}
1826
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001827uint16_t HInstructionBuilder::LookupQuickenedInfo(uint32_t quicken_index) {
1828 DCHECK(CanDecodeQuickenedInfo());
1829 return quicken_info_.GetData(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00001830}
1831
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001832bool HInstructionBuilder::ProcessDexInstruction(const Instruction& instruction,
1833 uint32_t dex_pc,
1834 size_t quicken_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001835 switch (instruction.Opcode()) {
1836 case Instruction::CONST_4: {
1837 int32_t register_index = instruction.VRegA();
1838 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n(), dex_pc);
1839 UpdateLocal(register_index, constant);
1840 break;
1841 }
1842
1843 case Instruction::CONST_16: {
1844 int32_t register_index = instruction.VRegA();
1845 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s(), dex_pc);
1846 UpdateLocal(register_index, constant);
1847 break;
1848 }
1849
1850 case Instruction::CONST: {
1851 int32_t register_index = instruction.VRegA();
1852 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i(), dex_pc);
1853 UpdateLocal(register_index, constant);
1854 break;
1855 }
1856
1857 case Instruction::CONST_HIGH16: {
1858 int32_t register_index = instruction.VRegA();
1859 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16, dex_pc);
1860 UpdateLocal(register_index, constant);
1861 break;
1862 }
1863
1864 case Instruction::CONST_WIDE_16: {
1865 int32_t register_index = instruction.VRegA();
1866 // Get 16 bits of constant value, sign extended to 64 bits.
1867 int64_t value = instruction.VRegB_21s();
1868 value <<= 48;
1869 value >>= 48;
1870 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1871 UpdateLocal(register_index, constant);
1872 break;
1873 }
1874
1875 case Instruction::CONST_WIDE_32: {
1876 int32_t register_index = instruction.VRegA();
1877 // Get 32 bits of constant value, sign extended to 64 bits.
1878 int64_t value = instruction.VRegB_31i();
1879 value <<= 32;
1880 value >>= 32;
1881 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1882 UpdateLocal(register_index, constant);
1883 break;
1884 }
1885
1886 case Instruction::CONST_WIDE: {
1887 int32_t register_index = instruction.VRegA();
1888 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l(), dex_pc);
1889 UpdateLocal(register_index, constant);
1890 break;
1891 }
1892
1893 case Instruction::CONST_WIDE_HIGH16: {
1894 int32_t register_index = instruction.VRegA();
1895 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
1896 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1897 UpdateLocal(register_index, constant);
1898 break;
1899 }
1900
1901 // Note that the SSA building will refine the types.
1902 case Instruction::MOVE:
1903 case Instruction::MOVE_FROM16:
1904 case Instruction::MOVE_16: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001905 HInstruction* value = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +00001906 UpdateLocal(instruction.VRegA(), value);
1907 break;
1908 }
1909
1910 // Note that the SSA building will refine the types.
1911 case Instruction::MOVE_WIDE:
1912 case Instruction::MOVE_WIDE_FROM16:
1913 case Instruction::MOVE_WIDE_16: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001914 HInstruction* value = LoadLocal(instruction.VRegB(), DataType::Type::kInt64);
David Brazdildee58d62016-04-07 09:54:26 +00001915 UpdateLocal(instruction.VRegA(), value);
1916 break;
1917 }
1918
1919 case Instruction::MOVE_OBJECT:
1920 case Instruction::MOVE_OBJECT_16:
1921 case Instruction::MOVE_OBJECT_FROM16: {
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01001922 // The verifier has no notion of a null type, so a move-object of constant 0
1923 // will lead to the same constant 0 in the destination register. To mimic
1924 // this behavior, we just pretend we haven't seen a type change (int to reference)
1925 // for the 0 constant and phis. We rely on our type propagation to eventually get the
1926 // types correct.
1927 uint32_t reg_number = instruction.VRegB();
1928 HInstruction* value = (*current_locals_)[reg_number];
1929 if (value->IsIntConstant()) {
1930 DCHECK_EQ(value->AsIntConstant()->GetValue(), 0);
1931 } else if (value->IsPhi()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001932 DCHECK(value->GetType() == DataType::Type::kInt32 ||
1933 value->GetType() == DataType::Type::kReference);
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01001934 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001935 value = LoadLocal(reg_number, DataType::Type::kReference);
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01001936 }
David Brazdildee58d62016-04-07 09:54:26 +00001937 UpdateLocal(instruction.VRegA(), value);
1938 break;
1939 }
1940
1941 case Instruction::RETURN_VOID_NO_BARRIER:
1942 case Instruction::RETURN_VOID: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001943 BuildReturn(instruction, DataType::Type::kVoid, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001944 break;
1945 }
1946
1947#define IF_XX(comparison, cond) \
1948 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
1949 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
1950
1951 IF_XX(HEqual, EQ);
1952 IF_XX(HNotEqual, NE);
1953 IF_XX(HLessThan, LT);
1954 IF_XX(HLessThanOrEqual, LE);
1955 IF_XX(HGreaterThan, GT);
1956 IF_XX(HGreaterThanOrEqual, GE);
1957
1958 case Instruction::GOTO:
1959 case Instruction::GOTO_16:
1960 case Instruction::GOTO_32: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001961 AppendInstruction(new (allocator_) HGoto(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001962 current_block_ = nullptr;
1963 break;
1964 }
1965
1966 case Instruction::RETURN: {
1967 BuildReturn(instruction, return_type_, dex_pc);
1968 break;
1969 }
1970
1971 case Instruction::RETURN_OBJECT: {
1972 BuildReturn(instruction, return_type_, dex_pc);
1973 break;
1974 }
1975
1976 case Instruction::RETURN_WIDE: {
1977 BuildReturn(instruction, return_type_, dex_pc);
1978 break;
1979 }
1980
1981 case Instruction::INVOKE_DIRECT:
1982 case Instruction::INVOKE_INTERFACE:
1983 case Instruction::INVOKE_STATIC:
1984 case Instruction::INVOKE_SUPER:
1985 case Instruction::INVOKE_VIRTUAL:
1986 case Instruction::INVOKE_VIRTUAL_QUICK: {
1987 uint16_t method_idx;
1988 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_QUICK) {
1989 if (!CanDecodeQuickenedInfo()) {
1990 return false;
1991 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001992 method_idx = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00001993 } else {
1994 method_idx = instruction.VRegB_35c();
1995 }
1996 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
1997 uint32_t args[5];
1998 instruction.GetVarArgs(args);
1999 if (!BuildInvoke(instruction, dex_pc, method_idx,
2000 number_of_vreg_arguments, false, args, -1)) {
2001 return false;
2002 }
2003 break;
2004 }
2005
2006 case Instruction::INVOKE_DIRECT_RANGE:
2007 case Instruction::INVOKE_INTERFACE_RANGE:
2008 case Instruction::INVOKE_STATIC_RANGE:
2009 case Instruction::INVOKE_SUPER_RANGE:
2010 case Instruction::INVOKE_VIRTUAL_RANGE:
2011 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2012 uint16_t method_idx;
2013 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK) {
2014 if (!CanDecodeQuickenedInfo()) {
2015 return false;
2016 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002017 method_idx = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00002018 } else {
2019 method_idx = instruction.VRegB_3rc();
2020 }
2021 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
2022 uint32_t register_index = instruction.VRegC();
2023 if (!BuildInvoke(instruction, dex_pc, method_idx,
2024 number_of_vreg_arguments, true, nullptr, register_index)) {
2025 return false;
2026 }
2027 break;
2028 }
2029
Orion Hodsonac141392017-01-13 11:53:47 +00002030 case Instruction::INVOKE_POLYMORPHIC: {
2031 uint16_t method_idx = instruction.VRegB_45cc();
2032 uint16_t proto_idx = instruction.VRegH_45cc();
2033 uint32_t number_of_vreg_arguments = instruction.VRegA_45cc();
2034 uint32_t args[5];
2035 instruction.GetVarArgs(args);
2036 return BuildInvokePolymorphic(instruction,
2037 dex_pc,
2038 method_idx,
2039 proto_idx,
2040 number_of_vreg_arguments,
2041 false,
2042 args,
2043 -1);
2044 }
2045
2046 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
2047 uint16_t method_idx = instruction.VRegB_4rcc();
2048 uint16_t proto_idx = instruction.VRegH_4rcc();
2049 uint32_t number_of_vreg_arguments = instruction.VRegA_4rcc();
2050 uint32_t register_index = instruction.VRegC_4rcc();
2051 return BuildInvokePolymorphic(instruction,
2052 dex_pc,
2053 method_idx,
2054 proto_idx,
2055 number_of_vreg_arguments,
2056 true,
2057 nullptr,
2058 register_index);
2059 }
2060
David Brazdildee58d62016-04-07 09:54:26 +00002061 case Instruction::NEG_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002062 Unop_12x<HNeg>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002063 break;
2064 }
2065
2066 case Instruction::NEG_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002067 Unop_12x<HNeg>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002068 break;
2069 }
2070
2071 case Instruction::NEG_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002072 Unop_12x<HNeg>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002073 break;
2074 }
2075
2076 case Instruction::NEG_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002077 Unop_12x<HNeg>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002078 break;
2079 }
2080
2081 case Instruction::NOT_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002082 Unop_12x<HNot>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002083 break;
2084 }
2085
2086 case Instruction::NOT_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002087 Unop_12x<HNot>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002088 break;
2089 }
2090
2091 case Instruction::INT_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002092 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002093 break;
2094 }
2095
2096 case Instruction::INT_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002097 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002098 break;
2099 }
2100
2101 case Instruction::INT_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002102 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002103 break;
2104 }
2105
2106 case Instruction::LONG_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002107 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002108 break;
2109 }
2110
2111 case Instruction::LONG_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002112 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002113 break;
2114 }
2115
2116 case Instruction::LONG_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002117 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002118 break;
2119 }
2120
2121 case Instruction::FLOAT_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002122 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002123 break;
2124 }
2125
2126 case Instruction::FLOAT_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002127 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002128 break;
2129 }
2130
2131 case Instruction::FLOAT_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002132 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002133 break;
2134 }
2135
2136 case Instruction::DOUBLE_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002137 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002138 break;
2139 }
2140
2141 case Instruction::DOUBLE_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002142 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002143 break;
2144 }
2145
2146 case Instruction::DOUBLE_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002147 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002148 break;
2149 }
2150
2151 case Instruction::INT_TO_BYTE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002152 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt8, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002153 break;
2154 }
2155
2156 case Instruction::INT_TO_SHORT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002157 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt16, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002158 break;
2159 }
2160
2161 case Instruction::INT_TO_CHAR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002162 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kUint16, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002163 break;
2164 }
2165
2166 case Instruction::ADD_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002167 Binop_23x<HAdd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002168 break;
2169 }
2170
2171 case Instruction::ADD_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002172 Binop_23x<HAdd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002173 break;
2174 }
2175
2176 case Instruction::ADD_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002177 Binop_23x<HAdd>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002178 break;
2179 }
2180
2181 case Instruction::ADD_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002182 Binop_23x<HAdd>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002183 break;
2184 }
2185
2186 case Instruction::SUB_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002187 Binop_23x<HSub>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002188 break;
2189 }
2190
2191 case Instruction::SUB_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002192 Binop_23x<HSub>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002193 break;
2194 }
2195
2196 case Instruction::SUB_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002197 Binop_23x<HSub>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002198 break;
2199 }
2200
2201 case Instruction::SUB_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002202 Binop_23x<HSub>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002203 break;
2204 }
2205
2206 case Instruction::ADD_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002207 Binop_12x<HAdd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002208 break;
2209 }
2210
2211 case Instruction::MUL_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002212 Binop_23x<HMul>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002213 break;
2214 }
2215
2216 case Instruction::MUL_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002217 Binop_23x<HMul>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002218 break;
2219 }
2220
2221 case Instruction::MUL_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002222 Binop_23x<HMul>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002223 break;
2224 }
2225
2226 case Instruction::MUL_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002227 Binop_23x<HMul>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002228 break;
2229 }
2230
2231 case Instruction::DIV_INT: {
2232 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002233 dex_pc, DataType::Type::kInt32, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002234 break;
2235 }
2236
2237 case Instruction::DIV_LONG: {
2238 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002239 dex_pc, DataType::Type::kInt64, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002240 break;
2241 }
2242
2243 case Instruction::DIV_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002244 Binop_23x<HDiv>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002245 break;
2246 }
2247
2248 case Instruction::DIV_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002249 Binop_23x<HDiv>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002250 break;
2251 }
2252
2253 case Instruction::REM_INT: {
2254 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002255 dex_pc, DataType::Type::kInt32, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002256 break;
2257 }
2258
2259 case Instruction::REM_LONG: {
2260 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002261 dex_pc, DataType::Type::kInt64, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002262 break;
2263 }
2264
2265 case Instruction::REM_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002266 Binop_23x<HRem>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002267 break;
2268 }
2269
2270 case Instruction::REM_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002271 Binop_23x<HRem>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002272 break;
2273 }
2274
2275 case Instruction::AND_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002276 Binop_23x<HAnd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002277 break;
2278 }
2279
2280 case Instruction::AND_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002281 Binop_23x<HAnd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002282 break;
2283 }
2284
2285 case Instruction::SHL_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002286 Binop_23x_shift<HShl>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002287 break;
2288 }
2289
2290 case Instruction::SHL_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002291 Binop_23x_shift<HShl>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002292 break;
2293 }
2294
2295 case Instruction::SHR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002296 Binop_23x_shift<HShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002297 break;
2298 }
2299
2300 case Instruction::SHR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002301 Binop_23x_shift<HShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002302 break;
2303 }
2304
2305 case Instruction::USHR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002306 Binop_23x_shift<HUShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002307 break;
2308 }
2309
2310 case Instruction::USHR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002311 Binop_23x_shift<HUShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002312 break;
2313 }
2314
2315 case Instruction::OR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002316 Binop_23x<HOr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002317 break;
2318 }
2319
2320 case Instruction::OR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002321 Binop_23x<HOr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002322 break;
2323 }
2324
2325 case Instruction::XOR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002326 Binop_23x<HXor>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002327 break;
2328 }
2329
2330 case Instruction::XOR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002331 Binop_23x<HXor>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002332 break;
2333 }
2334
2335 case Instruction::ADD_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002336 Binop_12x<HAdd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002337 break;
2338 }
2339
2340 case Instruction::ADD_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002341 Binop_12x<HAdd>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002342 break;
2343 }
2344
2345 case Instruction::ADD_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002346 Binop_12x<HAdd>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002347 break;
2348 }
2349
2350 case Instruction::SUB_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002351 Binop_12x<HSub>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002352 break;
2353 }
2354
2355 case Instruction::SUB_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002356 Binop_12x<HSub>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002357 break;
2358 }
2359
2360 case Instruction::SUB_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002361 Binop_12x<HSub>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002362 break;
2363 }
2364
2365 case Instruction::SUB_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002366 Binop_12x<HSub>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002367 break;
2368 }
2369
2370 case Instruction::MUL_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002371 Binop_12x<HMul>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002372 break;
2373 }
2374
2375 case Instruction::MUL_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002376 Binop_12x<HMul>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002377 break;
2378 }
2379
2380 case Instruction::MUL_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002381 Binop_12x<HMul>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002382 break;
2383 }
2384
2385 case Instruction::MUL_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002386 Binop_12x<HMul>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002387 break;
2388 }
2389
2390 case Instruction::DIV_INT_2ADDR: {
2391 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002392 dex_pc, DataType::Type::kInt32, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002393 break;
2394 }
2395
2396 case Instruction::DIV_LONG_2ADDR: {
2397 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002398 dex_pc, DataType::Type::kInt64, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002399 break;
2400 }
2401
2402 case Instruction::REM_INT_2ADDR: {
2403 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002404 dex_pc, DataType::Type::kInt32, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002405 break;
2406 }
2407
2408 case Instruction::REM_LONG_2ADDR: {
2409 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002410 dex_pc, DataType::Type::kInt64, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002411 break;
2412 }
2413
2414 case Instruction::REM_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002415 Binop_12x<HRem>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002416 break;
2417 }
2418
2419 case Instruction::REM_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002420 Binop_12x<HRem>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002421 break;
2422 }
2423
2424 case Instruction::SHL_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002425 Binop_12x_shift<HShl>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002426 break;
2427 }
2428
2429 case Instruction::SHL_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002430 Binop_12x_shift<HShl>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002431 break;
2432 }
2433
2434 case Instruction::SHR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002435 Binop_12x_shift<HShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002436 break;
2437 }
2438
2439 case Instruction::SHR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002440 Binop_12x_shift<HShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002441 break;
2442 }
2443
2444 case Instruction::USHR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002445 Binop_12x_shift<HUShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002446 break;
2447 }
2448
2449 case Instruction::USHR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002450 Binop_12x_shift<HUShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002451 break;
2452 }
2453
2454 case Instruction::DIV_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002455 Binop_12x<HDiv>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002456 break;
2457 }
2458
2459 case Instruction::DIV_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002460 Binop_12x<HDiv>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002461 break;
2462 }
2463
2464 case Instruction::AND_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002465 Binop_12x<HAnd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002466 break;
2467 }
2468
2469 case Instruction::AND_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002470 Binop_12x<HAnd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002471 break;
2472 }
2473
2474 case Instruction::OR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002475 Binop_12x<HOr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002476 break;
2477 }
2478
2479 case Instruction::OR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002480 Binop_12x<HOr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002481 break;
2482 }
2483
2484 case Instruction::XOR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002485 Binop_12x<HXor>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002486 break;
2487 }
2488
2489 case Instruction::XOR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002490 Binop_12x<HXor>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002491 break;
2492 }
2493
2494 case Instruction::ADD_INT_LIT16: {
2495 Binop_22s<HAdd>(instruction, false, dex_pc);
2496 break;
2497 }
2498
2499 case Instruction::AND_INT_LIT16: {
2500 Binop_22s<HAnd>(instruction, false, dex_pc);
2501 break;
2502 }
2503
2504 case Instruction::OR_INT_LIT16: {
2505 Binop_22s<HOr>(instruction, false, dex_pc);
2506 break;
2507 }
2508
2509 case Instruction::XOR_INT_LIT16: {
2510 Binop_22s<HXor>(instruction, false, dex_pc);
2511 break;
2512 }
2513
2514 case Instruction::RSUB_INT: {
2515 Binop_22s<HSub>(instruction, true, dex_pc);
2516 break;
2517 }
2518
2519 case Instruction::MUL_INT_LIT16: {
2520 Binop_22s<HMul>(instruction, false, dex_pc);
2521 break;
2522 }
2523
2524 case Instruction::ADD_INT_LIT8: {
2525 Binop_22b<HAdd>(instruction, false, dex_pc);
2526 break;
2527 }
2528
2529 case Instruction::AND_INT_LIT8: {
2530 Binop_22b<HAnd>(instruction, false, dex_pc);
2531 break;
2532 }
2533
2534 case Instruction::OR_INT_LIT8: {
2535 Binop_22b<HOr>(instruction, false, dex_pc);
2536 break;
2537 }
2538
2539 case Instruction::XOR_INT_LIT8: {
2540 Binop_22b<HXor>(instruction, false, dex_pc);
2541 break;
2542 }
2543
2544 case Instruction::RSUB_INT_LIT8: {
2545 Binop_22b<HSub>(instruction, true, dex_pc);
2546 break;
2547 }
2548
2549 case Instruction::MUL_INT_LIT8: {
2550 Binop_22b<HMul>(instruction, false, dex_pc);
2551 break;
2552 }
2553
2554 case Instruction::DIV_INT_LIT16:
2555 case Instruction::DIV_INT_LIT8: {
2556 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002557 dex_pc, DataType::Type::kInt32, true, true);
David Brazdildee58d62016-04-07 09:54:26 +00002558 break;
2559 }
2560
2561 case Instruction::REM_INT_LIT16:
2562 case Instruction::REM_INT_LIT8: {
2563 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002564 dex_pc, DataType::Type::kInt32, true, false);
David Brazdildee58d62016-04-07 09:54:26 +00002565 break;
2566 }
2567
2568 case Instruction::SHL_INT_LIT8: {
2569 Binop_22b<HShl>(instruction, false, dex_pc);
2570 break;
2571 }
2572
2573 case Instruction::SHR_INT_LIT8: {
2574 Binop_22b<HShr>(instruction, false, dex_pc);
2575 break;
2576 }
2577
2578 case Instruction::USHR_INT_LIT8: {
2579 Binop_22b<HUShr>(instruction, false, dex_pc);
2580 break;
2581 }
2582
2583 case Instruction::NEW_INSTANCE: {
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002584 HNewInstance* new_instance =
2585 BuildNewInstance(dex::TypeIndex(instruction.VRegB_21c()), dex_pc);
2586 DCHECK(new_instance != nullptr);
2587
David Brazdildee58d62016-04-07 09:54:26 +00002588 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002589 BuildConstructorFenceForAllocation(new_instance);
David Brazdildee58d62016-04-07 09:54:26 +00002590 break;
2591 }
2592
2593 case Instruction::NEW_ARRAY: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002594 dex::TypeIndex type_index(instruction.VRegC_22c());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002595 HInstruction* length = LoadLocal(instruction.VRegB_22c(), DataType::Type::kInt32);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002596 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002597
Vladimir Markoca6fff82017-10-03 14:49:14 +01002598 HNewArray* new_array = new (allocator_) HNewArray(cls, length, dex_pc);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002599 AppendInstruction(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002600 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002601 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002602 break;
2603 }
2604
2605 case Instruction::FILLED_NEW_ARRAY: {
2606 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002607 dex::TypeIndex type_index(instruction.VRegB_35c());
David Brazdildee58d62016-04-07 09:54:26 +00002608 uint32_t args[5];
2609 instruction.GetVarArgs(args);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002610 HNewArray* new_array = BuildFilledNewArray(dex_pc,
2611 type_index,
2612 number_of_vreg_arguments,
2613 /* is_range */ false,
2614 args,
2615 /* register_index */ 0);
2616 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002617 break;
2618 }
2619
2620 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2621 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002622 dex::TypeIndex type_index(instruction.VRegB_3rc());
David Brazdildee58d62016-04-07 09:54:26 +00002623 uint32_t register_index = instruction.VRegC_3rc();
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002624 HNewArray* new_array = BuildFilledNewArray(dex_pc,
2625 type_index,
2626 number_of_vreg_arguments,
2627 /* is_range */ true,
2628 /* args*/ nullptr,
2629 register_index);
2630 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002631 break;
2632 }
2633
2634 case Instruction::FILL_ARRAY_DATA: {
2635 BuildFillArrayData(instruction, dex_pc);
2636 break;
2637 }
2638
2639 case Instruction::MOVE_RESULT:
2640 case Instruction::MOVE_RESULT_WIDE:
2641 case Instruction::MOVE_RESULT_OBJECT: {
2642 DCHECK(latest_result_ != nullptr);
2643 UpdateLocal(instruction.VRegA(), latest_result_);
2644 latest_result_ = nullptr;
2645 break;
2646 }
2647
2648 case Instruction::CMP_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002649 Binop_23x_cmp(instruction, DataType::Type::kInt64, ComparisonBias::kNoBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002650 break;
2651 }
2652
2653 case Instruction::CMPG_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002654 Binop_23x_cmp(instruction, DataType::Type::kFloat32, ComparisonBias::kGtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002655 break;
2656 }
2657
2658 case Instruction::CMPG_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002659 Binop_23x_cmp(instruction, DataType::Type::kFloat64, ComparisonBias::kGtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002660 break;
2661 }
2662
2663 case Instruction::CMPL_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002664 Binop_23x_cmp(instruction, DataType::Type::kFloat32, ComparisonBias::kLtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002665 break;
2666 }
2667
2668 case Instruction::CMPL_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002669 Binop_23x_cmp(instruction, DataType::Type::kFloat64, ComparisonBias::kLtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002670 break;
2671 }
2672
2673 case Instruction::NOP:
2674 break;
2675
2676 case Instruction::IGET:
2677 case Instruction::IGET_QUICK:
2678 case Instruction::IGET_WIDE:
2679 case Instruction::IGET_WIDE_QUICK:
2680 case Instruction::IGET_OBJECT:
2681 case Instruction::IGET_OBJECT_QUICK:
2682 case Instruction::IGET_BOOLEAN:
2683 case Instruction::IGET_BOOLEAN_QUICK:
2684 case Instruction::IGET_BYTE:
2685 case Instruction::IGET_BYTE_QUICK:
2686 case Instruction::IGET_CHAR:
2687 case Instruction::IGET_CHAR_QUICK:
2688 case Instruction::IGET_SHORT:
2689 case Instruction::IGET_SHORT_QUICK: {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002690 if (!BuildInstanceFieldAccess(instruction, dex_pc, false, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00002691 return false;
2692 }
2693 break;
2694 }
2695
2696 case Instruction::IPUT:
2697 case Instruction::IPUT_QUICK:
2698 case Instruction::IPUT_WIDE:
2699 case Instruction::IPUT_WIDE_QUICK:
2700 case Instruction::IPUT_OBJECT:
2701 case Instruction::IPUT_OBJECT_QUICK:
2702 case Instruction::IPUT_BOOLEAN:
2703 case Instruction::IPUT_BOOLEAN_QUICK:
2704 case Instruction::IPUT_BYTE:
2705 case Instruction::IPUT_BYTE_QUICK:
2706 case Instruction::IPUT_CHAR:
2707 case Instruction::IPUT_CHAR_QUICK:
2708 case Instruction::IPUT_SHORT:
2709 case Instruction::IPUT_SHORT_QUICK: {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002710 if (!BuildInstanceFieldAccess(instruction, dex_pc, true, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00002711 return false;
2712 }
2713 break;
2714 }
2715
2716 case Instruction::SGET:
2717 case Instruction::SGET_WIDE:
2718 case Instruction::SGET_OBJECT:
2719 case Instruction::SGET_BOOLEAN:
2720 case Instruction::SGET_BYTE:
2721 case Instruction::SGET_CHAR:
2722 case Instruction::SGET_SHORT: {
2723 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
2724 return false;
2725 }
2726 break;
2727 }
2728
2729 case Instruction::SPUT:
2730 case Instruction::SPUT_WIDE:
2731 case Instruction::SPUT_OBJECT:
2732 case Instruction::SPUT_BOOLEAN:
2733 case Instruction::SPUT_BYTE:
2734 case Instruction::SPUT_CHAR:
2735 case Instruction::SPUT_SHORT: {
2736 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
2737 return false;
2738 }
2739 break;
2740 }
2741
2742#define ARRAY_XX(kind, anticipated_type) \
2743 case Instruction::AGET##kind: { \
2744 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
2745 break; \
2746 } \
2747 case Instruction::APUT##kind: { \
2748 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
2749 break; \
2750 }
2751
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002752 ARRAY_XX(, DataType::Type::kInt32);
2753 ARRAY_XX(_WIDE, DataType::Type::kInt64);
2754 ARRAY_XX(_OBJECT, DataType::Type::kReference);
2755 ARRAY_XX(_BOOLEAN, DataType::Type::kBool);
2756 ARRAY_XX(_BYTE, DataType::Type::kInt8);
2757 ARRAY_XX(_CHAR, DataType::Type::kUint16);
2758 ARRAY_XX(_SHORT, DataType::Type::kInt16);
David Brazdildee58d62016-04-07 09:54:26 +00002759
2760 case Instruction::ARRAY_LENGTH: {
David Brazdilc120bbe2016-04-22 16:57:00 +01002761 HInstruction* object = LoadNullCheckedLocal(instruction.VRegB_12x(), dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002762 AppendInstruction(new (allocator_) HArrayLength(object, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002763 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
2764 break;
2765 }
2766
2767 case Instruction::CONST_STRING: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08002768 dex::StringIndex string_index(instruction.VRegB_21c());
Vladimir Markoca6fff82017-10-03 14:49:14 +01002769 AppendInstruction(new (allocator_) HLoadString(graph_->GetCurrentMethod(),
2770 string_index,
2771 *dex_file_,
2772 dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002773 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2774 break;
2775 }
2776
2777 case Instruction::CONST_STRING_JUMBO: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08002778 dex::StringIndex string_index(instruction.VRegB_31c());
Vladimir Markoca6fff82017-10-03 14:49:14 +01002779 AppendInstruction(new (allocator_) HLoadString(graph_->GetCurrentMethod(),
2780 string_index,
2781 *dex_file_,
2782 dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002783 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
2784 break;
2785 }
2786
2787 case Instruction::CONST_CLASS: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002788 dex::TypeIndex type_index(instruction.VRegB_21c());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002789 BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002790 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2791 break;
2792 }
2793
2794 case Instruction::MOVE_EXCEPTION: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002795 AppendInstruction(new (allocator_) HLoadException(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002796 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
Vladimir Markoca6fff82017-10-03 14:49:14 +01002797 AppendInstruction(new (allocator_) HClearException(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002798 break;
2799 }
2800
2801 case Instruction::THROW: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002802 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002803 AppendInstruction(new (allocator_) HThrow(exception, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002804 // We finished building this block. Set the current block to null to avoid
2805 // adding dead instructions to it.
2806 current_block_ = nullptr;
2807 break;
2808 }
2809
2810 case Instruction::INSTANCE_OF: {
2811 uint8_t destination = instruction.VRegA_22c();
2812 uint8_t reference = instruction.VRegB_22c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002813 dex::TypeIndex type_index(instruction.VRegC_22c());
David Brazdildee58d62016-04-07 09:54:26 +00002814 BuildTypeCheck(instruction, destination, reference, type_index, dex_pc);
2815 break;
2816 }
2817
2818 case Instruction::CHECK_CAST: {
2819 uint8_t reference = instruction.VRegA_21c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002820 dex::TypeIndex type_index(instruction.VRegB_21c());
David Brazdildee58d62016-04-07 09:54:26 +00002821 BuildTypeCheck(instruction, -1, reference, type_index, dex_pc);
2822 break;
2823 }
2824
2825 case Instruction::MONITOR_ENTER: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002826 AppendInstruction(new (allocator_) HMonitorOperation(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002827 LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference),
David Brazdildee58d62016-04-07 09:54:26 +00002828 HMonitorOperation::OperationKind::kEnter,
2829 dex_pc));
2830 break;
2831 }
2832
2833 case Instruction::MONITOR_EXIT: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002834 AppendInstruction(new (allocator_) HMonitorOperation(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002835 LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference),
David Brazdildee58d62016-04-07 09:54:26 +00002836 HMonitorOperation::OperationKind::kExit,
2837 dex_pc));
2838 break;
2839 }
2840
2841 case Instruction::SPARSE_SWITCH:
2842 case Instruction::PACKED_SWITCH: {
2843 BuildSwitch(instruction, dex_pc);
2844 break;
2845 }
2846
2847 default:
2848 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07002849 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00002850 << " because of unhandled instruction "
2851 << instruction.Name();
Igor Murashkin1e065a52017-08-09 13:20:34 -07002852 MaybeRecordStat(compilation_stats_,
2853 MethodCompilationStat::kNotCompiledUnhandledInstruction);
David Brazdildee58d62016-04-07 09:54:26 +00002854 return false;
2855 }
2856 return true;
2857} // NOLINT(readability/fn_size)
2858
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002859ObjPtr<mirror::Class> HInstructionBuilder::LookupResolvedType(
2860 dex::TypeIndex type_index,
2861 const DexCompilationUnit& compilation_unit) const {
2862 return ClassLinker::LookupResolvedType(
2863 type_index, compilation_unit.GetDexCache().Get(), compilation_unit.GetClassLoader().Get());
2864}
2865
2866ObjPtr<mirror::Class> HInstructionBuilder::LookupReferrerClass() const {
2867 // TODO: Cache the result in a Handle<mirror::Class>.
2868 const DexFile::MethodId& method_id =
2869 dex_compilation_unit_->GetDexFile()->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
2870 return LookupResolvedType(method_id.class_idx_, *dex_compilation_unit_);
2871}
2872
David Brazdildee58d62016-04-07 09:54:26 +00002873} // namespace art