blob: 1650fd1ced8f30730fbbbd295d6a3c590fa4bf10 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002 * Copyright (C) 2014 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
Nicolas Geoffraye5038322014-07-04 09:41:32 +010017#include "builder.h"
18
Mathieu Chartierc7853442015-03-27 14:35:38 -070019#include "art_field-inl.h"
Andreas Gamped881df52014-11-24 23:28:39 -080020#include "base/logging.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010021#include "class_linker.h"
Jeff Hao848f70a2014-01-15 13:49:50 -080022#include "dex/verified_method.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000023#include "dex_file-inl.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000024#include "dex_instruction-inl.h"
Roland Levillain4c0eb422015-04-24 16:43:49 +010025#include "dex/verified_method.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010026#include "driver/compiler_driver-inl.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000027#include "driver/compiler_options.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010028#include "mirror/class_loader.h"
29#include "mirror/dex_cache.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000030#include "nodes.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000031#include "primitive.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010032#include "scoped_thread_state_change.h"
33#include "thread.h"
Vladimir Marko58155012015-08-19 12:49:41 +000034#include "utils/dex_cache_arrays_layout-inl.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000035
36namespace art {
37
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010038/**
39 * Helper class to add HTemporary instructions. This class is used when
40 * converting a DEX instruction to multiple HInstruction, and where those
41 * instructions do not die at the following instruction, but instead spans
42 * multiple instructions.
43 */
44class Temporaries : public ValueObject {
45 public:
Calin Juravlef97f9fb2014-11-11 15:38:19 +000046 explicit Temporaries(HGraph* graph) : graph_(graph), index_(0) {}
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010047
48 void Add(HInstruction* instruction) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +000049 HInstruction* temp = new (graph_->GetArena()) HTemporary(index_);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010050 instruction->GetBlock()->AddInstruction(temp);
Calin Juravlef97f9fb2014-11-11 15:38:19 +000051
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010052 DCHECK(temp->GetPrevious() == instruction);
Calin Juravlef97f9fb2014-11-11 15:38:19 +000053
54 size_t offset;
55 if (instruction->GetType() == Primitive::kPrimLong
56 || instruction->GetType() == Primitive::kPrimDouble) {
57 offset = 2;
58 } else {
59 offset = 1;
60 }
61 index_ += offset;
62
63 graph_->UpdateTemporariesVRegSlots(index_);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010064 }
65
66 private:
67 HGraph* const graph_;
68
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010069 // Current index in the temporary stack, updated by `Add`.
70 size_t index_;
71};
72
Andreas Gamped881df52014-11-24 23:28:39 -080073class SwitchTable : public ValueObject {
74 public:
75 SwitchTable(const Instruction& instruction, uint32_t dex_pc, bool sparse)
76 : instruction_(instruction), dex_pc_(dex_pc), sparse_(sparse) {
77 int32_t table_offset = instruction.VRegB_31t();
78 const uint16_t* table = reinterpret_cast<const uint16_t*>(&instruction) + table_offset;
79 if (sparse) {
80 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
81 } else {
82 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
83 }
84 num_entries_ = table[1];
85 values_ = reinterpret_cast<const int32_t*>(&table[2]);
86 }
87
88 uint16_t GetNumEntries() const {
89 return num_entries_;
90 }
91
Andreas Gampee4d4d322014-12-04 09:09:57 -080092 void CheckIndex(size_t index) const {
93 if (sparse_) {
94 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
95 DCHECK_LT(index, 2 * static_cast<size_t>(num_entries_));
96 } else {
97 // In a packed table, we have the starting key and num_entries_ values.
98 DCHECK_LT(index, 1 + static_cast<size_t>(num_entries_));
99 }
100 }
101
Andreas Gamped881df52014-11-24 23:28:39 -0800102 int32_t GetEntryAt(size_t index) const {
Andreas Gampee4d4d322014-12-04 09:09:57 -0800103 CheckIndex(index);
Andreas Gamped881df52014-11-24 23:28:39 -0800104 return values_[index];
105 }
106
107 uint32_t GetDexPcForIndex(size_t index) const {
Andreas Gampee4d4d322014-12-04 09:09:57 -0800108 CheckIndex(index);
Andreas Gamped881df52014-11-24 23:28:39 -0800109 return dex_pc_ +
110 (reinterpret_cast<const int16_t*>(values_ + index) -
111 reinterpret_cast<const int16_t*>(&instruction_));
112 }
113
Andreas Gampee4d4d322014-12-04 09:09:57 -0800114 // Index of the first value in the table.
115 size_t GetFirstValueIndex() const {
116 if (sparse_) {
117 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
118 return num_entries_;
119 } else {
120 // In a packed table, we have the starting key and num_entries_ values.
121 return 1;
122 }
123 }
124
Andreas Gamped881df52014-11-24 23:28:39 -0800125 private:
126 const Instruction& instruction_;
127 const uint32_t dex_pc_;
128
129 // Whether this is a sparse-switch table (or a packed-switch one).
130 const bool sparse_;
131
132 // This can't be const as it needs to be computed off of the given instruction, and complicated
133 // expressions in the initializer list seemed very ugly.
134 uint16_t num_entries_;
135
136 const int32_t* values_;
137
138 DISALLOW_COPY_AND_ASSIGN(SwitchTable);
139};
140
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100141void HGraphBuilder::InitializeLocals(uint16_t count) {
142 graph_->SetNumberOfVRegs(count);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000143 locals_.SetSize(count);
144 for (int i = 0; i < count; i++) {
145 HLocal* local = new (arena_) HLocal(i);
146 entry_block_->AddInstruction(local);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000147 locals_.Put(i, local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000148 }
149}
150
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000151void HGraphBuilder::InitializeParameters(uint16_t number_of_parameters) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100152 // dex_compilation_unit_ is null only when unit testing.
153 if (dex_compilation_unit_ == nullptr) {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000154 return;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100155 }
156
157 graph_->SetNumberOfInVRegs(number_of_parameters);
158 const char* shorty = dex_compilation_unit_->GetShorty();
159 int locals_index = locals_.Size() - number_of_parameters;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100160 int parameter_index = 0;
161
162 if (!dex_compilation_unit_->IsStatic()) {
163 // Add the implicit 'this' argument, not expressed in the signature.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100164 HParameterValue* parameter =
Calin Juravle10e244f2015-01-26 18:54:32 +0000165 new (arena_) HParameterValue(parameter_index++, Primitive::kPrimNot, true);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100166 entry_block_->AddInstruction(parameter);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100167 HLocal* local = GetLocalAt(locals_index++);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100168 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100169 number_of_parameters--;
170 }
171
172 uint32_t pos = 1;
173 for (int i = 0; i < number_of_parameters; i++) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100174 HParameterValue* parameter =
175 new (arena_) HParameterValue(parameter_index++, Primitive::GetType(shorty[pos++]));
176 entry_block_->AddInstruction(parameter);
177 HLocal* local = GetLocalAt(locals_index++);
178 // Store the parameter value in the local that the dex code will use
179 // to reference that parameter.
180 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter));
181 bool is_wide = (parameter->GetType() == Primitive::kPrimLong)
182 || (parameter->GetType() == Primitive::kPrimDouble);
183 if (is_wide) {
184 i++;
185 locals_index++;
186 parameter_index++;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100187 }
188 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100189}
190
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100191template<typename T>
Calin Juravle225ff812014-11-13 16:46:39 +0000192void HGraphBuilder::If_22t(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000193 int32_t target_offset = instruction.GetTargetOffset();
David Brazdil852eaff2015-02-02 15:23:05 +0000194 HBasicBlock* branch_target = FindBlockStartingAt(dex_pc + target_offset);
195 HBasicBlock* fallthrough_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
196 DCHECK(branch_target != nullptr);
197 DCHECK(fallthrough_target != nullptr);
198 PotentiallyAddSuspendCheck(branch_target, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100199 HInstruction* first = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
200 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Dave Allison20dfc792014-06-16 20:44:29 -0700201 T* comparison = new (arena_) T(first, second);
202 current_block_->AddInstruction(comparison);
203 HInstruction* ifinst = new (arena_) HIf(comparison);
204 current_block_->AddInstruction(ifinst);
David Brazdil852eaff2015-02-02 15:23:05 +0000205 current_block_->AddSuccessor(branch_target);
206 current_block_->AddSuccessor(fallthrough_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700207 current_block_ = nullptr;
208}
209
210template<typename T>
Calin Juravle225ff812014-11-13 16:46:39 +0000211void HGraphBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000212 int32_t target_offset = instruction.GetTargetOffset();
David Brazdil852eaff2015-02-02 15:23:05 +0000213 HBasicBlock* branch_target = FindBlockStartingAt(dex_pc + target_offset);
214 HBasicBlock* fallthrough_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
215 DCHECK(branch_target != nullptr);
216 DCHECK(fallthrough_target != nullptr);
217 PotentiallyAddSuspendCheck(branch_target, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700218 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000219 T* comparison = new (arena_) T(value, graph_->GetIntConstant(0));
Dave Allison20dfc792014-06-16 20:44:29 -0700220 current_block_->AddInstruction(comparison);
221 HInstruction* ifinst = new (arena_) HIf(comparison);
222 current_block_->AddInstruction(ifinst);
David Brazdil852eaff2015-02-02 15:23:05 +0000223 current_block_->AddSuccessor(branch_target);
224 current_block_->AddSuccessor(fallthrough_target);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100225 current_block_ = nullptr;
226}
227
Calin Juravle48c2b032014-12-09 18:11:36 +0000228void HGraphBuilder::MaybeRecordStat(MethodCompilationStat compilation_stat) {
229 if (compilation_stats_ != nullptr) {
230 compilation_stats_->RecordStat(compilation_stat);
231 }
232}
233
David Brazdil1b498722015-03-31 11:37:18 +0100234bool HGraphBuilder::SkipCompilation(const DexFile::CodeItem& code_item,
Calin Juravle48c2b032014-12-09 18:11:36 +0000235 size_t number_of_branches) {
236 const CompilerOptions& compiler_options = compiler_driver_->GetCompilerOptions();
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000237 CompilerOptions::CompilerFilter compiler_filter = compiler_options.GetCompilerFilter();
238 if (compiler_filter == CompilerOptions::kEverything) {
239 return false;
240 }
241
David Brazdil1b498722015-03-31 11:37:18 +0100242 if (compiler_options.IsHugeMethod(code_item.insns_size_in_code_units_)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000243 VLOG(compiler) << "Skip compilation of huge method "
244 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
David Brazdil1b498722015-03-31 11:37:18 +0100245 << ": " << code_item.insns_size_in_code_units_ << " code units";
Calin Juravle48c2b032014-12-09 18:11:36 +0000246 MaybeRecordStat(MethodCompilationStat::kNotCompiledHugeMethod);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000247 return true;
248 }
249
250 // If it's large and contains no branches, it's likely to be machine generated initialization.
David Brazdil1b498722015-03-31 11:37:18 +0100251 if (compiler_options.IsLargeMethod(code_item.insns_size_in_code_units_)
252 && (number_of_branches == 0)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000253 VLOG(compiler) << "Skip compilation of large method with no branch "
254 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
David Brazdil1b498722015-03-31 11:37:18 +0100255 << ": " << code_item.insns_size_in_code_units_ << " code units";
Calin Juravle48c2b032014-12-09 18:11:36 +0000256 MaybeRecordStat(MethodCompilationStat::kNotCompiledLargeMethodNoBranches);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000257 return true;
258 }
259
260 return false;
261}
262
David Brazdilbff75032015-07-08 17:26:51 +0000263static const DexFile::TryItem* GetTryItem(HBasicBlock* block,
264 const DexFile::CodeItem& code_item,
265 const ArenaBitVector& can_block_throw) {
266 DCHECK(!block->IsSingleTryBoundary());
267
268 // Block does not contain throwing instructions. Even if it is covered by
269 // a TryItem, we will consider it not in a try block.
270 if (!can_block_throw.IsBitSet(block->GetBlockId())) {
271 return nullptr;
272 }
273
274 // Instructions in the block may throw. Find a TryItem covering this block.
275 int32_t try_item_idx = DexFile::FindTryItem(code_item, block->GetDexPc());
David Brazdil6cd788f2015-07-08 16:44:00 +0100276 return (try_item_idx == -1) ? nullptr : DexFile::GetTryItems(code_item, try_item_idx);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000277}
278
279void HGraphBuilder::CreateBlocksForTryCatch(const DexFile::CodeItem& code_item) {
280 if (code_item.tries_size_ == 0) {
281 return;
282 }
283
284 // Create branch targets at the start/end of the TryItem range. These are
285 // places where the program might fall through into/out of the a block and
286 // where TryBoundary instructions will be inserted later. Other edges which
287 // enter/exit the try blocks are a result of branches/switches.
288 for (size_t idx = 0; idx < code_item.tries_size_; ++idx) {
289 const DexFile::TryItem* try_item = DexFile::GetTryItems(code_item, idx);
290 uint32_t dex_pc_start = try_item->start_addr_;
291 uint32_t dex_pc_end = dex_pc_start + try_item->insn_count_;
292 FindOrCreateBlockStartingAt(dex_pc_start);
293 if (dex_pc_end < code_item.insns_size_in_code_units_) {
294 // TODO: Do not create block if the last instruction cannot fall through.
295 FindOrCreateBlockStartingAt(dex_pc_end);
296 } else {
297 // The TryItem spans until the very end of the CodeItem (or beyond if
298 // invalid) and therefore cannot have any code afterwards.
299 }
300 }
301
302 // Create branch targets for exception handlers.
303 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(code_item, 0);
304 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
305 for (uint32_t idx = 0; idx < handlers_size; ++idx) {
306 CatchHandlerIterator iterator(handlers_ptr);
307 for (; iterator.HasNext(); iterator.Next()) {
308 uint32_t address = iterator.GetHandlerAddress();
309 HBasicBlock* block = FindOrCreateBlockStartingAt(address);
David Brazdilec16f792015-08-19 15:04:01 +0100310 block->SetTryCatchInformation(
311 new (arena_) TryCatchInformation(iterator.GetHandlerTypeIndex(), *dex_file_));
David Brazdilfc6a86a2015-06-26 10:33:45 +0000312 }
313 handlers_ptr = iterator.EndDataPointer();
314 }
315}
316
David Brazdil56e1acc2015-06-30 15:41:36 +0100317void HGraphBuilder::SplitTryBoundaryEdge(HBasicBlock* predecessor,
318 HBasicBlock* successor,
319 HTryBoundary::BoundaryKind kind,
320 const DexFile::CodeItem& code_item,
321 const DexFile::TryItem& try_item) {
322 // Split the edge with a single TryBoundary instruction.
323 HTryBoundary* try_boundary = new (arena_) HTryBoundary(kind);
324 HBasicBlock* try_entry_block = graph_->SplitEdge(predecessor, successor);
325 try_entry_block->AddInstruction(try_boundary);
326
327 // Link the TryBoundary to the handlers of `try_item`.
328 for (CatchHandlerIterator it(code_item, try_item); it.HasNext(); it.Next()) {
329 try_boundary->AddExceptionHandler(FindBlockStartingAt(it.GetHandlerAddress()));
330 }
331}
332
David Brazdilfc6a86a2015-06-26 10:33:45 +0000333void HGraphBuilder::InsertTryBoundaryBlocks(const DexFile::CodeItem& code_item) {
334 if (code_item.tries_size_ == 0) {
335 return;
336 }
337
David Brazdil72783ff2015-07-09 14:36:05 +0100338 // Bit vector stores information on which blocks contain throwing instructions.
339 // Must be expandable because catch blocks may be split into two.
340 ArenaBitVector can_block_throw(arena_, graph_->GetBlocks().Size(), /* expandable */ true);
David Brazdilbff75032015-07-08 17:26:51 +0000341
342 // Scan blocks and mark those which contain throwing instructions.
David Brazdil72783ff2015-07-09 14:36:05 +0100343 for (size_t block_id = 0, e = graph_->GetBlocks().Size(); block_id < e; ++block_id) {
David Brazdilbff75032015-07-08 17:26:51 +0000344 HBasicBlock* block = graph_->GetBlocks().Get(block_id);
David Brazdil72783ff2015-07-09 14:36:05 +0100345 bool can_throw = false;
David Brazdilbff75032015-07-08 17:26:51 +0000346 for (HInstructionIterator insn(block->GetInstructions()); !insn.Done(); insn.Advance()) {
347 if (insn.Current()->CanThrow()) {
David Brazdil72783ff2015-07-09 14:36:05 +0100348 can_throw = true;
David Brazdilbff75032015-07-08 17:26:51 +0000349 break;
350 }
351 }
David Brazdil72783ff2015-07-09 14:36:05 +0100352
353 if (can_throw) {
354 if (block->IsCatchBlock()) {
355 // Catch blocks are always considered an entry point into the TryItem in
356 // order to avoid splitting exceptional edges. We split the block after
357 // the move-exception (if present) and mark the first part non-throwing.
358 // Later on, a TryBoundary will be inserted between the two blocks.
359 HInstruction* first_insn = block->GetFirstInstruction();
360 if (first_insn->IsLoadException()) {
361 // Catch block starts with a LoadException. Split the block after the
David Brazdilcb1c0552015-08-04 16:22:25 +0100362 // StoreLocal and ClearException which must come after the load.
David Brazdil72783ff2015-07-09 14:36:05 +0100363 DCHECK(first_insn->GetNext()->IsStoreLocal());
David Brazdilcb1c0552015-08-04 16:22:25 +0100364 DCHECK(first_insn->GetNext()->GetNext()->IsClearException());
365 block = block->SplitBefore(first_insn->GetNext()->GetNext()->GetNext());
David Brazdil72783ff2015-07-09 14:36:05 +0100366 } else {
367 // Catch block does not load the exception. Split at the beginning to
368 // create an empty catch block.
369 block = block->SplitBefore(first_insn);
370 }
371 }
372 can_block_throw.SetBit(block->GetBlockId());
373 }
David Brazdilbff75032015-07-08 17:26:51 +0000374 }
375
David Brazdil281a6322015-07-03 10:34:57 +0100376 // Iterate over all blocks, find those covered by some TryItem and:
377 // (a) split edges which enter/exit the try range,
378 // (b) create TryBoundary instructions in the new blocks,
379 // (c) link the new blocks to corresponding exception handlers.
380 // We cannot iterate only over blocks in `branch_targets_` because switch-case
381 // blocks share the same dex_pc.
David Brazdil72783ff2015-07-09 14:36:05 +0100382 for (size_t block_id = 0, e = graph_->GetBlocks().Size(); block_id < e; ++block_id) {
David Brazdil281a6322015-07-03 10:34:57 +0100383 HBasicBlock* try_block = graph_->GetBlocks().Get(block_id);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000384
David Brazdil281a6322015-07-03 10:34:57 +0100385 // TryBoundary blocks are added at the end of the list and not iterated over.
386 DCHECK(!try_block->IsSingleTryBoundary());
David Brazdilfc6a86a2015-06-26 10:33:45 +0000387
David Brazdil281a6322015-07-03 10:34:57 +0100388 // Find the TryItem for this block.
David Brazdilbff75032015-07-08 17:26:51 +0000389 const DexFile::TryItem* try_item = GetTryItem(try_block, code_item, can_block_throw);
390 if (try_item == nullptr) {
David Brazdil281a6322015-07-03 10:34:57 +0100391 continue;
392 }
David Brazdil281a6322015-07-03 10:34:57 +0100393
David Brazdil72783ff2015-07-09 14:36:05 +0100394 // Catch blocks were split earlier and cannot throw.
395 DCHECK(!try_block->IsCatchBlock());
396
397 // Find predecessors which are not covered by the same TryItem range. Such
398 // edges enter the try block and will have a TryBoundary inserted.
399 for (size_t i = 0; i < try_block->GetPredecessors().Size(); ++i) {
400 HBasicBlock* predecessor = try_block->GetPredecessors().Get(i);
401 if (predecessor->IsSingleTryBoundary()) {
402 // The edge was already split because of an exit from a neighbouring
403 // TryItem. We split it again and insert an entry point.
404 if (kIsDebugBuild) {
405 HTryBoundary* last_insn = predecessor->GetLastInstruction()->AsTryBoundary();
406 const DexFile::TryItem* predecessor_try_item =
407 GetTryItem(predecessor->GetSinglePredecessor(), code_item, can_block_throw);
408 DCHECK(!last_insn->IsEntry());
409 DCHECK_EQ(last_insn->GetNormalFlowSuccessor(), try_block);
410 DCHECK(try_block->IsFirstIndexOfPredecessor(predecessor, i));
411 DCHECK_NE(try_item, predecessor_try_item);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000412 }
David Brazdil72783ff2015-07-09 14:36:05 +0100413 } else if (GetTryItem(predecessor, code_item, can_block_throw) != try_item) {
414 // This is an entry point into the TryItem and the edge has not been
415 // split yet. That means that `predecessor` is not in a TryItem, or
416 // it is in a different TryItem and we happened to iterate over this
417 // block first. We split the edge and insert an entry point.
418 } else {
419 // Not an edge on the boundary of the try block.
420 continue;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000421 }
David Brazdil72783ff2015-07-09 14:36:05 +0100422 SplitTryBoundaryEdge(predecessor, try_block, HTryBoundary::kEntry, code_item, *try_item);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000423 }
David Brazdil281a6322015-07-03 10:34:57 +0100424
425 // Find successors which are not covered by the same TryItem range. Such
426 // edges exit the try block and will have a TryBoundary inserted.
427 for (size_t i = 0; i < try_block->GetSuccessors().Size(); ++i) {
428 HBasicBlock* successor = try_block->GetSuccessors().Get(i);
429 if (successor->IsCatchBlock()) {
430 // A catch block is always considered an entry point into its TryItem.
431 // We therefore assume this is an exit point, regardless of whether
432 // the catch block is in a different TryItem or not.
433 } else if (successor->IsSingleTryBoundary()) {
434 // The edge was already split because of an entry into a neighbouring
435 // TryItem. We split it again and insert an exit.
436 if (kIsDebugBuild) {
437 HTryBoundary* last_insn = successor->GetLastInstruction()->AsTryBoundary();
David Brazdilbff75032015-07-08 17:26:51 +0000438 const DexFile::TryItem* successor_try_item =
439 GetTryItem(last_insn->GetNormalFlowSuccessor(), code_item, can_block_throw);
David Brazdil281a6322015-07-03 10:34:57 +0100440 DCHECK_EQ(try_block, successor->GetSinglePredecessor());
441 DCHECK(last_insn->IsEntry());
David Brazdilbff75032015-07-08 17:26:51 +0000442 DCHECK_NE(try_item, successor_try_item);
David Brazdil281a6322015-07-03 10:34:57 +0100443 }
David Brazdilbff75032015-07-08 17:26:51 +0000444 } else if (GetTryItem(successor, code_item, can_block_throw) != try_item) {
David Brazdil281a6322015-07-03 10:34:57 +0100445 // This is an exit out of the TryItem and the edge has not been split
446 // yet. That means that either `successor` is not in a TryItem, or it
447 // is in a different TryItem and we happened to iterate over this
448 // block first. We split the edge and insert an exit.
449 HInstruction* last_instruction = try_block->GetLastInstruction();
450 if (last_instruction->IsReturn() || last_instruction->IsReturnVoid()) {
451 DCHECK_EQ(successor, exit_block_);
452 // Control flow exits the try block with a Return(Void). Because
453 // splitting the edge would invalidate the invariant that Return
454 // always jumps to Exit, we move the Return outside the try block.
455 successor = try_block->SplitBefore(last_instruction);
456 }
457 } else {
458 // Not an edge on the boundary of the try block.
459 continue;
460 }
David Brazdilbff75032015-07-08 17:26:51 +0000461 SplitTryBoundaryEdge(try_block, successor, HTryBoundary::kExit, code_item, *try_item);
David Brazdil281a6322015-07-03 10:34:57 +0100462 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000463 }
464}
465
David Brazdil5e8b1372015-01-23 14:39:08 +0000466bool HGraphBuilder::BuildGraph(const DexFile::CodeItem& code_item) {
467 DCHECK(graph_->GetBlocks().IsEmpty());
468
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000469 const uint16_t* code_ptr = code_item.insns_;
470 const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100471 code_start_ = code_ptr;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000472
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000473 // Setup the graph with the entry block and exit block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100474 entry_block_ = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000475 graph_->AddBlock(entry_block_);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100476 exit_block_ = new (arena_) HBasicBlock(graph_, kNoDexPc);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000477 graph_->SetEntryBlock(entry_block_);
478 graph_->SetExitBlock(exit_block_);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000479
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000480 InitializeLocals(code_item.registers_size_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000481 graph_->SetMaximumNumberOfOutVRegs(code_item.outs_size_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000482
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000483 // Compute the number of dex instructions, blocks, and branches. We will
484 // check these values against limits given to the compiler.
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000485 size_t number_of_branches = 0;
486
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000487 // To avoid splitting blocks, we compute ahead of time the instructions that
488 // start a new block, and create these blocks.
Calin Juravle702d2602015-04-30 19:28:21 +0100489 if (!ComputeBranchTargets(code_ptr, code_end, &number_of_branches)) {
490 MaybeRecordStat(MethodCompilationStat::kNotCompiledBranchOutsideMethodCode);
491 return false;
492 }
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000493
494 // Note that the compiler driver is null when unit testing.
David Brazdil1b498722015-03-31 11:37:18 +0100495 if ((compiler_driver_ != nullptr) && SkipCompilation(code_item, number_of_branches)) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000496 return false;
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000497 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000498
David Brazdilfc6a86a2015-06-26 10:33:45 +0000499 CreateBlocksForTryCatch(code_item);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000500
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000501 InitializeParameters(code_item.ins_size_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100502
Calin Juravle225ff812014-11-13 16:46:39 +0000503 size_t dex_pc = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000504 while (code_ptr < code_end) {
Calin Juravle225ff812014-11-13 16:46:39 +0000505 // Update the current block if dex_pc starts a new block.
506 MaybeUpdateCurrentBlock(dex_pc);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000507 const Instruction& instruction = *Instruction::At(code_ptr);
Calin Juravle48c2b032014-12-09 18:11:36 +0000508 if (!AnalyzeDexInstruction(instruction, dex_pc)) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000509 return false;
Calin Juravle48c2b032014-12-09 18:11:36 +0000510 }
Calin Juravle225ff812014-11-13 16:46:39 +0000511 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000512 code_ptr += instruction.SizeInCodeUnits();
513 }
514
David Brazdilfc6a86a2015-06-26 10:33:45 +0000515 // Add Exit to the exit block.
David Brazdil3e187382015-06-26 09:59:52 +0000516 exit_block_->AddInstruction(new (arena_) HExit());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000517 // Add the suspend check to the entry block.
518 entry_block_->AddInstruction(new (arena_) HSuspendCheck(0));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000519 entry_block_->AddInstruction(new (arena_) HGoto());
David Brazdilbff75032015-07-08 17:26:51 +0000520 // Add the exit block at the end.
521 graph_->AddBlock(exit_block_);
David Brazdil5e8b1372015-01-23 14:39:08 +0000522
David Brazdilfc6a86a2015-06-26 10:33:45 +0000523 // Iterate over blocks covered by TryItems and insert TryBoundaries at entry
524 // and exit points. This requires all control-flow instructions and
525 // non-exceptional edges to have been created.
526 InsertTryBoundaryBlocks(code_item);
527
David Brazdil5e8b1372015-01-23 14:39:08 +0000528 return true;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000529}
530
David Brazdilfc6a86a2015-06-26 10:33:45 +0000531void HGraphBuilder::MaybeUpdateCurrentBlock(size_t dex_pc) {
532 HBasicBlock* block = FindBlockStartingAt(dex_pc);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000533 if (block == nullptr) {
534 return;
535 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000536
537 if (current_block_ != nullptr) {
538 // Branching instructions clear current_block, so we know
539 // the last instruction of the current block is not a branching
540 // instruction. We add an unconditional goto to the found block.
541 current_block_->AddInstruction(new (arena_) HGoto());
542 current_block_->AddSuccessor(block);
543 }
544 graph_->AddBlock(block);
545 current_block_ = block;
546}
547
Calin Juravle702d2602015-04-30 19:28:21 +0100548bool HGraphBuilder::ComputeBranchTargets(const uint16_t* code_ptr,
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000549 const uint16_t* code_end,
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000550 size_t* number_of_branches) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000551 branch_targets_.SetSize(code_end - code_ptr);
552
553 // Create the first block for the dex instructions, single successor of the entry block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100554 HBasicBlock* block = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000555 branch_targets_.Put(0, block);
556 entry_block_->AddSuccessor(block);
557
558 // Iterate over all instructions and find branching instructions. Create blocks for
559 // the locations these instructions branch to.
Andreas Gamped881df52014-11-24 23:28:39 -0800560 uint32_t dex_pc = 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000561 while (code_ptr < code_end) {
562 const Instruction& instruction = *Instruction::At(code_ptr);
563 if (instruction.IsBranch()) {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000564 (*number_of_branches)++;
Calin Juravle225ff812014-11-13 16:46:39 +0000565 int32_t target = instruction.GetTargetOffset() + dex_pc;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000566 // Create a block for the target instruction.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000567 FindOrCreateBlockStartingAt(target);
568
Calin Juravle225ff812014-11-13 16:46:39 +0000569 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000570 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle702d2602015-04-30 19:28:21 +0100571
David Brazdilfe659462015-06-24 14:23:56 +0100572 if (instruction.CanFlowThrough()) {
573 if (code_ptr >= code_end) {
Calin Juravle702d2602015-04-30 19:28:21 +0100574 // In the normal case we should never hit this but someone can artificially forge a dex
575 // file to fall-through out the method code. In this case we bail out compilation.
576 return false;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000577 } else {
578 FindOrCreateBlockStartingAt(dex_pc);
Calin Juravle702d2602015-04-30 19:28:21 +0100579 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000580 }
Andreas Gampee4d4d322014-12-04 09:09:57 -0800581 } else if (instruction.IsSwitch()) {
582 SwitchTable table(instruction, dex_pc, instruction.Opcode() == Instruction::SPARSE_SWITCH);
Andreas Gamped881df52014-11-24 23:28:39 -0800583
584 uint16_t num_entries = table.GetNumEntries();
585
Andreas Gampee4d4d322014-12-04 09:09:57 -0800586 // In a packed-switch, the entry at index 0 is the starting key. In a sparse-switch, the
587 // entry at index 0 is the first key, and values are after *all* keys.
588 size_t offset = table.GetFirstValueIndex();
589
590 // Use a larger loop counter type to avoid overflow issues.
591 for (size_t i = 0; i < num_entries; ++i) {
Andreas Gamped881df52014-11-24 23:28:39 -0800592 // The target of the case.
Andreas Gampee4d4d322014-12-04 09:09:57 -0800593 uint32_t target = dex_pc + table.GetEntryAt(i + offset);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000594 FindOrCreateBlockStartingAt(target);
Andreas Gamped881df52014-11-24 23:28:39 -0800595
David Brazdil281a6322015-07-03 10:34:57 +0100596 // Create a block for the switch-case logic. The block gets the dex_pc
597 // of the SWITCH instruction because it is part of its semantics.
598 block = new (arena_) HBasicBlock(graph_, dex_pc);
599 branch_targets_.Put(table.GetDexPcForIndex(i), block);
Andreas Gamped881df52014-11-24 23:28:39 -0800600 }
601
602 // Fall-through. Add a block if there is more code afterwards.
603 dex_pc += instruction.SizeInCodeUnits();
604 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle702d2602015-04-30 19:28:21 +0100605 if (code_ptr >= code_end) {
606 // In the normal case we should never hit this but someone can artificially forge a dex
607 // file to fall-through out the method code. In this case we bail out compilation.
608 // (A switch can fall-through so we don't need to check CanFlowThrough().)
609 return false;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000610 } else {
611 FindOrCreateBlockStartingAt(dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -0800612 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000613 } else {
614 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle225ff812014-11-13 16:46:39 +0000615 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000616 }
617 }
Calin Juravle702d2602015-04-30 19:28:21 +0100618 return true;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000619}
620
David Brazdilfc6a86a2015-06-26 10:33:45 +0000621HBasicBlock* HGraphBuilder::FindBlockStartingAt(int32_t dex_pc) const {
622 DCHECK_GE(dex_pc, 0);
623 DCHECK_LT(static_cast<size_t>(dex_pc), branch_targets_.Size());
624 return branch_targets_.Get(dex_pc);
625}
626
627HBasicBlock* HGraphBuilder::FindOrCreateBlockStartingAt(int32_t dex_pc) {
628 HBasicBlock* block = FindBlockStartingAt(dex_pc);
629 if (block == nullptr) {
630 block = new (arena_) HBasicBlock(graph_, dex_pc);
631 branch_targets_.Put(dex_pc, block);
632 }
633 return block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000634}
635
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100636template<typename T>
Roland Levillain88cb1752014-10-20 16:36:47 +0100637void HGraphBuilder::Unop_12x(const Instruction& instruction, Primitive::Type type) {
638 HInstruction* first = LoadLocal(instruction.VRegB(), type);
639 current_block_->AddInstruction(new (arena_) T(type, first));
640 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
641}
642
Roland Levillaindff1f282014-11-05 14:15:05 +0000643void HGraphBuilder::Conversion_12x(const Instruction& instruction,
644 Primitive::Type input_type,
Roland Levillain624279f2014-12-04 11:54:28 +0000645 Primitive::Type result_type,
646 uint32_t dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +0000647 HInstruction* first = LoadLocal(instruction.VRegB(), input_type);
Roland Levillain624279f2014-12-04 11:54:28 +0000648 current_block_->AddInstruction(new (arena_) HTypeConversion(result_type, first, dex_pc));
Roland Levillaindff1f282014-11-05 14:15:05 +0000649 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
650}
651
Roland Levillain88cb1752014-10-20 16:36:47 +0100652template<typename T>
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100653void HGraphBuilder::Binop_23x(const Instruction& instruction, Primitive::Type type) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100654 HInstruction* first = LoadLocal(instruction.VRegB(), type);
655 HInstruction* second = LoadLocal(instruction.VRegC(), type);
656 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100657 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
658}
659
660template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000661void HGraphBuilder::Binop_23x(const Instruction& instruction,
662 Primitive::Type type,
663 uint32_t dex_pc) {
664 HInstruction* first = LoadLocal(instruction.VRegB(), type);
665 HInstruction* second = LoadLocal(instruction.VRegC(), type);
666 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
667 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
668}
669
670template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000671void HGraphBuilder::Binop_23x_shift(const Instruction& instruction,
672 Primitive::Type type) {
673 HInstruction* first = LoadLocal(instruction.VRegB(), type);
674 HInstruction* second = LoadLocal(instruction.VRegC(), Primitive::kPrimInt);
675 current_block_->AddInstruction(new (arena_) T(type, first, second));
676 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
677}
678
Calin Juravleddb7df22014-11-25 20:56:51 +0000679void HGraphBuilder::Binop_23x_cmp(const Instruction& instruction,
680 Primitive::Type type,
Mark Mendellc4701932015-04-10 13:18:51 -0400681 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700682 uint32_t dex_pc) {
Calin Juravleddb7df22014-11-25 20:56:51 +0000683 HInstruction* first = LoadLocal(instruction.VRegB(), type);
684 HInstruction* second = LoadLocal(instruction.VRegC(), type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700685 current_block_->AddInstruction(new (arena_) HCompare(type, first, second, bias, dex_pc));
Calin Juravleddb7df22014-11-25 20:56:51 +0000686 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
687}
688
Calin Juravle9aec02f2014-11-18 23:06:35 +0000689template<typename T>
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100690void HGraphBuilder::Binop_12x(const Instruction& instruction, Primitive::Type type) {
691 HInstruction* first = LoadLocal(instruction.VRegA(), type);
692 HInstruction* second = LoadLocal(instruction.VRegB(), type);
693 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100694 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
695}
696
697template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000698void HGraphBuilder::Binop_12x_shift(const Instruction& instruction, Primitive::Type type) {
699 HInstruction* first = LoadLocal(instruction.VRegA(), type);
700 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
701 current_block_->AddInstruction(new (arena_) T(type, first, second));
702 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
703}
704
705template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000706void HGraphBuilder::Binop_12x(const Instruction& instruction,
707 Primitive::Type type,
708 uint32_t dex_pc) {
709 HInstruction* first = LoadLocal(instruction.VRegA(), type);
710 HInstruction* second = LoadLocal(instruction.VRegB(), type);
711 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
712 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
713}
714
715template<typename T>
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100716void HGraphBuilder::Binop_22s(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100717 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000718 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100719 if (reverse) {
720 std::swap(first, second);
721 }
722 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
723 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
724}
725
726template<typename T>
727void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100728 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000729 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100730 if (reverse) {
731 std::swap(first, second);
732 }
733 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
734 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
735}
736
Calin Juravle0c25d102015-04-20 14:49:09 +0100737static bool RequiresConstructorBarrier(const DexCompilationUnit* cu, const CompilerDriver& driver) {
Calin Juravle27df7582015-04-17 19:12:31 +0100738 Thread* self = Thread::Current();
Calin Juravle0c25d102015-04-20 14:49:09 +0100739 return cu->IsConstructor()
740 && driver.RequiresConstructorBarrier(self, cu->GetDexFile(), cu->GetClassDefIndex());
Calin Juravle27df7582015-04-17 19:12:31 +0100741}
742
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100743void HGraphBuilder::BuildReturn(const Instruction& instruction, Primitive::Type type) {
744 if (type == Primitive::kPrimVoid) {
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100745 if (graph_->ShouldGenerateConstructorBarrier()) {
746 // The compilation unit is null during testing.
747 if (dex_compilation_unit_ != nullptr) {
748 DCHECK(RequiresConstructorBarrier(dex_compilation_unit_, *compiler_driver_))
749 << "Inconsistent use of ShouldGenerateConstructorBarrier. Should not generate a barrier.";
750 }
Calin Juravle27df7582015-04-17 19:12:31 +0100751 current_block_->AddInstruction(new (arena_) HMemoryBarrier(kStoreStore));
752 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100753 current_block_->AddInstruction(new (arena_) HReturnVoid());
754 } else {
755 HInstruction* value = LoadLocal(instruction.VRegA(), type);
756 current_block_->AddInstruction(new (arena_) HReturn(value));
757 }
758 current_block_->AddSuccessor(exit_block_);
759 current_block_ = nullptr;
760}
761
Calin Juravle68ad6492015-08-18 17:08:12 +0100762static InvokeType GetInvokeTypeFromOpCode(Instruction::Code opcode) {
763 switch (opcode) {
764 case Instruction::INVOKE_STATIC:
765 case Instruction::INVOKE_STATIC_RANGE:
766 return kStatic;
767 case Instruction::INVOKE_DIRECT:
768 case Instruction::INVOKE_DIRECT_RANGE:
769 return kDirect;
770 case Instruction::INVOKE_VIRTUAL:
771 case Instruction::INVOKE_VIRTUAL_QUICK:
772 case Instruction::INVOKE_VIRTUAL_RANGE:
773 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
774 return kVirtual;
775 case Instruction::INVOKE_INTERFACE:
776 case Instruction::INVOKE_INTERFACE_RANGE:
777 return kInterface;
778 case Instruction::INVOKE_SUPER_RANGE:
779 case Instruction::INVOKE_SUPER:
780 return kSuper;
781 default:
782 LOG(FATAL) << "Unexpected invoke opcode: " << opcode;
783 UNREACHABLE();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100784 }
Calin Juravle68ad6492015-08-18 17:08:12 +0100785}
786
787bool HGraphBuilder::BuildInvoke(const Instruction& instruction,
788 uint32_t dex_pc,
789 uint32_t method_idx,
790 uint32_t number_of_vreg_arguments,
791 bool is_range,
792 uint32_t* args,
793 uint32_t register_index) {
794 InvokeType original_invoke_type = GetInvokeTypeFromOpCode(instruction.Opcode());
795 InvokeType optimized_invoke_type = original_invoke_type;
796 const char* descriptor = dex_file_->GetMethodShorty(method_idx);
797 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
798
799 // Remove the return type from the 'proto'.
800 size_t number_of_arguments = strlen(descriptor) - 1;
801 if (original_invoke_type != kStatic) { // instance call
802 // One extra argument for 'this'.
803 number_of_arguments++;
804 }
805
806 MethodReference target_method(dex_file_, method_idx);
807 int32_t table_index;
808 uintptr_t direct_code;
809 uintptr_t direct_method;
810
811 if (!compiler_driver_->ComputeInvokeInfo(dex_compilation_unit_,
812 dex_pc,
813 true /* update_stats */,
814 true /* enable_devirtualization */,
815 &optimized_invoke_type,
816 &target_method,
817 &table_index,
818 &direct_code,
819 &direct_method)) {
820 VLOG(compiler) << "Did not compile "
821 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
822 << " because a method call could not be resolved";
823 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedMethod);
824 return false;
825 }
826
827 DCHECK(optimized_invoke_type != kSuper);
828
829 // Special handling for string init.
830 int32_t string_init_offset = 0;
831 bool is_string_init = compiler_driver_->IsStringInit(method_idx, dex_file_,
832 &string_init_offset);
833
834 // Potential class initialization check, in the case of a static method call.
835 HClinitCheck* clinit_check = nullptr;
836 HInvoke* invoke = nullptr;
837
838 if (is_string_init
839 || optimized_invoke_type == kDirect
840 || optimized_invoke_type == kStatic) {
841 // By default, consider that the called method implicitly requires
842 // an initialization check of its declaring method.
843 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement
844 = HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
845 if (optimized_invoke_type == kStatic && !is_string_init) {
846 clinit_check = ProcessClinitCheckForInvoke(dex_pc, method_idx, &clinit_check_requirement);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100847 }
Calin Juravle68ad6492015-08-18 17:08:12 +0100848
849 // Replace calls to String.<init> with StringFactory.
850 if (is_string_init) {
851 return_type = Primitive::kPrimNot;
852 number_of_arguments--;
853 optimized_invoke_type = kStatic;
854 }
855
856 HInvokeStaticOrDirect::DispatchInfo dispatch_info = ComputeDispatchInfo(is_string_init,
857 string_init_offset,
858 target_method,
859 direct_method,
860 direct_code);
861 invoke = new (arena_) HInvokeStaticOrDirect(arena_,
862 number_of_arguments,
863 return_type,
864 dex_pc,
865 method_idx,
866 target_method,
867 dispatch_info,
868 original_invoke_type,
869 optimized_invoke_type,
870 clinit_check_requirement);
871 } else if (optimized_invoke_type == kVirtual) {
872 invoke = new (arena_) HInvokeVirtual(arena_,
873 number_of_arguments,
874 return_type,
875 dex_pc,
876 method_idx,
877 table_index);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100878 } else {
Calin Juravle68ad6492015-08-18 17:08:12 +0100879 DCHECK_EQ(optimized_invoke_type, kInterface);
880 invoke = new (arena_) HInvokeInterface(arena_,
881 number_of_arguments,
882 return_type,
883 dex_pc,
884 method_idx,
885 table_index);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100886 }
Calin Juravle68ad6492015-08-18 17:08:12 +0100887
Calin Juravle0eedd7e2015-08-20 14:48:00 +0100888 return SetupArgumentsAndAddInvoke(invoke,
889 number_of_vreg_arguments,
890 args,
891 register_index,
892 is_range,
893 descriptor,
Calin Juravle599262c2015-08-20 15:01:27 +0100894 clinit_check);
Calin Juravle68ad6492015-08-18 17:08:12 +0100895}
896
897HClinitCheck* HGraphBuilder::ProcessClinitCheckForInvoke(
898 uint32_t dex_pc,
899 uint32_t method_idx,
900 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
901 ScopedObjectAccess soa(Thread::Current());
902 StackHandleScope<4> hs(soa.Self());
903 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
904 dex_compilation_unit_->GetClassLinker()->FindDexCache(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700905 soa.Self(), *dex_compilation_unit_->GetDexFile())));
Calin Juravle68ad6492015-08-18 17:08:12 +0100906 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
907 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
908 ArtMethod* resolved_method = compiler_driver_->ResolveMethod(
909 soa, dex_cache, class_loader, dex_compilation_unit_, method_idx, InvokeType::kStatic);
910
911 DCHECK(resolved_method != nullptr);
912
913 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
914 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700915 outer_compilation_unit_->GetClassLinker()->FindDexCache(soa.Self(), outer_dex_file)));
Calin Juravle68ad6492015-08-18 17:08:12 +0100916 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
917
918 // The index at which the method's class is stored in the DexCache's type array.
919 uint32_t storage_index = DexFile::kDexNoIndex;
920 bool is_outer_class = (resolved_method->GetDeclaringClass() == outer_class.Get());
921 if (is_outer_class) {
922 storage_index = outer_class->GetDexTypeIndex();
923 } else if (outer_dex_cache.Get() == dex_cache.Get()) {
924 // Get `storage_index` from IsClassOfStaticMethodAvailableToReferrer.
925 compiler_driver_->IsClassOfStaticMethodAvailableToReferrer(outer_dex_cache.Get(),
926 GetCompilingClass(),
927 resolved_method,
928 method_idx,
929 &storage_index);
930 }
931
932 HClinitCheck* clinit_check = nullptr;
933
934 if (!outer_class->IsInterface()
935 && outer_class->IsSubClass(resolved_method->GetDeclaringClass())) {
936 // If the outer class is the declaring class or a subclass
937 // of the declaring class, no class initialization is needed
938 // before the static method call.
939 // Note that in case of inlining, we do not need to add clinit checks
940 // to calls that satisfy this subclass check with any inlined methods. This
941 // will be detected by the optimization passes.
942 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
943 } else if (storage_index != DexFile::kDexNoIndex) {
944 // If the method's class type index is available, check
945 // whether we should add an explicit class initialization
946 // check for its declaring class before the static method call.
947
948 // TODO: find out why this check is needed.
949 bool is_in_dex_cache = compiler_driver_->CanAssumeTypeIsPresentInDexCache(
950 *outer_compilation_unit_->GetDexFile(), storage_index);
951 bool is_initialized =
952 resolved_method->GetDeclaringClass()->IsInitialized() && is_in_dex_cache;
953
954 if (is_initialized) {
955 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
956 } else {
957 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
958 HLoadClass* load_class = new (arena_) HLoadClass(
959 graph_->GetCurrentMethod(),
960 storage_index,
961 *dex_compilation_unit_->GetDexFile(),
962 is_outer_class,
963 dex_pc);
964 current_block_->AddInstruction(load_class);
965 clinit_check = new (arena_) HClinitCheck(load_class, dex_pc);
966 current_block_->AddInstruction(clinit_check);
967 }
968 }
969 return clinit_check;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100970}
971
Vladimir Marko58155012015-08-19 12:49:41 +0000972HInvokeStaticOrDirect::DispatchInfo HGraphBuilder::ComputeDispatchInfo(
973 bool is_string_init,
974 int32_t string_init_offset,
975 MethodReference target_method,
976 uintptr_t direct_method,
977 uintptr_t direct_code) {
978 HInvokeStaticOrDirect::MethodLoadKind method_load_kind;
979 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location;
980 uint64_t method_load_data = 0u;
981 uint64_t direct_code_ptr = 0u;
982
983 if (is_string_init) {
984 // TODO: Use direct_method and direct_code for the appropriate StringFactory method.
985 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kStringInit;
986 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
987 method_load_data = string_init_offset;
988 } else if (target_method.dex_file == outer_compilation_unit_->GetDexFile() &&
989 target_method.dex_method_index == outer_compilation_unit_->GetDexMethodIndex()) {
990 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kRecursive;
991 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallSelf;
992 } else {
993 if (direct_method != 0u) { // Should we use a direct pointer to the method?
994 if (direct_method != static_cast<uintptr_t>(-1)) { // Is the method pointer known now?
995 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress;
996 method_load_data = direct_method;
997 } else { // The direct pointer will be known at link time.
998 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup;
999 }
1000 } else { // Use dex cache.
1001 DCHECK(target_method.dex_file == dex_compilation_unit_->GetDexFile());
1002 DexCacheArraysLayout layout =
1003 compiler_driver_->GetDexCacheArraysLayout(target_method.dex_file);
1004 if (layout.Valid()) { // Can we use PC-relative access to the dex cache arrays?
1005 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative;
1006 method_load_data = layout.MethodOffset(target_method.dex_method_index);
1007 } else { // We must go through the ArtMethod's pointer to resolved methods.
1008 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
1009 }
1010 }
1011 if (direct_code != 0u) { // Should we use a direct pointer to the code?
1012 if (direct_code != static_cast<uintptr_t>(-1)) { // Is the code pointer known now?
1013 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallDirect;
1014 direct_code_ptr = direct_code;
1015 } else if (compiler_driver_->IsImage() ||
1016 target_method.dex_file == dex_compilation_unit_->GetDexFile()) {
1017 // Use PC-relative calls for invokes within a multi-dex oat file.
1018 // TODO: Recognize when the target dex file is within the current oat file for
1019 // app compilation. At the moment we recognize only the boot image as multi-dex.
1020 // NOTE: This will require changing the ARM backend which currently falls
1021 // through from kCallPCRelative to kDirectCodeFixup for different dex files.
1022 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative;
1023 } else { // The direct pointer will be known at link time.
1024 // NOTE: This is used for app->boot calls when compiling an app against
1025 // a relocatable but not yet relocated image.
1026 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup;
1027 }
1028 } else { // We must use the code pointer from the ArtMethod.
1029 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
1030 }
1031 }
1032
1033 if (graph_->IsDebuggable()) {
1034 // For debuggable apps always use the code pointer from ArtMethod
1035 // so that we don't circumvent instrumentation stubs if installed.
1036 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
1037 }
1038
1039 return HInvokeStaticOrDirect::DispatchInfo {
1040 method_load_kind, code_ptr_location, method_load_data, direct_code_ptr };
1041}
1042
Calin Juravle0eedd7e2015-08-20 14:48:00 +01001043bool HGraphBuilder::SetupArgumentsAndAddInvoke(HInvoke* invoke,
1044 uint32_t number_of_vreg_arguments,
1045 uint32_t* args,
1046 uint32_t register_index,
1047 bool is_range,
1048 const char* descriptor,
1049 HClinitCheck* clinit_check) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001050 size_t start_index = 0;
Calin Juravle68ad6492015-08-18 17:08:12 +01001051 size_t argument_index = 0;
1052 uint32_t descriptor_index = 1; // Skip the return type.
1053
1054 bool is_instance_call = invoke->GetOriginalInvokeType() != InvokeType::kStatic;
1055 bool is_string_init = invoke->IsInvokeStaticOrDirect()
1056 && invoke->AsInvokeStaticOrDirect()->IsStringInit();
1057
1058 if (is_string_init) {
1059 start_index = 1;
1060 argument_index = 0;
1061 } else if (is_instance_call) {
1062 Temporaries temps(graph_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001063 HInstruction* arg = LoadLocal(is_range ? register_index : args[0], Primitive::kPrimNot);
Calin Juravle68ad6492015-08-18 17:08:12 +01001064 HNullCheck* null_check = new (arena_) HNullCheck(arg, invoke->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001065 current_block_->AddInstruction(null_check);
1066 temps.Add(null_check);
1067 invoke->SetArgumentAt(0, null_check);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001068 start_index = 1;
Calin Juravle68ad6492015-08-18 17:08:12 +01001069 argument_index = 1;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001070 }
1071
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001072 for (size_t i = start_index;
1073 // Make sure we don't go over the expected arguments or over the number of
1074 // dex registers given. If the instruction was seen as dead by the verifier,
1075 // it hasn't been properly checked.
Calin Juravle68ad6492015-08-18 17:08:12 +01001076 (i < number_of_vreg_arguments) && (argument_index < invoke->GetNumberOfArguments());
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001077 i++, argument_index++) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001078 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001079 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001080 if (!is_range
1081 && is_wide
1082 && ((i + 1 == number_of_vreg_arguments) || (args[i] + 1 != args[i + 1]))) {
1083 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
1084 // reject any class where this is violated. However, the verifier only does these checks
1085 // on non trivially dead instructions, so we just bailout the compilation.
1086 VLOG(compiler) << "Did not compile "
1087 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
1088 << " because of non-sequential dex register pair in wide argument";
1089 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1090 return false;
1091 }
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001092 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
1093 invoke->SetArgumentAt(argument_index, arg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001094 if (is_wide) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001095 i++;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001096 }
1097 }
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001098
Calin Juravle68ad6492015-08-18 17:08:12 +01001099 if (argument_index != invoke->GetNumberOfArguments()) {
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001100 VLOG(compiler) << "Did not compile "
1101 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
1102 << " because of wrong number of arguments in invoke instruction";
1103 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1104 return false;
1105 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001106
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001107 if (invoke->IsInvokeStaticOrDirect()) {
1108 invoke->SetArgumentAt(argument_index, graph_->GetCurrentMethod());
1109 argument_index++;
1110 }
1111
Calin Juravle68ad6492015-08-18 17:08:12 +01001112 if (clinit_check != nullptr) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001113 // Add the class initialization check as last input of `invoke`.
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001114 DCHECK(!is_string_init);
Calin Juravle68ad6492015-08-18 17:08:12 +01001115 DCHECK(invoke->IsInvokeStaticOrDirect());
1116 DCHECK(invoke->AsInvokeStaticOrDirect()->GetClinitCheckRequirement()
1117 == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit);
Roland Levillain3e3d7332015-04-28 11:00:54 +01001118 invoke->SetArgumentAt(argument_index, clinit_check);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001119 argument_index++;
Roland Levillain4c0eb422015-04-24 16:43:49 +01001120 }
1121
Jeff Hao848f70a2014-01-15 13:49:50 -08001122 // Add move-result for StringFactory method.
1123 if (is_string_init) {
1124 uint32_t orig_this_reg = is_range ? register_index : args[0];
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001125 HInstruction* fake_string = LoadLocal(orig_this_reg, Primitive::kPrimNot);
1126 invoke->SetArgumentAt(argument_index, fake_string);
Calin Juravle0eedd7e2015-08-20 14:48:00 +01001127 current_block_->AddInstruction(invoke);
Calin Juravle68ad6492015-08-18 17:08:12 +01001128 PotentiallySimplifyFakeString(orig_this_reg, invoke->GetDexPc(), invoke);
Calin Juravle0eedd7e2015-08-20 14:48:00 +01001129 } else {
1130 current_block_->AddInstruction(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001131 }
Calin Juravle0eedd7e2015-08-20 14:48:00 +01001132
1133 latest_result_ = invoke;
1134
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001135 return true;
1136}
1137
Calin Juravle68ad6492015-08-18 17:08:12 +01001138void HGraphBuilder::PotentiallySimplifyFakeString(uint16_t original_dex_register,
1139 uint32_t dex_pc,
1140 HInvoke* actual_string) {
1141 if (!graph_->IsDebuggable()) {
1142 // Notify that we cannot compile with baseline. The dex registers aliasing
1143 // with `original_dex_register` will be handled when we optimize
1144 // (see HInstructionSimplifer::VisitFakeString).
1145 can_use_baseline_for_string_init_ = false;
1146 return;
1147 }
1148 const VerifiedMethod* verified_method =
1149 compiler_driver_->GetVerifiedMethod(dex_file_, dex_compilation_unit_->GetDexMethodIndex());
1150 if (verified_method != nullptr) {
1151 UpdateLocal(original_dex_register, actual_string);
1152 const SafeMap<uint32_t, std::set<uint32_t>>& string_init_map =
1153 verified_method->GetStringInitPcRegMap();
1154 auto map_it = string_init_map.find(dex_pc);
1155 if (map_it != string_init_map.end()) {
1156 std::set<uint32_t> reg_set = map_it->second;
1157 for (auto set_it = reg_set.begin(); set_it != reg_set.end(); ++set_it) {
1158 HInstruction* load_local = LoadLocal(original_dex_register, Primitive::kPrimNot);
1159 UpdateLocal(*set_it, load_local);
1160 }
1161 }
1162 } else {
1163 can_use_baseline_for_string_init_ = false;
1164 }
1165}
1166
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001167bool HGraphBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001168 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001169 bool is_put) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001170 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1171 uint32_t obj_reg = instruction.VRegB_22c();
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001172 uint16_t field_index;
1173 if (instruction.IsQuickened()) {
1174 if (!CanDecodeQuickenedInfo()) {
1175 return false;
1176 }
1177 field_index = LookupQuickenedInfo(dex_pc);
1178 } else {
1179 field_index = instruction.VRegC_22c();
1180 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001181
1182 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierc7853442015-03-27 14:35:38 -07001183 ArtField* resolved_field =
1184 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001185
Mathieu Chartierc7853442015-03-27 14:35:38 -07001186 if (resolved_field == nullptr) {
Calin Juravle48c2b032014-12-09 18:11:36 +00001187 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedField);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001188 return false;
1189 }
Calin Juravle52c48962014-12-16 17:02:57 +00001190
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001191 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001192
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001193 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00001194 current_block_->AddInstruction(new (arena_) HNullCheck(object, dex_pc));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001195 if (is_put) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001196 Temporaries temps(graph_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001197 HInstruction* null_check = current_block_->GetLastInstruction();
1198 // We need one temporary for the null check.
1199 temps.Add(null_check);
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001200 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001201 current_block_->AddInstruction(new (arena_) HInstanceFieldSet(
1202 null_check,
1203 value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001204 field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00001205 resolved_field->GetOffset(),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001206 resolved_field->IsVolatile(),
1207 field_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07001208 *dex_file_,
1209 dex_compilation_unit_->GetDexCache()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001210 } else {
1211 current_block_->AddInstruction(new (arena_) HInstanceFieldGet(
1212 current_block_->GetLastInstruction(),
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001213 field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00001214 resolved_field->GetOffset(),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001215 resolved_field->IsVolatile(),
1216 field_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07001217 *dex_file_,
1218 dex_compilation_unit_->GetDexCache()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001219
1220 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1221 }
1222 return true;
1223}
1224
Nicolas Geoffray30451742015-06-19 13:32:41 +01001225static mirror::Class* GetClassFrom(CompilerDriver* driver,
1226 const DexCompilationUnit& compilation_unit) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001227 ScopedObjectAccess soa(Thread::Current());
1228 StackHandleScope<2> hs(soa.Self());
Nicolas Geoffray30451742015-06-19 13:32:41 +01001229 const DexFile& dex_file = *compilation_unit.GetDexFile();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001230 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Nicolas Geoffray30451742015-06-19 13:32:41 +01001231 soa.Decode<mirror::ClassLoader*>(compilation_unit.GetClassLoader())));
1232 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001233 compilation_unit.GetClassLinker()->FindDexCache(soa.Self(), dex_file)));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001234
Nicolas Geoffray30451742015-06-19 13:32:41 +01001235 return driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, &compilation_unit);
1236}
1237
1238mirror::Class* HGraphBuilder::GetOutermostCompilingClass() const {
1239 return GetClassFrom(compiler_driver_, *outer_compilation_unit_);
1240}
1241
1242mirror::Class* HGraphBuilder::GetCompilingClass() const {
1243 return GetClassFrom(compiler_driver_, *dex_compilation_unit_);
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001244}
1245
1246bool HGraphBuilder::IsOutermostCompilingClass(uint16_t type_index) const {
1247 ScopedObjectAccess soa(Thread::Current());
1248 StackHandleScope<4> hs(soa.Self());
1249 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001250 dex_compilation_unit_->GetClassLinker()->FindDexCache(
1251 soa.Self(), *dex_compilation_unit_->GetDexFile())));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001252 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1253 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
1254 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
1255 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
Nicolas Geoffrayafd06412015-06-20 22:44:47 +01001256 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001257
Nicolas Geoffrayafd06412015-06-20 22:44:47 +01001258 return outer_class.Get() == cls.Get();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001259}
1260
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001261bool HGraphBuilder::BuildStaticFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001262 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001263 bool is_put) {
1264 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1265 uint16_t field_index = instruction.VRegB_21c();
1266
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001267 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierc7853442015-03-27 14:35:38 -07001268 StackHandleScope<4> hs(soa.Self());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001269 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001270 dex_compilation_unit_->GetClassLinker()->FindDexCache(
1271 soa.Self(), *dex_compilation_unit_->GetDexFile())));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001272 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1273 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
Mathieu Chartierc7853442015-03-27 14:35:38 -07001274 ArtField* resolved_field = compiler_driver_->ResolveField(
1275 soa, dex_cache, class_loader, dex_compilation_unit_, field_index, true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001276
Mathieu Chartierc7853442015-03-27 14:35:38 -07001277 if (resolved_field == nullptr) {
Calin Juravle48c2b032014-12-09 18:11:36 +00001278 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedField);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001279 return false;
1280 }
1281
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001282 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
1283 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001284 outer_compilation_unit_->GetClassLinker()->FindDexCache(soa.Self(), outer_dex_file)));
Nicolas Geoffray30451742015-06-19 13:32:41 +01001285 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001286
1287 // The index at which the field's class is stored in the DexCache's type array.
1288 uint32_t storage_index;
Nicolas Geoffray30451742015-06-19 13:32:41 +01001289 bool is_outer_class = (outer_class.Get() == resolved_field->GetDeclaringClass());
1290 if (is_outer_class) {
1291 storage_index = outer_class->GetDexTypeIndex();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001292 } else if (outer_dex_cache.Get() != dex_cache.Get()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001293 // The compiler driver cannot currently understand multiple dex caches involved. Just bailout.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001294 return false;
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001295 } else {
1296 std::pair<bool, bool> pair = compiler_driver_->IsFastStaticField(
1297 outer_dex_cache.Get(),
Nicolas Geoffray30451742015-06-19 13:32:41 +01001298 GetCompilingClass(),
Mathieu Chartierc7853442015-03-27 14:35:38 -07001299 resolved_field,
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001300 field_index,
1301 &storage_index);
1302 bool can_easily_access = is_put ? pair.second : pair.first;
1303 if (!can_easily_access) {
1304 return false;
1305 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001306 }
1307
1308 // TODO: find out why this check is needed.
1309 bool is_in_dex_cache = compiler_driver_->CanAssumeTypeIsPresentInDexCache(
Nicolas Geoffray6a816cf2015-03-24 16:17:56 +00001310 *outer_compilation_unit_->GetDexFile(), storage_index);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001311 bool is_initialized = resolved_field->GetDeclaringClass()->IsInitialized() && is_in_dex_cache;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001312
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001313 HLoadClass* constant = new (arena_) HLoadClass(graph_->GetCurrentMethod(),
1314 storage_index,
1315 *dex_compilation_unit_->GetDexFile(),
Nicolas Geoffray30451742015-06-19 13:32:41 +01001316 is_outer_class,
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001317 dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001318 current_block_->AddInstruction(constant);
1319
1320 HInstruction* cls = constant;
Nicolas Geoffray30451742015-06-19 13:32:41 +01001321 if (!is_initialized && !is_outer_class) {
Calin Juravle225ff812014-11-13 16:46:39 +00001322 cls = new (arena_) HClinitCheck(constant, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001323 current_block_->AddInstruction(cls);
1324 }
1325
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001326 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001327 if (is_put) {
1328 // We need to keep the class alive before loading the value.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001329 Temporaries temps(graph_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001330 temps.Add(cls);
1331 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1332 DCHECK_EQ(value->GetType(), field_type);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001333 current_block_->AddInstruction(new (arena_) HStaticFieldSet(cls,
1334 value,
1335 field_type,
1336 resolved_field->GetOffset(),
1337 resolved_field->IsVolatile(),
1338 field_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07001339 *dex_file_,
1340 dex_cache_));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001341 } else {
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001342 current_block_->AddInstruction(new (arena_) HStaticFieldGet(cls,
1343 field_type,
1344 resolved_field->GetOffset(),
1345 resolved_field->IsVolatile(),
1346 field_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07001347 *dex_file_,
1348 dex_cache_));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001349 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1350 }
1351 return true;
1352}
1353
Calin Juravlebacfec32014-11-14 15:54:36 +00001354void HGraphBuilder::BuildCheckedDivRem(uint16_t out_vreg,
1355 uint16_t first_vreg,
1356 int64_t second_vreg_or_constant,
1357 uint32_t dex_pc,
1358 Primitive::Type type,
1359 bool second_is_constant,
1360 bool isDiv) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001361 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Calin Juravled0d48522014-11-04 16:40:20 +00001362
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001363 HInstruction* first = LoadLocal(first_vreg, type);
1364 HInstruction* second = nullptr;
1365 if (second_is_constant) {
1366 if (type == Primitive::kPrimInt) {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001367 second = graph_->GetIntConstant(second_vreg_or_constant);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001368 } else {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001369 second = graph_->GetLongConstant(second_vreg_or_constant);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001370 }
1371 } else {
1372 second = LoadLocal(second_vreg_or_constant, type);
1373 }
1374
1375 if (!second_is_constant
1376 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0)
1377 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) {
1378 second = new (arena_) HDivZeroCheck(second, dex_pc);
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001379 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +00001380 current_block_->AddInstruction(second);
1381 temps.Add(current_block_->GetLastInstruction());
1382 }
1383
Calin Juravlebacfec32014-11-14 15:54:36 +00001384 if (isDiv) {
1385 current_block_->AddInstruction(new (arena_) HDiv(type, first, second, dex_pc));
1386 } else {
1387 current_block_->AddInstruction(new (arena_) HRem(type, first, second, dex_pc));
1388 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001389 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
Calin Juravled0d48522014-11-04 16:40:20 +00001390}
1391
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001392void HGraphBuilder::BuildArrayAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001393 uint32_t dex_pc,
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001394 bool is_put,
1395 Primitive::Type anticipated_type) {
1396 uint8_t source_or_dest_reg = instruction.VRegA_23x();
1397 uint8_t array_reg = instruction.VRegB_23x();
1398 uint8_t index_reg = instruction.VRegC_23x();
1399
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001400 // We need one temporary for the null check, one for the index, and one for the length.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001401 Temporaries temps(graph_);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001402
1403 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00001404 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001405 current_block_->AddInstruction(object);
1406 temps.Add(object);
1407
1408 HInstruction* length = new (arena_) HArrayLength(object);
1409 current_block_->AddInstruction(length);
1410 temps.Add(length);
1411 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt);
Calin Juravle225ff812014-11-13 16:46:39 +00001412 index = new (arena_) HBoundsCheck(index, length, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001413 current_block_->AddInstruction(index);
1414 temps.Add(index);
1415 if (is_put) {
1416 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
1417 // TODO: Insert a type check node if the type is Object.
Nicolas Geoffray39468442014-09-02 15:17:15 +01001418 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001419 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001420 } else {
1421 current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type));
1422 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1423 }
Mark Mendell1152c922015-04-24 17:06:35 -04001424 graph_->SetHasBoundsChecks(true);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001425}
1426
Calin Juravle225ff812014-11-13 16:46:39 +00001427void HGraphBuilder::BuildFilledNewArray(uint32_t dex_pc,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001428 uint32_t type_index,
1429 uint32_t number_of_vreg_arguments,
1430 bool is_range,
1431 uint32_t* args,
1432 uint32_t register_index) {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001433 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments);
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001434 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
1435 ? kQuickAllocArrayWithAccessCheck
1436 : kQuickAllocArray;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01001437 HInstruction* object = new (arena_) HNewArray(length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01001438 graph_->GetCurrentMethod(),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01001439 dex_pc,
1440 type_index,
1441 *dex_compilation_unit_->GetDexFile(),
1442 entrypoint);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001443 current_block_->AddInstruction(object);
1444
1445 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1446 DCHECK_EQ(descriptor[0], '[') << descriptor;
1447 char primitive = descriptor[1];
1448 DCHECK(primitive == 'I'
1449 || primitive == 'L'
1450 || primitive == '[') << descriptor;
1451 bool is_reference_array = (primitive == 'L') || (primitive == '[');
1452 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
1453
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001454 Temporaries temps(graph_);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001455 temps.Add(object);
1456 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
1457 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
David Brazdil8d5b8b22015-03-24 10:51:52 +00001458 HInstruction* index = graph_->GetIntConstant(i);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001459 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001460 new (arena_) HArraySet(object, index, value, type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001461 }
1462 latest_result_ = object;
1463}
1464
1465template <typename T>
1466void HGraphBuilder::BuildFillArrayData(HInstruction* object,
1467 const T* data,
1468 uint32_t element_count,
1469 Primitive::Type anticipated_type,
Calin Juravle225ff812014-11-13 16:46:39 +00001470 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001471 for (uint32_t i = 0; i < element_count; ++i) {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001472 HInstruction* index = graph_->GetIntConstant(i);
1473 HInstruction* value = graph_->GetIntConstant(data[i]);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001474 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001475 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001476 }
1477}
1478
Calin Juravle225ff812014-11-13 16:46:39 +00001479void HGraphBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001480 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +00001481 HInstruction* array = LoadLocal(instruction.VRegA_31t(), Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00001482 HNullCheck* null_check = new (arena_) HNullCheck(array, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001483 current_block_->AddInstruction(null_check);
1484 temps.Add(null_check);
1485
1486 HInstruction* length = new (arena_) HArrayLength(null_check);
1487 current_block_->AddInstruction(length);
1488
Calin Juravle225ff812014-11-13 16:46:39 +00001489 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
Calin Juravled0d48522014-11-04 16:40:20 +00001490 const Instruction::ArrayDataPayload* payload =
1491 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_start_ + payload_offset);
1492 const uint8_t* data = payload->data;
1493 uint32_t element_count = payload->element_count;
1494
1495 // Implementation of this DEX instruction seems to be that the bounds check is
1496 // done before doing any stores.
David Brazdil8d5b8b22015-03-24 10:51:52 +00001497 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1);
Calin Juravle225ff812014-11-13 16:46:39 +00001498 current_block_->AddInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
Calin Juravled0d48522014-11-04 16:40:20 +00001499
1500 switch (payload->element_width) {
1501 case 1:
1502 BuildFillArrayData(null_check,
1503 reinterpret_cast<const int8_t*>(data),
1504 element_count,
1505 Primitive::kPrimByte,
Calin Juravle225ff812014-11-13 16:46:39 +00001506 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001507 break;
1508 case 2:
1509 BuildFillArrayData(null_check,
1510 reinterpret_cast<const int16_t*>(data),
1511 element_count,
1512 Primitive::kPrimShort,
Calin Juravle225ff812014-11-13 16:46:39 +00001513 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001514 break;
1515 case 4:
1516 BuildFillArrayData(null_check,
1517 reinterpret_cast<const int32_t*>(data),
1518 element_count,
1519 Primitive::kPrimInt,
Calin Juravle225ff812014-11-13 16:46:39 +00001520 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001521 break;
1522 case 8:
1523 BuildFillWideArrayData(null_check,
1524 reinterpret_cast<const int64_t*>(data),
1525 element_count,
Calin Juravle225ff812014-11-13 16:46:39 +00001526 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001527 break;
1528 default:
1529 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1530 }
Mark Mendell1152c922015-04-24 17:06:35 -04001531 graph_->SetHasBoundsChecks(true);
Calin Juravled0d48522014-11-04 16:40:20 +00001532}
1533
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001534void HGraphBuilder::BuildFillWideArrayData(HInstruction* object,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +01001535 const int64_t* data,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001536 uint32_t element_count,
Calin Juravle225ff812014-11-13 16:46:39 +00001537 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001538 for (uint32_t i = 0; i < element_count; ++i) {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001539 HInstruction* index = graph_->GetIntConstant(i);
1540 HInstruction* value = graph_->GetLongConstant(data[i]);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001541 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001542 object, index, value, Primitive::kPrimLong, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001543 }
1544}
1545
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001546bool HGraphBuilder::BuildTypeCheck(const Instruction& instruction,
1547 uint8_t destination,
1548 uint8_t reference,
1549 uint16_t type_index,
Calin Juravle225ff812014-11-13 16:46:39 +00001550 uint32_t dex_pc) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001551 bool type_known_final;
1552 bool type_known_abstract;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001553 // `CanAccessTypeWithoutChecks` will tell whether the method being
1554 // built is trying to access its own class, so that the generated
1555 // code can optimize for this case. However, the optimization does not
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001556 // work for inlining, so we use `IsOutermostCompilingClass` instead.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001557 bool dont_use_is_referrers_class;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001558 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
1559 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001560 &type_known_final, &type_known_abstract, &dont_use_is_referrers_class);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001561 if (!can_access) {
Calin Juravle48c2b032014-12-09 18:11:36 +00001562 MaybeRecordStat(MethodCompilationStat::kNotCompiledCantAccesType);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001563 return false;
1564 }
1565 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot);
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001566 HLoadClass* cls = new (arena_) HLoadClass(
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001567 graph_->GetCurrentMethod(),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01001568 type_index,
1569 *dex_compilation_unit_->GetDexFile(),
1570 IsOutermostCompilingClass(type_index),
1571 dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001572 current_block_->AddInstruction(cls);
1573 // The class needs a temporary before being used by the type check.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001574 Temporaries temps(graph_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001575 temps.Add(cls);
1576 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
1577 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001578 new (arena_) HInstanceOf(object, cls, type_known_final, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001579 UpdateLocal(destination, current_block_->GetLastInstruction());
1580 } else {
1581 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
1582 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001583 new (arena_) HCheckCast(object, cls, type_known_final, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001584 }
1585 return true;
1586}
1587
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001588bool HGraphBuilder::NeedsAccessCheck(uint32_t type_index) const {
1589 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
1590 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index);
1591}
1592
Calin Juravle48c2b032014-12-09 18:11:36 +00001593void HGraphBuilder::BuildPackedSwitch(const Instruction& instruction, uint32_t dex_pc) {
David Brazdil2ef645b2015-06-17 18:20:52 +01001594 // Verifier guarantees that the payload for PackedSwitch contains:
1595 // (a) number of entries (may be zero)
1596 // (b) first and lowest switch case value (entry 0, always present)
1597 // (c) list of target pcs (entries 1 <= i <= N)
Andreas Gamped881df52014-11-24 23:28:39 -08001598 SwitchTable table(instruction, dex_pc, false);
1599
1600 // Value to test against.
1601 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
1602
David Brazdil2ef645b2015-06-17 18:20:52 +01001603 // Retrieve number of entries.
Andreas Gampee4d4d322014-12-04 09:09:57 -08001604 uint16_t num_entries = table.GetNumEntries();
David Brazdil2ef645b2015-06-17 18:20:52 +01001605 if (num_entries == 0) {
1606 return;
1607 }
Andreas Gampee4d4d322014-12-04 09:09:57 -08001608
Andreas Gamped881df52014-11-24 23:28:39 -08001609 // Chained cmp-and-branch, starting from starting_key.
1610 int32_t starting_key = table.GetEntryAt(0);
1611
Andreas Gamped881df52014-11-24 23:28:39 -08001612 for (size_t i = 1; i <= num_entries; i++) {
Andreas Gampee4d4d322014-12-04 09:09:57 -08001613 BuildSwitchCaseHelper(instruction, i, i == num_entries, table, value, starting_key + i - 1,
1614 table.GetEntryAt(i), dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08001615 }
Andreas Gamped881df52014-11-24 23:28:39 -08001616}
1617
Calin Juravle48c2b032014-12-09 18:11:36 +00001618void HGraphBuilder::BuildSparseSwitch(const Instruction& instruction, uint32_t dex_pc) {
David Brazdil2ef645b2015-06-17 18:20:52 +01001619 // Verifier guarantees that the payload for SparseSwitch contains:
1620 // (a) number of entries (may be zero)
1621 // (b) sorted key values (entries 0 <= i < N)
1622 // (c) target pcs corresponding to the switch values (entries N <= i < 2*N)
Andreas Gampee4d4d322014-12-04 09:09:57 -08001623 SwitchTable table(instruction, dex_pc, true);
1624
1625 // Value to test against.
1626 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
1627
1628 uint16_t num_entries = table.GetNumEntries();
Andreas Gampee4d4d322014-12-04 09:09:57 -08001629
1630 for (size_t i = 0; i < num_entries; i++) {
1631 BuildSwitchCaseHelper(instruction, i, i == static_cast<size_t>(num_entries) - 1, table, value,
1632 table.GetEntryAt(i), table.GetEntryAt(i + num_entries), dex_pc);
1633 }
Andreas Gampee4d4d322014-12-04 09:09:57 -08001634}
1635
1636void HGraphBuilder::BuildSwitchCaseHelper(const Instruction& instruction, size_t index,
1637 bool is_last_case, const SwitchTable& table,
1638 HInstruction* value, int32_t case_value_int,
1639 int32_t target_offset, uint32_t dex_pc) {
David Brazdil852eaff2015-02-02 15:23:05 +00001640 HBasicBlock* case_target = FindBlockStartingAt(dex_pc + target_offset);
1641 DCHECK(case_target != nullptr);
1642 PotentiallyAddSuspendCheck(case_target, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001643
1644 // The current case's value.
David Brazdil8d5b8b22015-03-24 10:51:52 +00001645 HInstruction* this_case_value = graph_->GetIntConstant(case_value_int);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001646
1647 // Compare value and this_case_value.
1648 HEqual* comparison = new (arena_) HEqual(value, this_case_value);
1649 current_block_->AddInstruction(comparison);
1650 HInstruction* ifinst = new (arena_) HIf(comparison);
1651 current_block_->AddInstruction(ifinst);
1652
1653 // Case hit: use the target offset to determine where to go.
Andreas Gampee4d4d322014-12-04 09:09:57 -08001654 current_block_->AddSuccessor(case_target);
1655
1656 // Case miss: go to the next case (or default fall-through).
1657 // When there is a next case, we use the block stored with the table offset representing this
1658 // case (that is where we registered them in ComputeBranchTargets).
1659 // When there is no next case, we use the following instruction.
1660 // TODO: Find a good way to peel the last iteration to avoid conditional, but still have re-use.
1661 if (!is_last_case) {
1662 HBasicBlock* next_case_target = FindBlockStartingAt(table.GetDexPcForIndex(index));
1663 DCHECK(next_case_target != nullptr);
1664 current_block_->AddSuccessor(next_case_target);
1665
1666 // Need to manually add the block, as there is no dex-pc transition for the cases.
1667 graph_->AddBlock(next_case_target);
1668
1669 current_block_ = next_case_target;
1670 } else {
1671 HBasicBlock* default_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
1672 DCHECK(default_target != nullptr);
1673 current_block_->AddSuccessor(default_target);
1674 current_block_ = nullptr;
1675 }
1676}
1677
David Brazdil852eaff2015-02-02 15:23:05 +00001678void HGraphBuilder::PotentiallyAddSuspendCheck(HBasicBlock* target, uint32_t dex_pc) {
1679 int32_t target_offset = target->GetDexPc() - dex_pc;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001680 if (target_offset <= 0) {
David Brazdil852eaff2015-02-02 15:23:05 +00001681 // DX generates back edges to the first encountered return. We can save
1682 // time of later passes by not adding redundant suspend checks.
David Brazdil2fd6aa52015-02-02 18:58:27 +00001683 HInstruction* last_in_target = target->GetLastInstruction();
1684 if (last_in_target != nullptr &&
1685 (last_in_target->IsReturn() || last_in_target->IsReturnVoid())) {
1686 return;
David Brazdil852eaff2015-02-02 15:23:05 +00001687 }
1688
1689 // Add a suspend check to backward branches which may potentially loop. We
1690 // can remove them after we recognize loops in the graph.
Calin Juravle225ff812014-11-13 16:46:39 +00001691 current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_pc));
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001692 }
1693}
1694
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001695bool HGraphBuilder::CanDecodeQuickenedInfo() const {
1696 return interpreter_metadata_ != nullptr;
1697}
1698
1699uint16_t HGraphBuilder::LookupQuickenedInfo(uint32_t dex_pc) {
1700 DCHECK(interpreter_metadata_ != nullptr);
1701 uint32_t dex_pc_in_map = DecodeUnsignedLeb128(&interpreter_metadata_);
1702 DCHECK_EQ(dex_pc, dex_pc_in_map);
1703 return DecodeUnsignedLeb128(&interpreter_metadata_);
1704}
1705
Calin Juravle225ff812014-11-13 16:46:39 +00001706bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001707 if (current_block_ == nullptr) {
1708 return true; // Dead code
1709 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001710
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001711 switch (instruction.Opcode()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001712 case Instruction::CONST_4: {
1713 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001714 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001715 UpdateLocal(register_index, constant);
1716 break;
1717 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001718
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001719 case Instruction::CONST_16: {
1720 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001721 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001722 UpdateLocal(register_index, constant);
1723 break;
1724 }
1725
Dave Allison20dfc792014-06-16 20:44:29 -07001726 case Instruction::CONST: {
1727 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001728 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i());
Dave Allison20dfc792014-06-16 20:44:29 -07001729 UpdateLocal(register_index, constant);
1730 break;
1731 }
1732
1733 case Instruction::CONST_HIGH16: {
1734 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001735 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16);
Dave Allison20dfc792014-06-16 20:44:29 -07001736 UpdateLocal(register_index, constant);
1737 break;
1738 }
1739
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001740 case Instruction::CONST_WIDE_16: {
1741 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001742 // Get 16 bits of constant value, sign extended to 64 bits.
1743 int64_t value = instruction.VRegB_21s();
1744 value <<= 48;
1745 value >>= 48;
David Brazdil8d5b8b22015-03-24 10:51:52 +00001746 HLongConstant* constant = graph_->GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001747 UpdateLocal(register_index, constant);
1748 break;
1749 }
1750
1751 case Instruction::CONST_WIDE_32: {
1752 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001753 // Get 32 bits of constant value, sign extended to 64 bits.
1754 int64_t value = instruction.VRegB_31i();
1755 value <<= 32;
1756 value >>= 32;
David Brazdil8d5b8b22015-03-24 10:51:52 +00001757 HLongConstant* constant = graph_->GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001758 UpdateLocal(register_index, constant);
1759 break;
1760 }
1761
1762 case Instruction::CONST_WIDE: {
1763 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001764 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001765 UpdateLocal(register_index, constant);
1766 break;
1767 }
1768
Dave Allison20dfc792014-06-16 20:44:29 -07001769 case Instruction::CONST_WIDE_HIGH16: {
1770 int32_t register_index = instruction.VRegA();
1771 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
David Brazdil8d5b8b22015-03-24 10:51:52 +00001772 HLongConstant* constant = graph_->GetLongConstant(value);
Dave Allison20dfc792014-06-16 20:44:29 -07001773 UpdateLocal(register_index, constant);
1774 break;
1775 }
1776
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001777 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001778 case Instruction::MOVE:
1779 case Instruction::MOVE_FROM16:
1780 case Instruction::MOVE_16: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001781 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001782 UpdateLocal(instruction.VRegA(), value);
1783 break;
1784 }
1785
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001786 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001787 case Instruction::MOVE_WIDE:
1788 case Instruction::MOVE_WIDE_FROM16:
1789 case Instruction::MOVE_WIDE_16: {
1790 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong);
1791 UpdateLocal(instruction.VRegA(), value);
1792 break;
1793 }
1794
1795 case Instruction::MOVE_OBJECT:
1796 case Instruction::MOVE_OBJECT_16:
1797 case Instruction::MOVE_OBJECT_FROM16: {
1798 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot);
1799 UpdateLocal(instruction.VRegA(), value);
1800 break;
1801 }
1802
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001803 case Instruction::RETURN_VOID_NO_BARRIER:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001804 case Instruction::RETURN_VOID: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001805 BuildReturn(instruction, Primitive::kPrimVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001806 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001807 }
1808
Dave Allison20dfc792014-06-16 20:44:29 -07001809#define IF_XX(comparison, cond) \
Calin Juravle225ff812014-11-13 16:46:39 +00001810 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
1811 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001812
Dave Allison20dfc792014-06-16 20:44:29 -07001813 IF_XX(HEqual, EQ);
1814 IF_XX(HNotEqual, NE);
1815 IF_XX(HLessThan, LT);
1816 IF_XX(HLessThanOrEqual, LE);
1817 IF_XX(HGreaterThan, GT);
1818 IF_XX(HGreaterThanOrEqual, GE);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001819
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001820 case Instruction::GOTO:
1821 case Instruction::GOTO_16:
1822 case Instruction::GOTO_32: {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001823 int32_t offset = instruction.GetTargetOffset();
Calin Juravle225ff812014-11-13 16:46:39 +00001824 HBasicBlock* target = FindBlockStartingAt(offset + dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001825 DCHECK(target != nullptr);
David Brazdil852eaff2015-02-02 15:23:05 +00001826 PotentiallyAddSuspendCheck(target, dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001827 current_block_->AddInstruction(new (arena_) HGoto());
1828 current_block_->AddSuccessor(target);
1829 current_block_ = nullptr;
1830 break;
1831 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001832
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001833 case Instruction::RETURN: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001834 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001835 break;
1836 }
1837
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001838 case Instruction::RETURN_OBJECT: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001839 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001840 break;
1841 }
1842
1843 case Instruction::RETURN_WIDE: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001844 BuildReturn(instruction, return_type_);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001845 break;
1846 }
1847
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001848 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001849 case Instruction::INVOKE_INTERFACE:
1850 case Instruction::INVOKE_STATIC:
1851 case Instruction::INVOKE_SUPER:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001852 case Instruction::INVOKE_VIRTUAL:
1853 case Instruction::INVOKE_VIRTUAL_QUICK: {
1854 uint16_t method_idx;
1855 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_QUICK) {
1856 if (!CanDecodeQuickenedInfo()) {
1857 return false;
1858 }
1859 method_idx = LookupQuickenedInfo(dex_pc);
1860 } else {
1861 method_idx = instruction.VRegB_35c();
1862 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001863 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001864 uint32_t args[5];
Ian Rogers29a26482014-05-02 15:27:29 -07001865 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00001866 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001867 number_of_vreg_arguments, false, args, -1)) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001868 return false;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001869 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001870 break;
1871 }
1872
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001873 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001874 case Instruction::INVOKE_INTERFACE_RANGE:
1875 case Instruction::INVOKE_STATIC_RANGE:
1876 case Instruction::INVOKE_SUPER_RANGE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001877 case Instruction::INVOKE_VIRTUAL_RANGE:
1878 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
1879 uint16_t method_idx;
1880 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK) {
1881 if (!CanDecodeQuickenedInfo()) {
1882 return false;
1883 }
1884 method_idx = LookupQuickenedInfo(dex_pc);
1885 } else {
1886 method_idx = instruction.VRegB_3rc();
1887 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001888 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1889 uint32_t register_index = instruction.VRegC();
Calin Juravle225ff812014-11-13 16:46:39 +00001890 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001891 number_of_vreg_arguments, true, nullptr, register_index)) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001892 return false;
1893 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001894 break;
1895 }
1896
Roland Levillain88cb1752014-10-20 16:36:47 +01001897 case Instruction::NEG_INT: {
1898 Unop_12x<HNeg>(instruction, Primitive::kPrimInt);
1899 break;
1900 }
1901
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001902 case Instruction::NEG_LONG: {
1903 Unop_12x<HNeg>(instruction, Primitive::kPrimLong);
1904 break;
1905 }
1906
Roland Levillain3dbcb382014-10-28 17:30:07 +00001907 case Instruction::NEG_FLOAT: {
1908 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat);
1909 break;
1910 }
1911
1912 case Instruction::NEG_DOUBLE: {
1913 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble);
1914 break;
1915 }
1916
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001917 case Instruction::NOT_INT: {
1918 Unop_12x<HNot>(instruction, Primitive::kPrimInt);
1919 break;
1920 }
1921
Roland Levillain70566432014-10-24 16:20:17 +01001922 case Instruction::NOT_LONG: {
1923 Unop_12x<HNot>(instruction, Primitive::kPrimLong);
1924 break;
1925 }
1926
Roland Levillaindff1f282014-11-05 14:15:05 +00001927 case Instruction::INT_TO_LONG: {
Roland Levillain624279f2014-12-04 11:54:28 +00001928 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong, dex_pc);
Roland Levillaindff1f282014-11-05 14:15:05 +00001929 break;
1930 }
1931
Roland Levillaincff13742014-11-17 14:32:17 +00001932 case Instruction::INT_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001933 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00001934 break;
1935 }
1936
1937 case Instruction::INT_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001938 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00001939 break;
1940 }
1941
Roland Levillain946e1432014-11-11 17:35:19 +00001942 case Instruction::LONG_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001943 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt, dex_pc);
Roland Levillain946e1432014-11-11 17:35:19 +00001944 break;
1945 }
1946
Roland Levillain6d0e4832014-11-27 18:31:21 +00001947 case Instruction::LONG_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001948 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimFloat, dex_pc);
Roland Levillain6d0e4832014-11-27 18:31:21 +00001949 break;
1950 }
1951
Roland Levillain647b9ed2014-11-27 12:06:00 +00001952 case Instruction::LONG_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001953 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble, dex_pc);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001954 break;
1955 }
1956
Roland Levillain3f8f9362014-12-02 17:45:01 +00001957 case Instruction::FLOAT_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001958 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimInt, dex_pc);
1959 break;
1960 }
1961
1962 case Instruction::FLOAT_TO_LONG: {
1963 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimLong, dex_pc);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001964 break;
1965 }
1966
Roland Levillain8964e2b2014-12-04 12:10:50 +00001967 case Instruction::FLOAT_TO_DOUBLE: {
1968 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimDouble, dex_pc);
1969 break;
1970 }
1971
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001972 case Instruction::DOUBLE_TO_INT: {
1973 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimInt, dex_pc);
1974 break;
1975 }
1976
1977 case Instruction::DOUBLE_TO_LONG: {
1978 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimLong, dex_pc);
1979 break;
1980 }
1981
Roland Levillain8964e2b2014-12-04 12:10:50 +00001982 case Instruction::DOUBLE_TO_FLOAT: {
1983 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimFloat, dex_pc);
1984 break;
1985 }
1986
Roland Levillain51d3fc42014-11-13 14:11:42 +00001987 case Instruction::INT_TO_BYTE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001988 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte, dex_pc);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001989 break;
1990 }
1991
Roland Levillain01a8d712014-11-14 16:27:39 +00001992 case Instruction::INT_TO_SHORT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001993 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort, dex_pc);
Roland Levillain01a8d712014-11-14 16:27:39 +00001994 break;
1995 }
1996
Roland Levillain981e4542014-11-14 11:47:14 +00001997 case Instruction::INT_TO_CHAR: {
Roland Levillain624279f2014-12-04 11:54:28 +00001998 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar, dex_pc);
Roland Levillain981e4542014-11-14 11:47:14 +00001999 break;
2000 }
2001
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002002 case Instruction::ADD_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002003 Binop_23x<HAdd>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002004 break;
2005 }
2006
2007 case Instruction::ADD_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002008 Binop_23x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002009 break;
2010 }
2011
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002012 case Instruction::ADD_DOUBLE: {
2013 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble);
2014 break;
2015 }
2016
2017 case Instruction::ADD_FLOAT: {
2018 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat);
2019 break;
2020 }
2021
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002022 case Instruction::SUB_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002023 Binop_23x<HSub>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002024 break;
2025 }
2026
2027 case Instruction::SUB_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002028 Binop_23x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002029 break;
2030 }
2031
Calin Juravle096cc022014-10-23 17:01:13 +01002032 case Instruction::SUB_FLOAT: {
2033 Binop_23x<HSub>(instruction, Primitive::kPrimFloat);
2034 break;
2035 }
2036
2037 case Instruction::SUB_DOUBLE: {
2038 Binop_23x<HSub>(instruction, Primitive::kPrimDouble);
2039 break;
2040 }
2041
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002042 case Instruction::ADD_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002043 Binop_12x<HAdd>(instruction, Primitive::kPrimInt);
2044 break;
2045 }
2046
Calin Juravle34bacdf2014-10-07 20:23:36 +01002047 case Instruction::MUL_INT: {
2048 Binop_23x<HMul>(instruction, Primitive::kPrimInt);
2049 break;
2050 }
2051
2052 case Instruction::MUL_LONG: {
2053 Binop_23x<HMul>(instruction, Primitive::kPrimLong);
2054 break;
2055 }
2056
Calin Juravleb5bfa962014-10-21 18:02:24 +01002057 case Instruction::MUL_FLOAT: {
2058 Binop_23x<HMul>(instruction, Primitive::kPrimFloat);
2059 break;
2060 }
2061
2062 case Instruction::MUL_DOUBLE: {
2063 Binop_23x<HMul>(instruction, Primitive::kPrimDouble);
2064 break;
2065 }
2066
Calin Juravled0d48522014-11-04 16:40:20 +00002067 case Instruction::DIV_INT: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002068 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2069 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravled0d48522014-11-04 16:40:20 +00002070 break;
2071 }
2072
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002073 case Instruction::DIV_LONG: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002074 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2075 dex_pc, Primitive::kPrimLong, false, true);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002076 break;
2077 }
2078
Calin Juravle7c4954d2014-10-28 16:57:40 +00002079 case Instruction::DIV_FLOAT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002080 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002081 break;
2082 }
2083
2084 case Instruction::DIV_DOUBLE: {
Calin Juravle225ff812014-11-13 16:46:39 +00002085 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002086 break;
2087 }
2088
Calin Juravlebacfec32014-11-14 15:54:36 +00002089 case Instruction::REM_INT: {
2090 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2091 dex_pc, Primitive::kPrimInt, false, false);
2092 break;
2093 }
2094
2095 case Instruction::REM_LONG: {
2096 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2097 dex_pc, Primitive::kPrimLong, false, false);
2098 break;
2099 }
2100
Calin Juravled2ec87d2014-12-08 14:24:46 +00002101 case Instruction::REM_FLOAT: {
2102 Binop_23x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2103 break;
2104 }
2105
2106 case Instruction::REM_DOUBLE: {
2107 Binop_23x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2108 break;
2109 }
2110
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002111 case Instruction::AND_INT: {
2112 Binop_23x<HAnd>(instruction, Primitive::kPrimInt);
2113 break;
2114 }
2115
2116 case Instruction::AND_LONG: {
2117 Binop_23x<HAnd>(instruction, Primitive::kPrimLong);
2118 break;
2119 }
2120
Calin Juravle9aec02f2014-11-18 23:06:35 +00002121 case Instruction::SHL_INT: {
2122 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt);
2123 break;
2124 }
2125
2126 case Instruction::SHL_LONG: {
2127 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong);
2128 break;
2129 }
2130
2131 case Instruction::SHR_INT: {
2132 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt);
2133 break;
2134 }
2135
2136 case Instruction::SHR_LONG: {
2137 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong);
2138 break;
2139 }
2140
2141 case Instruction::USHR_INT: {
2142 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt);
2143 break;
2144 }
2145
2146 case Instruction::USHR_LONG: {
2147 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong);
2148 break;
2149 }
2150
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002151 case Instruction::OR_INT: {
2152 Binop_23x<HOr>(instruction, Primitive::kPrimInt);
2153 break;
2154 }
2155
2156 case Instruction::OR_LONG: {
2157 Binop_23x<HOr>(instruction, Primitive::kPrimLong);
2158 break;
2159 }
2160
2161 case Instruction::XOR_INT: {
2162 Binop_23x<HXor>(instruction, Primitive::kPrimInt);
2163 break;
2164 }
2165
2166 case Instruction::XOR_LONG: {
2167 Binop_23x<HXor>(instruction, Primitive::kPrimLong);
2168 break;
2169 }
2170
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002171 case Instruction::ADD_LONG_2ADDR: {
2172 Binop_12x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002173 break;
2174 }
2175
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002176 case Instruction::ADD_DOUBLE_2ADDR: {
2177 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble);
2178 break;
2179 }
2180
2181 case Instruction::ADD_FLOAT_2ADDR: {
2182 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat);
2183 break;
2184 }
2185
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002186 case Instruction::SUB_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002187 Binop_12x<HSub>(instruction, Primitive::kPrimInt);
2188 break;
2189 }
2190
2191 case Instruction::SUB_LONG_2ADDR: {
2192 Binop_12x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002193 break;
2194 }
2195
Calin Juravle096cc022014-10-23 17:01:13 +01002196 case Instruction::SUB_FLOAT_2ADDR: {
2197 Binop_12x<HSub>(instruction, Primitive::kPrimFloat);
2198 break;
2199 }
2200
2201 case Instruction::SUB_DOUBLE_2ADDR: {
2202 Binop_12x<HSub>(instruction, Primitive::kPrimDouble);
2203 break;
2204 }
2205
Calin Juravle34bacdf2014-10-07 20:23:36 +01002206 case Instruction::MUL_INT_2ADDR: {
2207 Binop_12x<HMul>(instruction, Primitive::kPrimInt);
2208 break;
2209 }
2210
2211 case Instruction::MUL_LONG_2ADDR: {
2212 Binop_12x<HMul>(instruction, Primitive::kPrimLong);
2213 break;
2214 }
2215
Calin Juravleb5bfa962014-10-21 18:02:24 +01002216 case Instruction::MUL_FLOAT_2ADDR: {
2217 Binop_12x<HMul>(instruction, Primitive::kPrimFloat);
2218 break;
2219 }
2220
2221 case Instruction::MUL_DOUBLE_2ADDR: {
2222 Binop_12x<HMul>(instruction, Primitive::kPrimDouble);
2223 break;
2224 }
2225
Calin Juravle865fc882014-11-06 17:09:03 +00002226 case Instruction::DIV_INT_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002227 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2228 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravle865fc882014-11-06 17:09:03 +00002229 break;
2230 }
2231
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002232 case Instruction::DIV_LONG_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002233 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2234 dex_pc, Primitive::kPrimLong, false, true);
2235 break;
2236 }
2237
2238 case Instruction::REM_INT_2ADDR: {
2239 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2240 dex_pc, Primitive::kPrimInt, false, false);
2241 break;
2242 }
2243
2244 case Instruction::REM_LONG_2ADDR: {
2245 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2246 dex_pc, Primitive::kPrimLong, false, false);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002247 break;
2248 }
2249
Calin Juravled2ec87d2014-12-08 14:24:46 +00002250 case Instruction::REM_FLOAT_2ADDR: {
2251 Binop_12x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2252 break;
2253 }
2254
2255 case Instruction::REM_DOUBLE_2ADDR: {
2256 Binop_12x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2257 break;
2258 }
2259
Calin Juravle9aec02f2014-11-18 23:06:35 +00002260 case Instruction::SHL_INT_2ADDR: {
2261 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt);
2262 break;
2263 }
2264
2265 case Instruction::SHL_LONG_2ADDR: {
2266 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong);
2267 break;
2268 }
2269
2270 case Instruction::SHR_INT_2ADDR: {
2271 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt);
2272 break;
2273 }
2274
2275 case Instruction::SHR_LONG_2ADDR: {
2276 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong);
2277 break;
2278 }
2279
2280 case Instruction::USHR_INT_2ADDR: {
2281 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt);
2282 break;
2283 }
2284
2285 case Instruction::USHR_LONG_2ADDR: {
2286 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong);
2287 break;
2288 }
2289
Calin Juravle7c4954d2014-10-28 16:57:40 +00002290 case Instruction::DIV_FLOAT_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00002291 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002292 break;
2293 }
2294
2295 case Instruction::DIV_DOUBLE_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00002296 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002297 break;
2298 }
2299
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002300 case Instruction::AND_INT_2ADDR: {
2301 Binop_12x<HAnd>(instruction, Primitive::kPrimInt);
2302 break;
2303 }
2304
2305 case Instruction::AND_LONG_2ADDR: {
2306 Binop_12x<HAnd>(instruction, Primitive::kPrimLong);
2307 break;
2308 }
2309
2310 case Instruction::OR_INT_2ADDR: {
2311 Binop_12x<HOr>(instruction, Primitive::kPrimInt);
2312 break;
2313 }
2314
2315 case Instruction::OR_LONG_2ADDR: {
2316 Binop_12x<HOr>(instruction, Primitive::kPrimLong);
2317 break;
2318 }
2319
2320 case Instruction::XOR_INT_2ADDR: {
2321 Binop_12x<HXor>(instruction, Primitive::kPrimInt);
2322 break;
2323 }
2324
2325 case Instruction::XOR_LONG_2ADDR: {
2326 Binop_12x<HXor>(instruction, Primitive::kPrimLong);
2327 break;
2328 }
2329
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002330 case Instruction::ADD_INT_LIT16: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002331 Binop_22s<HAdd>(instruction, false);
2332 break;
2333 }
2334
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002335 case Instruction::AND_INT_LIT16: {
2336 Binop_22s<HAnd>(instruction, false);
2337 break;
2338 }
2339
2340 case Instruction::OR_INT_LIT16: {
2341 Binop_22s<HOr>(instruction, false);
2342 break;
2343 }
2344
2345 case Instruction::XOR_INT_LIT16: {
2346 Binop_22s<HXor>(instruction, false);
2347 break;
2348 }
2349
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002350 case Instruction::RSUB_INT: {
2351 Binop_22s<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002352 break;
2353 }
2354
Calin Juravle34bacdf2014-10-07 20:23:36 +01002355 case Instruction::MUL_INT_LIT16: {
2356 Binop_22s<HMul>(instruction, false);
2357 break;
2358 }
2359
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002360 case Instruction::ADD_INT_LIT8: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002361 Binop_22b<HAdd>(instruction, false);
2362 break;
2363 }
2364
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002365 case Instruction::AND_INT_LIT8: {
2366 Binop_22b<HAnd>(instruction, false);
2367 break;
2368 }
2369
2370 case Instruction::OR_INT_LIT8: {
2371 Binop_22b<HOr>(instruction, false);
2372 break;
2373 }
2374
2375 case Instruction::XOR_INT_LIT8: {
2376 Binop_22b<HXor>(instruction, false);
2377 break;
2378 }
2379
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002380 case Instruction::RSUB_INT_LIT8: {
2381 Binop_22b<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002382 break;
2383 }
2384
Calin Juravle34bacdf2014-10-07 20:23:36 +01002385 case Instruction::MUL_INT_LIT8: {
2386 Binop_22b<HMul>(instruction, false);
2387 break;
2388 }
2389
Calin Juravled0d48522014-11-04 16:40:20 +00002390 case Instruction::DIV_INT_LIT16:
2391 case Instruction::DIV_INT_LIT8: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002392 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2393 dex_pc, Primitive::kPrimInt, true, true);
2394 break;
2395 }
2396
2397 case Instruction::REM_INT_LIT16:
2398 case Instruction::REM_INT_LIT8: {
2399 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2400 dex_pc, Primitive::kPrimInt, true, false);
Calin Juravled0d48522014-11-04 16:40:20 +00002401 break;
2402 }
2403
Calin Juravle9aec02f2014-11-18 23:06:35 +00002404 case Instruction::SHL_INT_LIT8: {
2405 Binop_22b<HShl>(instruction, false);
2406 break;
2407 }
2408
2409 case Instruction::SHR_INT_LIT8: {
2410 Binop_22b<HShr>(instruction, false);
2411 break;
2412 }
2413
2414 case Instruction::USHR_INT_LIT8: {
2415 Binop_22b<HUShr>(instruction, false);
2416 break;
2417 }
2418
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002419 case Instruction::NEW_INSTANCE: {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002420 uint16_t type_index = instruction.VRegB_21c();
Jeff Hao848f70a2014-01-15 13:49:50 -08002421 if (compiler_driver_->IsStringTypeIndex(type_index, dex_file_)) {
Jeff Hao848f70a2014-01-15 13:49:50 -08002422 int32_t register_index = instruction.VRegA();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002423 HFakeString* fake_string = new (arena_) HFakeString();
2424 current_block_->AddInstruction(fake_string);
2425 UpdateLocal(register_index, fake_string);
Jeff Hao848f70a2014-01-15 13:49:50 -08002426 } else {
2427 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
2428 ? kQuickAllocObjectWithAccessCheck
2429 : kQuickAllocObject;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002430
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002431 current_block_->AddInstruction(new (arena_) HNewInstance(
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002432 graph_->GetCurrentMethod(),
2433 dex_pc,
2434 type_index,
2435 *dex_compilation_unit_->GetDexFile(),
2436 entrypoint));
Jeff Hao848f70a2014-01-15 13:49:50 -08002437 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
2438 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002439 break;
2440 }
2441
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002442 case Instruction::NEW_ARRAY: {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002443 uint16_t type_index = instruction.VRegC_22c();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002444 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt);
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002445 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
2446 ? kQuickAllocArrayWithAccessCheck
2447 : kQuickAllocArray;
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002448 current_block_->AddInstruction(new (arena_) HNewArray(length,
2449 graph_->GetCurrentMethod(),
2450 dex_pc,
2451 type_index,
2452 *dex_compilation_unit_->GetDexFile(),
2453 entrypoint));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002454 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
2455 break;
2456 }
2457
2458 case Instruction::FILLED_NEW_ARRAY: {
2459 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
2460 uint32_t type_index = instruction.VRegB_35c();
2461 uint32_t args[5];
2462 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00002463 BuildFilledNewArray(dex_pc, type_index, number_of_vreg_arguments, false, args, 0);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002464 break;
2465 }
2466
2467 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2468 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
2469 uint32_t type_index = instruction.VRegB_3rc();
2470 uint32_t register_index = instruction.VRegC_3rc();
2471 BuildFilledNewArray(
Calin Juravle225ff812014-11-13 16:46:39 +00002472 dex_pc, type_index, number_of_vreg_arguments, true, nullptr, register_index);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002473 break;
2474 }
2475
2476 case Instruction::FILL_ARRAY_DATA: {
Calin Juravle225ff812014-11-13 16:46:39 +00002477 BuildFillArrayData(instruction, dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002478 break;
2479 }
2480
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002481 case Instruction::MOVE_RESULT:
Dave Allison20dfc792014-06-16 20:44:29 -07002482 case Instruction::MOVE_RESULT_WIDE:
David Brazdilfc6a86a2015-06-26 10:33:45 +00002483 case Instruction::MOVE_RESULT_OBJECT: {
Nicolas Geoffray1efcc222015-06-24 12:41:20 +01002484 if (latest_result_ == nullptr) {
2485 // Only dead code can lead to this situation, where the verifier
2486 // does not reject the method.
2487 } else {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002488 // An Invoke/FilledNewArray and its MoveResult could have landed in
2489 // different blocks if there was a try/catch block boundary between
2490 // them. For Invoke, we insert a StoreLocal after the instruction. For
2491 // FilledNewArray, the local needs to be updated after the array was
2492 // filled, otherwise we might overwrite an input vreg.
2493 HStoreLocal* update_local =
2494 new (arena_) HStoreLocal(GetLocalAt(instruction.VRegA()), latest_result_);
2495 HBasicBlock* block = latest_result_->GetBlock();
2496 if (block == current_block_) {
2497 // MoveResult and the previous instruction are in the same block.
2498 current_block_->AddInstruction(update_local);
2499 } else {
2500 // The two instructions are in different blocks. Insert the MoveResult
2501 // before the final control-flow instruction of the previous block.
2502 DCHECK(block->EndsWithControlFlowInstruction());
2503 DCHECK(current_block_->GetInstructions().IsEmpty());
2504 block->InsertInstructionBefore(update_local, block->GetLastInstruction());
2505 }
Nicolas Geoffray1efcc222015-06-24 12:41:20 +01002506 latest_result_ = nullptr;
2507 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002508 break;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002509 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002510
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002511 case Instruction::CMP_LONG: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002512 Binop_23x_cmp(instruction, Primitive::kPrimLong, ComparisonBias::kNoBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002513 break;
2514 }
2515
2516 case Instruction::CMPG_FLOAT: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002517 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kGtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002518 break;
2519 }
2520
2521 case Instruction::CMPG_DOUBLE: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002522 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kGtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002523 break;
2524 }
2525
2526 case Instruction::CMPL_FLOAT: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002527 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kLtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002528 break;
2529 }
2530
2531 case Instruction::CMPL_DOUBLE: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002532 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kLtBias, dex_pc);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002533 break;
2534 }
2535
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002536 case Instruction::NOP:
2537 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002538
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002539 case Instruction::IGET:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002540 case Instruction::IGET_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002541 case Instruction::IGET_WIDE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002542 case Instruction::IGET_WIDE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002543 case Instruction::IGET_OBJECT:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002544 case Instruction::IGET_OBJECT_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002545 case Instruction::IGET_BOOLEAN:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002546 case Instruction::IGET_BOOLEAN_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002547 case Instruction::IGET_BYTE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002548 case Instruction::IGET_BYTE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002549 case Instruction::IGET_CHAR:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002550 case Instruction::IGET_CHAR_QUICK:
2551 case Instruction::IGET_SHORT:
2552 case Instruction::IGET_SHORT_QUICK: {
Calin Juravle225ff812014-11-13 16:46:39 +00002553 if (!BuildInstanceFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002554 return false;
2555 }
2556 break;
2557 }
2558
2559 case Instruction::IPUT:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002560 case Instruction::IPUT_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002561 case Instruction::IPUT_WIDE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002562 case Instruction::IPUT_WIDE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002563 case Instruction::IPUT_OBJECT:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002564 case Instruction::IPUT_OBJECT_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002565 case Instruction::IPUT_BOOLEAN:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002566 case Instruction::IPUT_BOOLEAN_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002567 case Instruction::IPUT_BYTE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002568 case Instruction::IPUT_BYTE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002569 case Instruction::IPUT_CHAR:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002570 case Instruction::IPUT_CHAR_QUICK:
2571 case Instruction::IPUT_SHORT:
2572 case Instruction::IPUT_SHORT_QUICK: {
Calin Juravle225ff812014-11-13 16:46:39 +00002573 if (!BuildInstanceFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002574 return false;
2575 }
2576 break;
2577 }
2578
2579 case Instruction::SGET:
2580 case Instruction::SGET_WIDE:
2581 case Instruction::SGET_OBJECT:
2582 case Instruction::SGET_BOOLEAN:
2583 case Instruction::SGET_BYTE:
2584 case Instruction::SGET_CHAR:
2585 case Instruction::SGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002586 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002587 return false;
2588 }
2589 break;
2590 }
2591
2592 case Instruction::SPUT:
2593 case Instruction::SPUT_WIDE:
2594 case Instruction::SPUT_OBJECT:
2595 case Instruction::SPUT_BOOLEAN:
2596 case Instruction::SPUT_BYTE:
2597 case Instruction::SPUT_CHAR:
2598 case Instruction::SPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002599 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002600 return false;
2601 }
2602 break;
2603 }
2604
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002605#define ARRAY_XX(kind, anticipated_type) \
2606 case Instruction::AGET##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00002607 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002608 break; \
2609 } \
2610 case Instruction::APUT##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00002611 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002612 break; \
2613 }
2614
2615 ARRAY_XX(, Primitive::kPrimInt);
2616 ARRAY_XX(_WIDE, Primitive::kPrimLong);
2617 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
2618 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
2619 ARRAY_XX(_BYTE, Primitive::kPrimByte);
2620 ARRAY_XX(_CHAR, Primitive::kPrimChar);
2621 ARRAY_XX(_SHORT, Primitive::kPrimShort);
2622
Nicolas Geoffray39468442014-09-02 15:17:15 +01002623 case Instruction::ARRAY_LENGTH: {
2624 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002625 // No need for a temporary for the null check, it is the only input of the following
2626 // instruction.
Calin Juravle225ff812014-11-13 16:46:39 +00002627 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002628 current_block_->AddInstruction(object);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002629 current_block_->AddInstruction(new (arena_) HArrayLength(object));
2630 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
2631 break;
2632 }
2633
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002634 case Instruction::CONST_STRING: {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002635 current_block_->AddInstruction(
2636 new (arena_) HLoadString(graph_->GetCurrentMethod(), instruction.VRegB_21c(), dex_pc));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002637 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2638 break;
2639 }
2640
2641 case Instruction::CONST_STRING_JUMBO: {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002642 current_block_->AddInstruction(
2643 new (arena_) HLoadString(graph_->GetCurrentMethod(), instruction.VRegB_31c(), dex_pc));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002644 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
2645 break;
2646 }
2647
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002648 case Instruction::CONST_CLASS: {
2649 uint16_t type_index = instruction.VRegB_21c();
2650 bool type_known_final;
2651 bool type_known_abstract;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002652 bool dont_use_is_referrers_class;
2653 // `CanAccessTypeWithoutChecks` will tell whether the method being
2654 // built is trying to access its own class, so that the generated
2655 // code can optimize for this case. However, the optimization does not
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002656 // work for inlining, so we use `IsOutermostCompilingClass` instead.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002657 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
2658 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002659 &type_known_final, &type_known_abstract, &dont_use_is_referrers_class);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002660 if (!can_access) {
Calin Juravle48c2b032014-12-09 18:11:36 +00002661 MaybeRecordStat(MethodCompilationStat::kNotCompiledCantAccesType);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002662 return false;
2663 }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002664 current_block_->AddInstruction(new (arena_) HLoadClass(
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002665 graph_->GetCurrentMethod(),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002666 type_index,
2667 *dex_compilation_unit_->GetDexFile(),
2668 IsOutermostCompilingClass(type_index),
2669 dex_pc));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002670 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2671 break;
2672 }
2673
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002674 case Instruction::MOVE_EXCEPTION: {
2675 current_block_->AddInstruction(new (arena_) HLoadException());
2676 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
David Brazdilcb1c0552015-08-04 16:22:25 +01002677 current_block_->AddInstruction(new (arena_) HClearException());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002678 break;
2679 }
2680
2681 case Instruction::THROW: {
2682 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00002683 current_block_->AddInstruction(new (arena_) HThrow(exception, dex_pc));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002684 // A throw instruction must branch to the exit block.
2685 current_block_->AddSuccessor(exit_block_);
2686 // We finished building this block. Set the current block to null to avoid
2687 // adding dead instructions to it.
2688 current_block_ = nullptr;
2689 break;
2690 }
2691
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002692 case Instruction::INSTANCE_OF: {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002693 uint8_t destination = instruction.VRegA_22c();
2694 uint8_t reference = instruction.VRegB_22c();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002695 uint16_t type_index = instruction.VRegC_22c();
Calin Juravle225ff812014-11-13 16:46:39 +00002696 if (!BuildTypeCheck(instruction, destination, reference, type_index, dex_pc)) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002697 return false;
2698 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002699 break;
2700 }
2701
2702 case Instruction::CHECK_CAST: {
2703 uint8_t reference = instruction.VRegA_21c();
2704 uint16_t type_index = instruction.VRegB_21c();
Calin Juravle225ff812014-11-13 16:46:39 +00002705 if (!BuildTypeCheck(instruction, -1, reference, type_index, dex_pc)) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002706 return false;
2707 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002708 break;
2709 }
2710
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002711 case Instruction::MONITOR_ENTER: {
2712 current_block_->AddInstruction(new (arena_) HMonitorOperation(
2713 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2714 HMonitorOperation::kEnter,
Calin Juravle225ff812014-11-13 16:46:39 +00002715 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002716 break;
2717 }
2718
2719 case Instruction::MONITOR_EXIT: {
2720 current_block_->AddInstruction(new (arena_) HMonitorOperation(
2721 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2722 HMonitorOperation::kExit,
Calin Juravle225ff812014-11-13 16:46:39 +00002723 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002724 break;
2725 }
2726
Andreas Gamped881df52014-11-24 23:28:39 -08002727 case Instruction::PACKED_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002728 BuildPackedSwitch(instruction, dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08002729 break;
2730 }
2731
Andreas Gampee4d4d322014-12-04 09:09:57 -08002732 case Instruction::SPARSE_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002733 BuildSparseSwitch(instruction, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08002734 break;
2735 }
2736
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002737 default:
Calin Juravle48c2b032014-12-09 18:11:36 +00002738 VLOG(compiler) << "Did not compile "
2739 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
2740 << " because of unhandled instruction "
2741 << instruction.Name();
2742 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnhandledInstruction);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002743 return false;
2744 }
2745 return true;
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00002746} // NOLINT(readability/fn_size)
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002747
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002748HLocal* HGraphBuilder::GetLocalAt(int register_index) const {
2749 return locals_.Get(register_index);
2750}
2751
2752void HGraphBuilder::UpdateLocal(int register_index, HInstruction* instruction) const {
2753 HLocal* local = GetLocalAt(register_index);
2754 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction));
2755}
2756
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002757HInstruction* HGraphBuilder::LoadLocal(int register_index, Primitive::Type type) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002758 HLocal* local = GetLocalAt(register_index);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002759 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type));
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002760 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002761}
2762
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002763} // namespace art