blob: 21540e8ed7a1a10bdcbc3af541a12e8409bb894e [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) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060049 HInstruction* temp = new (graph_->GetArena()) HTemporary(index_, instruction->GetDexPc());
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);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100143 locals_.resize(count);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000144 for (int i = 0; i < count; i++) {
145 HLocal* local = new (arena_) HLocal(i);
146 entry_block_->AddInstruction(local);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100147 locals_[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();
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100159 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.
Calin Juravlec05aca72015-10-13 13:10:33 +0000164 HParameterValue* parameter = new (arena_) HParameterValue(parameter_index++,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600165 Primitive::kPrimNot,
166 true);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100167 entry_block_->AddInstruction(parameter);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100168 HLocal* local = GetLocalAt(locals_index++);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600169 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter, local->GetDexPc()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100170 number_of_parameters--;
171 }
172
Calin Juravlec05aca72015-10-13 13:10:33 +0000173 uint32_t pos = 1;
174 for (int i = 0; i < number_of_parameters; i++) {
175 HParameterValue* parameter = new (arena_) HParameterValue(parameter_index++,
176 Primitive::GetType(shorty[pos++]),
177 false);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100178 entry_block_->AddInstruction(parameter);
179 HLocal* local = GetLocalAt(locals_index++);
180 // Store the parameter value in the local that the dex code will use
181 // to reference that parameter.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600182 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter, local->GetDexPc()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100183 bool is_wide = (parameter->GetType() == Primitive::kPrimLong)
184 || (parameter->GetType() == Primitive::kPrimDouble);
185 if (is_wide) {
186 i++;
187 locals_index++;
188 parameter_index++;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100189 }
190 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100191}
192
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100193template<typename T>
Calin Juravle225ff812014-11-13 16:46:39 +0000194void HGraphBuilder::If_22t(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000195 int32_t target_offset = instruction.GetTargetOffset();
David Brazdil852eaff2015-02-02 15:23:05 +0000196 HBasicBlock* branch_target = FindBlockStartingAt(dex_pc + target_offset);
197 HBasicBlock* fallthrough_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
198 DCHECK(branch_target != nullptr);
199 DCHECK(fallthrough_target != nullptr);
200 PotentiallyAddSuspendCheck(branch_target, dex_pc);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600201 HInstruction* first = LoadLocal(instruction.VRegA(), Primitive::kPrimInt, dex_pc);
202 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
203 T* comparison = new (arena_) T(first, second, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700204 current_block_->AddInstruction(comparison);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600205 HInstruction* ifinst = new (arena_) HIf(comparison, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700206 current_block_->AddInstruction(ifinst);
David Brazdil852eaff2015-02-02 15:23:05 +0000207 current_block_->AddSuccessor(branch_target);
208 current_block_->AddSuccessor(fallthrough_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700209 current_block_ = nullptr;
210}
211
212template<typename T>
Calin Juravle225ff812014-11-13 16:46:39 +0000213void HGraphBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000214 int32_t target_offset = instruction.GetTargetOffset();
David Brazdil852eaff2015-02-02 15:23:05 +0000215 HBasicBlock* branch_target = FindBlockStartingAt(dex_pc + target_offset);
216 HBasicBlock* fallthrough_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
217 DCHECK(branch_target != nullptr);
218 DCHECK(fallthrough_target != nullptr);
219 PotentiallyAddSuspendCheck(branch_target, dex_pc);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600220 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt, dex_pc);
221 T* comparison = new (arena_) T(value, graph_->GetIntConstant(0, dex_pc), dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700222 current_block_->AddInstruction(comparison);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600223 HInstruction* ifinst = new (arena_) HIf(comparison, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700224 current_block_->AddInstruction(ifinst);
David Brazdil852eaff2015-02-02 15:23:05 +0000225 current_block_->AddSuccessor(branch_target);
226 current_block_->AddSuccessor(fallthrough_target);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100227 current_block_ = nullptr;
228}
229
Calin Juravle48c2b032014-12-09 18:11:36 +0000230void HGraphBuilder::MaybeRecordStat(MethodCompilationStat compilation_stat) {
231 if (compilation_stats_ != nullptr) {
232 compilation_stats_->RecordStat(compilation_stat);
233 }
234}
235
David Brazdil1b498722015-03-31 11:37:18 +0100236bool HGraphBuilder::SkipCompilation(const DexFile::CodeItem& code_item,
Calin Juravle48c2b032014-12-09 18:11:36 +0000237 size_t number_of_branches) {
238 const CompilerOptions& compiler_options = compiler_driver_->GetCompilerOptions();
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000239 CompilerOptions::CompilerFilter compiler_filter = compiler_options.GetCompilerFilter();
240 if (compiler_filter == CompilerOptions::kEverything) {
241 return false;
242 }
243
David Brazdil1b498722015-03-31 11:37:18 +0100244 if (compiler_options.IsHugeMethod(code_item.insns_size_in_code_units_)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000245 VLOG(compiler) << "Skip compilation of huge method "
246 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
David Brazdil1b498722015-03-31 11:37:18 +0100247 << ": " << code_item.insns_size_in_code_units_ << " code units";
Calin Juravle48c2b032014-12-09 18:11:36 +0000248 MaybeRecordStat(MethodCompilationStat::kNotCompiledHugeMethod);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000249 return true;
250 }
251
252 // If it's large and contains no branches, it's likely to be machine generated initialization.
David Brazdil1b498722015-03-31 11:37:18 +0100253 if (compiler_options.IsLargeMethod(code_item.insns_size_in_code_units_)
254 && (number_of_branches == 0)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000255 VLOG(compiler) << "Skip compilation of large method with no branch "
256 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
David Brazdil1b498722015-03-31 11:37:18 +0100257 << ": " << code_item.insns_size_in_code_units_ << " code units";
Calin Juravle48c2b032014-12-09 18:11:36 +0000258 MaybeRecordStat(MethodCompilationStat::kNotCompiledLargeMethodNoBranches);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000259 return true;
260 }
261
262 return false;
263}
264
David Brazdilfc6a86a2015-06-26 10:33:45 +0000265void HGraphBuilder::CreateBlocksForTryCatch(const DexFile::CodeItem& code_item) {
266 if (code_item.tries_size_ == 0) {
267 return;
268 }
269
270 // Create branch targets at the start/end of the TryItem range. These are
271 // places where the program might fall through into/out of the a block and
272 // where TryBoundary instructions will be inserted later. Other edges which
273 // enter/exit the try blocks are a result of branches/switches.
274 for (size_t idx = 0; idx < code_item.tries_size_; ++idx) {
275 const DexFile::TryItem* try_item = DexFile::GetTryItems(code_item, idx);
276 uint32_t dex_pc_start = try_item->start_addr_;
277 uint32_t dex_pc_end = dex_pc_start + try_item->insn_count_;
278 FindOrCreateBlockStartingAt(dex_pc_start);
279 if (dex_pc_end < code_item.insns_size_in_code_units_) {
280 // TODO: Do not create block if the last instruction cannot fall through.
281 FindOrCreateBlockStartingAt(dex_pc_end);
282 } else {
283 // The TryItem spans until the very end of the CodeItem (or beyond if
284 // invalid) and therefore cannot have any code afterwards.
285 }
286 }
287
288 // Create branch targets for exception handlers.
289 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(code_item, 0);
290 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
291 for (uint32_t idx = 0; idx < handlers_size; ++idx) {
292 CatchHandlerIterator iterator(handlers_ptr);
293 for (; iterator.HasNext(); iterator.Next()) {
294 uint32_t address = iterator.GetHandlerAddress();
295 HBasicBlock* block = FindOrCreateBlockStartingAt(address);
David Brazdilec16f792015-08-19 15:04:01 +0100296 block->SetTryCatchInformation(
297 new (arena_) TryCatchInformation(iterator.GetHandlerTypeIndex(), *dex_file_));
David Brazdilfc6a86a2015-06-26 10:33:45 +0000298 }
299 handlers_ptr = iterator.EndDataPointer();
300 }
301}
302
David Brazdild7558da2015-09-22 13:04:14 +0100303// Returns the TryItem stored for `block` or nullptr if there is no info for it.
304static const DexFile::TryItem* GetTryItem(
305 HBasicBlock* block,
306 const ArenaSafeMap<uint32_t, const DexFile::TryItem*>& try_block_info) {
307 auto iterator = try_block_info.find(block->GetBlockId());
308 return (iterator == try_block_info.end()) ? nullptr : iterator->second;
309}
David Brazdil56e1acc2015-06-30 15:41:36 +0100310
David Brazdild7558da2015-09-22 13:04:14 +0100311void HGraphBuilder::LinkToCatchBlocks(HTryBoundary* try_boundary,
312 const DexFile::CodeItem& code_item,
313 const DexFile::TryItem* try_item) {
314 for (CatchHandlerIterator it(code_item, *try_item); it.HasNext(); it.Next()) {
David Brazdil56e1acc2015-06-30 15:41:36 +0100315 try_boundary->AddExceptionHandler(FindBlockStartingAt(it.GetHandlerAddress()));
316 }
317}
318
David Brazdilfc6a86a2015-06-26 10:33:45 +0000319void HGraphBuilder::InsertTryBoundaryBlocks(const DexFile::CodeItem& code_item) {
320 if (code_item.tries_size_ == 0) {
321 return;
322 }
323
David Brazdild7558da2015-09-22 13:04:14 +0100324 // Keep a map of all try blocks and their respective TryItems. We do not use
325 // the block's pointer but rather its id to ensure deterministic iteration.
326 ArenaSafeMap<uint32_t, const DexFile::TryItem*> try_block_info(
Vladimir Marko5233f932015-09-29 19:01:15 +0100327 std::less<uint32_t>(), arena_->Adapter(kArenaAllocGraphBuilder));
David Brazdilbff75032015-07-08 17:26:51 +0000328
David Brazdild7558da2015-09-22 13:04:14 +0100329 // Obtain TryItem information for blocks with throwing instructions, and split
330 // blocks which are both try & catch to simplify the graph.
331 // NOTE: We are appending new blocks inside the loop, so we need to use index
332 // because iterators can be invalidated. We remember the initial size to avoid
333 // iterating over the new blocks which cannot throw.
334 for (size_t i = 0, e = graph_->GetBlocks().size(); i < e; ++i) {
335 HBasicBlock* block = graph_->GetBlocks()[i];
David Brazdil72783ff2015-07-09 14:36:05 +0100336
David Brazdild7558da2015-09-22 13:04:14 +0100337 // Do not bother creating exceptional edges for try blocks which have no
338 // throwing instructions. In that case we simply assume that the block is
339 // not covered by a TryItem. This prevents us from creating a throw-catch
340 // loop for synchronized blocks.
341 if (block->HasThrowingInstructions()) {
342 // Try to find a TryItem covering the block.
343 DCHECK_NE(block->GetDexPc(), kNoDexPc) << "Block must have a dec_pc to find its TryItem.";
344 const int32_t try_item_idx = DexFile::FindTryItem(code_item, block->GetDexPc());
345 if (try_item_idx != -1) {
346 // Block throwing and in a TryItem. Store the try block information.
347 HBasicBlock* throwing_block = block;
348 if (block->IsCatchBlock()) {
349 // Simplify blocks which are both try and catch, otherwise we would
350 // need a strategy for splitting exceptional edges. We split the block
351 // after the move-exception (if present) and mark the first part not
352 // throwing. The normal-flow edge between them will be split later.
353 HInstruction* first_insn = block->GetFirstInstruction();
354 if (first_insn->IsLoadException()) {
355 // Catch block starts with a LoadException. Split the block after
356 // the StoreLocal and ClearException which must come after the load.
357 DCHECK(first_insn->GetNext()->IsStoreLocal());
358 DCHECK(first_insn->GetNext()->GetNext()->IsClearException());
359 throwing_block = block->SplitBefore(first_insn->GetNext()->GetNext()->GetNext());
360 } else {
361 // Catch block does not load the exception. Split at the beginning
362 // to create an empty catch block.
363 throwing_block = block->SplitBefore(first_insn);
364 }
David Brazdil72783ff2015-07-09 14:36:05 +0100365 }
David Brazdild7558da2015-09-22 13:04:14 +0100366
367 try_block_info.Put(throwing_block->GetBlockId(),
368 DexFile::GetTryItems(code_item, try_item_idx));
David Brazdil72783ff2015-07-09 14:36:05 +0100369 }
David Brazdil72783ff2015-07-09 14:36:05 +0100370 }
David Brazdilbff75032015-07-08 17:26:51 +0000371 }
372
David Brazdild7558da2015-09-22 13:04:14 +0100373 // Do a pass over the try blocks and insert entering TryBoundaries where at
374 // least one predecessor is not covered by the same TryItem as the try block.
375 // We do not split each edge separately, but rather create one boundary block
376 // that all predecessors are relinked to. This preserves loop headers (b/23895756).
377 for (auto entry : try_block_info) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100378 HBasicBlock* try_block = graph_->GetBlocks()[entry.first];
David Brazdild7558da2015-09-22 13:04:14 +0100379 for (HBasicBlock* predecessor : try_block->GetPredecessors()) {
380 if (GetTryItem(predecessor, try_block_info) != entry.second) {
381 // Found a predecessor not covered by the same TryItem. Insert entering
382 // boundary block.
383 HTryBoundary* try_entry =
384 new (arena_) HTryBoundary(HTryBoundary::kEntry, try_block->GetDexPc());
385 try_block->CreateImmediateDominator()->AddInstruction(try_entry);
386 LinkToCatchBlocks(try_entry, code_item, entry.second);
387 break;
388 }
David Brazdil281a6322015-07-03 10:34:57 +0100389 }
David Brazdild7558da2015-09-22 13:04:14 +0100390 }
David Brazdil281a6322015-07-03 10:34:57 +0100391
David Brazdild7558da2015-09-22 13:04:14 +0100392 // Do a second pass over the try blocks and insert exit TryBoundaries where
393 // the successor is not in the same TryItem.
394 for (auto entry : try_block_info) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100395 HBasicBlock* try_block = graph_->GetBlocks()[entry.first];
David Brazdild7558da2015-09-22 13:04:14 +0100396 // NOTE: Do not use iterators because SplitEdge would invalidate them.
397 for (size_t i = 0, e = try_block->GetSuccessors().size(); i < e; ++i) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100398 HBasicBlock* successor = try_block->GetSuccessors()[i];
David Brazdil72783ff2015-07-09 14:36:05 +0100399
David Brazdild7558da2015-09-22 13:04:14 +0100400 // If the successor is a try block, all of its predecessors must be
401 // covered by the same TryItem. Otherwise the previous pass would have
402 // created a non-throwing boundary block.
403 if (GetTryItem(successor, try_block_info) != nullptr) {
404 DCHECK_EQ(entry.second, GetTryItem(successor, try_block_info));
David Brazdil72783ff2015-07-09 14:36:05 +0100405 continue;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000406 }
David Brazdil281a6322015-07-03 10:34:57 +0100407
David Brazdild7558da2015-09-22 13:04:14 +0100408 // Preserve the invariant that Return(Void) always jumps to Exit by moving
409 // it outside the try block if necessary.
410 HInstruction* last_instruction = try_block->GetLastInstruction();
411 if (last_instruction->IsReturn() || last_instruction->IsReturnVoid()) {
412 DCHECK_EQ(successor, exit_block_);
413 successor = try_block->SplitBefore(last_instruction);
David Brazdil281a6322015-07-03 10:34:57 +0100414 }
David Brazdild7558da2015-09-22 13:04:14 +0100415
416 // Insert TryBoundary and link to catch blocks.
417 HTryBoundary* try_exit =
418 new (arena_) HTryBoundary(HTryBoundary::kExit, successor->GetDexPc());
419 graph_->SplitEdge(try_block, successor)->AddInstruction(try_exit);
420 LinkToCatchBlocks(try_exit, code_item, entry.second);
David Brazdil281a6322015-07-03 10:34:57 +0100421 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000422 }
423}
424
David Brazdil5e8b1372015-01-23 14:39:08 +0000425bool HGraphBuilder::BuildGraph(const DexFile::CodeItem& code_item) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100426 DCHECK(graph_->GetBlocks().empty());
David Brazdil5e8b1372015-01-23 14:39:08 +0000427
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000428 const uint16_t* code_ptr = code_item.insns_;
429 const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100430 code_start_ = code_ptr;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000431
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000432 // Setup the graph with the entry block and exit block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100433 entry_block_ = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000434 graph_->AddBlock(entry_block_);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100435 exit_block_ = new (arena_) HBasicBlock(graph_, kNoDexPc);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000436 graph_->SetEntryBlock(entry_block_);
437 graph_->SetExitBlock(exit_block_);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000438
David Brazdil77a48ae2015-09-15 12:34:04 +0000439 graph_->SetHasTryCatch(code_item.tries_size_ != 0);
440
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000441 InitializeLocals(code_item.registers_size_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000442 graph_->SetMaximumNumberOfOutVRegs(code_item.outs_size_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000443
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000444 // Compute the number of dex instructions, blocks, and branches. We will
445 // check these values against limits given to the compiler.
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000446 size_t number_of_branches = 0;
447
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000448 // To avoid splitting blocks, we compute ahead of time the instructions that
449 // start a new block, and create these blocks.
Calin Juravle702d2602015-04-30 19:28:21 +0100450 if (!ComputeBranchTargets(code_ptr, code_end, &number_of_branches)) {
451 MaybeRecordStat(MethodCompilationStat::kNotCompiledBranchOutsideMethodCode);
452 return false;
453 }
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000454
455 // Note that the compiler driver is null when unit testing.
David Brazdil1b498722015-03-31 11:37:18 +0100456 if ((compiler_driver_ != nullptr) && SkipCompilation(code_item, number_of_branches)) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000457 return false;
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000458 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000459
David Brazdilfc6a86a2015-06-26 10:33:45 +0000460 CreateBlocksForTryCatch(code_item);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000461
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000462 InitializeParameters(code_item.ins_size_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100463
Calin Juravle225ff812014-11-13 16:46:39 +0000464 size_t dex_pc = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000465 while (code_ptr < code_end) {
Calin Juravle225ff812014-11-13 16:46:39 +0000466 // Update the current block if dex_pc starts a new block.
467 MaybeUpdateCurrentBlock(dex_pc);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000468 const Instruction& instruction = *Instruction::At(code_ptr);
Calin Juravle48c2b032014-12-09 18:11:36 +0000469 if (!AnalyzeDexInstruction(instruction, dex_pc)) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000470 return false;
Calin Juravle48c2b032014-12-09 18:11:36 +0000471 }
Calin Juravle225ff812014-11-13 16:46:39 +0000472 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000473 code_ptr += instruction.SizeInCodeUnits();
474 }
475
David Brazdilfc6a86a2015-06-26 10:33:45 +0000476 // Add Exit to the exit block.
David Brazdil3e187382015-06-26 09:59:52 +0000477 exit_block_->AddInstruction(new (arena_) HExit());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000478 // Add the suspend check to the entry block.
479 entry_block_->AddInstruction(new (arena_) HSuspendCheck(0));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000480 entry_block_->AddInstruction(new (arena_) HGoto());
David Brazdilbff75032015-07-08 17:26:51 +0000481 // Add the exit block at the end.
482 graph_->AddBlock(exit_block_);
David Brazdil5e8b1372015-01-23 14:39:08 +0000483
David Brazdilfc6a86a2015-06-26 10:33:45 +0000484 // Iterate over blocks covered by TryItems and insert TryBoundaries at entry
485 // and exit points. This requires all control-flow instructions and
486 // non-exceptional edges to have been created.
487 InsertTryBoundaryBlocks(code_item);
488
David Brazdil5e8b1372015-01-23 14:39:08 +0000489 return true;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000490}
491
David Brazdilfc6a86a2015-06-26 10:33:45 +0000492void HGraphBuilder::MaybeUpdateCurrentBlock(size_t dex_pc) {
493 HBasicBlock* block = FindBlockStartingAt(dex_pc);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000494 if (block == nullptr) {
495 return;
496 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000497
498 if (current_block_ != nullptr) {
499 // Branching instructions clear current_block, so we know
500 // the last instruction of the current block is not a branching
501 // instruction. We add an unconditional goto to the found block.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600502 current_block_->AddInstruction(new (arena_) HGoto(dex_pc));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000503 current_block_->AddSuccessor(block);
504 }
505 graph_->AddBlock(block);
506 current_block_ = block;
507}
508
Calin Juravle702d2602015-04-30 19:28:21 +0100509bool HGraphBuilder::ComputeBranchTargets(const uint16_t* code_ptr,
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000510 const uint16_t* code_end,
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000511 size_t* number_of_branches) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100512 branch_targets_.resize(code_end - code_ptr, nullptr);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000513
514 // Create the first block for the dex instructions, single successor of the entry block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100515 HBasicBlock* block = new (arena_) HBasicBlock(graph_, 0);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100516 branch_targets_[0] = block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000517 entry_block_->AddSuccessor(block);
518
519 // Iterate over all instructions and find branching instructions. Create blocks for
520 // the locations these instructions branch to.
Andreas Gamped881df52014-11-24 23:28:39 -0800521 uint32_t dex_pc = 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000522 while (code_ptr < code_end) {
523 const Instruction& instruction = *Instruction::At(code_ptr);
524 if (instruction.IsBranch()) {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000525 (*number_of_branches)++;
Calin Juravle225ff812014-11-13 16:46:39 +0000526 int32_t target = instruction.GetTargetOffset() + dex_pc;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000527 // Create a block for the target instruction.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000528 FindOrCreateBlockStartingAt(target);
529
Calin Juravle225ff812014-11-13 16:46:39 +0000530 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000531 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle702d2602015-04-30 19:28:21 +0100532
David Brazdilfe659462015-06-24 14:23:56 +0100533 if (instruction.CanFlowThrough()) {
534 if (code_ptr >= code_end) {
Calin Juravle702d2602015-04-30 19:28:21 +0100535 // In the normal case we should never hit this but someone can artificially forge a dex
536 // file to fall-through out the method code. In this case we bail out compilation.
537 return false;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000538 } else {
539 FindOrCreateBlockStartingAt(dex_pc);
Calin Juravle702d2602015-04-30 19:28:21 +0100540 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000541 }
Andreas Gampee4d4d322014-12-04 09:09:57 -0800542 } else if (instruction.IsSwitch()) {
543 SwitchTable table(instruction, dex_pc, instruction.Opcode() == Instruction::SPARSE_SWITCH);
Andreas Gamped881df52014-11-24 23:28:39 -0800544
545 uint16_t num_entries = table.GetNumEntries();
546
Andreas Gampee4d4d322014-12-04 09:09:57 -0800547 // In a packed-switch, the entry at index 0 is the starting key. In a sparse-switch, the
548 // entry at index 0 is the first key, and values are after *all* keys.
549 size_t offset = table.GetFirstValueIndex();
550
551 // Use a larger loop counter type to avoid overflow issues.
552 for (size_t i = 0; i < num_entries; ++i) {
Andreas Gamped881df52014-11-24 23:28:39 -0800553 // The target of the case.
Andreas Gampee4d4d322014-12-04 09:09:57 -0800554 uint32_t target = dex_pc + table.GetEntryAt(i + offset);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000555 FindOrCreateBlockStartingAt(target);
Andreas Gamped881df52014-11-24 23:28:39 -0800556
David Brazdil281a6322015-07-03 10:34:57 +0100557 // Create a block for the switch-case logic. The block gets the dex_pc
558 // of the SWITCH instruction because it is part of its semantics.
559 block = new (arena_) HBasicBlock(graph_, dex_pc);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100560 branch_targets_[table.GetDexPcForIndex(i)] = block;
Andreas Gamped881df52014-11-24 23:28:39 -0800561 }
562
563 // Fall-through. Add a block if there is more code afterwards.
564 dex_pc += instruction.SizeInCodeUnits();
565 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle702d2602015-04-30 19:28:21 +0100566 if (code_ptr >= code_end) {
567 // In the normal case we should never hit this but someone can artificially forge a dex
568 // file to fall-through out the method code. In this case we bail out compilation.
569 // (A switch can fall-through so we don't need to check CanFlowThrough().)
570 return false;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000571 } else {
572 FindOrCreateBlockStartingAt(dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -0800573 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000574 } else {
575 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle225ff812014-11-13 16:46:39 +0000576 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000577 }
578 }
Calin Juravle702d2602015-04-30 19:28:21 +0100579 return true;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000580}
581
David Brazdilfc6a86a2015-06-26 10:33:45 +0000582HBasicBlock* HGraphBuilder::FindBlockStartingAt(int32_t dex_pc) const {
583 DCHECK_GE(dex_pc, 0);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100584 return branch_targets_[dex_pc];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000585}
586
587HBasicBlock* HGraphBuilder::FindOrCreateBlockStartingAt(int32_t dex_pc) {
588 HBasicBlock* block = FindBlockStartingAt(dex_pc);
589 if (block == nullptr) {
590 block = new (arena_) HBasicBlock(graph_, dex_pc);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100591 branch_targets_[dex_pc] = block;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000592 }
593 return block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000594}
595
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100596template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600597void HGraphBuilder::Unop_12x(const Instruction& instruction,
598 Primitive::Type type,
599 uint32_t dex_pc) {
600 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
601 current_block_->AddInstruction(new (arena_) T(type, first, dex_pc));
602 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Roland Levillain88cb1752014-10-20 16:36:47 +0100603}
604
Roland Levillaindff1f282014-11-05 14:15:05 +0000605void HGraphBuilder::Conversion_12x(const Instruction& instruction,
606 Primitive::Type input_type,
Roland Levillain624279f2014-12-04 11:54:28 +0000607 Primitive::Type result_type,
608 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600609 HInstruction* first = LoadLocal(instruction.VRegB(), input_type, dex_pc);
Roland Levillain624279f2014-12-04 11:54:28 +0000610 current_block_->AddInstruction(new (arena_) HTypeConversion(result_type, first, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600611 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100612}
613
614template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000615void HGraphBuilder::Binop_23x(const Instruction& instruction,
616 Primitive::Type type,
617 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600618 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
619 HInstruction* second = LoadLocal(instruction.VRegC(), type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000620 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600621 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000622}
623
624template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000625void HGraphBuilder::Binop_23x_shift(const Instruction& instruction,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600626 Primitive::Type type,
627 uint32_t dex_pc) {
628 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
629 HInstruction* second = LoadLocal(instruction.VRegC(), Primitive::kPrimInt, dex_pc);
630 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
631 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000632}
633
Calin Juravleddb7df22014-11-25 20:56:51 +0000634void HGraphBuilder::Binop_23x_cmp(const Instruction& instruction,
635 Primitive::Type type,
Mark Mendellc4701932015-04-10 13:18:51 -0400636 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700637 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600638 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
639 HInstruction* second = LoadLocal(instruction.VRegC(), type, dex_pc);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700640 current_block_->AddInstruction(new (arena_) HCompare(type, first, second, bias, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600641 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +0000642}
643
Calin Juravle9aec02f2014-11-18 23:06:35 +0000644template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600645void HGraphBuilder::Binop_12x_shift(const Instruction& instruction, Primitive::Type type,
646 uint32_t dex_pc) {
647 HInstruction* first = LoadLocal(instruction.VRegA(), type, dex_pc);
648 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
649 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
650 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000651}
652
653template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000654void HGraphBuilder::Binop_12x(const Instruction& instruction,
655 Primitive::Type type,
656 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600657 HInstruction* first = LoadLocal(instruction.VRegA(), type, dex_pc);
658 HInstruction* second = LoadLocal(instruction.VRegB(), type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000659 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600660 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000661}
662
663template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600664void HGraphBuilder::Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
665 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
666 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100667 if (reverse) {
668 std::swap(first, second);
669 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600670 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second, dex_pc));
671 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100672}
673
674template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600675void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
676 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
677 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100678 if (reverse) {
679 std::swap(first, second);
680 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600681 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second, dex_pc));
682 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100683}
684
Calin Juravle0c25d102015-04-20 14:49:09 +0100685static bool RequiresConstructorBarrier(const DexCompilationUnit* cu, const CompilerDriver& driver) {
Calin Juravle27df7582015-04-17 19:12:31 +0100686 Thread* self = Thread::Current();
Calin Juravle0c25d102015-04-20 14:49:09 +0100687 return cu->IsConstructor()
688 && driver.RequiresConstructorBarrier(self, cu->GetDexFile(), cu->GetClassDefIndex());
Calin Juravle27df7582015-04-17 19:12:31 +0100689}
690
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600691void HGraphBuilder::BuildReturn(const Instruction& instruction,
692 Primitive::Type type,
693 uint32_t dex_pc) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100694 if (type == Primitive::kPrimVoid) {
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100695 if (graph_->ShouldGenerateConstructorBarrier()) {
696 // The compilation unit is null during testing.
697 if (dex_compilation_unit_ != nullptr) {
698 DCHECK(RequiresConstructorBarrier(dex_compilation_unit_, *compiler_driver_))
699 << "Inconsistent use of ShouldGenerateConstructorBarrier. Should not generate a barrier.";
700 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600701 current_block_->AddInstruction(new (arena_) HMemoryBarrier(kStoreStore, dex_pc));
Calin Juravle27df7582015-04-17 19:12:31 +0100702 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600703 current_block_->AddInstruction(new (arena_) HReturnVoid(dex_pc));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100704 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600705 HInstruction* value = LoadLocal(instruction.VRegA(), type, dex_pc);
706 current_block_->AddInstruction(new (arena_) HReturn(value, dex_pc));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100707 }
708 current_block_->AddSuccessor(exit_block_);
709 current_block_ = nullptr;
710}
711
Calin Juravle68ad6492015-08-18 17:08:12 +0100712static InvokeType GetInvokeTypeFromOpCode(Instruction::Code opcode) {
713 switch (opcode) {
714 case Instruction::INVOKE_STATIC:
715 case Instruction::INVOKE_STATIC_RANGE:
716 return kStatic;
717 case Instruction::INVOKE_DIRECT:
718 case Instruction::INVOKE_DIRECT_RANGE:
719 return kDirect;
720 case Instruction::INVOKE_VIRTUAL:
721 case Instruction::INVOKE_VIRTUAL_QUICK:
722 case Instruction::INVOKE_VIRTUAL_RANGE:
723 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
724 return kVirtual;
725 case Instruction::INVOKE_INTERFACE:
726 case Instruction::INVOKE_INTERFACE_RANGE:
727 return kInterface;
728 case Instruction::INVOKE_SUPER_RANGE:
729 case Instruction::INVOKE_SUPER:
730 return kSuper;
731 default:
732 LOG(FATAL) << "Unexpected invoke opcode: " << opcode;
733 UNREACHABLE();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100734 }
Calin Juravle68ad6492015-08-18 17:08:12 +0100735}
736
737bool HGraphBuilder::BuildInvoke(const Instruction& instruction,
738 uint32_t dex_pc,
739 uint32_t method_idx,
740 uint32_t number_of_vreg_arguments,
741 bool is_range,
742 uint32_t* args,
743 uint32_t register_index) {
744 InvokeType original_invoke_type = GetInvokeTypeFromOpCode(instruction.Opcode());
745 InvokeType optimized_invoke_type = original_invoke_type;
746 const char* descriptor = dex_file_->GetMethodShorty(method_idx);
747 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
748
749 // Remove the return type from the 'proto'.
750 size_t number_of_arguments = strlen(descriptor) - 1;
751 if (original_invoke_type != kStatic) { // instance call
752 // One extra argument for 'this'.
753 number_of_arguments++;
754 }
755
756 MethodReference target_method(dex_file_, method_idx);
Calin Juravle5d01db12015-08-25 15:02:42 +0100757 int32_t table_index = 0;
758 uintptr_t direct_code = 0;
759 uintptr_t direct_method = 0;
Calin Juravle68ad6492015-08-18 17:08:12 +0100760
Calin Juravle5d01db12015-08-25 15:02:42 +0100761 // Special handling for string init.
762 int32_t string_init_offset = 0;
763 bool is_string_init = compiler_driver_->IsStringInit(method_idx,
764 dex_file_,
765 &string_init_offset);
766 // Replace calls to String.<init> with StringFactory.
767 if (is_string_init) {
768 HInvokeStaticOrDirect::DispatchInfo dispatch_info = ComputeDispatchInfo(is_string_init,
769 string_init_offset,
770 target_method,
771 direct_method,
772 direct_code);
773 HInvoke* invoke = new (arena_) HInvokeStaticOrDirect(
774 arena_,
775 number_of_arguments - 1,
776 Primitive::kPrimNot /*return_type */,
777 dex_pc,
778 method_idx,
779 target_method,
780 dispatch_info,
781 original_invoke_type,
782 kStatic /* optimized_invoke_type */,
783 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit);
784 return HandleStringInit(invoke,
785 number_of_vreg_arguments,
786 args,
787 register_index,
788 is_range,
789 descriptor);
790 }
791
792 // Handle unresolved methods.
Calin Juravle68ad6492015-08-18 17:08:12 +0100793 if (!compiler_driver_->ComputeInvokeInfo(dex_compilation_unit_,
794 dex_pc,
795 true /* update_stats */,
796 true /* enable_devirtualization */,
797 &optimized_invoke_type,
798 &target_method,
799 &table_index,
800 &direct_code,
801 &direct_method)) {
Calin Juravle175dc732015-08-25 15:42:32 +0100802 MaybeRecordStat(MethodCompilationStat::kUnresolvedMethod);
803 HInvoke* invoke = new (arena_) HInvokeUnresolved(arena_,
804 number_of_arguments,
805 return_type,
806 dex_pc,
807 method_idx,
808 original_invoke_type);
809 return HandleInvoke(invoke,
810 number_of_vreg_arguments,
811 args,
812 register_index,
813 is_range,
814 descriptor,
815 nullptr /* clinit_check */);
Calin Juravle68ad6492015-08-18 17:08:12 +0100816 }
817
Calin Juravle5d01db12015-08-25 15:02:42 +0100818 // Handle resolved methods (non string init).
Calin Juravle68ad6492015-08-18 17:08:12 +0100819
Calin Juravle5d01db12015-08-25 15:02:42 +0100820 DCHECK(optimized_invoke_type != kSuper);
Calin Juravle68ad6492015-08-18 17:08:12 +0100821
822 // Potential class initialization check, in the case of a static method call.
823 HClinitCheck* clinit_check = nullptr;
824 HInvoke* invoke = nullptr;
825
Calin Juravle5d01db12015-08-25 15:02:42 +0100826 if (optimized_invoke_type == kDirect || optimized_invoke_type == kStatic) {
Calin Juravle68ad6492015-08-18 17:08:12 +0100827 // By default, consider that the called method implicitly requires
828 // an initialization check of its declaring method.
829 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement
830 = HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
Calin Juravle5d01db12015-08-25 15:02:42 +0100831 if (optimized_invoke_type == kStatic) {
Calin Juravle68ad6492015-08-18 17:08:12 +0100832 clinit_check = ProcessClinitCheckForInvoke(dex_pc, method_idx, &clinit_check_requirement);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100833 }
Calin Juravle68ad6492015-08-18 17:08:12 +0100834
Calin Juravle68ad6492015-08-18 17:08:12 +0100835 HInvokeStaticOrDirect::DispatchInfo dispatch_info = ComputeDispatchInfo(is_string_init,
836 string_init_offset,
837 target_method,
838 direct_method,
839 direct_code);
840 invoke = new (arena_) HInvokeStaticOrDirect(arena_,
841 number_of_arguments,
842 return_type,
843 dex_pc,
844 method_idx,
845 target_method,
846 dispatch_info,
847 original_invoke_type,
848 optimized_invoke_type,
849 clinit_check_requirement);
850 } else if (optimized_invoke_type == kVirtual) {
851 invoke = new (arena_) HInvokeVirtual(arena_,
852 number_of_arguments,
853 return_type,
854 dex_pc,
855 method_idx,
856 table_index);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100857 } else {
Calin Juravle68ad6492015-08-18 17:08:12 +0100858 DCHECK_EQ(optimized_invoke_type, kInterface);
859 invoke = new (arena_) HInvokeInterface(arena_,
860 number_of_arguments,
861 return_type,
862 dex_pc,
863 method_idx,
864 table_index);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100865 }
Calin Juravle68ad6492015-08-18 17:08:12 +0100866
Calin Juravle5d01db12015-08-25 15:02:42 +0100867 return HandleInvoke(invoke,
868 number_of_vreg_arguments,
869 args,
870 register_index,
871 is_range,
872 descriptor,
873 clinit_check);
Calin Juravle68ad6492015-08-18 17:08:12 +0100874}
875
876HClinitCheck* HGraphBuilder::ProcessClinitCheckForInvoke(
877 uint32_t dex_pc,
878 uint32_t method_idx,
879 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
880 ScopedObjectAccess soa(Thread::Current());
881 StackHandleScope<4> hs(soa.Self());
882 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
883 dex_compilation_unit_->GetClassLinker()->FindDexCache(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700884 soa.Self(), *dex_compilation_unit_->GetDexFile())));
Calin Juravle68ad6492015-08-18 17:08:12 +0100885 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
886 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
887 ArtMethod* resolved_method = compiler_driver_->ResolveMethod(
888 soa, dex_cache, class_loader, dex_compilation_unit_, method_idx, InvokeType::kStatic);
889
890 DCHECK(resolved_method != nullptr);
891
892 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
893 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700894 outer_compilation_unit_->GetClassLinker()->FindDexCache(soa.Self(), outer_dex_file)));
Calin Juravle68ad6492015-08-18 17:08:12 +0100895 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
896
897 // The index at which the method's class is stored in the DexCache's type array.
898 uint32_t storage_index = DexFile::kDexNoIndex;
899 bool is_outer_class = (resolved_method->GetDeclaringClass() == outer_class.Get());
900 if (is_outer_class) {
901 storage_index = outer_class->GetDexTypeIndex();
902 } else if (outer_dex_cache.Get() == dex_cache.Get()) {
903 // Get `storage_index` from IsClassOfStaticMethodAvailableToReferrer.
904 compiler_driver_->IsClassOfStaticMethodAvailableToReferrer(outer_dex_cache.Get(),
905 GetCompilingClass(),
906 resolved_method,
907 method_idx,
908 &storage_index);
909 }
910
911 HClinitCheck* clinit_check = nullptr;
912
913 if (!outer_class->IsInterface()
914 && outer_class->IsSubClass(resolved_method->GetDeclaringClass())) {
915 // If the outer class is the declaring class or a subclass
916 // of the declaring class, no class initialization is needed
917 // before the static method call.
918 // Note that in case of inlining, we do not need to add clinit checks
919 // to calls that satisfy this subclass check with any inlined methods. This
920 // will be detected by the optimization passes.
921 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
922 } else if (storage_index != DexFile::kDexNoIndex) {
923 // If the method's class type index is available, check
924 // whether we should add an explicit class initialization
925 // check for its declaring class before the static method call.
926
927 // TODO: find out why this check is needed.
928 bool is_in_dex_cache = compiler_driver_->CanAssumeTypeIsPresentInDexCache(
929 *outer_compilation_unit_->GetDexFile(), storage_index);
930 bool is_initialized =
931 resolved_method->GetDeclaringClass()->IsInitialized() && is_in_dex_cache;
932
933 if (is_initialized) {
934 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
935 } else {
936 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
937 HLoadClass* load_class = new (arena_) HLoadClass(
938 graph_->GetCurrentMethod(),
939 storage_index,
940 *dex_compilation_unit_->GetDexFile(),
941 is_outer_class,
Calin Juravle98893e12015-10-02 21:05:03 +0100942 dex_pc,
943 /*needs_access_check*/ false);
Calin Juravle68ad6492015-08-18 17:08:12 +0100944 current_block_->AddInstruction(load_class);
945 clinit_check = new (arena_) HClinitCheck(load_class, dex_pc);
946 current_block_->AddInstruction(clinit_check);
947 }
948 }
949 return clinit_check;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100950}
951
Vladimir Marko58155012015-08-19 12:49:41 +0000952HInvokeStaticOrDirect::DispatchInfo HGraphBuilder::ComputeDispatchInfo(
953 bool is_string_init,
954 int32_t string_init_offset,
955 MethodReference target_method,
956 uintptr_t direct_method,
957 uintptr_t direct_code) {
958 HInvokeStaticOrDirect::MethodLoadKind method_load_kind;
959 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location;
960 uint64_t method_load_data = 0u;
961 uint64_t direct_code_ptr = 0u;
962
963 if (is_string_init) {
964 // TODO: Use direct_method and direct_code for the appropriate StringFactory method.
965 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kStringInit;
966 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
967 method_load_data = string_init_offset;
968 } else if (target_method.dex_file == outer_compilation_unit_->GetDexFile() &&
969 target_method.dex_method_index == outer_compilation_unit_->GetDexMethodIndex()) {
970 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kRecursive;
971 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallSelf;
972 } else {
973 if (direct_method != 0u) { // Should we use a direct pointer to the method?
974 if (direct_method != static_cast<uintptr_t>(-1)) { // Is the method pointer known now?
975 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress;
976 method_load_data = direct_method;
977 } else { // The direct pointer will be known at link time.
978 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup;
979 }
980 } else { // Use dex cache.
981 DCHECK(target_method.dex_file == dex_compilation_unit_->GetDexFile());
982 DexCacheArraysLayout layout =
983 compiler_driver_->GetDexCacheArraysLayout(target_method.dex_file);
984 if (layout.Valid()) { // Can we use PC-relative access to the dex cache arrays?
985 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative;
986 method_load_data = layout.MethodOffset(target_method.dex_method_index);
987 } else { // We must go through the ArtMethod's pointer to resolved methods.
988 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
989 }
990 }
991 if (direct_code != 0u) { // Should we use a direct pointer to the code?
992 if (direct_code != static_cast<uintptr_t>(-1)) { // Is the code pointer known now?
993 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallDirect;
994 direct_code_ptr = direct_code;
995 } else if (compiler_driver_->IsImage() ||
996 target_method.dex_file == dex_compilation_unit_->GetDexFile()) {
997 // Use PC-relative calls for invokes within a multi-dex oat file.
998 // TODO: Recognize when the target dex file is within the current oat file for
999 // app compilation. At the moment we recognize only the boot image as multi-dex.
1000 // NOTE: This will require changing the ARM backend which currently falls
1001 // through from kCallPCRelative to kDirectCodeFixup for different dex files.
1002 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative;
1003 } else { // The direct pointer will be known at link time.
1004 // NOTE: This is used for app->boot calls when compiling an app against
1005 // a relocatable but not yet relocated image.
1006 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup;
1007 }
1008 } else { // We must use the code pointer from the ArtMethod.
1009 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
1010 }
1011 }
1012
1013 if (graph_->IsDebuggable()) {
1014 // For debuggable apps always use the code pointer from ArtMethod
1015 // so that we don't circumvent instrumentation stubs if installed.
1016 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
1017 }
1018
1019 return HInvokeStaticOrDirect::DispatchInfo {
1020 method_load_kind, code_ptr_location, method_load_data, direct_code_ptr };
1021}
1022
Calin Juravle5d01db12015-08-25 15:02:42 +01001023bool HGraphBuilder::SetupInvokeArguments(HInvoke* invoke,
1024 uint32_t number_of_vreg_arguments,
1025 uint32_t* args,
1026 uint32_t register_index,
1027 bool is_range,
1028 const char* descriptor,
1029 size_t start_index,
1030 size_t* argument_index) {
Calin Juravle68ad6492015-08-18 17:08:12 +01001031 uint32_t descriptor_index = 1; // Skip the return type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001032 uint32_t dex_pc = invoke->GetDexPc();
Calin Juravle68ad6492015-08-18 17:08:12 +01001033
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001034 for (size_t i = start_index;
1035 // Make sure we don't go over the expected arguments or over the number of
1036 // dex registers given. If the instruction was seen as dead by the verifier,
1037 // it hasn't been properly checked.
Calin Juravle5d01db12015-08-25 15:02:42 +01001038 (i < number_of_vreg_arguments) && (*argument_index < invoke->GetNumberOfArguments());
1039 i++, (*argument_index)++) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001040 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001041 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001042 if (!is_range
1043 && is_wide
1044 && ((i + 1 == number_of_vreg_arguments) || (args[i] + 1 != args[i + 1]))) {
1045 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
1046 // reject any class where this is violated. However, the verifier only does these checks
1047 // on non trivially dead instructions, so we just bailout the compilation.
1048 VLOG(compiler) << "Did not compile "
1049 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
1050 << " because of non-sequential dex register pair in wide argument";
1051 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1052 return false;
1053 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001054 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type, dex_pc);
Calin Juravle5d01db12015-08-25 15:02:42 +01001055 invoke->SetArgumentAt(*argument_index, arg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001056 if (is_wide) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001057 i++;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001058 }
1059 }
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001060
Calin Juravle5d01db12015-08-25 15:02:42 +01001061 if (*argument_index != invoke->GetNumberOfArguments()) {
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001062 VLOG(compiler) << "Did not compile "
1063 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
1064 << " because of wrong number of arguments in invoke instruction";
1065 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1066 return false;
1067 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001068
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001069 if (invoke->IsInvokeStaticOrDirect()) {
Calin Juravle5d01db12015-08-25 15:02:42 +01001070 invoke->SetArgumentAt(*argument_index, graph_->GetCurrentMethod());
1071 (*argument_index)++;
1072 }
1073
1074 return true;
1075}
1076
1077bool HGraphBuilder::HandleInvoke(HInvoke* invoke,
1078 uint32_t number_of_vreg_arguments,
1079 uint32_t* args,
1080 uint32_t register_index,
1081 bool is_range,
1082 const char* descriptor,
1083 HClinitCheck* clinit_check) {
1084 DCHECK(!invoke->IsInvokeStaticOrDirect() || !invoke->AsInvokeStaticOrDirect()->IsStringInit());
1085
1086 size_t start_index = 0;
1087 size_t argument_index = 0;
1088 if (invoke->GetOriginalInvokeType() != InvokeType::kStatic) { // Instance call.
1089 Temporaries temps(graph_);
1090 HInstruction* arg = LoadLocal(
1091 is_range ? register_index : args[0], Primitive::kPrimNot, invoke->GetDexPc());
1092 HNullCheck* null_check = new (arena_) HNullCheck(arg, invoke->GetDexPc());
1093 current_block_->AddInstruction(null_check);
1094 temps.Add(null_check);
1095 invoke->SetArgumentAt(0, null_check);
1096 start_index = 1;
1097 argument_index = 1;
1098 }
1099
1100 if (!SetupInvokeArguments(invoke,
1101 number_of_vreg_arguments,
1102 args,
1103 register_index,
1104 is_range,
1105 descriptor,
1106 start_index,
1107 &argument_index)) {
1108 return false;
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001109 }
1110
Calin Juravle68ad6492015-08-18 17:08:12 +01001111 if (clinit_check != nullptr) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001112 // Add the class initialization check as last input of `invoke`.
Calin Juravle68ad6492015-08-18 17:08:12 +01001113 DCHECK(invoke->IsInvokeStaticOrDirect());
1114 DCHECK(invoke->AsInvokeStaticOrDirect()->GetClinitCheckRequirement()
1115 == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit);
Roland Levillain3e3d7332015-04-28 11:00:54 +01001116 invoke->SetArgumentAt(argument_index, clinit_check);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001117 argument_index++;
Roland Levillain4c0eb422015-04-24 16:43:49 +01001118 }
1119
Calin Juravle5d01db12015-08-25 15:02:42 +01001120 current_block_->AddInstruction(invoke);
1121 latest_result_ = invoke;
1122
1123 return true;
1124}
1125
1126bool HGraphBuilder::HandleStringInit(HInvoke* invoke,
1127 uint32_t number_of_vreg_arguments,
1128 uint32_t* args,
1129 uint32_t register_index,
1130 bool is_range,
1131 const char* descriptor) {
1132 DCHECK(invoke->IsInvokeStaticOrDirect());
1133 DCHECK(invoke->AsInvokeStaticOrDirect()->IsStringInit());
1134
1135 size_t start_index = 1;
1136 size_t argument_index = 0;
1137 if (!SetupInvokeArguments(invoke,
1138 number_of_vreg_arguments,
1139 args,
1140 register_index,
1141 is_range,
1142 descriptor,
1143 start_index,
1144 &argument_index)) {
1145 return false;
Jeff Hao848f70a2014-01-15 13:49:50 -08001146 }
Calin Juravle0eedd7e2015-08-20 14:48:00 +01001147
Calin Juravle5d01db12015-08-25 15:02:42 +01001148 // Add move-result for StringFactory method.
1149 uint32_t orig_this_reg = is_range ? register_index : args[0];
1150 HInstruction* fake_string = LoadLocal(orig_this_reg, Primitive::kPrimNot, invoke->GetDexPc());
1151 invoke->SetArgumentAt(argument_index, fake_string);
1152 current_block_->AddInstruction(invoke);
1153 PotentiallySimplifyFakeString(orig_this_reg, invoke->GetDexPc(), invoke);
1154
Calin Juravle0eedd7e2015-08-20 14:48:00 +01001155 latest_result_ = invoke;
1156
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001157 return true;
1158}
1159
Calin Juravle68ad6492015-08-18 17:08:12 +01001160void HGraphBuilder::PotentiallySimplifyFakeString(uint16_t original_dex_register,
1161 uint32_t dex_pc,
1162 HInvoke* actual_string) {
1163 if (!graph_->IsDebuggable()) {
1164 // Notify that we cannot compile with baseline. The dex registers aliasing
1165 // with `original_dex_register` will be handled when we optimize
1166 // (see HInstructionSimplifer::VisitFakeString).
1167 can_use_baseline_for_string_init_ = false;
1168 return;
1169 }
1170 const VerifiedMethod* verified_method =
1171 compiler_driver_->GetVerifiedMethod(dex_file_, dex_compilation_unit_->GetDexMethodIndex());
1172 if (verified_method != nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001173 UpdateLocal(original_dex_register, actual_string, dex_pc);
Calin Juravle68ad6492015-08-18 17:08:12 +01001174 const SafeMap<uint32_t, std::set<uint32_t>>& string_init_map =
1175 verified_method->GetStringInitPcRegMap();
1176 auto map_it = string_init_map.find(dex_pc);
1177 if (map_it != string_init_map.end()) {
Vladimir Markodbc23372015-10-12 12:45:52 +01001178 for (uint32_t reg : map_it->second) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001179 HInstruction* load_local = LoadLocal(original_dex_register, Primitive::kPrimNot, dex_pc);
Vladimir Markodbc23372015-10-12 12:45:52 +01001180 UpdateLocal(reg, load_local, dex_pc);
Calin Juravle68ad6492015-08-18 17:08:12 +01001181 }
1182 }
1183 } else {
1184 can_use_baseline_for_string_init_ = false;
1185 }
1186}
1187
Calin Juravlee460d1d2015-09-29 04:52:17 +01001188static Primitive::Type GetFieldAccessType(const DexFile& dex_file, uint16_t field_index) {
1189 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
1190 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
1191 return Primitive::GetType(type[0]);
1192}
1193
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001194bool HGraphBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001195 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001196 bool is_put) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001197 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1198 uint32_t obj_reg = instruction.VRegB_22c();
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001199 uint16_t field_index;
1200 if (instruction.IsQuickened()) {
1201 if (!CanDecodeQuickenedInfo()) {
1202 return false;
1203 }
1204 field_index = LookupQuickenedInfo(dex_pc);
1205 } else {
1206 field_index = instruction.VRegC_22c();
1207 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001208
1209 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierc7853442015-03-27 14:35:38 -07001210 ArtField* resolved_field =
1211 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001212
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001213
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001214 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot, dex_pc);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001215 HInstruction* null_check = new (arena_) HNullCheck(object, dex_pc);
1216 current_block_->AddInstruction(null_check);
1217
1218 Primitive::Type field_type = (resolved_field == nullptr)
1219 ? GetFieldAccessType(*dex_file_, field_index)
1220 : resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001221 if (is_put) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001222 Temporaries temps(graph_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001223 // We need one temporary for the null check.
1224 temps.Add(null_check);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001225 HInstruction* value = LoadLocal(source_or_dest_reg, field_type, dex_pc);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001226 HInstruction* field_set = nullptr;
1227 if (resolved_field == nullptr) {
1228 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
1229 field_set = new (arena_) HUnresolvedInstanceFieldSet(null_check,
1230 value,
1231 field_type,
1232 field_index,
1233 dex_pc);
1234 } else {
1235 field_set = new (arena_) HInstanceFieldSet(null_check,
1236 value,
1237 field_type,
1238 resolved_field->GetOffset(),
1239 resolved_field->IsVolatile(),
1240 field_index,
1241 *dex_file_,
1242 dex_compilation_unit_->GetDexCache(),
1243 dex_pc);
1244 }
1245 current_block_->AddInstruction(field_set);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001246 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001247 HInstruction* field_get = nullptr;
1248 if (resolved_field == nullptr) {
1249 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
1250 field_get = new (arena_) HUnresolvedInstanceFieldGet(null_check,
1251 field_type,
1252 field_index,
1253 dex_pc);
1254 } else {
1255 field_get = new (arena_) HInstanceFieldGet(null_check,
1256 field_type,
1257 resolved_field->GetOffset(),
1258 resolved_field->IsVolatile(),
1259 field_index,
1260 *dex_file_,
1261 dex_compilation_unit_->GetDexCache(),
1262 dex_pc);
1263 }
1264 current_block_->AddInstruction(field_get);
1265 UpdateLocal(source_or_dest_reg, field_get, dex_pc);
Calin Juravlee6f49b42015-09-17 14:04:33 +00001266 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001267
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001268 return true;
1269}
1270
Nicolas Geoffray30451742015-06-19 13:32:41 +01001271static mirror::Class* GetClassFrom(CompilerDriver* driver,
1272 const DexCompilationUnit& compilation_unit) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001273 ScopedObjectAccess soa(Thread::Current());
1274 StackHandleScope<2> hs(soa.Self());
Nicolas Geoffray30451742015-06-19 13:32:41 +01001275 const DexFile& dex_file = *compilation_unit.GetDexFile();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001276 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Nicolas Geoffray30451742015-06-19 13:32:41 +01001277 soa.Decode<mirror::ClassLoader*>(compilation_unit.GetClassLoader())));
1278 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001279 compilation_unit.GetClassLinker()->FindDexCache(soa.Self(), dex_file)));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001280
Nicolas Geoffray30451742015-06-19 13:32:41 +01001281 return driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, &compilation_unit);
1282}
1283
1284mirror::Class* HGraphBuilder::GetOutermostCompilingClass() const {
1285 return GetClassFrom(compiler_driver_, *outer_compilation_unit_);
1286}
1287
1288mirror::Class* HGraphBuilder::GetCompilingClass() const {
1289 return GetClassFrom(compiler_driver_, *dex_compilation_unit_);
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001290}
1291
1292bool HGraphBuilder::IsOutermostCompilingClass(uint16_t type_index) const {
1293 ScopedObjectAccess soa(Thread::Current());
1294 StackHandleScope<4> hs(soa.Self());
1295 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001296 dex_compilation_unit_->GetClassLinker()->FindDexCache(
1297 soa.Self(), *dex_compilation_unit_->GetDexFile())));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001298 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1299 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
1300 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
1301 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
Nicolas Geoffrayafd06412015-06-20 22:44:47 +01001302 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001303
Calin Juravle4e2a5572015-10-07 18:55:43 +01001304 // GetOutermostCompilingClass returns null when the class is unresolved
1305 // (e.g. if it derives from an unresolved class). This is bogus knowing that
1306 // we are compiling it.
1307 // When this happens we cannot establish a direct relation between the current
1308 // class and the outer class, so we return false.
1309 // (Note that this is only used for optimizing invokes and field accesses)
1310 return (cls.Get() != nullptr) && (outer_class.Get() == cls.Get());
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001311}
1312
Calin Juravle07380a22015-09-17 14:15:12 +01001313void HGraphBuilder::BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
1314 uint32_t dex_pc,
1315 bool is_put,
1316 Primitive::Type field_type) {
1317 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1318 uint16_t field_index = instruction.VRegB_21c();
1319
1320 if (is_put) {
1321 HInstruction* value = LoadLocal(source_or_dest_reg, field_type, dex_pc);
1322 current_block_->AddInstruction(
1323 new (arena_) HUnresolvedStaticFieldSet(value, field_type, field_index, dex_pc));
1324 } else {
1325 current_block_->AddInstruction(
1326 new (arena_) HUnresolvedStaticFieldGet(field_type, field_index, dex_pc));
1327 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction(), dex_pc);
1328 }
1329}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001330bool HGraphBuilder::BuildStaticFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001331 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001332 bool is_put) {
1333 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1334 uint16_t field_index = instruction.VRegB_21c();
1335
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001336 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierc7853442015-03-27 14:35:38 -07001337 StackHandleScope<4> hs(soa.Self());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001338 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001339 dex_compilation_unit_->GetClassLinker()->FindDexCache(
1340 soa.Self(), *dex_compilation_unit_->GetDexFile())));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001341 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1342 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
Mathieu Chartierc7853442015-03-27 14:35:38 -07001343 ArtField* resolved_field = compiler_driver_->ResolveField(
1344 soa, dex_cache, class_loader, dex_compilation_unit_, field_index, true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001345
Mathieu Chartierc7853442015-03-27 14:35:38 -07001346 if (resolved_field == nullptr) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001347 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
1348 Primitive::Type field_type = GetFieldAccessType(*dex_file_, field_index);
Calin Juravle07380a22015-09-17 14:15:12 +01001349 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001350 return true;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001351 }
1352
Calin Juravle07380a22015-09-17 14:15:12 +01001353 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001354 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
1355 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001356 outer_compilation_unit_->GetClassLinker()->FindDexCache(soa.Self(), outer_dex_file)));
Nicolas Geoffray30451742015-06-19 13:32:41 +01001357 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001358
1359 // The index at which the field's class is stored in the DexCache's type array.
1360 uint32_t storage_index;
Nicolas Geoffray30451742015-06-19 13:32:41 +01001361 bool is_outer_class = (outer_class.Get() == resolved_field->GetDeclaringClass());
1362 if (is_outer_class) {
1363 storage_index = outer_class->GetDexTypeIndex();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001364 } else if (outer_dex_cache.Get() != dex_cache.Get()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001365 // The compiler driver cannot currently understand multiple dex caches involved. Just bailout.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001366 return false;
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001367 } else {
Calin Juravle07380a22015-09-17 14:15:12 +01001368 // TODO: This is rather expensive. Perf it and cache the results if needed.
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001369 std::pair<bool, bool> pair = compiler_driver_->IsFastStaticField(
1370 outer_dex_cache.Get(),
Nicolas Geoffray30451742015-06-19 13:32:41 +01001371 GetCompilingClass(),
Mathieu Chartierc7853442015-03-27 14:35:38 -07001372 resolved_field,
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001373 field_index,
1374 &storage_index);
1375 bool can_easily_access = is_put ? pair.second : pair.first;
1376 if (!can_easily_access) {
Calin Juravle07380a22015-09-17 14:15:12 +01001377 MaybeRecordStat(MethodCompilationStat::kUnresolvedFieldNotAFastAccess);
1378 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
1379 return true;
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001380 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001381 }
1382
1383 // TODO: find out why this check is needed.
1384 bool is_in_dex_cache = compiler_driver_->CanAssumeTypeIsPresentInDexCache(
Nicolas Geoffray6a816cf2015-03-24 16:17:56 +00001385 *outer_compilation_unit_->GetDexFile(), storage_index);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001386 bool is_initialized = resolved_field->GetDeclaringClass()->IsInitialized() && is_in_dex_cache;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001387
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001388 HLoadClass* constant = new (arena_) HLoadClass(graph_->GetCurrentMethod(),
1389 storage_index,
1390 *dex_compilation_unit_->GetDexFile(),
Nicolas Geoffray30451742015-06-19 13:32:41 +01001391 is_outer_class,
Calin Juravle98893e12015-10-02 21:05:03 +01001392 dex_pc,
1393 /*needs_access_check*/ false);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001394 current_block_->AddInstruction(constant);
1395
1396 HInstruction* cls = constant;
Nicolas Geoffray30451742015-06-19 13:32:41 +01001397 if (!is_initialized && !is_outer_class) {
Calin Juravle225ff812014-11-13 16:46:39 +00001398 cls = new (arena_) HClinitCheck(constant, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001399 current_block_->AddInstruction(cls);
1400 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001401 if (is_put) {
1402 // We need to keep the class alive before loading the value.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001403 Temporaries temps(graph_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001404 temps.Add(cls);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001405 HInstruction* value = LoadLocal(source_or_dest_reg, field_type, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001406 DCHECK_EQ(value->GetType(), field_type);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001407 current_block_->AddInstruction(new (arena_) HStaticFieldSet(cls,
1408 value,
1409 field_type,
1410 resolved_field->GetOffset(),
1411 resolved_field->IsVolatile(),
1412 field_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07001413 *dex_file_,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001414 dex_cache_,
1415 dex_pc));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001416 } else {
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001417 current_block_->AddInstruction(new (arena_) HStaticFieldGet(cls,
1418 field_type,
1419 resolved_field->GetOffset(),
1420 resolved_field->IsVolatile(),
1421 field_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07001422 *dex_file_,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001423 dex_cache_,
1424 dex_pc));
1425 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001426 }
1427 return true;
1428}
1429
Calin Juravlebacfec32014-11-14 15:54:36 +00001430void HGraphBuilder::BuildCheckedDivRem(uint16_t out_vreg,
1431 uint16_t first_vreg,
1432 int64_t second_vreg_or_constant,
1433 uint32_t dex_pc,
1434 Primitive::Type type,
1435 bool second_is_constant,
1436 bool isDiv) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001437 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Calin Juravled0d48522014-11-04 16:40:20 +00001438
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001439 HInstruction* first = LoadLocal(first_vreg, type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001440 HInstruction* second = nullptr;
1441 if (second_is_constant) {
1442 if (type == Primitive::kPrimInt) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001443 second = graph_->GetIntConstant(second_vreg_or_constant, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001444 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001445 second = graph_->GetLongConstant(second_vreg_or_constant, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001446 }
1447 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001448 second = LoadLocal(second_vreg_or_constant, type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001449 }
1450
1451 if (!second_is_constant
1452 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0)
1453 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) {
1454 second = new (arena_) HDivZeroCheck(second, dex_pc);
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001455 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +00001456 current_block_->AddInstruction(second);
1457 temps.Add(current_block_->GetLastInstruction());
1458 }
1459
Calin Juravlebacfec32014-11-14 15:54:36 +00001460 if (isDiv) {
1461 current_block_->AddInstruction(new (arena_) HDiv(type, first, second, dex_pc));
1462 } else {
1463 current_block_->AddInstruction(new (arena_) HRem(type, first, second, dex_pc));
1464 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001465 UpdateLocal(out_vreg, current_block_->GetLastInstruction(), dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001466}
1467
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001468void HGraphBuilder::BuildArrayAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001469 uint32_t dex_pc,
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001470 bool is_put,
1471 Primitive::Type anticipated_type) {
1472 uint8_t source_or_dest_reg = instruction.VRegA_23x();
1473 uint8_t array_reg = instruction.VRegB_23x();
1474 uint8_t index_reg = instruction.VRegC_23x();
1475
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001476 // We need one temporary for the null check, one for the index, and one for the length.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001477 Temporaries temps(graph_);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001478
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001479 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001480 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001481 current_block_->AddInstruction(object);
1482 temps.Add(object);
1483
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001484 HInstruction* length = new (arena_) HArrayLength(object, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001485 current_block_->AddInstruction(length);
1486 temps.Add(length);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001487 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001488 index = new (arena_) HBoundsCheck(index, length, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001489 current_block_->AddInstruction(index);
1490 temps.Add(index);
1491 if (is_put) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001492 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001493 // TODO: Insert a type check node if the type is Object.
Nicolas Geoffray39468442014-09-02 15:17:15 +01001494 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001495 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001496 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001497 current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type, dex_pc));
1498 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001499 }
Mark Mendell1152c922015-04-24 17:06:35 -04001500 graph_->SetHasBoundsChecks(true);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001501}
1502
Calin Juravle225ff812014-11-13 16:46:39 +00001503void HGraphBuilder::BuildFilledNewArray(uint32_t dex_pc,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001504 uint32_t type_index,
1505 uint32_t number_of_vreg_arguments,
1506 bool is_range,
1507 uint32_t* args,
1508 uint32_t register_index) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001509 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments, dex_pc);
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001510 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
1511 ? kQuickAllocArrayWithAccessCheck
1512 : kQuickAllocArray;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01001513 HInstruction* object = new (arena_) HNewArray(length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01001514 graph_->GetCurrentMethod(),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01001515 dex_pc,
1516 type_index,
1517 *dex_compilation_unit_->GetDexFile(),
1518 entrypoint);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001519 current_block_->AddInstruction(object);
1520
1521 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1522 DCHECK_EQ(descriptor[0], '[') << descriptor;
1523 char primitive = descriptor[1];
1524 DCHECK(primitive == 'I'
1525 || primitive == 'L'
1526 || primitive == '[') << descriptor;
1527 bool is_reference_array = (primitive == 'L') || (primitive == '[');
1528 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
1529
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001530 Temporaries temps(graph_);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001531 temps.Add(object);
1532 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001533 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type, dex_pc);
1534 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001535 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001536 new (arena_) HArraySet(object, index, value, type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001537 }
1538 latest_result_ = object;
1539}
1540
1541template <typename T>
1542void HGraphBuilder::BuildFillArrayData(HInstruction* object,
1543 const T* data,
1544 uint32_t element_count,
1545 Primitive::Type anticipated_type,
Calin Juravle225ff812014-11-13 16:46:39 +00001546 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001547 for (uint32_t i = 0; i < element_count; ++i) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001548 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1549 HInstruction* value = graph_->GetIntConstant(data[i], dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001550 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001551 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001552 }
1553}
1554
Calin Juravle225ff812014-11-13 16:46:39 +00001555void HGraphBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001556 Temporaries temps(graph_);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001557 HInstruction* array = LoadLocal(instruction.VRegA_31t(), Primitive::kPrimNot, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001558 HNullCheck* null_check = new (arena_) HNullCheck(array, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001559 current_block_->AddInstruction(null_check);
1560 temps.Add(null_check);
1561
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001562 HInstruction* length = new (arena_) HArrayLength(null_check, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001563 current_block_->AddInstruction(length);
1564
Calin Juravle225ff812014-11-13 16:46:39 +00001565 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
Calin Juravled0d48522014-11-04 16:40:20 +00001566 const Instruction::ArrayDataPayload* payload =
1567 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_start_ + payload_offset);
1568 const uint8_t* data = payload->data;
1569 uint32_t element_count = payload->element_count;
1570
1571 // Implementation of this DEX instruction seems to be that the bounds check is
1572 // done before doing any stores.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001573 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001574 current_block_->AddInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
Calin Juravled0d48522014-11-04 16:40:20 +00001575
1576 switch (payload->element_width) {
1577 case 1:
1578 BuildFillArrayData(null_check,
1579 reinterpret_cast<const int8_t*>(data),
1580 element_count,
1581 Primitive::kPrimByte,
Calin Juravle225ff812014-11-13 16:46:39 +00001582 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001583 break;
1584 case 2:
1585 BuildFillArrayData(null_check,
1586 reinterpret_cast<const int16_t*>(data),
1587 element_count,
1588 Primitive::kPrimShort,
Calin Juravle225ff812014-11-13 16:46:39 +00001589 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001590 break;
1591 case 4:
1592 BuildFillArrayData(null_check,
1593 reinterpret_cast<const int32_t*>(data),
1594 element_count,
1595 Primitive::kPrimInt,
Calin Juravle225ff812014-11-13 16:46:39 +00001596 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001597 break;
1598 case 8:
1599 BuildFillWideArrayData(null_check,
1600 reinterpret_cast<const int64_t*>(data),
1601 element_count,
Calin Juravle225ff812014-11-13 16:46:39 +00001602 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001603 break;
1604 default:
1605 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1606 }
Mark Mendell1152c922015-04-24 17:06:35 -04001607 graph_->SetHasBoundsChecks(true);
Calin Juravled0d48522014-11-04 16:40:20 +00001608}
1609
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001610void HGraphBuilder::BuildFillWideArrayData(HInstruction* object,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +01001611 const int64_t* data,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001612 uint32_t element_count,
Calin Juravle225ff812014-11-13 16:46:39 +00001613 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001614 for (uint32_t i = 0; i < element_count; ++i) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001615 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1616 HInstruction* value = graph_->GetLongConstant(data[i], dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001617 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001618 object, index, value, Primitive::kPrimLong, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001619 }
1620}
1621
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001622static TypeCheckKind ComputeTypeCheckKind(Handle<mirror::Class> cls)
1623 SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle98893e12015-10-02 21:05:03 +01001624 if (cls.Get() == nullptr) {
1625 return TypeCheckKind::kUnresolvedCheck;
1626 } else if (cls->IsInterface()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001627 return TypeCheckKind::kInterfaceCheck;
1628 } else if (cls->IsArrayClass()) {
1629 if (cls->GetComponentType()->IsObjectClass()) {
1630 return TypeCheckKind::kArrayObjectCheck;
1631 } else if (cls->CannotBeAssignedFromOtherTypes()) {
1632 return TypeCheckKind::kExactCheck;
1633 } else {
1634 return TypeCheckKind::kArrayCheck;
1635 }
1636 } else if (cls->IsFinal()) {
1637 return TypeCheckKind::kExactCheck;
1638 } else if (cls->IsAbstract()) {
1639 return TypeCheckKind::kAbstractClassCheck;
1640 } else {
1641 return TypeCheckKind::kClassHierarchyCheck;
1642 }
1643}
1644
Calin Juravle98893e12015-10-02 21:05:03 +01001645void HGraphBuilder::BuildTypeCheck(const Instruction& instruction,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001646 uint8_t destination,
1647 uint8_t reference,
1648 uint16_t type_index,
Calin Juravle225ff812014-11-13 16:46:39 +00001649 uint32_t dex_pc) {
Calin Juravle98893e12015-10-02 21:05:03 +01001650 bool type_known_final, type_known_abstract, use_declaring_class;
1651 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
1652 dex_compilation_unit_->GetDexMethodIndex(),
1653 *dex_compilation_unit_->GetDexFile(),
1654 type_index,
1655 &type_known_final,
1656 &type_known_abstract,
1657 &use_declaring_class);
1658
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001659 ScopedObjectAccess soa(Thread::Current());
1660 StackHandleScope<2> hs(soa.Self());
1661 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
1662 dex_compilation_unit_->GetClassLinker()->FindDexCache(
1663 soa.Self(), *dex_compilation_unit_->GetDexFile())));
1664 Handle<mirror::Class> resolved_class(hs.NewHandle(dex_cache->GetResolvedType(type_index)));
1665
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001666 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot, dex_pc);
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001667 HLoadClass* cls = new (arena_) HLoadClass(
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001668 graph_->GetCurrentMethod(),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01001669 type_index,
1670 *dex_compilation_unit_->GetDexFile(),
1671 IsOutermostCompilingClass(type_index),
Calin Juravle98893e12015-10-02 21:05:03 +01001672 dex_pc,
1673 !can_access);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001674 current_block_->AddInstruction(cls);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001675
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001676 // The class needs a temporary before being used by the type check.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001677 Temporaries temps(graph_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001678 temps.Add(cls);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001679
1680 TypeCheckKind check_kind = ComputeTypeCheckKind(resolved_class);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001681 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001682 current_block_->AddInstruction(new (arena_) HInstanceOf(object, cls, check_kind, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001683 UpdateLocal(destination, current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001684 } else {
1685 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001686 current_block_->AddInstruction(new (arena_) HCheckCast(object, cls, check_kind, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001687 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001688}
1689
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001690bool HGraphBuilder::NeedsAccessCheck(uint32_t type_index) const {
1691 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
1692 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index);
1693}
1694
Mark Mendellfe57faa2015-09-18 09:26:15 -04001695void HGraphBuilder::BuildSwitchJumpTable(const SwitchTable& table,
1696 const Instruction& instruction,
1697 HInstruction* value,
1698 uint32_t dex_pc) {
1699 // Add the successor blocks to the current block.
1700 uint16_t num_entries = table.GetNumEntries();
1701 for (size_t i = 1; i <= num_entries; i++) {
1702 int32_t target_offset = table.GetEntryAt(i);
1703 HBasicBlock* case_target = FindBlockStartingAt(dex_pc + target_offset);
1704 DCHECK(case_target != nullptr);
1705
1706 // Add the target block as a successor.
1707 current_block_->AddSuccessor(case_target);
1708 }
1709
1710 // Add the default target block as the last successor.
1711 HBasicBlock* default_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
1712 DCHECK(default_target != nullptr);
1713 current_block_->AddSuccessor(default_target);
1714
1715 // Now add the Switch instruction.
1716 int32_t starting_key = table.GetEntryAt(0);
1717 current_block_->AddInstruction(
1718 new (arena_) HPackedSwitch(starting_key, num_entries, value, dex_pc));
1719 // This block ends with control flow.
1720 current_block_ = nullptr;
1721}
1722
Calin Juravle48c2b032014-12-09 18:11:36 +00001723void HGraphBuilder::BuildPackedSwitch(const Instruction& instruction, uint32_t dex_pc) {
David Brazdil2ef645b2015-06-17 18:20:52 +01001724 // Verifier guarantees that the payload for PackedSwitch contains:
1725 // (a) number of entries (may be zero)
1726 // (b) first and lowest switch case value (entry 0, always present)
1727 // (c) list of target pcs (entries 1 <= i <= N)
Andreas Gamped881df52014-11-24 23:28:39 -08001728 SwitchTable table(instruction, dex_pc, false);
1729
1730 // Value to test against.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001731 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt, dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08001732
Mark Mendellfe57faa2015-09-18 09:26:15 -04001733 // Starting key value.
1734 int32_t starting_key = table.GetEntryAt(0);
1735
David Brazdil2ef645b2015-06-17 18:20:52 +01001736 // Retrieve number of entries.
Andreas Gampee4d4d322014-12-04 09:09:57 -08001737 uint16_t num_entries = table.GetNumEntries();
David Brazdil2ef645b2015-06-17 18:20:52 +01001738 if (num_entries == 0) {
1739 return;
1740 }
Andreas Gampee4d4d322014-12-04 09:09:57 -08001741
Mark Mendellfe57faa2015-09-18 09:26:15 -04001742 // Don't use a packed switch if there are very few entries.
1743 if (num_entries > kSmallSwitchThreshold) {
1744 BuildSwitchJumpTable(table, instruction, value, dex_pc);
1745 } else {
1746 // Chained cmp-and-branch, starting from starting_key.
1747 for (size_t i = 1; i <= num_entries; i++) {
Mark Mendell3b9f3042015-09-24 08:43:40 -04001748 BuildSwitchCaseHelper(instruction,
1749 i,
1750 i == num_entries,
1751 table,
1752 value,
1753 starting_key + i - 1,
1754 table.GetEntryAt(i),
1755 dex_pc);
Mark Mendellfe57faa2015-09-18 09:26:15 -04001756 }
Andreas Gamped881df52014-11-24 23:28:39 -08001757 }
Andreas Gamped881df52014-11-24 23:28:39 -08001758}
1759
Calin Juravle48c2b032014-12-09 18:11:36 +00001760void HGraphBuilder::BuildSparseSwitch(const Instruction& instruction, uint32_t dex_pc) {
David Brazdil2ef645b2015-06-17 18:20:52 +01001761 // Verifier guarantees that the payload for SparseSwitch contains:
1762 // (a) number of entries (may be zero)
1763 // (b) sorted key values (entries 0 <= i < N)
1764 // (c) target pcs corresponding to the switch values (entries N <= i < 2*N)
Andreas Gampee4d4d322014-12-04 09:09:57 -08001765 SwitchTable table(instruction, dex_pc, true);
1766
1767 // Value to test against.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001768 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001769
1770 uint16_t num_entries = table.GetNumEntries();
Andreas Gampee4d4d322014-12-04 09:09:57 -08001771
1772 for (size_t i = 0; i < num_entries; i++) {
1773 BuildSwitchCaseHelper(instruction, i, i == static_cast<size_t>(num_entries) - 1, table, value,
1774 table.GetEntryAt(i), table.GetEntryAt(i + num_entries), dex_pc);
1775 }
Andreas Gampee4d4d322014-12-04 09:09:57 -08001776}
1777
1778void HGraphBuilder::BuildSwitchCaseHelper(const Instruction& instruction, size_t index,
1779 bool is_last_case, const SwitchTable& table,
1780 HInstruction* value, int32_t case_value_int,
1781 int32_t target_offset, uint32_t dex_pc) {
David Brazdil852eaff2015-02-02 15:23:05 +00001782 HBasicBlock* case_target = FindBlockStartingAt(dex_pc + target_offset);
1783 DCHECK(case_target != nullptr);
1784 PotentiallyAddSuspendCheck(case_target, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001785
1786 // The current case's value.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001787 HInstruction* this_case_value = graph_->GetIntConstant(case_value_int, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001788
1789 // Compare value and this_case_value.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001790 HEqual* comparison = new (arena_) HEqual(value, this_case_value, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001791 current_block_->AddInstruction(comparison);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001792 HInstruction* ifinst = new (arena_) HIf(comparison, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001793 current_block_->AddInstruction(ifinst);
1794
1795 // Case hit: use the target offset to determine where to go.
Andreas Gampee4d4d322014-12-04 09:09:57 -08001796 current_block_->AddSuccessor(case_target);
1797
1798 // Case miss: go to the next case (or default fall-through).
1799 // When there is a next case, we use the block stored with the table offset representing this
1800 // case (that is where we registered them in ComputeBranchTargets).
1801 // When there is no next case, we use the following instruction.
1802 // TODO: Find a good way to peel the last iteration to avoid conditional, but still have re-use.
1803 if (!is_last_case) {
1804 HBasicBlock* next_case_target = FindBlockStartingAt(table.GetDexPcForIndex(index));
1805 DCHECK(next_case_target != nullptr);
1806 current_block_->AddSuccessor(next_case_target);
1807
1808 // Need to manually add the block, as there is no dex-pc transition for the cases.
1809 graph_->AddBlock(next_case_target);
1810
1811 current_block_ = next_case_target;
1812 } else {
1813 HBasicBlock* default_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
1814 DCHECK(default_target != nullptr);
1815 current_block_->AddSuccessor(default_target);
1816 current_block_ = nullptr;
1817 }
1818}
1819
David Brazdil852eaff2015-02-02 15:23:05 +00001820void HGraphBuilder::PotentiallyAddSuspendCheck(HBasicBlock* target, uint32_t dex_pc) {
1821 int32_t target_offset = target->GetDexPc() - dex_pc;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001822 if (target_offset <= 0) {
David Brazdil852eaff2015-02-02 15:23:05 +00001823 // DX generates back edges to the first encountered return. We can save
1824 // time of later passes by not adding redundant suspend checks.
David Brazdil2fd6aa52015-02-02 18:58:27 +00001825 HInstruction* last_in_target = target->GetLastInstruction();
1826 if (last_in_target != nullptr &&
1827 (last_in_target->IsReturn() || last_in_target->IsReturnVoid())) {
1828 return;
David Brazdil852eaff2015-02-02 15:23:05 +00001829 }
1830
1831 // Add a suspend check to backward branches which may potentially loop. We
1832 // can remove them after we recognize loops in the graph.
Calin Juravle225ff812014-11-13 16:46:39 +00001833 current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_pc));
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001834 }
1835}
1836
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001837bool HGraphBuilder::CanDecodeQuickenedInfo() const {
1838 return interpreter_metadata_ != nullptr;
1839}
1840
1841uint16_t HGraphBuilder::LookupQuickenedInfo(uint32_t dex_pc) {
1842 DCHECK(interpreter_metadata_ != nullptr);
1843 uint32_t dex_pc_in_map = DecodeUnsignedLeb128(&interpreter_metadata_);
1844 DCHECK_EQ(dex_pc, dex_pc_in_map);
1845 return DecodeUnsignedLeb128(&interpreter_metadata_);
1846}
1847
Calin Juravle225ff812014-11-13 16:46:39 +00001848bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001849 if (current_block_ == nullptr) {
1850 return true; // Dead code
1851 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001852
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001853 switch (instruction.Opcode()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001854 case Instruction::CONST_4: {
1855 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001856 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n(), dex_pc);
1857 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001858 break;
1859 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001860
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001861 case Instruction::CONST_16: {
1862 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001863 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s(), dex_pc);
1864 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001865 break;
1866 }
1867
Dave Allison20dfc792014-06-16 20:44:29 -07001868 case Instruction::CONST: {
1869 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001870 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i(), dex_pc);
1871 UpdateLocal(register_index, constant, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001872 break;
1873 }
1874
1875 case Instruction::CONST_HIGH16: {
1876 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001877 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16, dex_pc);
1878 UpdateLocal(register_index, constant, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001879 break;
1880 }
1881
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001882 case Instruction::CONST_WIDE_16: {
1883 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001884 // Get 16 bits of constant value, sign extended to 64 bits.
1885 int64_t value = instruction.VRegB_21s();
1886 value <<= 48;
1887 value >>= 48;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001888 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1889 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001890 break;
1891 }
1892
1893 case Instruction::CONST_WIDE_32: {
1894 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001895 // Get 32 bits of constant value, sign extended to 64 bits.
1896 int64_t value = instruction.VRegB_31i();
1897 value <<= 32;
1898 value >>= 32;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001899 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1900 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001901 break;
1902 }
1903
1904 case Instruction::CONST_WIDE: {
1905 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001906 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l(), dex_pc);
1907 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001908 break;
1909 }
1910
Dave Allison20dfc792014-06-16 20:44:29 -07001911 case Instruction::CONST_WIDE_HIGH16: {
1912 int32_t register_index = instruction.VRegA();
1913 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001914 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1915 UpdateLocal(register_index, constant, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001916 break;
1917 }
1918
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001919 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001920 case Instruction::MOVE:
1921 case Instruction::MOVE_FROM16:
1922 case Instruction::MOVE_16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001923 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
1924 UpdateLocal(instruction.VRegA(), value, dex_pc);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001925 break;
1926 }
1927
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001928 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001929 case Instruction::MOVE_WIDE:
1930 case Instruction::MOVE_WIDE_FROM16:
1931 case Instruction::MOVE_WIDE_16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001932 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong, dex_pc);
1933 UpdateLocal(instruction.VRegA(), value, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001934 break;
1935 }
1936
1937 case Instruction::MOVE_OBJECT:
1938 case Instruction::MOVE_OBJECT_16:
1939 case Instruction::MOVE_OBJECT_FROM16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001940 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot, dex_pc);
1941 UpdateLocal(instruction.VRegA(), value, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001942 break;
1943 }
1944
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001945 case Instruction::RETURN_VOID_NO_BARRIER:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001946 case Instruction::RETURN_VOID: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001947 BuildReturn(instruction, Primitive::kPrimVoid, dex_pc);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001948 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001949 }
1950
Dave Allison20dfc792014-06-16 20:44:29 -07001951#define IF_XX(comparison, cond) \
Calin Juravle225ff812014-11-13 16:46:39 +00001952 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
1953 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001954
Dave Allison20dfc792014-06-16 20:44:29 -07001955 IF_XX(HEqual, EQ);
1956 IF_XX(HNotEqual, NE);
1957 IF_XX(HLessThan, LT);
1958 IF_XX(HLessThanOrEqual, LE);
1959 IF_XX(HGreaterThan, GT);
1960 IF_XX(HGreaterThanOrEqual, GE);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001961
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001962 case Instruction::GOTO:
1963 case Instruction::GOTO_16:
1964 case Instruction::GOTO_32: {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001965 int32_t offset = instruction.GetTargetOffset();
Calin Juravle225ff812014-11-13 16:46:39 +00001966 HBasicBlock* target = FindBlockStartingAt(offset + dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001967 DCHECK(target != nullptr);
David Brazdil852eaff2015-02-02 15:23:05 +00001968 PotentiallyAddSuspendCheck(target, dex_pc);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001969 current_block_->AddInstruction(new (arena_) HGoto(dex_pc));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001970 current_block_->AddSuccessor(target);
1971 current_block_ = nullptr;
1972 break;
1973 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001974
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001975 case Instruction::RETURN: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001976 BuildReturn(instruction, return_type_, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001977 break;
1978 }
1979
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001980 case Instruction::RETURN_OBJECT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001981 BuildReturn(instruction, return_type_, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001982 break;
1983 }
1984
1985 case Instruction::RETURN_WIDE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001986 BuildReturn(instruction, return_type_, dex_pc);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001987 break;
1988 }
1989
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001990 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001991 case Instruction::INVOKE_INTERFACE:
1992 case Instruction::INVOKE_STATIC:
1993 case Instruction::INVOKE_SUPER:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001994 case Instruction::INVOKE_VIRTUAL:
1995 case Instruction::INVOKE_VIRTUAL_QUICK: {
1996 uint16_t method_idx;
1997 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_QUICK) {
1998 if (!CanDecodeQuickenedInfo()) {
1999 return false;
2000 }
2001 method_idx = LookupQuickenedInfo(dex_pc);
2002 } else {
2003 method_idx = instruction.VRegB_35c();
2004 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002005 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002006 uint32_t args[5];
Ian Rogers29a26482014-05-02 15:27:29 -07002007 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00002008 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00002009 number_of_vreg_arguments, false, args, -1)) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002010 return false;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002011 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002012 break;
2013 }
2014
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002015 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00002016 case Instruction::INVOKE_INTERFACE_RANGE:
2017 case Instruction::INVOKE_STATIC_RANGE:
2018 case Instruction::INVOKE_SUPER_RANGE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002019 case Instruction::INVOKE_VIRTUAL_RANGE:
2020 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2021 uint16_t method_idx;
2022 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK) {
2023 if (!CanDecodeQuickenedInfo()) {
2024 return false;
2025 }
2026 method_idx = LookupQuickenedInfo(dex_pc);
2027 } else {
2028 method_idx = instruction.VRegB_3rc();
2029 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002030 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
2031 uint32_t register_index = instruction.VRegC();
Calin Juravle225ff812014-11-13 16:46:39 +00002032 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002033 number_of_vreg_arguments, true, nullptr, register_index)) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002034 return false;
2035 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002036 break;
2037 }
2038
Roland Levillain88cb1752014-10-20 16:36:47 +01002039 case Instruction::NEG_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002040 Unop_12x<HNeg>(instruction, Primitive::kPrimInt, dex_pc);
Roland Levillain88cb1752014-10-20 16:36:47 +01002041 break;
2042 }
2043
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002044 case Instruction::NEG_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002045 Unop_12x<HNeg>(instruction, Primitive::kPrimLong, dex_pc);
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002046 break;
2047 }
2048
Roland Levillain3dbcb382014-10-28 17:30:07 +00002049 case Instruction::NEG_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002050 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat, dex_pc);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002051 break;
2052 }
2053
2054 case Instruction::NEG_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002055 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble, dex_pc);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002056 break;
2057 }
2058
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002059 case Instruction::NOT_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002060 Unop_12x<HNot>(instruction, Primitive::kPrimInt, dex_pc);
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002061 break;
2062 }
2063
Roland Levillain70566432014-10-24 16:20:17 +01002064 case Instruction::NOT_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002065 Unop_12x<HNot>(instruction, Primitive::kPrimLong, dex_pc);
Roland Levillain70566432014-10-24 16:20:17 +01002066 break;
2067 }
2068
Roland Levillaindff1f282014-11-05 14:15:05 +00002069 case Instruction::INT_TO_LONG: {
Roland Levillain624279f2014-12-04 11:54:28 +00002070 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong, dex_pc);
Roland Levillaindff1f282014-11-05 14:15:05 +00002071 break;
2072 }
2073
Roland Levillaincff13742014-11-17 14:32:17 +00002074 case Instruction::INT_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00002075 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00002076 break;
2077 }
2078
2079 case Instruction::INT_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00002080 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00002081 break;
2082 }
2083
Roland Levillain946e1432014-11-11 17:35:19 +00002084 case Instruction::LONG_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00002085 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt, dex_pc);
Roland Levillain946e1432014-11-11 17:35:19 +00002086 break;
2087 }
2088
Roland Levillain6d0e4832014-11-27 18:31:21 +00002089 case Instruction::LONG_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00002090 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimFloat, dex_pc);
Roland Levillain6d0e4832014-11-27 18:31:21 +00002091 break;
2092 }
2093
Roland Levillain647b9ed2014-11-27 12:06:00 +00002094 case Instruction::LONG_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00002095 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble, dex_pc);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002096 break;
2097 }
2098
Roland Levillain3f8f9362014-12-02 17:45:01 +00002099 case Instruction::FLOAT_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00002100 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimInt, dex_pc);
2101 break;
2102 }
2103
2104 case Instruction::FLOAT_TO_LONG: {
2105 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimLong, dex_pc);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002106 break;
2107 }
2108
Roland Levillain8964e2b2014-12-04 12:10:50 +00002109 case Instruction::FLOAT_TO_DOUBLE: {
2110 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimDouble, dex_pc);
2111 break;
2112 }
2113
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002114 case Instruction::DOUBLE_TO_INT: {
2115 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimInt, dex_pc);
2116 break;
2117 }
2118
2119 case Instruction::DOUBLE_TO_LONG: {
2120 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimLong, dex_pc);
2121 break;
2122 }
2123
Roland Levillain8964e2b2014-12-04 12:10:50 +00002124 case Instruction::DOUBLE_TO_FLOAT: {
2125 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimFloat, dex_pc);
2126 break;
2127 }
2128
Roland Levillain51d3fc42014-11-13 14:11:42 +00002129 case Instruction::INT_TO_BYTE: {
Roland Levillain624279f2014-12-04 11:54:28 +00002130 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte, dex_pc);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002131 break;
2132 }
2133
Roland Levillain01a8d712014-11-14 16:27:39 +00002134 case Instruction::INT_TO_SHORT: {
Roland Levillain624279f2014-12-04 11:54:28 +00002135 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort, dex_pc);
Roland Levillain01a8d712014-11-14 16:27:39 +00002136 break;
2137 }
2138
Roland Levillain981e4542014-11-14 11:47:14 +00002139 case Instruction::INT_TO_CHAR: {
Roland Levillain624279f2014-12-04 11:54:28 +00002140 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar, dex_pc);
Roland Levillain981e4542014-11-14 11:47:14 +00002141 break;
2142 }
2143
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002144 case Instruction::ADD_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002145 Binop_23x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002146 break;
2147 }
2148
2149 case Instruction::ADD_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002150 Binop_23x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002151 break;
2152 }
2153
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002154 case Instruction::ADD_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002155 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002156 break;
2157 }
2158
2159 case Instruction::ADD_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002160 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002161 break;
2162 }
2163
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002164 case Instruction::SUB_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002165 Binop_23x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002166 break;
2167 }
2168
2169 case Instruction::SUB_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002170 Binop_23x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002171 break;
2172 }
2173
Calin Juravle096cc022014-10-23 17:01:13 +01002174 case Instruction::SUB_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002175 Binop_23x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01002176 break;
2177 }
2178
2179 case Instruction::SUB_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002180 Binop_23x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01002181 break;
2182 }
2183
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002184 case Instruction::ADD_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002185 Binop_12x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002186 break;
2187 }
2188
Calin Juravle34bacdf2014-10-07 20:23:36 +01002189 case Instruction::MUL_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002190 Binop_23x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002191 break;
2192 }
2193
2194 case Instruction::MUL_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002195 Binop_23x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002196 break;
2197 }
2198
Calin Juravleb5bfa962014-10-21 18:02:24 +01002199 case Instruction::MUL_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002200 Binop_23x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01002201 break;
2202 }
2203
2204 case Instruction::MUL_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002205 Binop_23x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01002206 break;
2207 }
2208
Calin Juravled0d48522014-11-04 16:40:20 +00002209 case Instruction::DIV_INT: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002210 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2211 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravled0d48522014-11-04 16:40:20 +00002212 break;
2213 }
2214
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002215 case Instruction::DIV_LONG: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002216 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2217 dex_pc, Primitive::kPrimLong, false, true);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002218 break;
2219 }
2220
Calin Juravle7c4954d2014-10-28 16:57:40 +00002221 case Instruction::DIV_FLOAT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002222 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002223 break;
2224 }
2225
2226 case Instruction::DIV_DOUBLE: {
Calin Juravle225ff812014-11-13 16:46:39 +00002227 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002228 break;
2229 }
2230
Calin Juravlebacfec32014-11-14 15:54:36 +00002231 case Instruction::REM_INT: {
2232 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2233 dex_pc, Primitive::kPrimInt, false, false);
2234 break;
2235 }
2236
2237 case Instruction::REM_LONG: {
2238 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2239 dex_pc, Primitive::kPrimLong, false, false);
2240 break;
2241 }
2242
Calin Juravled2ec87d2014-12-08 14:24:46 +00002243 case Instruction::REM_FLOAT: {
2244 Binop_23x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2245 break;
2246 }
2247
2248 case Instruction::REM_DOUBLE: {
2249 Binop_23x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2250 break;
2251 }
2252
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002253 case Instruction::AND_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002254 Binop_23x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002255 break;
2256 }
2257
2258 case Instruction::AND_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002259 Binop_23x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002260 break;
2261 }
2262
Calin Juravle9aec02f2014-11-18 23:06:35 +00002263 case Instruction::SHL_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002264 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002265 break;
2266 }
2267
2268 case Instruction::SHL_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002269 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002270 break;
2271 }
2272
2273 case Instruction::SHR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002274 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002275 break;
2276 }
2277
2278 case Instruction::SHR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002279 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002280 break;
2281 }
2282
2283 case Instruction::USHR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002284 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002285 break;
2286 }
2287
2288 case Instruction::USHR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002289 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002290 break;
2291 }
2292
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002293 case Instruction::OR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002294 Binop_23x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002295 break;
2296 }
2297
2298 case Instruction::OR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002299 Binop_23x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002300 break;
2301 }
2302
2303 case Instruction::XOR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002304 Binop_23x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002305 break;
2306 }
2307
2308 case Instruction::XOR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002309 Binop_23x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002310 break;
2311 }
2312
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002313 case Instruction::ADD_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002314 Binop_12x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002315 break;
2316 }
2317
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002318 case Instruction::ADD_DOUBLE_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002319 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002320 break;
2321 }
2322
2323 case Instruction::ADD_FLOAT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002324 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002325 break;
2326 }
2327
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002328 case Instruction::SUB_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002329 Binop_12x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002330 break;
2331 }
2332
2333 case Instruction::SUB_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002334 Binop_12x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002335 break;
2336 }
2337
Calin Juravle096cc022014-10-23 17:01:13 +01002338 case Instruction::SUB_FLOAT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002339 Binop_12x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01002340 break;
2341 }
2342
2343 case Instruction::SUB_DOUBLE_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002344 Binop_12x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01002345 break;
2346 }
2347
Calin Juravle34bacdf2014-10-07 20:23:36 +01002348 case Instruction::MUL_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002349 Binop_12x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002350 break;
2351 }
2352
2353 case Instruction::MUL_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002354 Binop_12x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002355 break;
2356 }
2357
Calin Juravleb5bfa962014-10-21 18:02:24 +01002358 case Instruction::MUL_FLOAT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002359 Binop_12x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01002360 break;
2361 }
2362
2363 case Instruction::MUL_DOUBLE_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002364 Binop_12x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01002365 break;
2366 }
2367
Calin Juravle865fc882014-11-06 17:09:03 +00002368 case Instruction::DIV_INT_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002369 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2370 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravle865fc882014-11-06 17:09:03 +00002371 break;
2372 }
2373
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002374 case Instruction::DIV_LONG_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002375 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2376 dex_pc, Primitive::kPrimLong, false, true);
2377 break;
2378 }
2379
2380 case Instruction::REM_INT_2ADDR: {
2381 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2382 dex_pc, Primitive::kPrimInt, false, false);
2383 break;
2384 }
2385
2386 case Instruction::REM_LONG_2ADDR: {
2387 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2388 dex_pc, Primitive::kPrimLong, false, false);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002389 break;
2390 }
2391
Calin Juravled2ec87d2014-12-08 14:24:46 +00002392 case Instruction::REM_FLOAT_2ADDR: {
2393 Binop_12x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2394 break;
2395 }
2396
2397 case Instruction::REM_DOUBLE_2ADDR: {
2398 Binop_12x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2399 break;
2400 }
2401
Calin Juravle9aec02f2014-11-18 23:06:35 +00002402 case Instruction::SHL_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002403 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002404 break;
2405 }
2406
2407 case Instruction::SHL_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002408 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002409 break;
2410 }
2411
2412 case Instruction::SHR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002413 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002414 break;
2415 }
2416
2417 case Instruction::SHR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002418 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002419 break;
2420 }
2421
2422 case Instruction::USHR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002423 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002424 break;
2425 }
2426
2427 case Instruction::USHR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002428 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002429 break;
2430 }
2431
Calin Juravle7c4954d2014-10-28 16:57:40 +00002432 case Instruction::DIV_FLOAT_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00002433 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002434 break;
2435 }
2436
2437 case Instruction::DIV_DOUBLE_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00002438 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002439 break;
2440 }
2441
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002442 case Instruction::AND_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002443 Binop_12x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002444 break;
2445 }
2446
2447 case Instruction::AND_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002448 Binop_12x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002449 break;
2450 }
2451
2452 case Instruction::OR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002453 Binop_12x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002454 break;
2455 }
2456
2457 case Instruction::OR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002458 Binop_12x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002459 break;
2460 }
2461
2462 case Instruction::XOR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002463 Binop_12x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002464 break;
2465 }
2466
2467 case Instruction::XOR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002468 Binop_12x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002469 break;
2470 }
2471
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002472 case Instruction::ADD_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002473 Binop_22s<HAdd>(instruction, false, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002474 break;
2475 }
2476
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002477 case Instruction::AND_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002478 Binop_22s<HAnd>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002479 break;
2480 }
2481
2482 case Instruction::OR_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002483 Binop_22s<HOr>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002484 break;
2485 }
2486
2487 case Instruction::XOR_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002488 Binop_22s<HXor>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002489 break;
2490 }
2491
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002492 case Instruction::RSUB_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002493 Binop_22s<HSub>(instruction, true, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002494 break;
2495 }
2496
Calin Juravle34bacdf2014-10-07 20:23:36 +01002497 case Instruction::MUL_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002498 Binop_22s<HMul>(instruction, false, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002499 break;
2500 }
2501
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002502 case Instruction::ADD_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002503 Binop_22b<HAdd>(instruction, false, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002504 break;
2505 }
2506
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002507 case Instruction::AND_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002508 Binop_22b<HAnd>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002509 break;
2510 }
2511
2512 case Instruction::OR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002513 Binop_22b<HOr>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002514 break;
2515 }
2516
2517 case Instruction::XOR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002518 Binop_22b<HXor>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002519 break;
2520 }
2521
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002522 case Instruction::RSUB_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002523 Binop_22b<HSub>(instruction, true, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002524 break;
2525 }
2526
Calin Juravle34bacdf2014-10-07 20:23:36 +01002527 case Instruction::MUL_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002528 Binop_22b<HMul>(instruction, false, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002529 break;
2530 }
2531
Calin Juravled0d48522014-11-04 16:40:20 +00002532 case Instruction::DIV_INT_LIT16:
2533 case Instruction::DIV_INT_LIT8: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002534 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2535 dex_pc, Primitive::kPrimInt, true, true);
2536 break;
2537 }
2538
2539 case Instruction::REM_INT_LIT16:
2540 case Instruction::REM_INT_LIT8: {
2541 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2542 dex_pc, Primitive::kPrimInt, true, false);
Calin Juravled0d48522014-11-04 16:40:20 +00002543 break;
2544 }
2545
Calin Juravle9aec02f2014-11-18 23:06:35 +00002546 case Instruction::SHL_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002547 Binop_22b<HShl>(instruction, false, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002548 break;
2549 }
2550
2551 case Instruction::SHR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002552 Binop_22b<HShr>(instruction, false, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002553 break;
2554 }
2555
2556 case Instruction::USHR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002557 Binop_22b<HUShr>(instruction, false, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002558 break;
2559 }
2560
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002561 case Instruction::NEW_INSTANCE: {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002562 uint16_t type_index = instruction.VRegB_21c();
Jeff Hao848f70a2014-01-15 13:49:50 -08002563 if (compiler_driver_->IsStringTypeIndex(type_index, dex_file_)) {
Jeff Hao848f70a2014-01-15 13:49:50 -08002564 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002565 HFakeString* fake_string = new (arena_) HFakeString(dex_pc);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002566 current_block_->AddInstruction(fake_string);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002567 UpdateLocal(register_index, fake_string, dex_pc);
Jeff Hao848f70a2014-01-15 13:49:50 -08002568 } else {
2569 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
2570 ? kQuickAllocObjectWithAccessCheck
2571 : kQuickAllocObject;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002572
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002573 current_block_->AddInstruction(new (arena_) HNewInstance(
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002574 graph_->GetCurrentMethod(),
2575 dex_pc,
2576 type_index,
2577 *dex_compilation_unit_->GetDexFile(),
2578 entrypoint));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002579 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Jeff Hao848f70a2014-01-15 13:49:50 -08002580 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002581 break;
2582 }
2583
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002584 case Instruction::NEW_ARRAY: {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002585 uint16_t type_index = instruction.VRegC_22c();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002586 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt, dex_pc);
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002587 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
2588 ? kQuickAllocArrayWithAccessCheck
2589 : kQuickAllocArray;
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002590 current_block_->AddInstruction(new (arena_) HNewArray(length,
2591 graph_->GetCurrentMethod(),
2592 dex_pc,
2593 type_index,
2594 *dex_compilation_unit_->GetDexFile(),
2595 entrypoint));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002596 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002597 break;
2598 }
2599
2600 case Instruction::FILLED_NEW_ARRAY: {
2601 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
2602 uint32_t type_index = instruction.VRegB_35c();
2603 uint32_t args[5];
2604 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00002605 BuildFilledNewArray(dex_pc, type_index, number_of_vreg_arguments, false, args, 0);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002606 break;
2607 }
2608
2609 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2610 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
2611 uint32_t type_index = instruction.VRegB_3rc();
2612 uint32_t register_index = instruction.VRegC_3rc();
2613 BuildFilledNewArray(
Calin Juravle225ff812014-11-13 16:46:39 +00002614 dex_pc, type_index, number_of_vreg_arguments, true, nullptr, register_index);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002615 break;
2616 }
2617
2618 case Instruction::FILL_ARRAY_DATA: {
Calin Juravle225ff812014-11-13 16:46:39 +00002619 BuildFillArrayData(instruction, dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002620 break;
2621 }
2622
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002623 case Instruction::MOVE_RESULT:
Dave Allison20dfc792014-06-16 20:44:29 -07002624 case Instruction::MOVE_RESULT_WIDE:
David Brazdilfc6a86a2015-06-26 10:33:45 +00002625 case Instruction::MOVE_RESULT_OBJECT: {
Nicolas Geoffray1efcc222015-06-24 12:41:20 +01002626 if (latest_result_ == nullptr) {
2627 // Only dead code can lead to this situation, where the verifier
2628 // does not reject the method.
2629 } else {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002630 // An Invoke/FilledNewArray and its MoveResult could have landed in
2631 // different blocks if there was a try/catch block boundary between
2632 // them. For Invoke, we insert a StoreLocal after the instruction. For
2633 // FilledNewArray, the local needs to be updated after the array was
2634 // filled, otherwise we might overwrite an input vreg.
2635 HStoreLocal* update_local =
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002636 new (arena_) HStoreLocal(GetLocalAt(instruction.VRegA()), latest_result_, dex_pc);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002637 HBasicBlock* block = latest_result_->GetBlock();
2638 if (block == current_block_) {
2639 // MoveResult and the previous instruction are in the same block.
2640 current_block_->AddInstruction(update_local);
2641 } else {
2642 // The two instructions are in different blocks. Insert the MoveResult
2643 // before the final control-flow instruction of the previous block.
2644 DCHECK(block->EndsWithControlFlowInstruction());
2645 DCHECK(current_block_->GetInstructions().IsEmpty());
2646 block->InsertInstructionBefore(update_local, block->GetLastInstruction());
2647 }
Nicolas Geoffray1efcc222015-06-24 12:41:20 +01002648 latest_result_ = nullptr;
2649 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002650 break;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002651 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002652
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002653 case Instruction::CMP_LONG: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002654 Binop_23x_cmp(instruction, Primitive::kPrimLong, ComparisonBias::kNoBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002655 break;
2656 }
2657
2658 case Instruction::CMPG_FLOAT: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002659 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kGtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002660 break;
2661 }
2662
2663 case Instruction::CMPG_DOUBLE: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002664 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kGtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002665 break;
2666 }
2667
2668 case Instruction::CMPL_FLOAT: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002669 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kLtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002670 break;
2671 }
2672
2673 case Instruction::CMPL_DOUBLE: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002674 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kLtBias, dex_pc);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002675 break;
2676 }
2677
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002678 case Instruction::NOP:
2679 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002680
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002681 case Instruction::IGET:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002682 case Instruction::IGET_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002683 case Instruction::IGET_WIDE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002684 case Instruction::IGET_WIDE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002685 case Instruction::IGET_OBJECT:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002686 case Instruction::IGET_OBJECT_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002687 case Instruction::IGET_BOOLEAN:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002688 case Instruction::IGET_BOOLEAN_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002689 case Instruction::IGET_BYTE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002690 case Instruction::IGET_BYTE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002691 case Instruction::IGET_CHAR:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002692 case Instruction::IGET_CHAR_QUICK:
2693 case Instruction::IGET_SHORT:
2694 case Instruction::IGET_SHORT_QUICK: {
Calin Juravle225ff812014-11-13 16:46:39 +00002695 if (!BuildInstanceFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002696 return false;
2697 }
2698 break;
2699 }
2700
2701 case Instruction::IPUT:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002702 case Instruction::IPUT_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002703 case Instruction::IPUT_WIDE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002704 case Instruction::IPUT_WIDE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002705 case Instruction::IPUT_OBJECT:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002706 case Instruction::IPUT_OBJECT_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002707 case Instruction::IPUT_BOOLEAN:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002708 case Instruction::IPUT_BOOLEAN_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002709 case Instruction::IPUT_BYTE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002710 case Instruction::IPUT_BYTE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002711 case Instruction::IPUT_CHAR:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002712 case Instruction::IPUT_CHAR_QUICK:
2713 case Instruction::IPUT_SHORT:
2714 case Instruction::IPUT_SHORT_QUICK: {
Calin Juravle225ff812014-11-13 16:46:39 +00002715 if (!BuildInstanceFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002716 return false;
2717 }
2718 break;
2719 }
2720
2721 case Instruction::SGET:
2722 case Instruction::SGET_WIDE:
2723 case Instruction::SGET_OBJECT:
2724 case Instruction::SGET_BOOLEAN:
2725 case Instruction::SGET_BYTE:
2726 case Instruction::SGET_CHAR:
2727 case Instruction::SGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002728 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002729 return false;
2730 }
2731 break;
2732 }
2733
2734 case Instruction::SPUT:
2735 case Instruction::SPUT_WIDE:
2736 case Instruction::SPUT_OBJECT:
2737 case Instruction::SPUT_BOOLEAN:
2738 case Instruction::SPUT_BYTE:
2739 case Instruction::SPUT_CHAR:
2740 case Instruction::SPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002741 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002742 return false;
2743 }
2744 break;
2745 }
2746
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002747#define ARRAY_XX(kind, anticipated_type) \
2748 case Instruction::AGET##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00002749 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002750 break; \
2751 } \
2752 case Instruction::APUT##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00002753 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002754 break; \
2755 }
2756
2757 ARRAY_XX(, Primitive::kPrimInt);
2758 ARRAY_XX(_WIDE, Primitive::kPrimLong);
2759 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
2760 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
2761 ARRAY_XX(_BYTE, Primitive::kPrimByte);
2762 ARRAY_XX(_CHAR, Primitive::kPrimChar);
2763 ARRAY_XX(_SHORT, Primitive::kPrimShort);
2764
Nicolas Geoffray39468442014-09-02 15:17:15 +01002765 case Instruction::ARRAY_LENGTH: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002766 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot, dex_pc);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002767 // No need for a temporary for the null check, it is the only input of the following
2768 // instruction.
Calin Juravle225ff812014-11-13 16:46:39 +00002769 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002770 current_block_->AddInstruction(object);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002771 current_block_->AddInstruction(new (arena_) HArrayLength(object, dex_pc));
2772 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002773 break;
2774 }
2775
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002776 case Instruction::CONST_STRING: {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002777 current_block_->AddInstruction(
2778 new (arena_) HLoadString(graph_->GetCurrentMethod(), instruction.VRegB_21c(), dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002779 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002780 break;
2781 }
2782
2783 case Instruction::CONST_STRING_JUMBO: {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002784 current_block_->AddInstruction(
2785 new (arena_) HLoadString(graph_->GetCurrentMethod(), instruction.VRegB_31c(), dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002786 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002787 break;
2788 }
2789
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002790 case Instruction::CONST_CLASS: {
2791 uint16_t type_index = instruction.VRegB_21c();
2792 bool type_known_final;
2793 bool type_known_abstract;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002794 bool dont_use_is_referrers_class;
2795 // `CanAccessTypeWithoutChecks` will tell whether the method being
2796 // built is trying to access its own class, so that the generated
2797 // code can optimize for this case. However, the optimization does not
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002798 // work for inlining, so we use `IsOutermostCompilingClass` instead.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002799 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
2800 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002801 &type_known_final, &type_known_abstract, &dont_use_is_referrers_class);
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002802 current_block_->AddInstruction(new (arena_) HLoadClass(
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002803 graph_->GetCurrentMethod(),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002804 type_index,
2805 *dex_compilation_unit_->GetDexFile(),
2806 IsOutermostCompilingClass(type_index),
Calin Juravle98893e12015-10-02 21:05:03 +01002807 dex_pc,
2808 !can_access));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002809 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002810 break;
2811 }
2812
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002813 case Instruction::MOVE_EXCEPTION: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002814 current_block_->AddInstruction(new (arena_) HLoadException(dex_pc));
2815 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction(), dex_pc);
2816 current_block_->AddInstruction(new (arena_) HClearException(dex_pc));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002817 break;
2818 }
2819
2820 case Instruction::THROW: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002821 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00002822 current_block_->AddInstruction(new (arena_) HThrow(exception, dex_pc));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002823 // A throw instruction must branch to the exit block.
2824 current_block_->AddSuccessor(exit_block_);
2825 // We finished building this block. Set the current block to null to avoid
2826 // adding dead instructions to it.
2827 current_block_ = nullptr;
2828 break;
2829 }
2830
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002831 case Instruction::INSTANCE_OF: {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002832 uint8_t destination = instruction.VRegA_22c();
2833 uint8_t reference = instruction.VRegB_22c();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002834 uint16_t type_index = instruction.VRegC_22c();
Calin Juravle98893e12015-10-02 21:05:03 +01002835 BuildTypeCheck(instruction, destination, reference, type_index, dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002836 break;
2837 }
2838
2839 case Instruction::CHECK_CAST: {
2840 uint8_t reference = instruction.VRegA_21c();
2841 uint16_t type_index = instruction.VRegB_21c();
Calin Juravle98893e12015-10-02 21:05:03 +01002842 BuildTypeCheck(instruction, -1, reference, type_index, dex_pc);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002843 break;
2844 }
2845
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002846 case Instruction::MONITOR_ENTER: {
2847 current_block_->AddInstruction(new (arena_) HMonitorOperation(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002848 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot, dex_pc),
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002849 HMonitorOperation::kEnter,
Calin Juravle225ff812014-11-13 16:46:39 +00002850 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002851 break;
2852 }
2853
2854 case Instruction::MONITOR_EXIT: {
2855 current_block_->AddInstruction(new (arena_) HMonitorOperation(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002856 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot, dex_pc),
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002857 HMonitorOperation::kExit,
Calin Juravle225ff812014-11-13 16:46:39 +00002858 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002859 break;
2860 }
2861
Andreas Gamped881df52014-11-24 23:28:39 -08002862 case Instruction::PACKED_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002863 BuildPackedSwitch(instruction, dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08002864 break;
2865 }
2866
Andreas Gampee4d4d322014-12-04 09:09:57 -08002867 case Instruction::SPARSE_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002868 BuildSparseSwitch(instruction, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08002869 break;
2870 }
2871
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002872 default:
Calin Juravle48c2b032014-12-09 18:11:36 +00002873 VLOG(compiler) << "Did not compile "
2874 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
2875 << " because of unhandled instruction "
2876 << instruction.Name();
2877 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnhandledInstruction);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002878 return false;
2879 }
2880 return true;
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00002881} // NOLINT(readability/fn_size)
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002882
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01002883HLocal* HGraphBuilder::GetLocalAt(uint32_t register_index) const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01002884 return locals_[register_index];
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002885}
2886
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01002887void HGraphBuilder::UpdateLocal(uint32_t register_index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002888 HInstruction* instruction,
2889 uint32_t dex_pc) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002890 HLocal* local = GetLocalAt(register_index);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002891 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction, dex_pc));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002892}
2893
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01002894HInstruction* HGraphBuilder::LoadLocal(uint32_t register_index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002895 Primitive::Type type,
2896 uint32_t dex_pc) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002897 HLocal* local = GetLocalAt(register_index);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002898 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type, dex_pc));
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002899 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002900}
2901
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002902} // namespace art