blob: 8e9b8187226b0128b81a807b75398f09741141e3 [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
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800324 for (const DexInstructionPcPair& pair : code_item_.Instructions(block_dex_pc)) {
David Brazdildee58d62016-04-07 09:54:26 +0000325 if (current_block_ == nullptr) {
326 // The previous instruction ended this block.
327 break;
328 }
329
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800330 const uint32_t dex_pc = pair.DexPc();
David Brazdildee58d62016-04-07 09:54:26 +0000331 if (dex_pc != block_dex_pc && FindBlockStartingAt(dex_pc) != nullptr) {
332 // This dex_pc starts a new basic block.
333 break;
334 }
335
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800336 if (current_block_->IsTryBlock() && IsThrowingDexInstruction(pair.Inst())) {
David Brazdildee58d62016-04-07 09:54:26 +0000337 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 Chartier0021feb2017-11-07 00:08:52 -0800344 if (!ProcessDexInstruction(pair.Inst(), dex_pc, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +0000345 return false;
346 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700347
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800348 if (QuickenInfoTable::NeedsIndexForInstruction(&pair.Inst())) {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700349 ++quicken_index;
350 }
David Brazdildee58d62016-04-07 09:54:26 +0000351 }
352
353 if (current_block_ != nullptr) {
354 // Branching instructions clear current_block, so we know the last
355 // instruction of the current block is not a branching instruction.
356 // We add an unconditional Goto to the next block.
357 DCHECK_EQ(current_block_->GetSuccessors().size(), 1u);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100358 AppendInstruction(new (allocator_) HGoto());
David Brazdildee58d62016-04-07 09:54:26 +0000359 }
360 }
361
362 SetLoopHeaderPhiInputs();
363
364 return true;
365}
366
Vladimir Marko69d310e2017-10-09 14:12:23 +0100367ArenaBitVector* HInstructionBuilder::FindNativeDebugInfoLocations() {
David Brazdildee58d62016-04-07 09:54:26 +0000368 // The callback gets called when the line number changes.
369 // In other words, it marks the start of new java statement.
370 struct Callback {
371 static bool Position(void* ctx, const DexFile::PositionInfo& entry) {
372 static_cast<ArenaBitVector*>(ctx)->SetBit(entry.address_);
373 return false;
374 }
375 };
Vladimir Marko69d310e2017-10-09 14:12:23 +0100376 const uint32_t num_instructions = code_item_.insns_size_in_code_units_;
377 ArenaBitVector* locations = ArenaBitVector::Create(local_allocator_,
378 num_instructions,
379 /* expandable */ false,
380 kArenaAllocGraphBuilder);
381 locations->ClearAllBits();
David Brazdildee58d62016-04-07 09:54:26 +0000382 dex_file_->DecodeDebugPositionInfo(&code_item_, Callback::Position, locations);
383 // Instruction-specific tweaks.
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -0700384 IterationRange<DexInstructionIterator> instructions = code_item_.Instructions();
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800385 for (const DexInstructionPcPair& 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 Chartier0021feb2017-11-07 00:08:52 -0800391 locations->ClearBit(inst.DexPc());
392 DexInstructionIterator next = std::next(DexInstructionIterator(inst));
393 DCHECK(next.DexPc() != inst.DexPc());
Mathieu Chartier2b2bef22017-10-26 17:10:19 -0700394 if (next != instructions.end()) {
395 locations->SetBit(next.DexPc());
David Brazdildee58d62016-04-07 09:54:26 +0000396 }
397 break;
398 }
399 default:
400 break;
401 }
402 }
Vladimir Marko69d310e2017-10-09 14:12:23 +0100403 return locations;
David Brazdildee58d62016-04-07 09:54:26 +0000404}
405
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100406HInstruction* HInstructionBuilder::LoadLocal(uint32_t reg_number, DataType::Type type) const {
David Brazdildee58d62016-04-07 09:54:26 +0000407 HInstruction* value = (*current_locals_)[reg_number];
408 DCHECK(value != nullptr);
409
410 // If the operation requests a specific type, we make sure its input is of that type.
411 if (type != value->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100412 if (DataType::IsFloatingPointType(type)) {
Aart Bik31883642016-06-06 15:02:44 -0700413 value = ssa_builder_->GetFloatOrDoubleEquivalent(value, type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100414 } else if (type == DataType::Type::kReference) {
Aart Bik31883642016-06-06 15:02:44 -0700415 value = ssa_builder_->GetReferenceTypeEquivalent(value);
David Brazdildee58d62016-04-07 09:54:26 +0000416 }
Aart Bik31883642016-06-06 15:02:44 -0700417 DCHECK(value != nullptr);
David Brazdildee58d62016-04-07 09:54:26 +0000418 }
419
420 return value;
421}
422
423void HInstructionBuilder::UpdateLocal(uint32_t reg_number, HInstruction* stored_value) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100424 DataType::Type stored_type = stored_value->GetType();
425 DCHECK_NE(stored_type, DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +0000426
427 // Storing into vreg `reg_number` may implicitly invalidate the surrounding
428 // registers. Consider the following cases:
429 // (1) Storing a wide value must overwrite previous values in both `reg_number`
430 // and `reg_number+1`. We store `nullptr` in `reg_number+1`.
431 // (2) If vreg `reg_number-1` holds a wide value, writing into `reg_number`
432 // must invalidate it. We store `nullptr` in `reg_number-1`.
433 // Consequently, storing a wide value into the high vreg of another wide value
434 // will invalidate both `reg_number-1` and `reg_number+1`.
435
436 if (reg_number != 0) {
437 HInstruction* local_low = (*current_locals_)[reg_number - 1];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100438 if (local_low != nullptr && DataType::Is64BitType(local_low->GetType())) {
David Brazdildee58d62016-04-07 09:54:26 +0000439 // The vreg we are storing into was previously the high vreg of a pair.
440 // We need to invalidate its low vreg.
441 DCHECK((*current_locals_)[reg_number] == nullptr);
442 (*current_locals_)[reg_number - 1] = nullptr;
443 }
444 }
445
446 (*current_locals_)[reg_number] = stored_value;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100447 if (DataType::Is64BitType(stored_type)) {
David Brazdildee58d62016-04-07 09:54:26 +0000448 // We are storing a pair. Invalidate the instruction in the high vreg.
449 (*current_locals_)[reg_number + 1] = nullptr;
450 }
451}
452
453void HInstructionBuilder::InitializeParameters() {
454 DCHECK(current_block_->IsEntryBlock());
455
Vladimir Marko69d310e2017-10-09 14:12:23 +0100456 // outer_compilation_unit_ is null only when unit testing.
457 if (outer_compilation_unit_ == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +0000458 return;
459 }
460
461 const char* shorty = dex_compilation_unit_->GetShorty();
462 uint16_t number_of_parameters = graph_->GetNumberOfInVRegs();
463 uint16_t locals_index = graph_->GetNumberOfLocalVRegs();
464 uint16_t parameter_index = 0;
465
466 const DexFile::MethodId& referrer_method_id =
467 dex_file_->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
468 if (!dex_compilation_unit_->IsStatic()) {
469 // Add the implicit 'this' argument, not expressed in the signature.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100470 HParameterValue* parameter = new (allocator_) HParameterValue(*dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +0000471 referrer_method_id.class_idx_,
472 parameter_index++,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100473 DataType::Type::kReference,
Igor Murashkind01745e2017-04-05 16:40:31 -0700474 /* is_this */ true);
David Brazdildee58d62016-04-07 09:54:26 +0000475 AppendInstruction(parameter);
476 UpdateLocal(locals_index++, parameter);
477 number_of_parameters--;
Igor Murashkind01745e2017-04-05 16:40:31 -0700478 current_this_parameter_ = parameter;
479 } else {
480 DCHECK(current_this_parameter_ == nullptr);
David Brazdildee58d62016-04-07 09:54:26 +0000481 }
482
483 const DexFile::ProtoId& proto = dex_file_->GetMethodPrototype(referrer_method_id);
484 const DexFile::TypeList* arg_types = dex_file_->GetProtoParameters(proto);
485 for (int i = 0, shorty_pos = 1; i < number_of_parameters; i++) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100486 HParameterValue* parameter = new (allocator_) HParameterValue(
David Brazdildee58d62016-04-07 09:54:26 +0000487 *dex_file_,
488 arg_types->GetTypeItem(shorty_pos - 1).type_idx_,
489 parameter_index++,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100490 DataType::FromShorty(shorty[shorty_pos]),
Igor Murashkind01745e2017-04-05 16:40:31 -0700491 /* is_this */ false);
David Brazdildee58d62016-04-07 09:54:26 +0000492 ++shorty_pos;
493 AppendInstruction(parameter);
494 // Store the parameter value in the local that the dex code will use
495 // to reference that parameter.
496 UpdateLocal(locals_index++, parameter);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100497 if (DataType::Is64BitType(parameter->GetType())) {
David Brazdildee58d62016-04-07 09:54:26 +0000498 i++;
499 locals_index++;
500 parameter_index++;
501 }
502 }
503}
504
505template<typename T>
506void HInstructionBuilder::If_22t(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100507 HInstruction* first = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
508 HInstruction* second = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100509 T* comparison = new (allocator_) T(first, second, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000510 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100511 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000512 current_block_ = nullptr;
513}
514
515template<typename T>
516void HInstructionBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100517 HInstruction* value = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100518 T* comparison = new (allocator_) T(value, graph_->GetIntConstant(0, dex_pc), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000519 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100520 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000521 current_block_ = nullptr;
522}
523
524template<typename T>
525void HInstructionBuilder::Unop_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100526 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000527 uint32_t dex_pc) {
528 HInstruction* first = LoadLocal(instruction.VRegB(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100529 AppendInstruction(new (allocator_) T(type, first, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000530 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
531}
532
533void HInstructionBuilder::Conversion_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100534 DataType::Type input_type,
535 DataType::Type result_type,
David Brazdildee58d62016-04-07 09:54:26 +0000536 uint32_t dex_pc) {
537 HInstruction* first = LoadLocal(instruction.VRegB(), input_type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100538 AppendInstruction(new (allocator_) HTypeConversion(result_type, first, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000539 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
540}
541
542template<typename T>
543void HInstructionBuilder::Binop_23x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100544 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000545 uint32_t dex_pc) {
546 HInstruction* first = LoadLocal(instruction.VRegB(), type);
547 HInstruction* second = LoadLocal(instruction.VRegC(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100548 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000549 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
550}
551
552template<typename T>
553void HInstructionBuilder::Binop_23x_shift(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100554 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000555 uint32_t dex_pc) {
556 HInstruction* first = LoadLocal(instruction.VRegB(), type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100557 HInstruction* second = LoadLocal(instruction.VRegC(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100558 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000559 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
560}
561
562void HInstructionBuilder::Binop_23x_cmp(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100563 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000564 ComparisonBias bias,
565 uint32_t dex_pc) {
566 HInstruction* first = LoadLocal(instruction.VRegB(), type);
567 HInstruction* second = LoadLocal(instruction.VRegC(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100568 AppendInstruction(new (allocator_) HCompare(type, first, second, bias, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000569 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
570}
571
572template<typename T>
573void HInstructionBuilder::Binop_12x_shift(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100574 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000575 uint32_t dex_pc) {
576 HInstruction* first = LoadLocal(instruction.VRegA(), type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100577 HInstruction* second = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100578 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000579 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
580}
581
582template<typename T>
583void HInstructionBuilder::Binop_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100584 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000585 uint32_t dex_pc) {
586 HInstruction* first = LoadLocal(instruction.VRegA(), type);
587 HInstruction* second = LoadLocal(instruction.VRegB(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100588 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000589 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
590}
591
592template<typename T>
593void HInstructionBuilder::Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100594 HInstruction* first = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000595 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s(), dex_pc);
596 if (reverse) {
597 std::swap(first, second);
598 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100599 AppendInstruction(new (allocator_) T(DataType::Type::kInt32, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000600 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
601}
602
603template<typename T>
604void HInstructionBuilder::Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100605 HInstruction* first = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000606 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b(), dex_pc);
607 if (reverse) {
608 std::swap(first, second);
609 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100610 AppendInstruction(new (allocator_) T(DataType::Type::kInt32, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000611 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
612}
613
Igor Murashkind01745e2017-04-05 16:40:31 -0700614// Does the method being compiled need any constructor barriers being inserted?
615// (Always 'false' for methods that aren't <init>.)
Mathieu Chartierc4ae9162016-04-07 13:19:19 -0700616static bool RequiresConstructorBarrier(const DexCompilationUnit* cu, CompilerDriver* driver) {
Igor Murashkin032cacd2017-04-06 14:40:08 -0700617 // Can be null in unit tests only.
618 if (UNLIKELY(cu == nullptr)) {
619 return false;
620 }
621
David Brazdildee58d62016-04-07 09:54:26 +0000622 Thread* self = Thread::Current();
623 return cu->IsConstructor()
Igor Murashkind01745e2017-04-05 16:40:31 -0700624 && !cu->IsStatic()
625 // RequiresConstructorBarrier must only be queried for <init> methods;
626 // it's effectively "false" for every other method.
627 //
628 // See CompilerDriver::RequiresConstructBarrier for more explanation.
Mathieu Chartierc4ae9162016-04-07 13:19:19 -0700629 && driver->RequiresConstructorBarrier(self, cu->GetDexFile(), cu->GetClassDefIndex());
David Brazdildee58d62016-04-07 09:54:26 +0000630}
631
632// Returns true if `block` has only one successor which starts at the next
633// dex_pc after `instruction` at `dex_pc`.
634static bool IsFallthroughInstruction(const Instruction& instruction,
635 uint32_t dex_pc,
636 HBasicBlock* block) {
637 uint32_t next_dex_pc = dex_pc + instruction.SizeInCodeUnits();
638 return block->GetSingleSuccessor()->GetDexPc() == next_dex_pc;
639}
640
641void HInstructionBuilder::BuildSwitch(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100642 HInstruction* value = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000643 DexSwitchTable table(instruction, dex_pc);
644
645 if (table.GetNumEntries() == 0) {
646 // Empty Switch. Code falls through to the next block.
647 DCHECK(IsFallthroughInstruction(instruction, dex_pc, current_block_));
Vladimir Markoca6fff82017-10-03 14:49:14 +0100648 AppendInstruction(new (allocator_) HGoto(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000649 } else if (table.ShouldBuildDecisionTree()) {
650 for (DexSwitchTableIterator it(table); !it.Done(); it.Advance()) {
651 HInstruction* case_value = graph_->GetIntConstant(it.CurrentKey(), dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100652 HEqual* comparison = new (allocator_) HEqual(value, case_value, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000653 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100654 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000655
656 if (!it.IsLast()) {
657 current_block_ = FindBlockStartingAt(it.GetDexPcForCurrentIndex());
658 }
659 }
660 } else {
661 AppendInstruction(
Vladimir Markoca6fff82017-10-03 14:49:14 +0100662 new (allocator_) HPackedSwitch(table.GetEntryAt(0), table.GetNumEntries(), value, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000663 }
664
665 current_block_ = nullptr;
666}
667
668void HInstructionBuilder::BuildReturn(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100669 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000670 uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100671 if (type == DataType::Type::kVoid) {
Igor Murashkind01745e2017-04-05 16:40:31 -0700672 // Only <init> (which is a return-void) could possibly have a constructor fence.
Igor Murashkin032cacd2017-04-06 14:40:08 -0700673 // This may insert additional redundant constructor fences from the super constructors.
674 // TODO: remove redundant constructor fences (b/36656456).
675 if (RequiresConstructorBarrier(dex_compilation_unit_, compiler_driver_)) {
Igor Murashkind01745e2017-04-05 16:40:31 -0700676 // Compiling instance constructor.
Vladimir Markoba118822017-06-12 15:41:56 +0100677 DCHECK_STREQ("<init>", graph_->GetMethodName());
Igor Murashkind01745e2017-04-05 16:40:31 -0700678
679 HInstruction* fence_target = current_this_parameter_;
680 DCHECK(fence_target != nullptr);
681
Vladimir Markoca6fff82017-10-03 14:49:14 +0100682 AppendInstruction(new (allocator_) HConstructorFence(fence_target, dex_pc, allocator_));
Igor Murashkin6ef45672017-08-08 13:59:55 -0700683 MaybeRecordStat(
684 compilation_stats_,
685 MethodCompilationStat::kConstructorFenceGeneratedFinal);
David Brazdildee58d62016-04-07 09:54:26 +0000686 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100687 AppendInstruction(new (allocator_) HReturnVoid(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000688 } else {
Igor Murashkind01745e2017-04-05 16:40:31 -0700689 DCHECK(!RequiresConstructorBarrier(dex_compilation_unit_, compiler_driver_));
David Brazdildee58d62016-04-07 09:54:26 +0000690 HInstruction* value = LoadLocal(instruction.VRegA(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100691 AppendInstruction(new (allocator_) HReturn(value, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000692 }
693 current_block_ = nullptr;
694}
695
696static InvokeType GetInvokeTypeFromOpCode(Instruction::Code opcode) {
697 switch (opcode) {
698 case Instruction::INVOKE_STATIC:
699 case Instruction::INVOKE_STATIC_RANGE:
700 return kStatic;
701 case Instruction::INVOKE_DIRECT:
702 case Instruction::INVOKE_DIRECT_RANGE:
703 return kDirect;
704 case Instruction::INVOKE_VIRTUAL:
705 case Instruction::INVOKE_VIRTUAL_QUICK:
706 case Instruction::INVOKE_VIRTUAL_RANGE:
707 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
708 return kVirtual;
709 case Instruction::INVOKE_INTERFACE:
710 case Instruction::INVOKE_INTERFACE_RANGE:
711 return kInterface;
712 case Instruction::INVOKE_SUPER_RANGE:
713 case Instruction::INVOKE_SUPER:
714 return kSuper;
715 default:
716 LOG(FATAL) << "Unexpected invoke opcode: " << opcode;
717 UNREACHABLE();
718 }
719}
720
721ArtMethod* HInstructionBuilder::ResolveMethod(uint16_t method_idx, InvokeType invoke_type) {
722 ScopedObjectAccess soa(Thread::Current());
David Brazdildee58d62016-04-07 09:54:26 +0000723
724 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000725 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100726
Vladimir Markoba118822017-06-12 15:41:56 +0100727 ArtMethod* resolved_method =
728 class_linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
729 *dex_compilation_unit_->GetDexFile(),
730 method_idx,
731 dex_compilation_unit_->GetDexCache(),
732 class_loader,
733 graph_->GetArtMethod(),
734 invoke_type);
David Brazdildee58d62016-04-07 09:54:26 +0000735
736 if (UNLIKELY(resolved_method == nullptr)) {
737 // Clean up any exception left by type resolution.
738 soa.Self()->ClearException();
739 return nullptr;
740 }
741
Vladimir Markoba118822017-06-12 15:41:56 +0100742 // The referrer may be unresolved for AOT if we're compiling a class that cannot be
743 // resolved because, for example, we don't find a superclass in the classpath.
744 if (graph_->GetArtMethod() == nullptr) {
745 // The class linker cannot check access without a referrer, so we have to do it.
746 // Fall back to HInvokeUnresolved if the method isn't public.
David Brazdildee58d62016-04-07 09:54:26 +0000747 if (!resolved_method->IsPublic()) {
748 return nullptr;
749 }
David Brazdildee58d62016-04-07 09:54:26 +0000750 }
751
752 // We have to special case the invoke-super case, as ClassLinker::ResolveMethod does not.
753 // We need to look at the referrer's super class vtable. We need to do this to know if we need to
754 // make this an invoke-unresolved to handle cross-dex invokes or abstract super methods, both of
755 // which require runtime handling.
756 if (invoke_type == kSuper) {
Vladimir Markoba118822017-06-12 15:41:56 +0100757 ObjPtr<mirror::Class> compiling_class = GetCompilingClass();
Andreas Gampefa4333d2017-02-14 11:10:34 -0800758 if (compiling_class == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +0000759 // We could not determine the method's class we need to wait until runtime.
760 DCHECK(Runtime::Current()->IsAotCompiler());
761 return nullptr;
762 }
Vladimir Markoba118822017-06-12 15:41:56 +0100763 ObjPtr<mirror::Class> referenced_class = class_linker->LookupResolvedType(
764 *dex_compilation_unit_->GetDexFile(),
765 dex_compilation_unit_->GetDexFile()->GetMethodId(method_idx).class_idx_,
766 dex_compilation_unit_->GetDexCache().Get(),
767 class_loader.Get());
768 DCHECK(referenced_class != nullptr); // We have already resolved a method from this class.
769 if (!referenced_class->IsAssignableFrom(compiling_class)) {
Aart Bikf663e342016-04-04 17:28:59 -0700770 // We cannot statically determine the target method. The runtime will throw a
771 // NoSuchMethodError on this one.
772 return nullptr;
773 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100774 ArtMethod* actual_method;
Vladimir Markoba118822017-06-12 15:41:56 +0100775 if (referenced_class->IsInterface()) {
776 actual_method = referenced_class->FindVirtualMethodForInterfaceSuper(
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100777 resolved_method, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000778 } else {
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100779 uint16_t vtable_index = resolved_method->GetMethodIndex();
780 actual_method = compiling_class->GetSuperClass()->GetVTableEntry(
781 vtable_index, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000782 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100783 if (actual_method != resolved_method &&
784 !IsSameDexFile(*actual_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
785 // The back-end code generator relies on this check in order to ensure that it will not
786 // attempt to read the dex_cache with a dex_method_index that is not from the correct
787 // dex_file. If we didn't do this check then the dex_method_index will not be updated in the
788 // builder, which means that the code-generator (and compiler driver during sharpening and
789 // inliner, maybe) might invoke an incorrect method.
790 // TODO: The actual method could still be referenced in the current dex file, so we
791 // could try locating it.
792 // TODO: Remove the dex_file restriction.
793 return nullptr;
794 }
795 if (!actual_method->IsInvokable()) {
796 // Fail if the actual method cannot be invoked. Otherwise, the runtime resolution stub
797 // could resolve the callee to the wrong method.
798 return nullptr;
799 }
800 resolved_method = actual_method;
David Brazdildee58d62016-04-07 09:54:26 +0000801 }
802
David Brazdildee58d62016-04-07 09:54:26 +0000803 return resolved_method;
804}
805
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100806static bool IsStringConstructor(ArtMethod* method) {
807 ScopedObjectAccess soa(Thread::Current());
808 return method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
809}
810
David Brazdildee58d62016-04-07 09:54:26 +0000811bool HInstructionBuilder::BuildInvoke(const Instruction& instruction,
812 uint32_t dex_pc,
813 uint32_t method_idx,
814 uint32_t number_of_vreg_arguments,
815 bool is_range,
816 uint32_t* args,
817 uint32_t register_index) {
818 InvokeType invoke_type = GetInvokeTypeFromOpCode(instruction.Opcode());
819 const char* descriptor = dex_file_->GetMethodShorty(method_idx);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100820 DataType::Type return_type = DataType::FromShorty(descriptor[0]);
David Brazdildee58d62016-04-07 09:54:26 +0000821
822 // Remove the return type from the 'proto'.
823 size_t number_of_arguments = strlen(descriptor) - 1;
824 if (invoke_type != kStatic) { // instance call
825 // One extra argument for 'this'.
826 number_of_arguments++;
827 }
828
David Brazdildee58d62016-04-07 09:54:26 +0000829 ArtMethod* resolved_method = ResolveMethod(method_idx, invoke_type);
830
831 if (UNLIKELY(resolved_method == nullptr)) {
Igor Murashkin1e065a52017-08-09 13:20:34 -0700832 MaybeRecordStat(compilation_stats_,
833 MethodCompilationStat::kUnresolvedMethod);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100834 HInvoke* invoke = new (allocator_) HInvokeUnresolved(allocator_,
835 number_of_arguments,
836 return_type,
837 dex_pc,
838 method_idx,
839 invoke_type);
David Brazdildee58d62016-04-07 09:54:26 +0000840 return HandleInvoke(invoke,
841 number_of_vreg_arguments,
842 args,
843 register_index,
844 is_range,
845 descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -0700846 nullptr, /* clinit_check */
847 true /* is_unresolved */);
David Brazdildee58d62016-04-07 09:54:26 +0000848 }
849
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100850 // Replace calls to String.<init> with StringFactory.
851 if (IsStringConstructor(resolved_method)) {
852 uint32_t string_init_entry_point = WellKnownClasses::StringInitToEntryPoint(resolved_method);
853 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
854 HInvokeStaticOrDirect::MethodLoadKind::kStringInit,
855 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000856 dchecked_integral_cast<uint64_t>(string_init_entry_point)
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100857 };
858 MethodReference target_method(dex_file_, method_idx);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100859 HInvoke* invoke = new (allocator_) HInvokeStaticOrDirect(
860 allocator_,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100861 number_of_arguments - 1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100862 DataType::Type::kReference /*return_type */,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100863 dex_pc,
864 method_idx,
865 nullptr,
866 dispatch_info,
867 invoke_type,
868 target_method,
869 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit);
870 return HandleStringInit(invoke,
871 number_of_vreg_arguments,
872 args,
873 register_index,
874 is_range,
875 descriptor);
876 }
877
David Brazdildee58d62016-04-07 09:54:26 +0000878 // Potential class initialization check, in the case of a static method call.
879 HClinitCheck* clinit_check = nullptr;
880 HInvoke* invoke = nullptr;
881 if (invoke_type == kDirect || invoke_type == kStatic || invoke_type == kSuper) {
882 // By default, consider that the called method implicitly requires
883 // an initialization check of its declaring method.
884 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement
885 = HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
886 ScopedObjectAccess soa(Thread::Current());
887 if (invoke_type == kStatic) {
888 clinit_check = ProcessClinitCheckForInvoke(
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000889 dex_pc, resolved_method, &clinit_check_requirement);
David Brazdildee58d62016-04-07 09:54:26 +0000890 } else if (invoke_type == kSuper) {
891 if (IsSameDexFile(*resolved_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100892 // Update the method index to the one resolved. Note that this may be a no-op if
David Brazdildee58d62016-04-07 09:54:26 +0000893 // we resolved to the method referenced by the instruction.
894 method_idx = resolved_method->GetDexMethodIndex();
David Brazdildee58d62016-04-07 09:54:26 +0000895 }
896 }
897
898 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
Vladimir Markoe7197bf2017-06-02 17:00:23 +0100899 HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall,
David Brazdildee58d62016-04-07 09:54:26 +0000900 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000901 0u
David Brazdildee58d62016-04-07 09:54:26 +0000902 };
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100903 MethodReference target_method(resolved_method->GetDexFile(),
904 resolved_method->GetDexMethodIndex());
Vladimir Markoca6fff82017-10-03 14:49:14 +0100905 invoke = new (allocator_) HInvokeStaticOrDirect(allocator_,
906 number_of_arguments,
907 return_type,
908 dex_pc,
909 method_idx,
910 resolved_method,
911 dispatch_info,
912 invoke_type,
913 target_method,
914 clinit_check_requirement);
David Brazdildee58d62016-04-07 09:54:26 +0000915 } else if (invoke_type == kVirtual) {
916 ScopedObjectAccess soa(Thread::Current()); // Needed for the method index
Vladimir Markoca6fff82017-10-03 14:49:14 +0100917 invoke = new (allocator_) HInvokeVirtual(allocator_,
918 number_of_arguments,
919 return_type,
920 dex_pc,
921 method_idx,
922 resolved_method,
923 resolved_method->GetMethodIndex());
David Brazdildee58d62016-04-07 09:54:26 +0000924 } else {
925 DCHECK_EQ(invoke_type, kInterface);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100926 ScopedObjectAccess soa(Thread::Current()); // Needed for the IMT index.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100927 invoke = new (allocator_) HInvokeInterface(allocator_,
928 number_of_arguments,
929 return_type,
930 dex_pc,
931 method_idx,
932 resolved_method,
933 ImTable::GetImtIndex(resolved_method));
David Brazdildee58d62016-04-07 09:54:26 +0000934 }
935
936 return HandleInvoke(invoke,
937 number_of_vreg_arguments,
938 args,
939 register_index,
940 is_range,
941 descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -0700942 clinit_check,
943 false /* is_unresolved */);
David Brazdildee58d62016-04-07 09:54:26 +0000944}
945
Orion Hodsonac141392017-01-13 11:53:47 +0000946bool HInstructionBuilder::BuildInvokePolymorphic(const Instruction& instruction ATTRIBUTE_UNUSED,
947 uint32_t dex_pc,
948 uint32_t method_idx,
949 uint32_t proto_idx,
950 uint32_t number_of_vreg_arguments,
951 bool is_range,
952 uint32_t* args,
953 uint32_t register_index) {
954 const char* descriptor = dex_file_->GetShorty(proto_idx);
955 DCHECK_EQ(1 + ArtMethod::NumArgRegisters(descriptor), number_of_vreg_arguments);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100956 DataType::Type return_type = DataType::FromShorty(descriptor[0]);
Orion Hodsonac141392017-01-13 11:53:47 +0000957 size_t number_of_arguments = strlen(descriptor);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100958 HInvoke* invoke = new (allocator_) HInvokePolymorphic(allocator_,
959 number_of_arguments,
960 return_type,
961 dex_pc,
962 method_idx);
Orion Hodsonac141392017-01-13 11:53:47 +0000963 return HandleInvoke(invoke,
964 number_of_vreg_arguments,
965 args,
966 register_index,
967 is_range,
968 descriptor,
969 nullptr /* clinit_check */,
970 false /* is_unresolved */);
971}
972
Igor Murashkin79d8fa72017-04-18 09:37:23 -0700973HNewInstance* HInstructionBuilder::BuildNewInstance(dex::TypeIndex type_index, uint32_t dex_pc) {
Vladimir Marko3cd50df2016-04-13 19:29:26 +0100974 ScopedObjectAccess soa(Thread::Current());
David Brazdildee58d62016-04-07 09:54:26 +0000975
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000976 HLoadClass* load_class = BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000977
David Brazdildee58d62016-04-07 09:54:26 +0000978 HInstruction* cls = load_class;
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000979 Handle<mirror::Class> klass = load_class->GetClass();
980
981 if (!IsInitialized(klass)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100982 cls = new (allocator_) HClinitCheck(load_class, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000983 AppendInstruction(cls);
984 }
985
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000986 // Only the access check entrypoint handles the finalizable class case. If we
987 // need access checks, then we haven't resolved the method and the class may
988 // again be finalizable.
989 QuickEntrypointEnum entrypoint = kQuickAllocObjectInitialized;
990 if (load_class->NeedsAccessCheck() || klass->IsFinalizable() || !klass->IsInstantiable()) {
991 entrypoint = kQuickAllocObjectWithChecks;
992 }
993
994 // Consider classes we haven't resolved as potentially finalizable.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800995 bool finalizable = (klass == nullptr) || klass->IsFinalizable();
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000996
Vladimir Markoca6fff82017-10-03 14:49:14 +0100997 HNewInstance* new_instance = new (allocator_) HNewInstance(
David Brazdildee58d62016-04-07 09:54:26 +0000998 cls,
David Brazdildee58d62016-04-07 09:54:26 +0000999 dex_pc,
1000 type_index,
1001 *dex_compilation_unit_->GetDexFile(),
David Brazdildee58d62016-04-07 09:54:26 +00001002 finalizable,
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001003 entrypoint);
1004 AppendInstruction(new_instance);
1005
1006 return new_instance;
1007}
1008
1009void HInstructionBuilder::BuildConstructorFenceForAllocation(HInstruction* allocation) {
1010 DCHECK(allocation != nullptr &&
George Burgess IVf2072992017-05-23 15:36:41 -07001011 (allocation->IsNewInstance() ||
1012 allocation->IsNewArray())); // corresponding to "new" keyword in JLS.
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001013
1014 if (allocation->IsNewInstance()) {
1015 // STRING SPECIAL HANDLING:
1016 // -------------------------------
1017 // Strings have a real HNewInstance node but they end up always having 0 uses.
1018 // All uses of a String HNewInstance are always transformed to replace their input
1019 // of the HNewInstance with an input of the invoke to StringFactory.
1020 //
1021 // Do not emit an HConstructorFence here since it can inhibit some String new-instance
1022 // optimizations (to pass checker tests that rely on those optimizations).
1023 HNewInstance* new_inst = allocation->AsNewInstance();
1024 HLoadClass* load_class = new_inst->GetLoadClass();
1025
1026 Thread* self = Thread::Current();
1027 ScopedObjectAccess soa(self);
1028 StackHandleScope<1> hs(self);
1029 Handle<mirror::Class> klass = load_class->GetClass();
1030 if (klass != nullptr && klass->IsStringClass()) {
1031 return;
1032 // Note: Do not use allocation->IsStringAlloc which requires
1033 // a valid ReferenceTypeInfo, but that doesn't get made until after reference type
1034 // propagation (and instruction builder is too early).
1035 }
1036 // (In terms of correctness, the StringFactory needs to provide its own
1037 // default initialization barrier, see below.)
1038 }
1039
1040 // JLS 17.4.5 "Happens-before Order" describes:
1041 //
1042 // The default initialization of any object happens-before any other actions (other than
1043 // default-writes) of a program.
1044 //
1045 // In our implementation the default initialization of an object to type T means
1046 // setting all of its initial data (object[0..size)) to 0, and setting the
1047 // object's class header (i.e. object.getClass() == T.class).
1048 //
1049 // In practice this fence ensures that the writes to the object header
1050 // are visible to other threads if this object escapes the current thread.
1051 // (and in theory the 0-initializing, but that happens automatically
1052 // when new memory pages are mapped in by the OS).
1053 HConstructorFence* ctor_fence =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001054 new (allocator_) HConstructorFence(allocation, allocation->GetDexPc(), allocator_);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001055 AppendInstruction(ctor_fence);
Igor Murashkin6ef45672017-08-08 13:59:55 -07001056 MaybeRecordStat(
1057 compilation_stats_,
1058 MethodCompilationStat::kConstructorFenceGeneratedNew);
David Brazdildee58d62016-04-07 09:54:26 +00001059}
1060
1061static bool IsSubClass(mirror::Class* to_test, mirror::Class* super_class)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001062 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdildee58d62016-04-07 09:54:26 +00001063 return to_test != nullptr && !to_test->IsInterface() && to_test->IsSubClass(super_class);
1064}
1065
1066bool HInstructionBuilder::IsInitialized(Handle<mirror::Class> cls) const {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001067 if (cls == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +00001068 return false;
1069 }
1070
1071 // `CanAssumeClassIsLoaded` will return true if we're JITting, or will
1072 // check whether the class is in an image for the AOT compilation.
1073 if (cls->IsInitialized() &&
1074 compiler_driver_->CanAssumeClassIsLoaded(cls.Get())) {
1075 return true;
1076 }
1077
1078 if (IsSubClass(GetOutermostCompilingClass(), cls.Get())) {
1079 return true;
1080 }
1081
1082 // TODO: We should walk over the inlined methods, but we don't pass
1083 // that information to the builder.
1084 if (IsSubClass(GetCompilingClass(), cls.Get())) {
1085 return true;
1086 }
1087
1088 return false;
1089}
1090
1091HClinitCheck* HInstructionBuilder::ProcessClinitCheckForInvoke(
1092 uint32_t dex_pc,
1093 ArtMethod* resolved_method,
David Brazdildee58d62016-04-07 09:54:26 +00001094 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001095 Handle<mirror::Class> klass = handles_->NewHandle(resolved_method->GetDeclaringClass());
David Brazdildee58d62016-04-07 09:54:26 +00001096
1097 HClinitCheck* clinit_check = nullptr;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001098 if (IsInitialized(klass)) {
David Brazdildee58d62016-04-07 09:54:26 +00001099 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001100 } else {
1101 HLoadClass* cls = BuildLoadClass(klass->GetDexTypeIndex(),
1102 klass->GetDexFile(),
1103 klass,
1104 dex_pc,
1105 /* needs_access_check */ false);
1106 if (cls != nullptr) {
1107 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
Vladimir Markoca6fff82017-10-03 14:49:14 +01001108 clinit_check = new (allocator_) HClinitCheck(cls, dex_pc);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001109 AppendInstruction(clinit_check);
1110 }
David Brazdildee58d62016-04-07 09:54:26 +00001111 }
1112 return clinit_check;
1113}
1114
1115bool HInstructionBuilder::SetupInvokeArguments(HInvoke* invoke,
1116 uint32_t number_of_vreg_arguments,
1117 uint32_t* args,
1118 uint32_t register_index,
1119 bool is_range,
1120 const char* descriptor,
1121 size_t start_index,
1122 size_t* argument_index) {
1123 uint32_t descriptor_index = 1; // Skip the return type.
1124
1125 for (size_t i = start_index;
1126 // Make sure we don't go over the expected arguments or over the number of
1127 // dex registers given. If the instruction was seen as dead by the verifier,
1128 // it hasn't been properly checked.
1129 (i < number_of_vreg_arguments) && (*argument_index < invoke->GetNumberOfArguments());
1130 i++, (*argument_index)++) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001131 DataType::Type type = DataType::FromShorty(descriptor[descriptor_index++]);
1132 bool is_wide = (type == DataType::Type::kInt64) || (type == DataType::Type::kFloat64);
David Brazdildee58d62016-04-07 09:54:26 +00001133 if (!is_range
1134 && is_wide
1135 && ((i + 1 == number_of_vreg_arguments) || (args[i] + 1 != args[i + 1]))) {
1136 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
1137 // reject any class where this is violated. However, the verifier only does these checks
1138 // on non trivially dead instructions, so we just bailout the compilation.
1139 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001140 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001141 << " because of non-sequential dex register pair in wide argument";
Igor Murashkin1e065a52017-08-09 13:20:34 -07001142 MaybeRecordStat(compilation_stats_,
1143 MethodCompilationStat::kNotCompiledMalformedOpcode);
David Brazdildee58d62016-04-07 09:54:26 +00001144 return false;
1145 }
1146 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
1147 invoke->SetArgumentAt(*argument_index, arg);
1148 if (is_wide) {
1149 i++;
1150 }
1151 }
1152
1153 if (*argument_index != invoke->GetNumberOfArguments()) {
1154 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001155 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001156 << " because of wrong number of arguments in invoke instruction";
Igor Murashkin1e065a52017-08-09 13:20:34 -07001157 MaybeRecordStat(compilation_stats_,
1158 MethodCompilationStat::kNotCompiledMalformedOpcode);
David Brazdildee58d62016-04-07 09:54:26 +00001159 return false;
1160 }
1161
1162 if (invoke->IsInvokeStaticOrDirect() &&
1163 HInvokeStaticOrDirect::NeedsCurrentMethodInput(
1164 invoke->AsInvokeStaticOrDirect()->GetMethodLoadKind())) {
1165 invoke->SetArgumentAt(*argument_index, graph_->GetCurrentMethod());
1166 (*argument_index)++;
1167 }
1168
1169 return true;
1170}
1171
1172bool HInstructionBuilder::HandleInvoke(HInvoke* invoke,
1173 uint32_t number_of_vreg_arguments,
1174 uint32_t* args,
1175 uint32_t register_index,
1176 bool is_range,
1177 const char* descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -07001178 HClinitCheck* clinit_check,
1179 bool is_unresolved) {
David Brazdildee58d62016-04-07 09:54:26 +00001180 DCHECK(!invoke->IsInvokeStaticOrDirect() || !invoke->AsInvokeStaticOrDirect()->IsStringInit());
1181
1182 size_t start_index = 0;
1183 size_t argument_index = 0;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001184 if (invoke->GetInvokeType() != InvokeType::kStatic) { // Instance call.
Aart Bik296fbb42016-06-07 13:49:12 -07001185 uint32_t obj_reg = is_range ? register_index : args[0];
1186 HInstruction* arg = is_unresolved
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001187 ? LoadLocal(obj_reg, DataType::Type::kReference)
Aart Bik296fbb42016-06-07 13:49:12 -07001188 : LoadNullCheckedLocal(obj_reg, invoke->GetDexPc());
David Brazdilc120bbe2016-04-22 16:57:00 +01001189 invoke->SetArgumentAt(0, arg);
David Brazdildee58d62016-04-07 09:54:26 +00001190 start_index = 1;
1191 argument_index = 1;
1192 }
1193
1194 if (!SetupInvokeArguments(invoke,
1195 number_of_vreg_arguments,
1196 args,
1197 register_index,
1198 is_range,
1199 descriptor,
1200 start_index,
1201 &argument_index)) {
1202 return false;
1203 }
1204
1205 if (clinit_check != nullptr) {
1206 // Add the class initialization check as last input of `invoke`.
1207 DCHECK(invoke->IsInvokeStaticOrDirect());
1208 DCHECK(invoke->AsInvokeStaticOrDirect()->GetClinitCheckRequirement()
1209 == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit);
1210 invoke->SetArgumentAt(argument_index, clinit_check);
1211 argument_index++;
1212 }
1213
1214 AppendInstruction(invoke);
1215 latest_result_ = invoke;
1216
1217 return true;
1218}
1219
1220bool HInstructionBuilder::HandleStringInit(HInvoke* invoke,
1221 uint32_t number_of_vreg_arguments,
1222 uint32_t* args,
1223 uint32_t register_index,
1224 bool is_range,
1225 const char* descriptor) {
1226 DCHECK(invoke->IsInvokeStaticOrDirect());
1227 DCHECK(invoke->AsInvokeStaticOrDirect()->IsStringInit());
1228
1229 size_t start_index = 1;
1230 size_t argument_index = 0;
1231 if (!SetupInvokeArguments(invoke,
1232 number_of_vreg_arguments,
1233 args,
1234 register_index,
1235 is_range,
1236 descriptor,
1237 start_index,
1238 &argument_index)) {
1239 return false;
1240 }
1241
1242 AppendInstruction(invoke);
1243
1244 // This is a StringFactory call, not an actual String constructor. Its result
1245 // replaces the empty String pre-allocated by NewInstance.
1246 uint32_t orig_this_reg = is_range ? register_index : args[0];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001247 HInstruction* arg_this = LoadLocal(orig_this_reg, DataType::Type::kReference);
David Brazdildee58d62016-04-07 09:54:26 +00001248
1249 // Replacing the NewInstance might render it redundant. Keep a list of these
1250 // to be visited once it is clear whether it is has remaining uses.
1251 if (arg_this->IsNewInstance()) {
1252 ssa_builder_->AddUninitializedString(arg_this->AsNewInstance());
1253 } else {
1254 DCHECK(arg_this->IsPhi());
1255 // NewInstance is not the direct input of the StringFactory call. It might
1256 // be redundant but optimizing this case is not worth the effort.
1257 }
1258
1259 // Walk over all vregs and replace any occurrence of `arg_this` with `invoke`.
1260 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
1261 if ((*current_locals_)[vreg] == arg_this) {
1262 (*current_locals_)[vreg] = invoke;
1263 }
1264 }
1265
1266 return true;
1267}
1268
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001269static DataType::Type GetFieldAccessType(const DexFile& dex_file, uint16_t field_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001270 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
1271 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001272 return DataType::FromShorty(type[0]);
David Brazdildee58d62016-04-07 09:54:26 +00001273}
1274
1275bool HInstructionBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
1276 uint32_t dex_pc,
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001277 bool is_put,
1278 size_t quicken_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001279 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1280 uint32_t obj_reg = instruction.VRegB_22c();
1281 uint16_t field_index;
1282 if (instruction.IsQuickened()) {
1283 if (!CanDecodeQuickenedInfo()) {
1284 return false;
1285 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001286 field_index = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00001287 } else {
1288 field_index = instruction.VRegC_22c();
1289 }
1290
1291 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001292 ArtField* resolved_field = ResolveField(field_index, /* is_static */ false, is_put);
David Brazdildee58d62016-04-07 09:54:26 +00001293
Aart Bik14154132016-06-02 17:53:58 -07001294 // Generate an explicit null check on the reference, unless the field access
1295 // is unresolved. In that case, we rely on the runtime to perform various
1296 // checks first, followed by a null check.
1297 HInstruction* object = (resolved_field == nullptr)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001298 ? LoadLocal(obj_reg, DataType::Type::kReference)
Aart Bik14154132016-06-02 17:53:58 -07001299 : LoadNullCheckedLocal(obj_reg, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001300
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001301 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001302 if (is_put) {
1303 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1304 HInstruction* field_set = nullptr;
1305 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001306 MaybeRecordStat(compilation_stats_,
1307 MethodCompilationStat::kUnresolvedField);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001308 field_set = new (allocator_) HUnresolvedInstanceFieldSet(object,
1309 value,
1310 field_type,
1311 field_index,
1312 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001313 } else {
1314 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Vladimir Markoca6fff82017-10-03 14:49:14 +01001315 field_set = new (allocator_) HInstanceFieldSet(object,
1316 value,
1317 resolved_field,
1318 field_type,
1319 resolved_field->GetOffset(),
1320 resolved_field->IsVolatile(),
1321 field_index,
1322 class_def_index,
1323 *dex_file_,
1324 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001325 }
1326 AppendInstruction(field_set);
1327 } else {
1328 HInstruction* field_get = nullptr;
1329 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001330 MaybeRecordStat(compilation_stats_,
1331 MethodCompilationStat::kUnresolvedField);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001332 field_get = new (allocator_) HUnresolvedInstanceFieldGet(object,
1333 field_type,
1334 field_index,
1335 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001336 } else {
1337 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Vladimir Markoca6fff82017-10-03 14:49:14 +01001338 field_get = new (allocator_) HInstanceFieldGet(object,
1339 resolved_field,
1340 field_type,
1341 resolved_field->GetOffset(),
1342 resolved_field->IsVolatile(),
1343 field_index,
1344 class_def_index,
1345 *dex_file_,
1346 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001347 }
1348 AppendInstruction(field_get);
1349 UpdateLocal(source_or_dest_reg, field_get);
1350 }
1351
1352 return true;
1353}
1354
1355static mirror::Class* GetClassFrom(CompilerDriver* driver,
1356 const DexCompilationUnit& compilation_unit) {
1357 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001358 Handle<mirror::ClassLoader> class_loader = compilation_unit.GetClassLoader();
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001359 Handle<mirror::DexCache> dex_cache = compilation_unit.GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00001360
1361 return driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, &compilation_unit);
1362}
1363
1364mirror::Class* HInstructionBuilder::GetOutermostCompilingClass() const {
1365 return GetClassFrom(compiler_driver_, *outer_compilation_unit_);
1366}
1367
1368mirror::Class* HInstructionBuilder::GetCompilingClass() const {
1369 return GetClassFrom(compiler_driver_, *dex_compilation_unit_);
1370}
1371
Andreas Gampea5b09a62016-11-17 15:21:22 -08001372bool HInstructionBuilder::IsOutermostCompilingClass(dex::TypeIndex type_index) const {
David Brazdildee58d62016-04-07 09:54:26 +00001373 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001374 StackHandleScope<2> hs(soa.Self());
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001375 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001376 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
David Brazdildee58d62016-04-07 09:54:26 +00001377 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
1378 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
1379 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
1380
1381 // GetOutermostCompilingClass returns null when the class is unresolved
1382 // (e.g. if it derives from an unresolved class). This is bogus knowing that
1383 // we are compiling it.
1384 // When this happens we cannot establish a direct relation between the current
1385 // class and the outer class, so we return false.
1386 // (Note that this is only used for optimizing invokes and field accesses)
Andreas Gampefa4333d2017-02-14 11:10:34 -08001387 return (cls != nullptr) && (outer_class.Get() == cls.Get());
David Brazdildee58d62016-04-07 09:54:26 +00001388}
1389
1390void HInstructionBuilder::BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001391 uint32_t dex_pc,
1392 bool is_put,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001393 DataType::Type field_type) {
David Brazdildee58d62016-04-07 09:54:26 +00001394 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1395 uint16_t field_index = instruction.VRegB_21c();
1396
1397 if (is_put) {
1398 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1399 AppendInstruction(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001400 new (allocator_) HUnresolvedStaticFieldSet(value, field_type, field_index, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001401 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001402 AppendInstruction(new (allocator_) HUnresolvedStaticFieldGet(field_type, field_index, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001403 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1404 }
1405}
1406
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001407ArtField* HInstructionBuilder::ResolveField(uint16_t field_idx, bool is_static, bool is_put) {
1408 ScopedObjectAccess soa(Thread::Current());
1409 StackHandleScope<2> hs(soa.Self());
1410
1411 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001412 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001413 Handle<mirror::Class> compiling_class(hs.NewHandle(GetCompilingClass()));
1414
1415 ArtField* resolved_field = class_linker->ResolveField(*dex_compilation_unit_->GetDexFile(),
1416 field_idx,
1417 dex_compilation_unit_->GetDexCache(),
1418 class_loader,
1419 is_static);
1420
1421 if (UNLIKELY(resolved_field == nullptr)) {
1422 // Clean up any exception left by type resolution.
1423 soa.Self()->ClearException();
1424 return nullptr;
1425 }
1426
1427 // Check static/instance. The class linker has a fast path for looking into the dex cache
1428 // and does not check static/instance if it hits it.
1429 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
1430 return nullptr;
1431 }
1432
1433 // Check access.
Andreas Gampefa4333d2017-02-14 11:10:34 -08001434 if (compiling_class == nullptr) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001435 if (!resolved_field->IsPublic()) {
1436 return nullptr;
1437 }
1438 } else if (!compiling_class->CanAccessResolvedField(resolved_field->GetDeclaringClass(),
1439 resolved_field,
1440 dex_compilation_unit_->GetDexCache().Get(),
1441 field_idx)) {
1442 return nullptr;
1443 }
1444
1445 if (is_put &&
1446 resolved_field->IsFinal() &&
1447 (compiling_class.Get() != resolved_field->GetDeclaringClass())) {
1448 // Final fields can only be updated within their own class.
1449 // TODO: Only allow it in constructors. b/34966607.
1450 return nullptr;
1451 }
1452
1453 return resolved_field;
1454}
1455
David Brazdildee58d62016-04-07 09:54:26 +00001456bool HInstructionBuilder::BuildStaticFieldAccess(const Instruction& instruction,
1457 uint32_t dex_pc,
1458 bool is_put) {
1459 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1460 uint16_t field_index = instruction.VRegB_21c();
1461
1462 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001463 ArtField* resolved_field = ResolveField(field_index, /* is_static */ true, is_put);
David Brazdildee58d62016-04-07 09:54:26 +00001464
1465 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001466 MaybeRecordStat(compilation_stats_,
1467 MethodCompilationStat::kUnresolvedField);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001468 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001469 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
1470 return true;
1471 }
1472
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001473 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001474
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001475 Handle<mirror::Class> klass = handles_->NewHandle(resolved_field->GetDeclaringClass());
1476 HLoadClass* constant = BuildLoadClass(klass->GetDexTypeIndex(),
1477 klass->GetDexFile(),
1478 klass,
1479 dex_pc,
1480 /* needs_access_check */ false);
1481
1482 if (constant == nullptr) {
1483 // The class cannot be referenced from this compiled code. Generate
1484 // an unresolved access.
Igor Murashkin1e065a52017-08-09 13:20:34 -07001485 MaybeRecordStat(compilation_stats_,
1486 MethodCompilationStat::kUnresolvedFieldNotAFastAccess);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001487 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
1488 return true;
David Brazdildee58d62016-04-07 09:54:26 +00001489 }
1490
David Brazdildee58d62016-04-07 09:54:26 +00001491 HInstruction* cls = constant;
David Brazdildee58d62016-04-07 09:54:26 +00001492 if (!IsInitialized(klass)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001493 cls = new (allocator_) HClinitCheck(constant, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001494 AppendInstruction(cls);
1495 }
1496
1497 uint16_t class_def_index = klass->GetDexClassDefIndex();
1498 if (is_put) {
1499 // We need to keep the class alive before loading the value.
1500 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1501 DCHECK_EQ(HPhi::ToPhiType(value->GetType()), HPhi::ToPhiType(field_type));
Vladimir Markoca6fff82017-10-03 14:49:14 +01001502 AppendInstruction(new (allocator_) HStaticFieldSet(cls,
1503 value,
1504 resolved_field,
1505 field_type,
1506 resolved_field->GetOffset(),
1507 resolved_field->IsVolatile(),
1508 field_index,
1509 class_def_index,
1510 *dex_file_,
1511 dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001512 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001513 AppendInstruction(new (allocator_) HStaticFieldGet(cls,
1514 resolved_field,
1515 field_type,
1516 resolved_field->GetOffset(),
1517 resolved_field->IsVolatile(),
1518 field_index,
1519 class_def_index,
1520 *dex_file_,
1521 dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001522 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1523 }
1524 return true;
1525}
1526
1527void HInstructionBuilder::BuildCheckedDivRem(uint16_t out_vreg,
Vladimir Markoca6fff82017-10-03 14:49:14 +01001528 uint16_t first_vreg,
1529 int64_t second_vreg_or_constant,
1530 uint32_t dex_pc,
1531 DataType::Type type,
1532 bool second_is_constant,
1533 bool isDiv) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001534 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
David Brazdildee58d62016-04-07 09:54:26 +00001535
1536 HInstruction* first = LoadLocal(first_vreg, type);
1537 HInstruction* second = nullptr;
1538 if (second_is_constant) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001539 if (type == DataType::Type::kInt32) {
David Brazdildee58d62016-04-07 09:54:26 +00001540 second = graph_->GetIntConstant(second_vreg_or_constant, dex_pc);
1541 } else {
1542 second = graph_->GetLongConstant(second_vreg_or_constant, dex_pc);
1543 }
1544 } else {
1545 second = LoadLocal(second_vreg_or_constant, type);
1546 }
1547
1548 if (!second_is_constant
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001549 || (type == DataType::Type::kInt32 && second->AsIntConstant()->GetValue() == 0)
1550 || (type == DataType::Type::kInt64 && second->AsLongConstant()->GetValue() == 0)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001551 second = new (allocator_) HDivZeroCheck(second, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001552 AppendInstruction(second);
1553 }
1554
1555 if (isDiv) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001556 AppendInstruction(new (allocator_) HDiv(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001557 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001558 AppendInstruction(new (allocator_) HRem(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001559 }
1560 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
1561}
1562
1563void HInstructionBuilder::BuildArrayAccess(const Instruction& instruction,
1564 uint32_t dex_pc,
1565 bool is_put,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001566 DataType::Type anticipated_type) {
David Brazdildee58d62016-04-07 09:54:26 +00001567 uint8_t source_or_dest_reg = instruction.VRegA_23x();
1568 uint8_t array_reg = instruction.VRegB_23x();
1569 uint8_t index_reg = instruction.VRegC_23x();
1570
David Brazdilc120bbe2016-04-22 16:57:00 +01001571 HInstruction* object = LoadNullCheckedLocal(array_reg, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001572 HInstruction* length = new (allocator_) HArrayLength(object, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001573 AppendInstruction(length);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001574 HInstruction* index = LoadLocal(index_reg, DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001575 index = new (allocator_) HBoundsCheck(index, length, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001576 AppendInstruction(index);
1577 if (is_put) {
1578 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
1579 // TODO: Insert a type check node if the type is Object.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001580 HArraySet* aset = new (allocator_) HArraySet(object, index, value, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001581 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1582 AppendInstruction(aset);
1583 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001584 HArrayGet* aget = new (allocator_) HArrayGet(object, index, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001585 ssa_builder_->MaybeAddAmbiguousArrayGet(aget);
1586 AppendInstruction(aget);
1587 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1588 }
1589 graph_->SetHasBoundsChecks(true);
1590}
1591
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001592HNewArray* HInstructionBuilder::BuildFilledNewArray(uint32_t dex_pc,
1593 dex::TypeIndex type_index,
1594 uint32_t number_of_vreg_arguments,
1595 bool is_range,
1596 uint32_t* args,
1597 uint32_t register_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001598 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments, dex_pc);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001599 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001600 HNewArray* const object = new (allocator_) HNewArray(cls, length, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001601 AppendInstruction(object);
1602
1603 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1604 DCHECK_EQ(descriptor[0], '[') << descriptor;
1605 char primitive = descriptor[1];
1606 DCHECK(primitive == 'I'
1607 || primitive == 'L'
1608 || primitive == '[') << descriptor;
1609 bool is_reference_array = (primitive == 'L') || (primitive == '[');
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001610 DataType::Type type = is_reference_array ? DataType::Type::kReference : DataType::Type::kInt32;
David Brazdildee58d62016-04-07 09:54:26 +00001611
1612 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
1613 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
1614 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001615 HArraySet* aset = new (allocator_) HArraySet(object, index, value, type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001616 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1617 AppendInstruction(aset);
1618 }
1619 latest_result_ = object;
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001620
1621 return object;
David Brazdildee58d62016-04-07 09:54:26 +00001622}
1623
1624template <typename T>
1625void HInstructionBuilder::BuildFillArrayData(HInstruction* object,
1626 const T* data,
1627 uint32_t element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001628 DataType::Type anticipated_type,
David Brazdildee58d62016-04-07 09:54:26 +00001629 uint32_t dex_pc) {
1630 for (uint32_t i = 0; i < element_count; ++i) {
1631 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1632 HInstruction* value = graph_->GetIntConstant(data[i], dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001633 HArraySet* aset = new (allocator_) HArraySet(object, index, value, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001634 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1635 AppendInstruction(aset);
1636 }
1637}
1638
1639void HInstructionBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
David Brazdilc120bbe2016-04-22 16:57:00 +01001640 HInstruction* array = LoadNullCheckedLocal(instruction.VRegA_31t(), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001641
1642 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
1643 const Instruction::ArrayDataPayload* payload =
1644 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_item_.insns_ + payload_offset);
1645 const uint8_t* data = payload->data;
1646 uint32_t element_count = payload->element_count;
1647
Vladimir Markoc69fba22016-09-06 16:49:15 +01001648 if (element_count == 0u) {
1649 // For empty payload we emit only the null check above.
1650 return;
1651 }
1652
Vladimir Markoca6fff82017-10-03 14:49:14 +01001653 HInstruction* length = new (allocator_) HArrayLength(array, dex_pc);
Vladimir Markoc69fba22016-09-06 16:49:15 +01001654 AppendInstruction(length);
1655
David Brazdildee58d62016-04-07 09:54:26 +00001656 // Implementation of this DEX instruction seems to be that the bounds check is
1657 // done before doing any stores.
1658 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001659 AppendInstruction(new (allocator_) HBoundsCheck(last_index, length, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001660
1661 switch (payload->element_width) {
1662 case 1:
David Brazdilc120bbe2016-04-22 16:57:00 +01001663 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001664 reinterpret_cast<const int8_t*>(data),
1665 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001666 DataType::Type::kInt8,
David Brazdildee58d62016-04-07 09:54:26 +00001667 dex_pc);
1668 break;
1669 case 2:
David Brazdilc120bbe2016-04-22 16:57:00 +01001670 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001671 reinterpret_cast<const int16_t*>(data),
1672 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001673 DataType::Type::kInt16,
David Brazdildee58d62016-04-07 09:54:26 +00001674 dex_pc);
1675 break;
1676 case 4:
David Brazdilc120bbe2016-04-22 16:57:00 +01001677 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001678 reinterpret_cast<const int32_t*>(data),
1679 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001680 DataType::Type::kInt32,
David Brazdildee58d62016-04-07 09:54:26 +00001681 dex_pc);
1682 break;
1683 case 8:
David Brazdilc120bbe2016-04-22 16:57:00 +01001684 BuildFillWideArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001685 reinterpret_cast<const int64_t*>(data),
1686 element_count,
1687 dex_pc);
1688 break;
1689 default:
1690 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1691 }
1692 graph_->SetHasBoundsChecks(true);
1693}
1694
1695void HInstructionBuilder::BuildFillWideArrayData(HInstruction* object,
1696 const int64_t* data,
1697 uint32_t element_count,
1698 uint32_t dex_pc) {
1699 for (uint32_t i = 0; i < element_count; ++i) {
1700 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1701 HInstruction* value = graph_->GetLongConstant(data[i], dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001702 HArraySet* aset =
1703 new (allocator_) HArraySet(object, index, value, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001704 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1705 AppendInstruction(aset);
1706 }
1707}
1708
1709static TypeCheckKind ComputeTypeCheckKind(Handle<mirror::Class> cls)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001710 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001711 if (cls == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +00001712 return TypeCheckKind::kUnresolvedCheck;
1713 } else if (cls->IsInterface()) {
1714 return TypeCheckKind::kInterfaceCheck;
1715 } else if (cls->IsArrayClass()) {
1716 if (cls->GetComponentType()->IsObjectClass()) {
1717 return TypeCheckKind::kArrayObjectCheck;
1718 } else if (cls->CannotBeAssignedFromOtherTypes()) {
1719 return TypeCheckKind::kExactCheck;
1720 } else {
1721 return TypeCheckKind::kArrayCheck;
1722 }
1723 } else if (cls->IsFinal()) {
1724 return TypeCheckKind::kExactCheck;
1725 } else if (cls->IsAbstract()) {
1726 return TypeCheckKind::kAbstractClassCheck;
1727 } else {
1728 return TypeCheckKind::kClassHierarchyCheck;
1729 }
1730}
1731
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001732HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index, uint32_t dex_pc) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001733 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001734 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001735 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00001736 Handle<mirror::Class> klass = handles_->NewHandle(compiler_driver_->ResolveClass(
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001737 soa, dex_compilation_unit_->GetDexCache(), class_loader, type_index, dex_compilation_unit_));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00001738
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001739 bool needs_access_check = true;
Andreas Gampefa4333d2017-02-14 11:10:34 -08001740 if (klass != nullptr) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001741 if (klass->IsPublic()) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001742 needs_access_check = false;
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001743 } else {
1744 mirror::Class* compiling_class = GetCompilingClass();
1745 if (compiling_class != nullptr && compiling_class->CanAccess(klass.Get())) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001746 needs_access_check = false;
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001747 }
1748 }
1749 }
1750
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001751 return BuildLoadClass(type_index, dex_file, klass, dex_pc, needs_access_check);
1752}
1753
1754HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index,
1755 const DexFile& dex_file,
1756 Handle<mirror::Class> klass,
1757 uint32_t dex_pc,
1758 bool needs_access_check) {
1759 // Try to find a reference in the compiling dex file.
1760 const DexFile* actual_dex_file = &dex_file;
1761 if (!IsSameDexFile(dex_file, *dex_compilation_unit_->GetDexFile())) {
1762 dex::TypeIndex local_type_index =
1763 klass->FindTypeIndexInOtherDexFile(*dex_compilation_unit_->GetDexFile());
1764 if (local_type_index.IsValid()) {
1765 type_index = local_type_index;
1766 actual_dex_file = dex_compilation_unit_->GetDexFile();
1767 }
1768 }
1769
1770 // Note: `klass` must be from `handles_`.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001771 HLoadClass* load_class = new (allocator_) HLoadClass(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001772 graph_->GetCurrentMethod(),
1773 type_index,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001774 *actual_dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001775 klass,
Andreas Gampefa4333d2017-02-14 11:10:34 -08001776 klass != nullptr && (klass.Get() == GetOutermostCompilingClass()),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001777 dex_pc,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001778 needs_access_check);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001779
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00001780 HLoadClass::LoadKind load_kind = HSharpening::ComputeLoadClassKind(load_class,
1781 code_generator_,
1782 compiler_driver_,
1783 *dex_compilation_unit_);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001784
1785 if (load_kind == HLoadClass::LoadKind::kInvalid) {
1786 // We actually cannot reference this class, we're forced to bail.
1787 return nullptr;
1788 }
1789 // Append the instruction first, as setting the load kind affects the inputs.
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001790 AppendInstruction(load_class);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001791 load_class->SetLoadKind(load_kind);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001792 return load_class;
1793}
1794
David Brazdildee58d62016-04-07 09:54:26 +00001795void HInstructionBuilder::BuildTypeCheck(const Instruction& instruction,
1796 uint8_t destination,
1797 uint8_t reference,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001798 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +00001799 uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001800 HInstruction* object = LoadLocal(reference, DataType::Type::kReference);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001801 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001802
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001803 ScopedObjectAccess soa(Thread::Current());
1804 TypeCheckKind check_kind = ComputeTypeCheckKind(cls->GetClass());
David Brazdildee58d62016-04-07 09:54:26 +00001805 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001806 AppendInstruction(new (allocator_) HInstanceOf(object, cls, check_kind, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001807 UpdateLocal(destination, current_block_->GetLastInstruction());
1808 } else {
1809 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
1810 // We emit a CheckCast followed by a BoundType. CheckCast is a statement
1811 // which may throw. If it succeeds BoundType sets the new type of `object`
1812 // for all subsequent uses.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001813 AppendInstruction(new (allocator_) HCheckCast(object, cls, check_kind, dex_pc));
1814 AppendInstruction(new (allocator_) HBoundType(object, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001815 UpdateLocal(reference, current_block_->GetLastInstruction());
1816 }
1817}
1818
Vladimir Marko0b66d612017-03-13 14:50:04 +00001819bool HInstructionBuilder::NeedsAccessCheck(dex::TypeIndex type_index, bool* finalizable) const {
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001820 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
1821 LookupReferrerClass(), LookupResolvedType(type_index, *dex_compilation_unit_), finalizable);
David Brazdildee58d62016-04-07 09:54:26 +00001822}
1823
1824bool HInstructionBuilder::CanDecodeQuickenedInfo() const {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001825 return !quicken_info_.IsNull();
David Brazdildee58d62016-04-07 09:54:26 +00001826}
1827
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001828uint16_t HInstructionBuilder::LookupQuickenedInfo(uint32_t quicken_index) {
1829 DCHECK(CanDecodeQuickenedInfo());
1830 return quicken_info_.GetData(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00001831}
1832
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001833bool HInstructionBuilder::ProcessDexInstruction(const Instruction& instruction,
1834 uint32_t dex_pc,
1835 size_t quicken_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001836 switch (instruction.Opcode()) {
1837 case Instruction::CONST_4: {
1838 int32_t register_index = instruction.VRegA();
1839 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n(), dex_pc);
1840 UpdateLocal(register_index, constant);
1841 break;
1842 }
1843
1844 case Instruction::CONST_16: {
1845 int32_t register_index = instruction.VRegA();
1846 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s(), dex_pc);
1847 UpdateLocal(register_index, constant);
1848 break;
1849 }
1850
1851 case Instruction::CONST: {
1852 int32_t register_index = instruction.VRegA();
1853 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i(), dex_pc);
1854 UpdateLocal(register_index, constant);
1855 break;
1856 }
1857
1858 case Instruction::CONST_HIGH16: {
1859 int32_t register_index = instruction.VRegA();
1860 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16, dex_pc);
1861 UpdateLocal(register_index, constant);
1862 break;
1863 }
1864
1865 case Instruction::CONST_WIDE_16: {
1866 int32_t register_index = instruction.VRegA();
1867 // Get 16 bits of constant value, sign extended to 64 bits.
1868 int64_t value = instruction.VRegB_21s();
1869 value <<= 48;
1870 value >>= 48;
1871 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1872 UpdateLocal(register_index, constant);
1873 break;
1874 }
1875
1876 case Instruction::CONST_WIDE_32: {
1877 int32_t register_index = instruction.VRegA();
1878 // Get 32 bits of constant value, sign extended to 64 bits.
1879 int64_t value = instruction.VRegB_31i();
1880 value <<= 32;
1881 value >>= 32;
1882 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1883 UpdateLocal(register_index, constant);
1884 break;
1885 }
1886
1887 case Instruction::CONST_WIDE: {
1888 int32_t register_index = instruction.VRegA();
1889 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l(), dex_pc);
1890 UpdateLocal(register_index, constant);
1891 break;
1892 }
1893
1894 case Instruction::CONST_WIDE_HIGH16: {
1895 int32_t register_index = instruction.VRegA();
1896 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
1897 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1898 UpdateLocal(register_index, constant);
1899 break;
1900 }
1901
1902 // Note that the SSA building will refine the types.
1903 case Instruction::MOVE:
1904 case Instruction::MOVE_FROM16:
1905 case Instruction::MOVE_16: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001906 HInstruction* value = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +00001907 UpdateLocal(instruction.VRegA(), value);
1908 break;
1909 }
1910
1911 // Note that the SSA building will refine the types.
1912 case Instruction::MOVE_WIDE:
1913 case Instruction::MOVE_WIDE_FROM16:
1914 case Instruction::MOVE_WIDE_16: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001915 HInstruction* value = LoadLocal(instruction.VRegB(), DataType::Type::kInt64);
David Brazdildee58d62016-04-07 09:54:26 +00001916 UpdateLocal(instruction.VRegA(), value);
1917 break;
1918 }
1919
1920 case Instruction::MOVE_OBJECT:
1921 case Instruction::MOVE_OBJECT_16:
1922 case Instruction::MOVE_OBJECT_FROM16: {
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01001923 // The verifier has no notion of a null type, so a move-object of constant 0
1924 // will lead to the same constant 0 in the destination register. To mimic
1925 // this behavior, we just pretend we haven't seen a type change (int to reference)
1926 // for the 0 constant and phis. We rely on our type propagation to eventually get the
1927 // types correct.
1928 uint32_t reg_number = instruction.VRegB();
1929 HInstruction* value = (*current_locals_)[reg_number];
1930 if (value->IsIntConstant()) {
1931 DCHECK_EQ(value->AsIntConstant()->GetValue(), 0);
1932 } else if (value->IsPhi()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001933 DCHECK(value->GetType() == DataType::Type::kInt32 ||
1934 value->GetType() == DataType::Type::kReference);
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01001935 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001936 value = LoadLocal(reg_number, DataType::Type::kReference);
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01001937 }
David Brazdildee58d62016-04-07 09:54:26 +00001938 UpdateLocal(instruction.VRegA(), value);
1939 break;
1940 }
1941
1942 case Instruction::RETURN_VOID_NO_BARRIER:
1943 case Instruction::RETURN_VOID: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001944 BuildReturn(instruction, DataType::Type::kVoid, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001945 break;
1946 }
1947
1948#define IF_XX(comparison, cond) \
1949 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
1950 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
1951
1952 IF_XX(HEqual, EQ);
1953 IF_XX(HNotEqual, NE);
1954 IF_XX(HLessThan, LT);
1955 IF_XX(HLessThanOrEqual, LE);
1956 IF_XX(HGreaterThan, GT);
1957 IF_XX(HGreaterThanOrEqual, GE);
1958
1959 case Instruction::GOTO:
1960 case Instruction::GOTO_16:
1961 case Instruction::GOTO_32: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001962 AppendInstruction(new (allocator_) HGoto(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001963 current_block_ = nullptr;
1964 break;
1965 }
1966
1967 case Instruction::RETURN: {
1968 BuildReturn(instruction, return_type_, dex_pc);
1969 break;
1970 }
1971
1972 case Instruction::RETURN_OBJECT: {
1973 BuildReturn(instruction, return_type_, dex_pc);
1974 break;
1975 }
1976
1977 case Instruction::RETURN_WIDE: {
1978 BuildReturn(instruction, return_type_, dex_pc);
1979 break;
1980 }
1981
1982 case Instruction::INVOKE_DIRECT:
1983 case Instruction::INVOKE_INTERFACE:
1984 case Instruction::INVOKE_STATIC:
1985 case Instruction::INVOKE_SUPER:
1986 case Instruction::INVOKE_VIRTUAL:
1987 case Instruction::INVOKE_VIRTUAL_QUICK: {
1988 uint16_t method_idx;
1989 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_QUICK) {
1990 if (!CanDecodeQuickenedInfo()) {
1991 return false;
1992 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001993 method_idx = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00001994 } else {
1995 method_idx = instruction.VRegB_35c();
1996 }
1997 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
1998 uint32_t args[5];
1999 instruction.GetVarArgs(args);
2000 if (!BuildInvoke(instruction, dex_pc, method_idx,
2001 number_of_vreg_arguments, false, args, -1)) {
2002 return false;
2003 }
2004 break;
2005 }
2006
2007 case Instruction::INVOKE_DIRECT_RANGE:
2008 case Instruction::INVOKE_INTERFACE_RANGE:
2009 case Instruction::INVOKE_STATIC_RANGE:
2010 case Instruction::INVOKE_SUPER_RANGE:
2011 case Instruction::INVOKE_VIRTUAL_RANGE:
2012 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2013 uint16_t method_idx;
2014 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK) {
2015 if (!CanDecodeQuickenedInfo()) {
2016 return false;
2017 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002018 method_idx = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00002019 } else {
2020 method_idx = instruction.VRegB_3rc();
2021 }
2022 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
2023 uint32_t register_index = instruction.VRegC();
2024 if (!BuildInvoke(instruction, dex_pc, method_idx,
2025 number_of_vreg_arguments, true, nullptr, register_index)) {
2026 return false;
2027 }
2028 break;
2029 }
2030
Orion Hodsonac141392017-01-13 11:53:47 +00002031 case Instruction::INVOKE_POLYMORPHIC: {
2032 uint16_t method_idx = instruction.VRegB_45cc();
2033 uint16_t proto_idx = instruction.VRegH_45cc();
2034 uint32_t number_of_vreg_arguments = instruction.VRegA_45cc();
2035 uint32_t args[5];
2036 instruction.GetVarArgs(args);
2037 return BuildInvokePolymorphic(instruction,
2038 dex_pc,
2039 method_idx,
2040 proto_idx,
2041 number_of_vreg_arguments,
2042 false,
2043 args,
2044 -1);
2045 }
2046
2047 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
2048 uint16_t method_idx = instruction.VRegB_4rcc();
2049 uint16_t proto_idx = instruction.VRegH_4rcc();
2050 uint32_t number_of_vreg_arguments = instruction.VRegA_4rcc();
2051 uint32_t register_index = instruction.VRegC_4rcc();
2052 return BuildInvokePolymorphic(instruction,
2053 dex_pc,
2054 method_idx,
2055 proto_idx,
2056 number_of_vreg_arguments,
2057 true,
2058 nullptr,
2059 register_index);
2060 }
2061
David Brazdildee58d62016-04-07 09:54:26 +00002062 case Instruction::NEG_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002063 Unop_12x<HNeg>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002064 break;
2065 }
2066
2067 case Instruction::NEG_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002068 Unop_12x<HNeg>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002069 break;
2070 }
2071
2072 case Instruction::NEG_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002073 Unop_12x<HNeg>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002074 break;
2075 }
2076
2077 case Instruction::NEG_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002078 Unop_12x<HNeg>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002079 break;
2080 }
2081
2082 case Instruction::NOT_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002083 Unop_12x<HNot>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002084 break;
2085 }
2086
2087 case Instruction::NOT_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002088 Unop_12x<HNot>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002089 break;
2090 }
2091
2092 case Instruction::INT_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002093 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002094 break;
2095 }
2096
2097 case Instruction::INT_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002098 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002099 break;
2100 }
2101
2102 case Instruction::INT_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002103 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002104 break;
2105 }
2106
2107 case Instruction::LONG_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002108 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002109 break;
2110 }
2111
2112 case Instruction::LONG_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002113 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002114 break;
2115 }
2116
2117 case Instruction::LONG_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002118 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002119 break;
2120 }
2121
2122 case Instruction::FLOAT_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002123 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002124 break;
2125 }
2126
2127 case Instruction::FLOAT_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002128 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002129 break;
2130 }
2131
2132 case Instruction::FLOAT_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002133 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002134 break;
2135 }
2136
2137 case Instruction::DOUBLE_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002138 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002139 break;
2140 }
2141
2142 case Instruction::DOUBLE_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002143 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002144 break;
2145 }
2146
2147 case Instruction::DOUBLE_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002148 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002149 break;
2150 }
2151
2152 case Instruction::INT_TO_BYTE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002153 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt8, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002154 break;
2155 }
2156
2157 case Instruction::INT_TO_SHORT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002158 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt16, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002159 break;
2160 }
2161
2162 case Instruction::INT_TO_CHAR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002163 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kUint16, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002164 break;
2165 }
2166
2167 case Instruction::ADD_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002168 Binop_23x<HAdd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002169 break;
2170 }
2171
2172 case Instruction::ADD_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002173 Binop_23x<HAdd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002174 break;
2175 }
2176
2177 case Instruction::ADD_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002178 Binop_23x<HAdd>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002179 break;
2180 }
2181
2182 case Instruction::ADD_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002183 Binop_23x<HAdd>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002184 break;
2185 }
2186
2187 case Instruction::SUB_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002188 Binop_23x<HSub>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002189 break;
2190 }
2191
2192 case Instruction::SUB_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002193 Binop_23x<HSub>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002194 break;
2195 }
2196
2197 case Instruction::SUB_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002198 Binop_23x<HSub>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002199 break;
2200 }
2201
2202 case Instruction::SUB_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002203 Binop_23x<HSub>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002204 break;
2205 }
2206
2207 case Instruction::ADD_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002208 Binop_12x<HAdd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002209 break;
2210 }
2211
2212 case Instruction::MUL_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002213 Binop_23x<HMul>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002214 break;
2215 }
2216
2217 case Instruction::MUL_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002218 Binop_23x<HMul>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002219 break;
2220 }
2221
2222 case Instruction::MUL_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002223 Binop_23x<HMul>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002224 break;
2225 }
2226
2227 case Instruction::MUL_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002228 Binop_23x<HMul>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002229 break;
2230 }
2231
2232 case Instruction::DIV_INT: {
2233 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002234 dex_pc, DataType::Type::kInt32, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002235 break;
2236 }
2237
2238 case Instruction::DIV_LONG: {
2239 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002240 dex_pc, DataType::Type::kInt64, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002241 break;
2242 }
2243
2244 case Instruction::DIV_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002245 Binop_23x<HDiv>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002246 break;
2247 }
2248
2249 case Instruction::DIV_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002250 Binop_23x<HDiv>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002251 break;
2252 }
2253
2254 case Instruction::REM_INT: {
2255 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002256 dex_pc, DataType::Type::kInt32, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002257 break;
2258 }
2259
2260 case Instruction::REM_LONG: {
2261 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002262 dex_pc, DataType::Type::kInt64, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002263 break;
2264 }
2265
2266 case Instruction::REM_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002267 Binop_23x<HRem>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002268 break;
2269 }
2270
2271 case Instruction::REM_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002272 Binop_23x<HRem>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002273 break;
2274 }
2275
2276 case Instruction::AND_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002277 Binop_23x<HAnd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002278 break;
2279 }
2280
2281 case Instruction::AND_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002282 Binop_23x<HAnd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002283 break;
2284 }
2285
2286 case Instruction::SHL_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002287 Binop_23x_shift<HShl>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002288 break;
2289 }
2290
2291 case Instruction::SHL_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002292 Binop_23x_shift<HShl>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002293 break;
2294 }
2295
2296 case Instruction::SHR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002297 Binop_23x_shift<HShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002298 break;
2299 }
2300
2301 case Instruction::SHR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002302 Binop_23x_shift<HShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002303 break;
2304 }
2305
2306 case Instruction::USHR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002307 Binop_23x_shift<HUShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002308 break;
2309 }
2310
2311 case Instruction::USHR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002312 Binop_23x_shift<HUShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002313 break;
2314 }
2315
2316 case Instruction::OR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002317 Binop_23x<HOr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002318 break;
2319 }
2320
2321 case Instruction::OR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002322 Binop_23x<HOr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002323 break;
2324 }
2325
2326 case Instruction::XOR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002327 Binop_23x<HXor>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002328 break;
2329 }
2330
2331 case Instruction::XOR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002332 Binop_23x<HXor>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002333 break;
2334 }
2335
2336 case Instruction::ADD_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002337 Binop_12x<HAdd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002338 break;
2339 }
2340
2341 case Instruction::ADD_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002342 Binop_12x<HAdd>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002343 break;
2344 }
2345
2346 case Instruction::ADD_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002347 Binop_12x<HAdd>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002348 break;
2349 }
2350
2351 case Instruction::SUB_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002352 Binop_12x<HSub>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002353 break;
2354 }
2355
2356 case Instruction::SUB_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002357 Binop_12x<HSub>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002358 break;
2359 }
2360
2361 case Instruction::SUB_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002362 Binop_12x<HSub>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002363 break;
2364 }
2365
2366 case Instruction::SUB_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002367 Binop_12x<HSub>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002368 break;
2369 }
2370
2371 case Instruction::MUL_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002372 Binop_12x<HMul>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002373 break;
2374 }
2375
2376 case Instruction::MUL_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002377 Binop_12x<HMul>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002378 break;
2379 }
2380
2381 case Instruction::MUL_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002382 Binop_12x<HMul>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002383 break;
2384 }
2385
2386 case Instruction::MUL_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002387 Binop_12x<HMul>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002388 break;
2389 }
2390
2391 case Instruction::DIV_INT_2ADDR: {
2392 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002393 dex_pc, DataType::Type::kInt32, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002394 break;
2395 }
2396
2397 case Instruction::DIV_LONG_2ADDR: {
2398 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002399 dex_pc, DataType::Type::kInt64, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002400 break;
2401 }
2402
2403 case Instruction::REM_INT_2ADDR: {
2404 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002405 dex_pc, DataType::Type::kInt32, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002406 break;
2407 }
2408
2409 case Instruction::REM_LONG_2ADDR: {
2410 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002411 dex_pc, DataType::Type::kInt64, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002412 break;
2413 }
2414
2415 case Instruction::REM_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002416 Binop_12x<HRem>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002417 break;
2418 }
2419
2420 case Instruction::REM_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002421 Binop_12x<HRem>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002422 break;
2423 }
2424
2425 case Instruction::SHL_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002426 Binop_12x_shift<HShl>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002427 break;
2428 }
2429
2430 case Instruction::SHL_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002431 Binop_12x_shift<HShl>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002432 break;
2433 }
2434
2435 case Instruction::SHR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002436 Binop_12x_shift<HShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002437 break;
2438 }
2439
2440 case Instruction::SHR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002441 Binop_12x_shift<HShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002442 break;
2443 }
2444
2445 case Instruction::USHR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002446 Binop_12x_shift<HUShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002447 break;
2448 }
2449
2450 case Instruction::USHR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002451 Binop_12x_shift<HUShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002452 break;
2453 }
2454
2455 case Instruction::DIV_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002456 Binop_12x<HDiv>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002457 break;
2458 }
2459
2460 case Instruction::DIV_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002461 Binop_12x<HDiv>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002462 break;
2463 }
2464
2465 case Instruction::AND_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002466 Binop_12x<HAnd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002467 break;
2468 }
2469
2470 case Instruction::AND_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002471 Binop_12x<HAnd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002472 break;
2473 }
2474
2475 case Instruction::OR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002476 Binop_12x<HOr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002477 break;
2478 }
2479
2480 case Instruction::OR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002481 Binop_12x<HOr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002482 break;
2483 }
2484
2485 case Instruction::XOR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002486 Binop_12x<HXor>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002487 break;
2488 }
2489
2490 case Instruction::XOR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002491 Binop_12x<HXor>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002492 break;
2493 }
2494
2495 case Instruction::ADD_INT_LIT16: {
2496 Binop_22s<HAdd>(instruction, false, dex_pc);
2497 break;
2498 }
2499
2500 case Instruction::AND_INT_LIT16: {
2501 Binop_22s<HAnd>(instruction, false, dex_pc);
2502 break;
2503 }
2504
2505 case Instruction::OR_INT_LIT16: {
2506 Binop_22s<HOr>(instruction, false, dex_pc);
2507 break;
2508 }
2509
2510 case Instruction::XOR_INT_LIT16: {
2511 Binop_22s<HXor>(instruction, false, dex_pc);
2512 break;
2513 }
2514
2515 case Instruction::RSUB_INT: {
2516 Binop_22s<HSub>(instruction, true, dex_pc);
2517 break;
2518 }
2519
2520 case Instruction::MUL_INT_LIT16: {
2521 Binop_22s<HMul>(instruction, false, dex_pc);
2522 break;
2523 }
2524
2525 case Instruction::ADD_INT_LIT8: {
2526 Binop_22b<HAdd>(instruction, false, dex_pc);
2527 break;
2528 }
2529
2530 case Instruction::AND_INT_LIT8: {
2531 Binop_22b<HAnd>(instruction, false, dex_pc);
2532 break;
2533 }
2534
2535 case Instruction::OR_INT_LIT8: {
2536 Binop_22b<HOr>(instruction, false, dex_pc);
2537 break;
2538 }
2539
2540 case Instruction::XOR_INT_LIT8: {
2541 Binop_22b<HXor>(instruction, false, dex_pc);
2542 break;
2543 }
2544
2545 case Instruction::RSUB_INT_LIT8: {
2546 Binop_22b<HSub>(instruction, true, dex_pc);
2547 break;
2548 }
2549
2550 case Instruction::MUL_INT_LIT8: {
2551 Binop_22b<HMul>(instruction, false, dex_pc);
2552 break;
2553 }
2554
2555 case Instruction::DIV_INT_LIT16:
2556 case Instruction::DIV_INT_LIT8: {
2557 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002558 dex_pc, DataType::Type::kInt32, true, true);
David Brazdildee58d62016-04-07 09:54:26 +00002559 break;
2560 }
2561
2562 case Instruction::REM_INT_LIT16:
2563 case Instruction::REM_INT_LIT8: {
2564 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002565 dex_pc, DataType::Type::kInt32, true, false);
David Brazdildee58d62016-04-07 09:54:26 +00002566 break;
2567 }
2568
2569 case Instruction::SHL_INT_LIT8: {
2570 Binop_22b<HShl>(instruction, false, dex_pc);
2571 break;
2572 }
2573
2574 case Instruction::SHR_INT_LIT8: {
2575 Binop_22b<HShr>(instruction, false, dex_pc);
2576 break;
2577 }
2578
2579 case Instruction::USHR_INT_LIT8: {
2580 Binop_22b<HUShr>(instruction, false, dex_pc);
2581 break;
2582 }
2583
2584 case Instruction::NEW_INSTANCE: {
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002585 HNewInstance* new_instance =
2586 BuildNewInstance(dex::TypeIndex(instruction.VRegB_21c()), dex_pc);
2587 DCHECK(new_instance != nullptr);
2588
David Brazdildee58d62016-04-07 09:54:26 +00002589 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002590 BuildConstructorFenceForAllocation(new_instance);
David Brazdildee58d62016-04-07 09:54:26 +00002591 break;
2592 }
2593
2594 case Instruction::NEW_ARRAY: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002595 dex::TypeIndex type_index(instruction.VRegC_22c());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002596 HInstruction* length = LoadLocal(instruction.VRegB_22c(), DataType::Type::kInt32);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002597 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002598
Vladimir Markoca6fff82017-10-03 14:49:14 +01002599 HNewArray* new_array = new (allocator_) HNewArray(cls, length, dex_pc);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002600 AppendInstruction(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002601 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002602 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002603 break;
2604 }
2605
2606 case Instruction::FILLED_NEW_ARRAY: {
2607 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002608 dex::TypeIndex type_index(instruction.VRegB_35c());
David Brazdildee58d62016-04-07 09:54:26 +00002609 uint32_t args[5];
2610 instruction.GetVarArgs(args);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002611 HNewArray* new_array = BuildFilledNewArray(dex_pc,
2612 type_index,
2613 number_of_vreg_arguments,
2614 /* is_range */ false,
2615 args,
2616 /* register_index */ 0);
2617 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002618 break;
2619 }
2620
2621 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2622 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002623 dex::TypeIndex type_index(instruction.VRegB_3rc());
David Brazdildee58d62016-04-07 09:54:26 +00002624 uint32_t register_index = instruction.VRegC_3rc();
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002625 HNewArray* new_array = BuildFilledNewArray(dex_pc,
2626 type_index,
2627 number_of_vreg_arguments,
2628 /* is_range */ true,
2629 /* args*/ nullptr,
2630 register_index);
2631 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002632 break;
2633 }
2634
2635 case Instruction::FILL_ARRAY_DATA: {
2636 BuildFillArrayData(instruction, dex_pc);
2637 break;
2638 }
2639
2640 case Instruction::MOVE_RESULT:
2641 case Instruction::MOVE_RESULT_WIDE:
2642 case Instruction::MOVE_RESULT_OBJECT: {
2643 DCHECK(latest_result_ != nullptr);
2644 UpdateLocal(instruction.VRegA(), latest_result_);
2645 latest_result_ = nullptr;
2646 break;
2647 }
2648
2649 case Instruction::CMP_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002650 Binop_23x_cmp(instruction, DataType::Type::kInt64, ComparisonBias::kNoBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002651 break;
2652 }
2653
2654 case Instruction::CMPG_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002655 Binop_23x_cmp(instruction, DataType::Type::kFloat32, ComparisonBias::kGtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002656 break;
2657 }
2658
2659 case Instruction::CMPG_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002660 Binop_23x_cmp(instruction, DataType::Type::kFloat64, ComparisonBias::kGtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002661 break;
2662 }
2663
2664 case Instruction::CMPL_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002665 Binop_23x_cmp(instruction, DataType::Type::kFloat32, ComparisonBias::kLtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002666 break;
2667 }
2668
2669 case Instruction::CMPL_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002670 Binop_23x_cmp(instruction, DataType::Type::kFloat64, ComparisonBias::kLtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002671 break;
2672 }
2673
2674 case Instruction::NOP:
2675 break;
2676
2677 case Instruction::IGET:
2678 case Instruction::IGET_QUICK:
2679 case Instruction::IGET_WIDE:
2680 case Instruction::IGET_WIDE_QUICK:
2681 case Instruction::IGET_OBJECT:
2682 case Instruction::IGET_OBJECT_QUICK:
2683 case Instruction::IGET_BOOLEAN:
2684 case Instruction::IGET_BOOLEAN_QUICK:
2685 case Instruction::IGET_BYTE:
2686 case Instruction::IGET_BYTE_QUICK:
2687 case Instruction::IGET_CHAR:
2688 case Instruction::IGET_CHAR_QUICK:
2689 case Instruction::IGET_SHORT:
2690 case Instruction::IGET_SHORT_QUICK: {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002691 if (!BuildInstanceFieldAccess(instruction, dex_pc, false, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00002692 return false;
2693 }
2694 break;
2695 }
2696
2697 case Instruction::IPUT:
2698 case Instruction::IPUT_QUICK:
2699 case Instruction::IPUT_WIDE:
2700 case Instruction::IPUT_WIDE_QUICK:
2701 case Instruction::IPUT_OBJECT:
2702 case Instruction::IPUT_OBJECT_QUICK:
2703 case Instruction::IPUT_BOOLEAN:
2704 case Instruction::IPUT_BOOLEAN_QUICK:
2705 case Instruction::IPUT_BYTE:
2706 case Instruction::IPUT_BYTE_QUICK:
2707 case Instruction::IPUT_CHAR:
2708 case Instruction::IPUT_CHAR_QUICK:
2709 case Instruction::IPUT_SHORT:
2710 case Instruction::IPUT_SHORT_QUICK: {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002711 if (!BuildInstanceFieldAccess(instruction, dex_pc, true, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00002712 return false;
2713 }
2714 break;
2715 }
2716
2717 case Instruction::SGET:
2718 case Instruction::SGET_WIDE:
2719 case Instruction::SGET_OBJECT:
2720 case Instruction::SGET_BOOLEAN:
2721 case Instruction::SGET_BYTE:
2722 case Instruction::SGET_CHAR:
2723 case Instruction::SGET_SHORT: {
2724 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
2725 return false;
2726 }
2727 break;
2728 }
2729
2730 case Instruction::SPUT:
2731 case Instruction::SPUT_WIDE:
2732 case Instruction::SPUT_OBJECT:
2733 case Instruction::SPUT_BOOLEAN:
2734 case Instruction::SPUT_BYTE:
2735 case Instruction::SPUT_CHAR:
2736 case Instruction::SPUT_SHORT: {
2737 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
2738 return false;
2739 }
2740 break;
2741 }
2742
2743#define ARRAY_XX(kind, anticipated_type) \
2744 case Instruction::AGET##kind: { \
2745 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
2746 break; \
2747 } \
2748 case Instruction::APUT##kind: { \
2749 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
2750 break; \
2751 }
2752
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002753 ARRAY_XX(, DataType::Type::kInt32);
2754 ARRAY_XX(_WIDE, DataType::Type::kInt64);
2755 ARRAY_XX(_OBJECT, DataType::Type::kReference);
2756 ARRAY_XX(_BOOLEAN, DataType::Type::kBool);
2757 ARRAY_XX(_BYTE, DataType::Type::kInt8);
2758 ARRAY_XX(_CHAR, DataType::Type::kUint16);
2759 ARRAY_XX(_SHORT, DataType::Type::kInt16);
David Brazdildee58d62016-04-07 09:54:26 +00002760
2761 case Instruction::ARRAY_LENGTH: {
David Brazdilc120bbe2016-04-22 16:57:00 +01002762 HInstruction* object = LoadNullCheckedLocal(instruction.VRegB_12x(), dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002763 AppendInstruction(new (allocator_) HArrayLength(object, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002764 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
2765 break;
2766 }
2767
2768 case Instruction::CONST_STRING: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08002769 dex::StringIndex string_index(instruction.VRegB_21c());
Vladimir Markoca6fff82017-10-03 14:49:14 +01002770 AppendInstruction(new (allocator_) HLoadString(graph_->GetCurrentMethod(),
2771 string_index,
2772 *dex_file_,
2773 dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002774 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2775 break;
2776 }
2777
2778 case Instruction::CONST_STRING_JUMBO: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08002779 dex::StringIndex string_index(instruction.VRegB_31c());
Vladimir Markoca6fff82017-10-03 14:49:14 +01002780 AppendInstruction(new (allocator_) HLoadString(graph_->GetCurrentMethod(),
2781 string_index,
2782 *dex_file_,
2783 dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002784 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
2785 break;
2786 }
2787
2788 case Instruction::CONST_CLASS: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002789 dex::TypeIndex type_index(instruction.VRegB_21c());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002790 BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002791 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2792 break;
2793 }
2794
2795 case Instruction::MOVE_EXCEPTION: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002796 AppendInstruction(new (allocator_) HLoadException(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002797 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
Vladimir Markoca6fff82017-10-03 14:49:14 +01002798 AppendInstruction(new (allocator_) HClearException(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002799 break;
2800 }
2801
2802 case Instruction::THROW: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002803 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002804 AppendInstruction(new (allocator_) HThrow(exception, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002805 // We finished building this block. Set the current block to null to avoid
2806 // adding dead instructions to it.
2807 current_block_ = nullptr;
2808 break;
2809 }
2810
2811 case Instruction::INSTANCE_OF: {
2812 uint8_t destination = instruction.VRegA_22c();
2813 uint8_t reference = instruction.VRegB_22c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002814 dex::TypeIndex type_index(instruction.VRegC_22c());
David Brazdildee58d62016-04-07 09:54:26 +00002815 BuildTypeCheck(instruction, destination, reference, type_index, dex_pc);
2816 break;
2817 }
2818
2819 case Instruction::CHECK_CAST: {
2820 uint8_t reference = instruction.VRegA_21c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002821 dex::TypeIndex type_index(instruction.VRegB_21c());
David Brazdildee58d62016-04-07 09:54:26 +00002822 BuildTypeCheck(instruction, -1, reference, type_index, dex_pc);
2823 break;
2824 }
2825
2826 case Instruction::MONITOR_ENTER: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002827 AppendInstruction(new (allocator_) HMonitorOperation(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002828 LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference),
David Brazdildee58d62016-04-07 09:54:26 +00002829 HMonitorOperation::OperationKind::kEnter,
2830 dex_pc));
2831 break;
2832 }
2833
2834 case Instruction::MONITOR_EXIT: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002835 AppendInstruction(new (allocator_) HMonitorOperation(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002836 LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference),
David Brazdildee58d62016-04-07 09:54:26 +00002837 HMonitorOperation::OperationKind::kExit,
2838 dex_pc));
2839 break;
2840 }
2841
2842 case Instruction::SPARSE_SWITCH:
2843 case Instruction::PACKED_SWITCH: {
2844 BuildSwitch(instruction, dex_pc);
2845 break;
2846 }
2847
2848 default:
2849 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07002850 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00002851 << " because of unhandled instruction "
2852 << instruction.Name();
Igor Murashkin1e065a52017-08-09 13:20:34 -07002853 MaybeRecordStat(compilation_stats_,
2854 MethodCompilationStat::kNotCompiledUnhandledInstruction);
David Brazdildee58d62016-04-07 09:54:26 +00002855 return false;
2856 }
2857 return true;
2858} // NOLINT(readability/fn_size)
2859
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002860ObjPtr<mirror::Class> HInstructionBuilder::LookupResolvedType(
2861 dex::TypeIndex type_index,
2862 const DexCompilationUnit& compilation_unit) const {
2863 return ClassLinker::LookupResolvedType(
2864 type_index, compilation_unit.GetDexCache().Get(), compilation_unit.GetClassLoader().Get());
2865}
2866
2867ObjPtr<mirror::Class> HInstructionBuilder::LookupReferrerClass() const {
2868 // TODO: Cache the result in a Handle<mirror::Class>.
2869 const DexFile::MethodId& method_id =
2870 dex_compilation_unit_->GetDexFile()->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
2871 return LookupResolvedType(method_id.class_idx_, *dex_compilation_unit_);
2872}
2873
David Brazdildee58d62016-04-07 09:54:26 +00002874} // namespace art