blob: 9d70124a4c02fdc3accf5519728003be36531c3e [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);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000143 locals_.SetSize(count);
144 for (int i = 0; i < count; i++) {
145 HLocal* local = new (arena_) HLocal(i);
146 entry_block_->AddInstruction(local);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000147 locals_.Put(i, local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000148 }
149}
150
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000151void HGraphBuilder::InitializeParameters(uint16_t number_of_parameters) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100152 // dex_compilation_unit_ is null only when unit testing.
153 if (dex_compilation_unit_ == nullptr) {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000154 return;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100155 }
156
157 graph_->SetNumberOfInVRegs(number_of_parameters);
158 const char* shorty = dex_compilation_unit_->GetShorty();
159 int locals_index = locals_.Size() - number_of_parameters;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100160 int parameter_index = 0;
161
162 if (!dex_compilation_unit_->IsStatic()) {
163 // Add the implicit 'this' argument, not expressed in the signature.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600164 HParameterValue* parameter = new (arena_) HParameterValue(parameter_index++,
165 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
173 uint32_t pos = 1;
174 for (int i = 0; i < number_of_parameters; i++) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600175 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 Brazdilbff75032015-07-08 17:26:51 +0000265static const DexFile::TryItem* GetTryItem(HBasicBlock* block,
266 const DexFile::CodeItem& code_item,
267 const ArenaBitVector& can_block_throw) {
268 DCHECK(!block->IsSingleTryBoundary());
269
270 // Block does not contain throwing instructions. Even if it is covered by
271 // a TryItem, we will consider it not in a try block.
272 if (!can_block_throw.IsBitSet(block->GetBlockId())) {
273 return nullptr;
274 }
275
276 // Instructions in the block may throw. Find a TryItem covering this block.
277 int32_t try_item_idx = DexFile::FindTryItem(code_item, block->GetDexPc());
David Brazdil6cd788f2015-07-08 16:44:00 +0100278 return (try_item_idx == -1) ? nullptr : DexFile::GetTryItems(code_item, try_item_idx);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000279}
280
281void HGraphBuilder::CreateBlocksForTryCatch(const DexFile::CodeItem& code_item) {
282 if (code_item.tries_size_ == 0) {
283 return;
284 }
285
286 // Create branch targets at the start/end of the TryItem range. These are
287 // places where the program might fall through into/out of the a block and
288 // where TryBoundary instructions will be inserted later. Other edges which
289 // enter/exit the try blocks are a result of branches/switches.
290 for (size_t idx = 0; idx < code_item.tries_size_; ++idx) {
291 const DexFile::TryItem* try_item = DexFile::GetTryItems(code_item, idx);
292 uint32_t dex_pc_start = try_item->start_addr_;
293 uint32_t dex_pc_end = dex_pc_start + try_item->insn_count_;
294 FindOrCreateBlockStartingAt(dex_pc_start);
295 if (dex_pc_end < code_item.insns_size_in_code_units_) {
296 // TODO: Do not create block if the last instruction cannot fall through.
297 FindOrCreateBlockStartingAt(dex_pc_end);
298 } else {
299 // The TryItem spans until the very end of the CodeItem (or beyond if
300 // invalid) and therefore cannot have any code afterwards.
301 }
302 }
303
304 // Create branch targets for exception handlers.
305 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(code_item, 0);
306 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
307 for (uint32_t idx = 0; idx < handlers_size; ++idx) {
308 CatchHandlerIterator iterator(handlers_ptr);
309 for (; iterator.HasNext(); iterator.Next()) {
310 uint32_t address = iterator.GetHandlerAddress();
311 HBasicBlock* block = FindOrCreateBlockStartingAt(address);
David Brazdilec16f792015-08-19 15:04:01 +0100312 block->SetTryCatchInformation(
313 new (arena_) TryCatchInformation(iterator.GetHandlerTypeIndex(), *dex_file_));
David Brazdilfc6a86a2015-06-26 10:33:45 +0000314 }
315 handlers_ptr = iterator.EndDataPointer();
316 }
317}
318
David Brazdil56e1acc2015-06-30 15:41:36 +0100319void HGraphBuilder::SplitTryBoundaryEdge(HBasicBlock* predecessor,
320 HBasicBlock* successor,
321 HTryBoundary::BoundaryKind kind,
322 const DexFile::CodeItem& code_item,
323 const DexFile::TryItem& try_item) {
324 // Split the edge with a single TryBoundary instruction.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600325 HTryBoundary* try_boundary = new (arena_) HTryBoundary(kind, successor->GetDexPc());
David Brazdil56e1acc2015-06-30 15:41:36 +0100326 HBasicBlock* try_entry_block = graph_->SplitEdge(predecessor, successor);
327 try_entry_block->AddInstruction(try_boundary);
328
329 // Link the TryBoundary to the handlers of `try_item`.
330 for (CatchHandlerIterator it(code_item, try_item); it.HasNext(); it.Next()) {
331 try_boundary->AddExceptionHandler(FindBlockStartingAt(it.GetHandlerAddress()));
332 }
333}
334
David Brazdilfc6a86a2015-06-26 10:33:45 +0000335void HGraphBuilder::InsertTryBoundaryBlocks(const DexFile::CodeItem& code_item) {
336 if (code_item.tries_size_ == 0) {
337 return;
338 }
339
David Brazdil72783ff2015-07-09 14:36:05 +0100340 // Bit vector stores information on which blocks contain throwing instructions.
341 // Must be expandable because catch blocks may be split into two.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100342 ArenaBitVector can_block_throw(arena_, graph_->GetBlocks().size(), /* expandable */ true);
David Brazdilbff75032015-07-08 17:26:51 +0000343
344 // Scan blocks and mark those which contain throwing instructions.
Vladimir Markob7d8e8c2015-09-17 15:47:05 +0100345 // NOTE: We're appending new blocks inside the loop, so we need to use index because iterators
346 // can be invalidated. We remember the initial size to avoid iterating over the new blocks.
347 for (size_t block_id = 0u, end = graph_->GetBlocks().size(); block_id != end; ++block_id) {
348 HBasicBlock* block = graph_->GetBlocks()[block_id];
David Brazdil72783ff2015-07-09 14:36:05 +0100349 bool can_throw = false;
David Brazdilbff75032015-07-08 17:26:51 +0000350 for (HInstructionIterator insn(block->GetInstructions()); !insn.Done(); insn.Advance()) {
351 if (insn.Current()->CanThrow()) {
David Brazdil72783ff2015-07-09 14:36:05 +0100352 can_throw = true;
David Brazdilbff75032015-07-08 17:26:51 +0000353 break;
354 }
355 }
David Brazdil72783ff2015-07-09 14:36:05 +0100356
357 if (can_throw) {
358 if (block->IsCatchBlock()) {
359 // Catch blocks are always considered an entry point into the TryItem in
360 // order to avoid splitting exceptional edges. We split the block after
361 // the move-exception (if present) and mark the first part non-throwing.
362 // Later on, a TryBoundary will be inserted between the two blocks.
363 HInstruction* first_insn = block->GetFirstInstruction();
364 if (first_insn->IsLoadException()) {
365 // Catch block starts with a LoadException. Split the block after the
David Brazdilcb1c0552015-08-04 16:22:25 +0100366 // StoreLocal and ClearException which must come after the load.
David Brazdil72783ff2015-07-09 14:36:05 +0100367 DCHECK(first_insn->GetNext()->IsStoreLocal());
David Brazdilcb1c0552015-08-04 16:22:25 +0100368 DCHECK(first_insn->GetNext()->GetNext()->IsClearException());
369 block = block->SplitBefore(first_insn->GetNext()->GetNext()->GetNext());
David Brazdil72783ff2015-07-09 14:36:05 +0100370 } else {
371 // Catch block does not load the exception. Split at the beginning to
372 // create an empty catch block.
373 block = block->SplitBefore(first_insn);
374 }
375 }
376 can_block_throw.SetBit(block->GetBlockId());
377 }
David Brazdilbff75032015-07-08 17:26:51 +0000378 }
379
David Brazdil281a6322015-07-03 10:34:57 +0100380 // Iterate over all blocks, find those covered by some TryItem and:
381 // (a) split edges which enter/exit the try range,
382 // (b) create TryBoundary instructions in the new blocks,
383 // (c) link the new blocks to corresponding exception handlers.
384 // We cannot iterate only over blocks in `branch_targets_` because switch-case
385 // blocks share the same dex_pc.
Vladimir Markob7d8e8c2015-09-17 15:47:05 +0100386 // NOTE: We're appending new blocks inside the loop, so we need to use index because iterators
387 // can be invalidated. We remember the initial size to avoid iterating over the new blocks.
388 for (size_t block_id = 0u, end = graph_->GetBlocks().size(); block_id != end; ++block_id) {
389 HBasicBlock* try_block = graph_->GetBlocks()[block_id];
David Brazdil281a6322015-07-03 10:34:57 +0100390 // TryBoundary blocks are added at the end of the list and not iterated over.
391 DCHECK(!try_block->IsSingleTryBoundary());
David Brazdilfc6a86a2015-06-26 10:33:45 +0000392
David Brazdil281a6322015-07-03 10:34:57 +0100393 // Find the TryItem for this block.
David Brazdilbff75032015-07-08 17:26:51 +0000394 const DexFile::TryItem* try_item = GetTryItem(try_block, code_item, can_block_throw);
395 if (try_item == nullptr) {
David Brazdil281a6322015-07-03 10:34:57 +0100396 continue;
397 }
David Brazdil281a6322015-07-03 10:34:57 +0100398
David Brazdil72783ff2015-07-09 14:36:05 +0100399 // Catch blocks were split earlier and cannot throw.
400 DCHECK(!try_block->IsCatchBlock());
401
402 // Find predecessors which are not covered by the same TryItem range. Such
403 // edges enter the try block and will have a TryBoundary inserted.
Vladimir Marko60584552015-09-03 13:35:12 +0000404 for (size_t i = 0; i < try_block->GetPredecessors().size(); ++i) {
405 HBasicBlock* predecessor = try_block->GetPredecessor(i);
David Brazdil72783ff2015-07-09 14:36:05 +0100406 if (predecessor->IsSingleTryBoundary()) {
407 // The edge was already split because of an exit from a neighbouring
408 // TryItem. We split it again and insert an entry point.
409 if (kIsDebugBuild) {
410 HTryBoundary* last_insn = predecessor->GetLastInstruction()->AsTryBoundary();
411 const DexFile::TryItem* predecessor_try_item =
412 GetTryItem(predecessor->GetSinglePredecessor(), code_item, can_block_throw);
413 DCHECK(!last_insn->IsEntry());
414 DCHECK_EQ(last_insn->GetNormalFlowSuccessor(), try_block);
415 DCHECK(try_block->IsFirstIndexOfPredecessor(predecessor, i));
416 DCHECK_NE(try_item, predecessor_try_item);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000417 }
David Brazdil72783ff2015-07-09 14:36:05 +0100418 } else if (GetTryItem(predecessor, code_item, can_block_throw) != try_item) {
419 // This is an entry point into the TryItem and the edge has not been
420 // split yet. That means that `predecessor` is not in a TryItem, or
421 // it is in a different TryItem and we happened to iterate over this
422 // block first. We split the edge and insert an entry point.
423 } else {
424 // Not an edge on the boundary of the try block.
425 continue;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000426 }
David Brazdil72783ff2015-07-09 14:36:05 +0100427 SplitTryBoundaryEdge(predecessor, try_block, HTryBoundary::kEntry, code_item, *try_item);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000428 }
David Brazdil281a6322015-07-03 10:34:57 +0100429
430 // Find successors which are not covered by the same TryItem range. Such
431 // edges exit the try block and will have a TryBoundary inserted.
Vladimir Marko60584552015-09-03 13:35:12 +0000432 for (HBasicBlock* successor : try_block->GetSuccessors()) {
David Brazdil281a6322015-07-03 10:34:57 +0100433 if (successor->IsCatchBlock()) {
434 // A catch block is always considered an entry point into its TryItem.
435 // We therefore assume this is an exit point, regardless of whether
436 // the catch block is in a different TryItem or not.
437 } else if (successor->IsSingleTryBoundary()) {
438 // The edge was already split because of an entry into a neighbouring
439 // TryItem. We split it again and insert an exit.
440 if (kIsDebugBuild) {
441 HTryBoundary* last_insn = successor->GetLastInstruction()->AsTryBoundary();
David Brazdilbff75032015-07-08 17:26:51 +0000442 const DexFile::TryItem* successor_try_item =
443 GetTryItem(last_insn->GetNormalFlowSuccessor(), code_item, can_block_throw);
David Brazdil281a6322015-07-03 10:34:57 +0100444 DCHECK_EQ(try_block, successor->GetSinglePredecessor());
445 DCHECK(last_insn->IsEntry());
David Brazdilbff75032015-07-08 17:26:51 +0000446 DCHECK_NE(try_item, successor_try_item);
David Brazdil281a6322015-07-03 10:34:57 +0100447 }
David Brazdilbff75032015-07-08 17:26:51 +0000448 } else if (GetTryItem(successor, code_item, can_block_throw) != try_item) {
David Brazdil281a6322015-07-03 10:34:57 +0100449 // This is an exit out of the TryItem and the edge has not been split
450 // yet. That means that either `successor` is not in a TryItem, or it
451 // is in a different TryItem and we happened to iterate over this
452 // block first. We split the edge and insert an exit.
453 HInstruction* last_instruction = try_block->GetLastInstruction();
454 if (last_instruction->IsReturn() || last_instruction->IsReturnVoid()) {
455 DCHECK_EQ(successor, exit_block_);
456 // Control flow exits the try block with a Return(Void). Because
457 // splitting the edge would invalidate the invariant that Return
458 // always jumps to Exit, we move the Return outside the try block.
459 successor = try_block->SplitBefore(last_instruction);
460 }
461 } else {
462 // Not an edge on the boundary of the try block.
463 continue;
464 }
David Brazdilbff75032015-07-08 17:26:51 +0000465 SplitTryBoundaryEdge(try_block, successor, HTryBoundary::kExit, code_item, *try_item);
David Brazdil281a6322015-07-03 10:34:57 +0100466 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000467 }
468}
469
David Brazdil5e8b1372015-01-23 14:39:08 +0000470bool HGraphBuilder::BuildGraph(const DexFile::CodeItem& code_item) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100471 DCHECK(graph_->GetBlocks().empty());
David Brazdil5e8b1372015-01-23 14:39:08 +0000472
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000473 const uint16_t* code_ptr = code_item.insns_;
474 const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100475 code_start_ = code_ptr;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000476
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000477 // Setup the graph with the entry block and exit block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100478 entry_block_ = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000479 graph_->AddBlock(entry_block_);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100480 exit_block_ = new (arena_) HBasicBlock(graph_, kNoDexPc);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000481 graph_->SetEntryBlock(entry_block_);
482 graph_->SetExitBlock(exit_block_);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000483
David Brazdil77a48ae2015-09-15 12:34:04 +0000484 graph_->SetHasTryCatch(code_item.tries_size_ != 0);
485
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000486 InitializeLocals(code_item.registers_size_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000487 graph_->SetMaximumNumberOfOutVRegs(code_item.outs_size_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000488
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000489 // Compute the number of dex instructions, blocks, and branches. We will
490 // check these values against limits given to the compiler.
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000491 size_t number_of_branches = 0;
492
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000493 // To avoid splitting blocks, we compute ahead of time the instructions that
494 // start a new block, and create these blocks.
Calin Juravle702d2602015-04-30 19:28:21 +0100495 if (!ComputeBranchTargets(code_ptr, code_end, &number_of_branches)) {
496 MaybeRecordStat(MethodCompilationStat::kNotCompiledBranchOutsideMethodCode);
497 return false;
498 }
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000499
500 // Note that the compiler driver is null when unit testing.
David Brazdil1b498722015-03-31 11:37:18 +0100501 if ((compiler_driver_ != nullptr) && SkipCompilation(code_item, number_of_branches)) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000502 return false;
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000503 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000504
David Brazdilfc6a86a2015-06-26 10:33:45 +0000505 CreateBlocksForTryCatch(code_item);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000506
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000507 InitializeParameters(code_item.ins_size_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100508
Calin Juravle225ff812014-11-13 16:46:39 +0000509 size_t dex_pc = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000510 while (code_ptr < code_end) {
Calin Juravle225ff812014-11-13 16:46:39 +0000511 // Update the current block if dex_pc starts a new block.
512 MaybeUpdateCurrentBlock(dex_pc);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000513 const Instruction& instruction = *Instruction::At(code_ptr);
Calin Juravle48c2b032014-12-09 18:11:36 +0000514 if (!AnalyzeDexInstruction(instruction, dex_pc)) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000515 return false;
Calin Juravle48c2b032014-12-09 18:11:36 +0000516 }
Calin Juravle225ff812014-11-13 16:46:39 +0000517 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000518 code_ptr += instruction.SizeInCodeUnits();
519 }
520
David Brazdilfc6a86a2015-06-26 10:33:45 +0000521 // Add Exit to the exit block.
David Brazdil3e187382015-06-26 09:59:52 +0000522 exit_block_->AddInstruction(new (arena_) HExit());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000523 // Add the suspend check to the entry block.
524 entry_block_->AddInstruction(new (arena_) HSuspendCheck(0));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000525 entry_block_->AddInstruction(new (arena_) HGoto());
David Brazdilbff75032015-07-08 17:26:51 +0000526 // Add the exit block at the end.
527 graph_->AddBlock(exit_block_);
David Brazdil5e8b1372015-01-23 14:39:08 +0000528
David Brazdilfc6a86a2015-06-26 10:33:45 +0000529 // Iterate over blocks covered by TryItems and insert TryBoundaries at entry
530 // and exit points. This requires all control-flow instructions and
531 // non-exceptional edges to have been created.
532 InsertTryBoundaryBlocks(code_item);
533
David Brazdil5e8b1372015-01-23 14:39:08 +0000534 return true;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000535}
536
David Brazdilfc6a86a2015-06-26 10:33:45 +0000537void HGraphBuilder::MaybeUpdateCurrentBlock(size_t dex_pc) {
538 HBasicBlock* block = FindBlockStartingAt(dex_pc);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000539 if (block == nullptr) {
540 return;
541 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000542
543 if (current_block_ != nullptr) {
544 // Branching instructions clear current_block, so we know
545 // the last instruction of the current block is not a branching
546 // instruction. We add an unconditional goto to the found block.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600547 current_block_->AddInstruction(new (arena_) HGoto(dex_pc));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000548 current_block_->AddSuccessor(block);
549 }
550 graph_->AddBlock(block);
551 current_block_ = block;
552}
553
Calin Juravle702d2602015-04-30 19:28:21 +0100554bool HGraphBuilder::ComputeBranchTargets(const uint16_t* code_ptr,
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000555 const uint16_t* code_end,
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000556 size_t* number_of_branches) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000557 branch_targets_.SetSize(code_end - code_ptr);
558
559 // Create the first block for the dex instructions, single successor of the entry block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100560 HBasicBlock* block = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000561 branch_targets_.Put(0, block);
562 entry_block_->AddSuccessor(block);
563
564 // Iterate over all instructions and find branching instructions. Create blocks for
565 // the locations these instructions branch to.
Andreas Gamped881df52014-11-24 23:28:39 -0800566 uint32_t dex_pc = 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000567 while (code_ptr < code_end) {
568 const Instruction& instruction = *Instruction::At(code_ptr);
569 if (instruction.IsBranch()) {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000570 (*number_of_branches)++;
Calin Juravle225ff812014-11-13 16:46:39 +0000571 int32_t target = instruction.GetTargetOffset() + dex_pc;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000572 // Create a block for the target instruction.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000573 FindOrCreateBlockStartingAt(target);
574
Calin Juravle225ff812014-11-13 16:46:39 +0000575 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000576 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle702d2602015-04-30 19:28:21 +0100577
David Brazdilfe659462015-06-24 14:23:56 +0100578 if (instruction.CanFlowThrough()) {
579 if (code_ptr >= code_end) {
Calin Juravle702d2602015-04-30 19:28:21 +0100580 // In the normal case we should never hit this but someone can artificially forge a dex
581 // file to fall-through out the method code. In this case we bail out compilation.
582 return false;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000583 } else {
584 FindOrCreateBlockStartingAt(dex_pc);
Calin Juravle702d2602015-04-30 19:28:21 +0100585 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000586 }
Andreas Gampee4d4d322014-12-04 09:09:57 -0800587 } else if (instruction.IsSwitch()) {
588 SwitchTable table(instruction, dex_pc, instruction.Opcode() == Instruction::SPARSE_SWITCH);
Andreas Gamped881df52014-11-24 23:28:39 -0800589
590 uint16_t num_entries = table.GetNumEntries();
591
Andreas Gampee4d4d322014-12-04 09:09:57 -0800592 // In a packed-switch, the entry at index 0 is the starting key. In a sparse-switch, the
593 // entry at index 0 is the first key, and values are after *all* keys.
594 size_t offset = table.GetFirstValueIndex();
595
596 // Use a larger loop counter type to avoid overflow issues.
597 for (size_t i = 0; i < num_entries; ++i) {
Andreas Gamped881df52014-11-24 23:28:39 -0800598 // The target of the case.
Andreas Gampee4d4d322014-12-04 09:09:57 -0800599 uint32_t target = dex_pc + table.GetEntryAt(i + offset);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000600 FindOrCreateBlockStartingAt(target);
Andreas Gamped881df52014-11-24 23:28:39 -0800601
David Brazdil281a6322015-07-03 10:34:57 +0100602 // Create a block for the switch-case logic. The block gets the dex_pc
603 // of the SWITCH instruction because it is part of its semantics.
604 block = new (arena_) HBasicBlock(graph_, dex_pc);
605 branch_targets_.Put(table.GetDexPcForIndex(i), block);
Andreas Gamped881df52014-11-24 23:28:39 -0800606 }
607
608 // Fall-through. Add a block if there is more code afterwards.
609 dex_pc += instruction.SizeInCodeUnits();
610 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle702d2602015-04-30 19:28:21 +0100611 if (code_ptr >= code_end) {
612 // In the normal case we should never hit this but someone can artificially forge a dex
613 // file to fall-through out the method code. In this case we bail out compilation.
614 // (A switch can fall-through so we don't need to check CanFlowThrough().)
615 return false;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000616 } else {
617 FindOrCreateBlockStartingAt(dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -0800618 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000619 } else {
620 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle225ff812014-11-13 16:46:39 +0000621 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000622 }
623 }
Calin Juravle702d2602015-04-30 19:28:21 +0100624 return true;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000625}
626
David Brazdilfc6a86a2015-06-26 10:33:45 +0000627HBasicBlock* HGraphBuilder::FindBlockStartingAt(int32_t dex_pc) const {
628 DCHECK_GE(dex_pc, 0);
629 DCHECK_LT(static_cast<size_t>(dex_pc), branch_targets_.Size());
630 return branch_targets_.Get(dex_pc);
631}
632
633HBasicBlock* HGraphBuilder::FindOrCreateBlockStartingAt(int32_t dex_pc) {
634 HBasicBlock* block = FindBlockStartingAt(dex_pc);
635 if (block == nullptr) {
636 block = new (arena_) HBasicBlock(graph_, dex_pc);
637 branch_targets_.Put(dex_pc, block);
638 }
639 return block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000640}
641
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100642template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600643void HGraphBuilder::Unop_12x(const Instruction& instruction,
644 Primitive::Type type,
645 uint32_t dex_pc) {
646 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
647 current_block_->AddInstruction(new (arena_) T(type, first, dex_pc));
648 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Roland Levillain88cb1752014-10-20 16:36:47 +0100649}
650
Roland Levillaindff1f282014-11-05 14:15:05 +0000651void HGraphBuilder::Conversion_12x(const Instruction& instruction,
652 Primitive::Type input_type,
Roland Levillain624279f2014-12-04 11:54:28 +0000653 Primitive::Type result_type,
654 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600655 HInstruction* first = LoadLocal(instruction.VRegB(), input_type, dex_pc);
Roland Levillain624279f2014-12-04 11:54:28 +0000656 current_block_->AddInstruction(new (arena_) HTypeConversion(result_type, first, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600657 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100658}
659
660template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000661void HGraphBuilder::Binop_23x(const Instruction& instruction,
662 Primitive::Type type,
663 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600664 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
665 HInstruction* second = LoadLocal(instruction.VRegC(), type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000666 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600667 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000668}
669
670template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000671void HGraphBuilder::Binop_23x_shift(const Instruction& instruction,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600672 Primitive::Type type,
673 uint32_t dex_pc) {
674 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
675 HInstruction* second = LoadLocal(instruction.VRegC(), Primitive::kPrimInt, dex_pc);
676 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
677 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000678}
679
Calin Juravleddb7df22014-11-25 20:56:51 +0000680void HGraphBuilder::Binop_23x_cmp(const Instruction& instruction,
681 Primitive::Type type,
Mark Mendellc4701932015-04-10 13:18:51 -0400682 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700683 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600684 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
685 HInstruction* second = LoadLocal(instruction.VRegC(), type, dex_pc);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700686 current_block_->AddInstruction(new (arena_) HCompare(type, first, second, bias, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600687 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +0000688}
689
Calin Juravle9aec02f2014-11-18 23:06:35 +0000690template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600691void HGraphBuilder::Binop_12x_shift(const Instruction& instruction, Primitive::Type type,
692 uint32_t dex_pc) {
693 HInstruction* first = LoadLocal(instruction.VRegA(), type, dex_pc);
694 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
695 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
696 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000697}
698
699template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000700void HGraphBuilder::Binop_12x(const Instruction& instruction,
701 Primitive::Type type,
702 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600703 HInstruction* first = LoadLocal(instruction.VRegA(), type, dex_pc);
704 HInstruction* second = LoadLocal(instruction.VRegB(), type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000705 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600706 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000707}
708
709template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600710void HGraphBuilder::Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
711 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
712 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100713 if (reverse) {
714 std::swap(first, second);
715 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600716 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second, dex_pc));
717 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100718}
719
720template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600721void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
722 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
723 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100724 if (reverse) {
725 std::swap(first, second);
726 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600727 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second, dex_pc));
728 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100729}
730
Calin Juravle0c25d102015-04-20 14:49:09 +0100731static bool RequiresConstructorBarrier(const DexCompilationUnit* cu, const CompilerDriver& driver) {
Calin Juravle27df7582015-04-17 19:12:31 +0100732 Thread* self = Thread::Current();
Calin Juravle0c25d102015-04-20 14:49:09 +0100733 return cu->IsConstructor()
734 && driver.RequiresConstructorBarrier(self, cu->GetDexFile(), cu->GetClassDefIndex());
Calin Juravle27df7582015-04-17 19:12:31 +0100735}
736
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600737void HGraphBuilder::BuildReturn(const Instruction& instruction,
738 Primitive::Type type,
739 uint32_t dex_pc) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100740 if (type == Primitive::kPrimVoid) {
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100741 if (graph_->ShouldGenerateConstructorBarrier()) {
742 // The compilation unit is null during testing.
743 if (dex_compilation_unit_ != nullptr) {
744 DCHECK(RequiresConstructorBarrier(dex_compilation_unit_, *compiler_driver_))
745 << "Inconsistent use of ShouldGenerateConstructorBarrier. Should not generate a barrier.";
746 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600747 current_block_->AddInstruction(new (arena_) HMemoryBarrier(kStoreStore, dex_pc));
Calin Juravle27df7582015-04-17 19:12:31 +0100748 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600749 current_block_->AddInstruction(new (arena_) HReturnVoid(dex_pc));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100750 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600751 HInstruction* value = LoadLocal(instruction.VRegA(), type, dex_pc);
752 current_block_->AddInstruction(new (arena_) HReturn(value, dex_pc));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100753 }
754 current_block_->AddSuccessor(exit_block_);
755 current_block_ = nullptr;
756}
757
Calin Juravle68ad6492015-08-18 17:08:12 +0100758static InvokeType GetInvokeTypeFromOpCode(Instruction::Code opcode) {
759 switch (opcode) {
760 case Instruction::INVOKE_STATIC:
761 case Instruction::INVOKE_STATIC_RANGE:
762 return kStatic;
763 case Instruction::INVOKE_DIRECT:
764 case Instruction::INVOKE_DIRECT_RANGE:
765 return kDirect;
766 case Instruction::INVOKE_VIRTUAL:
767 case Instruction::INVOKE_VIRTUAL_QUICK:
768 case Instruction::INVOKE_VIRTUAL_RANGE:
769 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
770 return kVirtual;
771 case Instruction::INVOKE_INTERFACE:
772 case Instruction::INVOKE_INTERFACE_RANGE:
773 return kInterface;
774 case Instruction::INVOKE_SUPER_RANGE:
775 case Instruction::INVOKE_SUPER:
776 return kSuper;
777 default:
778 LOG(FATAL) << "Unexpected invoke opcode: " << opcode;
779 UNREACHABLE();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100780 }
Calin Juravle68ad6492015-08-18 17:08:12 +0100781}
782
783bool HGraphBuilder::BuildInvoke(const Instruction& instruction,
784 uint32_t dex_pc,
785 uint32_t method_idx,
786 uint32_t number_of_vreg_arguments,
787 bool is_range,
788 uint32_t* args,
789 uint32_t register_index) {
790 InvokeType original_invoke_type = GetInvokeTypeFromOpCode(instruction.Opcode());
791 InvokeType optimized_invoke_type = original_invoke_type;
792 const char* descriptor = dex_file_->GetMethodShorty(method_idx);
793 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
794
795 // Remove the return type from the 'proto'.
796 size_t number_of_arguments = strlen(descriptor) - 1;
797 if (original_invoke_type != kStatic) { // instance call
798 // One extra argument for 'this'.
799 number_of_arguments++;
800 }
801
802 MethodReference target_method(dex_file_, method_idx);
Calin Juravle5d01db12015-08-25 15:02:42 +0100803 int32_t table_index = 0;
804 uintptr_t direct_code = 0;
805 uintptr_t direct_method = 0;
Calin Juravle68ad6492015-08-18 17:08:12 +0100806
Calin Juravle5d01db12015-08-25 15:02:42 +0100807 // Special handling for string init.
808 int32_t string_init_offset = 0;
809 bool is_string_init = compiler_driver_->IsStringInit(method_idx,
810 dex_file_,
811 &string_init_offset);
812 // Replace calls to String.<init> with StringFactory.
813 if (is_string_init) {
814 HInvokeStaticOrDirect::DispatchInfo dispatch_info = ComputeDispatchInfo(is_string_init,
815 string_init_offset,
816 target_method,
817 direct_method,
818 direct_code);
819 HInvoke* invoke = new (arena_) HInvokeStaticOrDirect(
820 arena_,
821 number_of_arguments - 1,
822 Primitive::kPrimNot /*return_type */,
823 dex_pc,
824 method_idx,
825 target_method,
826 dispatch_info,
827 original_invoke_type,
828 kStatic /* optimized_invoke_type */,
829 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit);
830 return HandleStringInit(invoke,
831 number_of_vreg_arguments,
832 args,
833 register_index,
834 is_range,
835 descriptor);
836 }
837
838 // Handle unresolved methods.
Calin Juravle68ad6492015-08-18 17:08:12 +0100839 if (!compiler_driver_->ComputeInvokeInfo(dex_compilation_unit_,
840 dex_pc,
841 true /* update_stats */,
842 true /* enable_devirtualization */,
843 &optimized_invoke_type,
844 &target_method,
845 &table_index,
846 &direct_code,
847 &direct_method)) {
Calin Juravle175dc732015-08-25 15:42:32 +0100848 MaybeRecordStat(MethodCompilationStat::kUnresolvedMethod);
849 HInvoke* invoke = new (arena_) HInvokeUnresolved(arena_,
850 number_of_arguments,
851 return_type,
852 dex_pc,
853 method_idx,
854 original_invoke_type);
855 return HandleInvoke(invoke,
856 number_of_vreg_arguments,
857 args,
858 register_index,
859 is_range,
860 descriptor,
861 nullptr /* clinit_check */);
Calin Juravle68ad6492015-08-18 17:08:12 +0100862 }
863
Calin Juravle5d01db12015-08-25 15:02:42 +0100864 // Handle resolved methods (non string init).
Calin Juravle68ad6492015-08-18 17:08:12 +0100865
Calin Juravle5d01db12015-08-25 15:02:42 +0100866 DCHECK(optimized_invoke_type != kSuper);
Calin Juravle68ad6492015-08-18 17:08:12 +0100867
868 // Potential class initialization check, in the case of a static method call.
869 HClinitCheck* clinit_check = nullptr;
870 HInvoke* invoke = nullptr;
871
Calin Juravle5d01db12015-08-25 15:02:42 +0100872 if (optimized_invoke_type == kDirect || optimized_invoke_type == kStatic) {
Calin Juravle68ad6492015-08-18 17:08:12 +0100873 // By default, consider that the called method implicitly requires
874 // an initialization check of its declaring method.
875 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement
876 = HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
Calin Juravle5d01db12015-08-25 15:02:42 +0100877 if (optimized_invoke_type == kStatic) {
Calin Juravle68ad6492015-08-18 17:08:12 +0100878 clinit_check = ProcessClinitCheckForInvoke(dex_pc, method_idx, &clinit_check_requirement);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100879 }
Calin Juravle68ad6492015-08-18 17:08:12 +0100880
Calin Juravle68ad6492015-08-18 17:08:12 +0100881 HInvokeStaticOrDirect::DispatchInfo dispatch_info = ComputeDispatchInfo(is_string_init,
882 string_init_offset,
883 target_method,
884 direct_method,
885 direct_code);
886 invoke = new (arena_) HInvokeStaticOrDirect(arena_,
887 number_of_arguments,
888 return_type,
889 dex_pc,
890 method_idx,
891 target_method,
892 dispatch_info,
893 original_invoke_type,
894 optimized_invoke_type,
895 clinit_check_requirement);
896 } else if (optimized_invoke_type == kVirtual) {
897 invoke = new (arena_) HInvokeVirtual(arena_,
898 number_of_arguments,
899 return_type,
900 dex_pc,
901 method_idx,
902 table_index);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100903 } else {
Calin Juravle68ad6492015-08-18 17:08:12 +0100904 DCHECK_EQ(optimized_invoke_type, kInterface);
905 invoke = new (arena_) HInvokeInterface(arena_,
906 number_of_arguments,
907 return_type,
908 dex_pc,
909 method_idx,
910 table_index);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100911 }
Calin Juravle68ad6492015-08-18 17:08:12 +0100912
Calin Juravle5d01db12015-08-25 15:02:42 +0100913 return HandleInvoke(invoke,
914 number_of_vreg_arguments,
915 args,
916 register_index,
917 is_range,
918 descriptor,
919 clinit_check);
Calin Juravle68ad6492015-08-18 17:08:12 +0100920}
921
922HClinitCheck* HGraphBuilder::ProcessClinitCheckForInvoke(
923 uint32_t dex_pc,
924 uint32_t method_idx,
925 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
926 ScopedObjectAccess soa(Thread::Current());
927 StackHandleScope<4> hs(soa.Self());
928 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
929 dex_compilation_unit_->GetClassLinker()->FindDexCache(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700930 soa.Self(), *dex_compilation_unit_->GetDexFile())));
Calin Juravle68ad6492015-08-18 17:08:12 +0100931 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
932 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
933 ArtMethod* resolved_method = compiler_driver_->ResolveMethod(
934 soa, dex_cache, class_loader, dex_compilation_unit_, method_idx, InvokeType::kStatic);
935
936 DCHECK(resolved_method != nullptr);
937
938 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
939 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700940 outer_compilation_unit_->GetClassLinker()->FindDexCache(soa.Self(), outer_dex_file)));
Calin Juravle68ad6492015-08-18 17:08:12 +0100941 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
942
943 // The index at which the method's class is stored in the DexCache's type array.
944 uint32_t storage_index = DexFile::kDexNoIndex;
945 bool is_outer_class = (resolved_method->GetDeclaringClass() == outer_class.Get());
946 if (is_outer_class) {
947 storage_index = outer_class->GetDexTypeIndex();
948 } else if (outer_dex_cache.Get() == dex_cache.Get()) {
949 // Get `storage_index` from IsClassOfStaticMethodAvailableToReferrer.
950 compiler_driver_->IsClassOfStaticMethodAvailableToReferrer(outer_dex_cache.Get(),
951 GetCompilingClass(),
952 resolved_method,
953 method_idx,
954 &storage_index);
955 }
956
957 HClinitCheck* clinit_check = nullptr;
958
959 if (!outer_class->IsInterface()
960 && outer_class->IsSubClass(resolved_method->GetDeclaringClass())) {
961 // If the outer class is the declaring class or a subclass
962 // of the declaring class, no class initialization is needed
963 // before the static method call.
964 // Note that in case of inlining, we do not need to add clinit checks
965 // to calls that satisfy this subclass check with any inlined methods. This
966 // will be detected by the optimization passes.
967 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
968 } else if (storage_index != DexFile::kDexNoIndex) {
969 // If the method's class type index is available, check
970 // whether we should add an explicit class initialization
971 // check for its declaring class before the static method call.
972
973 // TODO: find out why this check is needed.
974 bool is_in_dex_cache = compiler_driver_->CanAssumeTypeIsPresentInDexCache(
975 *outer_compilation_unit_->GetDexFile(), storage_index);
976 bool is_initialized =
977 resolved_method->GetDeclaringClass()->IsInitialized() && is_in_dex_cache;
978
979 if (is_initialized) {
980 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
981 } else {
982 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
983 HLoadClass* load_class = new (arena_) HLoadClass(
984 graph_->GetCurrentMethod(),
985 storage_index,
986 *dex_compilation_unit_->GetDexFile(),
987 is_outer_class,
988 dex_pc);
989 current_block_->AddInstruction(load_class);
990 clinit_check = new (arena_) HClinitCheck(load_class, dex_pc);
991 current_block_->AddInstruction(clinit_check);
992 }
993 }
994 return clinit_check;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100995}
996
Vladimir Marko58155012015-08-19 12:49:41 +0000997HInvokeStaticOrDirect::DispatchInfo HGraphBuilder::ComputeDispatchInfo(
998 bool is_string_init,
999 int32_t string_init_offset,
1000 MethodReference target_method,
1001 uintptr_t direct_method,
1002 uintptr_t direct_code) {
1003 HInvokeStaticOrDirect::MethodLoadKind method_load_kind;
1004 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location;
1005 uint64_t method_load_data = 0u;
1006 uint64_t direct_code_ptr = 0u;
1007
1008 if (is_string_init) {
1009 // TODO: Use direct_method and direct_code for the appropriate StringFactory method.
1010 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kStringInit;
1011 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
1012 method_load_data = string_init_offset;
1013 } else if (target_method.dex_file == outer_compilation_unit_->GetDexFile() &&
1014 target_method.dex_method_index == outer_compilation_unit_->GetDexMethodIndex()) {
1015 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kRecursive;
1016 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallSelf;
1017 } else {
1018 if (direct_method != 0u) { // Should we use a direct pointer to the method?
1019 if (direct_method != static_cast<uintptr_t>(-1)) { // Is the method pointer known now?
1020 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress;
1021 method_load_data = direct_method;
1022 } else { // The direct pointer will be known at link time.
1023 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup;
1024 }
1025 } else { // Use dex cache.
1026 DCHECK(target_method.dex_file == dex_compilation_unit_->GetDexFile());
1027 DexCacheArraysLayout layout =
1028 compiler_driver_->GetDexCacheArraysLayout(target_method.dex_file);
1029 if (layout.Valid()) { // Can we use PC-relative access to the dex cache arrays?
1030 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative;
1031 method_load_data = layout.MethodOffset(target_method.dex_method_index);
1032 } else { // We must go through the ArtMethod's pointer to resolved methods.
1033 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
1034 }
1035 }
1036 if (direct_code != 0u) { // Should we use a direct pointer to the code?
1037 if (direct_code != static_cast<uintptr_t>(-1)) { // Is the code pointer known now?
1038 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallDirect;
1039 direct_code_ptr = direct_code;
1040 } else if (compiler_driver_->IsImage() ||
1041 target_method.dex_file == dex_compilation_unit_->GetDexFile()) {
1042 // Use PC-relative calls for invokes within a multi-dex oat file.
1043 // TODO: Recognize when the target dex file is within the current oat file for
1044 // app compilation. At the moment we recognize only the boot image as multi-dex.
1045 // NOTE: This will require changing the ARM backend which currently falls
1046 // through from kCallPCRelative to kDirectCodeFixup for different dex files.
1047 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative;
1048 } else { // The direct pointer will be known at link time.
1049 // NOTE: This is used for app->boot calls when compiling an app against
1050 // a relocatable but not yet relocated image.
1051 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup;
1052 }
1053 } else { // We must use the code pointer from the ArtMethod.
1054 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
1055 }
1056 }
1057
1058 if (graph_->IsDebuggable()) {
1059 // For debuggable apps always use the code pointer from ArtMethod
1060 // so that we don't circumvent instrumentation stubs if installed.
1061 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
1062 }
1063
1064 return HInvokeStaticOrDirect::DispatchInfo {
1065 method_load_kind, code_ptr_location, method_load_data, direct_code_ptr };
1066}
1067
Calin Juravle5d01db12015-08-25 15:02:42 +01001068bool HGraphBuilder::SetupInvokeArguments(HInvoke* invoke,
1069 uint32_t number_of_vreg_arguments,
1070 uint32_t* args,
1071 uint32_t register_index,
1072 bool is_range,
1073 const char* descriptor,
1074 size_t start_index,
1075 size_t* argument_index) {
Calin Juravle68ad6492015-08-18 17:08:12 +01001076 uint32_t descriptor_index = 1; // Skip the return type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001077 uint32_t dex_pc = invoke->GetDexPc();
Calin Juravle68ad6492015-08-18 17:08:12 +01001078
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001079 for (size_t i = start_index;
1080 // Make sure we don't go over the expected arguments or over the number of
1081 // dex registers given. If the instruction was seen as dead by the verifier,
1082 // it hasn't been properly checked.
Calin Juravle5d01db12015-08-25 15:02:42 +01001083 (i < number_of_vreg_arguments) && (*argument_index < invoke->GetNumberOfArguments());
1084 i++, (*argument_index)++) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001085 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001086 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001087 if (!is_range
1088 && is_wide
1089 && ((i + 1 == number_of_vreg_arguments) || (args[i] + 1 != args[i + 1]))) {
1090 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
1091 // reject any class where this is violated. However, the verifier only does these checks
1092 // on non trivially dead instructions, so we just bailout the compilation.
1093 VLOG(compiler) << "Did not compile "
1094 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
1095 << " because of non-sequential dex register pair in wide argument";
1096 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1097 return false;
1098 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001099 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type, dex_pc);
Calin Juravle5d01db12015-08-25 15:02:42 +01001100 invoke->SetArgumentAt(*argument_index, arg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001101 if (is_wide) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001102 i++;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001103 }
1104 }
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001105
Calin Juravle5d01db12015-08-25 15:02:42 +01001106 if (*argument_index != invoke->GetNumberOfArguments()) {
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001107 VLOG(compiler) << "Did not compile "
1108 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
1109 << " because of wrong number of arguments in invoke instruction";
1110 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1111 return false;
1112 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001113
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001114 if (invoke->IsInvokeStaticOrDirect()) {
Calin Juravle5d01db12015-08-25 15:02:42 +01001115 invoke->SetArgumentAt(*argument_index, graph_->GetCurrentMethod());
1116 (*argument_index)++;
1117 }
1118
1119 return true;
1120}
1121
1122bool HGraphBuilder::HandleInvoke(HInvoke* invoke,
1123 uint32_t number_of_vreg_arguments,
1124 uint32_t* args,
1125 uint32_t register_index,
1126 bool is_range,
1127 const char* descriptor,
1128 HClinitCheck* clinit_check) {
1129 DCHECK(!invoke->IsInvokeStaticOrDirect() || !invoke->AsInvokeStaticOrDirect()->IsStringInit());
1130
1131 size_t start_index = 0;
1132 size_t argument_index = 0;
1133 if (invoke->GetOriginalInvokeType() != InvokeType::kStatic) { // Instance call.
1134 Temporaries temps(graph_);
1135 HInstruction* arg = LoadLocal(
1136 is_range ? register_index : args[0], Primitive::kPrimNot, invoke->GetDexPc());
1137 HNullCheck* null_check = new (arena_) HNullCheck(arg, invoke->GetDexPc());
1138 current_block_->AddInstruction(null_check);
1139 temps.Add(null_check);
1140 invoke->SetArgumentAt(0, null_check);
1141 start_index = 1;
1142 argument_index = 1;
1143 }
1144
1145 if (!SetupInvokeArguments(invoke,
1146 number_of_vreg_arguments,
1147 args,
1148 register_index,
1149 is_range,
1150 descriptor,
1151 start_index,
1152 &argument_index)) {
1153 return false;
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001154 }
1155
Calin Juravle68ad6492015-08-18 17:08:12 +01001156 if (clinit_check != nullptr) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001157 // Add the class initialization check as last input of `invoke`.
Calin Juravle68ad6492015-08-18 17:08:12 +01001158 DCHECK(invoke->IsInvokeStaticOrDirect());
1159 DCHECK(invoke->AsInvokeStaticOrDirect()->GetClinitCheckRequirement()
1160 == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit);
Roland Levillain3e3d7332015-04-28 11:00:54 +01001161 invoke->SetArgumentAt(argument_index, clinit_check);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001162 argument_index++;
Roland Levillain4c0eb422015-04-24 16:43:49 +01001163 }
1164
Calin Juravle5d01db12015-08-25 15:02:42 +01001165 current_block_->AddInstruction(invoke);
1166 latest_result_ = invoke;
1167
1168 return true;
1169}
1170
1171bool HGraphBuilder::HandleStringInit(HInvoke* invoke,
1172 uint32_t number_of_vreg_arguments,
1173 uint32_t* args,
1174 uint32_t register_index,
1175 bool is_range,
1176 const char* descriptor) {
1177 DCHECK(invoke->IsInvokeStaticOrDirect());
1178 DCHECK(invoke->AsInvokeStaticOrDirect()->IsStringInit());
1179
1180 size_t start_index = 1;
1181 size_t argument_index = 0;
1182 if (!SetupInvokeArguments(invoke,
1183 number_of_vreg_arguments,
1184 args,
1185 register_index,
1186 is_range,
1187 descriptor,
1188 start_index,
1189 &argument_index)) {
1190 return false;
Jeff Hao848f70a2014-01-15 13:49:50 -08001191 }
Calin Juravle0eedd7e2015-08-20 14:48:00 +01001192
Calin Juravle5d01db12015-08-25 15:02:42 +01001193 // Add move-result for StringFactory method.
1194 uint32_t orig_this_reg = is_range ? register_index : args[0];
1195 HInstruction* fake_string = LoadLocal(orig_this_reg, Primitive::kPrimNot, invoke->GetDexPc());
1196 invoke->SetArgumentAt(argument_index, fake_string);
1197 current_block_->AddInstruction(invoke);
1198 PotentiallySimplifyFakeString(orig_this_reg, invoke->GetDexPc(), invoke);
1199
Calin Juravle0eedd7e2015-08-20 14:48:00 +01001200 latest_result_ = invoke;
1201
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001202 return true;
1203}
1204
Calin Juravle68ad6492015-08-18 17:08:12 +01001205void HGraphBuilder::PotentiallySimplifyFakeString(uint16_t original_dex_register,
1206 uint32_t dex_pc,
1207 HInvoke* actual_string) {
1208 if (!graph_->IsDebuggable()) {
1209 // Notify that we cannot compile with baseline. The dex registers aliasing
1210 // with `original_dex_register` will be handled when we optimize
1211 // (see HInstructionSimplifer::VisitFakeString).
1212 can_use_baseline_for_string_init_ = false;
1213 return;
1214 }
1215 const VerifiedMethod* verified_method =
1216 compiler_driver_->GetVerifiedMethod(dex_file_, dex_compilation_unit_->GetDexMethodIndex());
1217 if (verified_method != nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001218 UpdateLocal(original_dex_register, actual_string, dex_pc);
Calin Juravle68ad6492015-08-18 17:08:12 +01001219 const SafeMap<uint32_t, std::set<uint32_t>>& string_init_map =
1220 verified_method->GetStringInitPcRegMap();
1221 auto map_it = string_init_map.find(dex_pc);
1222 if (map_it != string_init_map.end()) {
1223 std::set<uint32_t> reg_set = map_it->second;
1224 for (auto set_it = reg_set.begin(); set_it != reg_set.end(); ++set_it) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001225 HInstruction* load_local = LoadLocal(original_dex_register, Primitive::kPrimNot, dex_pc);
1226 UpdateLocal(*set_it, load_local, dex_pc);
Calin Juravle68ad6492015-08-18 17:08:12 +01001227 }
1228 }
1229 } else {
1230 can_use_baseline_for_string_init_ = false;
1231 }
1232}
1233
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001234bool HGraphBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001235 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001236 bool is_put) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001237 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1238 uint32_t obj_reg = instruction.VRegB_22c();
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001239 uint16_t field_index;
1240 if (instruction.IsQuickened()) {
1241 if (!CanDecodeQuickenedInfo()) {
1242 return false;
1243 }
1244 field_index = LookupQuickenedInfo(dex_pc);
1245 } else {
1246 field_index = instruction.VRegC_22c();
1247 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001248
1249 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierc7853442015-03-27 14:35:38 -07001250 ArtField* resolved_field =
1251 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001252
Calin Juravlee6f49b42015-09-17 14:04:33 +00001253 if (resolved_field == nullptr) {
1254 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedField);
1255 return false;
1256 }
1257
1258 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001259
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001260 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot, dex_pc);
Calin Juravlee6f49b42015-09-17 14:04:33 +00001261 current_block_->AddInstruction(new (arena_) HNullCheck(object, dex_pc));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001262 if (is_put) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001263 Temporaries temps(graph_);
Calin Juravlee6f49b42015-09-17 14:04:33 +00001264 HInstruction* null_check = current_block_->GetLastInstruction();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001265 // We need one temporary for the null check.
1266 temps.Add(null_check);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001267 HInstruction* value = LoadLocal(source_or_dest_reg, field_type, dex_pc);
Calin Juravlee6f49b42015-09-17 14:04:33 +00001268 current_block_->AddInstruction(new (arena_) HInstanceFieldSet(
1269 null_check,
1270 value,
1271 field_type,
1272 resolved_field->GetOffset(),
1273 resolved_field->IsVolatile(),
1274 field_index,
1275 *dex_file_,
1276 dex_compilation_unit_->GetDexCache(),
1277 dex_pc));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001278 } else {
Calin Juravlee6f49b42015-09-17 14:04:33 +00001279 current_block_->AddInstruction(new (arena_) HInstanceFieldGet(
1280 current_block_->GetLastInstruction(),
1281 field_type,
1282 resolved_field->GetOffset(),
1283 resolved_field->IsVolatile(),
1284 field_index,
1285 *dex_file_,
1286 dex_compilation_unit_->GetDexCache(),
1287 dex_pc));
Calin Juravle23a8e352015-09-08 19:56:31 +01001288
Calin Juravlee6f49b42015-09-17 14:04:33 +00001289 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction(), dex_pc);
1290 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001291 return true;
1292}
1293
Nicolas Geoffray30451742015-06-19 13:32:41 +01001294static mirror::Class* GetClassFrom(CompilerDriver* driver,
1295 const DexCompilationUnit& compilation_unit) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001296 ScopedObjectAccess soa(Thread::Current());
1297 StackHandleScope<2> hs(soa.Self());
Nicolas Geoffray30451742015-06-19 13:32:41 +01001298 const DexFile& dex_file = *compilation_unit.GetDexFile();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001299 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Nicolas Geoffray30451742015-06-19 13:32:41 +01001300 soa.Decode<mirror::ClassLoader*>(compilation_unit.GetClassLoader())));
1301 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001302 compilation_unit.GetClassLinker()->FindDexCache(soa.Self(), dex_file)));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001303
Nicolas Geoffray30451742015-06-19 13:32:41 +01001304 return driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, &compilation_unit);
1305}
1306
1307mirror::Class* HGraphBuilder::GetOutermostCompilingClass() const {
1308 return GetClassFrom(compiler_driver_, *outer_compilation_unit_);
1309}
1310
1311mirror::Class* HGraphBuilder::GetCompilingClass() const {
1312 return GetClassFrom(compiler_driver_, *dex_compilation_unit_);
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001313}
1314
1315bool HGraphBuilder::IsOutermostCompilingClass(uint16_t type_index) const {
1316 ScopedObjectAccess soa(Thread::Current());
1317 StackHandleScope<4> hs(soa.Self());
1318 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001319 dex_compilation_unit_->GetClassLinker()->FindDexCache(
1320 soa.Self(), *dex_compilation_unit_->GetDexFile())));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001321 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1322 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
1323 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
1324 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
Nicolas Geoffrayafd06412015-06-20 22:44:47 +01001325 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001326
Nicolas Geoffrayafd06412015-06-20 22:44:47 +01001327 return outer_class.Get() == cls.Get();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001328}
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 Juravlee6f49b42015-09-17 14:04:33 +00001347 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedField);
1348 return false;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001349 }
1350
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001351 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
1352 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001353 outer_compilation_unit_->GetClassLinker()->FindDexCache(soa.Self(), outer_dex_file)));
Nicolas Geoffray30451742015-06-19 13:32:41 +01001354 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001355
1356 // The index at which the field's class is stored in the DexCache's type array.
1357 uint32_t storage_index;
Nicolas Geoffray30451742015-06-19 13:32:41 +01001358 bool is_outer_class = (outer_class.Get() == resolved_field->GetDeclaringClass());
1359 if (is_outer_class) {
1360 storage_index = outer_class->GetDexTypeIndex();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001361 } else if (outer_dex_cache.Get() != dex_cache.Get()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001362 // The compiler driver cannot currently understand multiple dex caches involved. Just bailout.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001363 return false;
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001364 } else {
1365 std::pair<bool, bool> pair = compiler_driver_->IsFastStaticField(
1366 outer_dex_cache.Get(),
Nicolas Geoffray30451742015-06-19 13:32:41 +01001367 GetCompilingClass(),
Mathieu Chartierc7853442015-03-27 14:35:38 -07001368 resolved_field,
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001369 field_index,
1370 &storage_index);
1371 bool can_easily_access = is_put ? pair.second : pair.first;
1372 if (!can_easily_access) {
1373 return false;
1374 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001375 }
1376
1377 // TODO: find out why this check is needed.
1378 bool is_in_dex_cache = compiler_driver_->CanAssumeTypeIsPresentInDexCache(
Nicolas Geoffray6a816cf2015-03-24 16:17:56 +00001379 *outer_compilation_unit_->GetDexFile(), storage_index);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001380 bool is_initialized = resolved_field->GetDeclaringClass()->IsInitialized() && is_in_dex_cache;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001381
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001382 HLoadClass* constant = new (arena_) HLoadClass(graph_->GetCurrentMethod(),
1383 storage_index,
1384 *dex_compilation_unit_->GetDexFile(),
Nicolas Geoffray30451742015-06-19 13:32:41 +01001385 is_outer_class,
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001386 dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001387 current_block_->AddInstruction(constant);
1388
1389 HInstruction* cls = constant;
Nicolas Geoffray30451742015-06-19 13:32:41 +01001390 if (!is_initialized && !is_outer_class) {
Calin Juravle225ff812014-11-13 16:46:39 +00001391 cls = new (arena_) HClinitCheck(constant, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001392 current_block_->AddInstruction(cls);
1393 }
1394
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001395 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001396 if (is_put) {
1397 // We need to keep the class alive before loading the value.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001398 Temporaries temps(graph_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001399 temps.Add(cls);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001400 HInstruction* value = LoadLocal(source_or_dest_reg, field_type, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001401 DCHECK_EQ(value->GetType(), field_type);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001402 current_block_->AddInstruction(new (arena_) HStaticFieldSet(cls,
1403 value,
1404 field_type,
1405 resolved_field->GetOffset(),
1406 resolved_field->IsVolatile(),
1407 field_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07001408 *dex_file_,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001409 dex_cache_,
1410 dex_pc));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001411 } else {
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001412 current_block_->AddInstruction(new (arena_) HStaticFieldGet(cls,
1413 field_type,
1414 resolved_field->GetOffset(),
1415 resolved_field->IsVolatile(),
1416 field_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07001417 *dex_file_,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001418 dex_cache_,
1419 dex_pc));
1420 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001421 }
1422 return true;
1423}
1424
Calin Juravlebacfec32014-11-14 15:54:36 +00001425void HGraphBuilder::BuildCheckedDivRem(uint16_t out_vreg,
1426 uint16_t first_vreg,
1427 int64_t second_vreg_or_constant,
1428 uint32_t dex_pc,
1429 Primitive::Type type,
1430 bool second_is_constant,
1431 bool isDiv) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001432 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Calin Juravled0d48522014-11-04 16:40:20 +00001433
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001434 HInstruction* first = LoadLocal(first_vreg, type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001435 HInstruction* second = nullptr;
1436 if (second_is_constant) {
1437 if (type == Primitive::kPrimInt) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001438 second = graph_->GetIntConstant(second_vreg_or_constant, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001439 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001440 second = graph_->GetLongConstant(second_vreg_or_constant, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001441 }
1442 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001443 second = LoadLocal(second_vreg_or_constant, type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001444 }
1445
1446 if (!second_is_constant
1447 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0)
1448 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) {
1449 second = new (arena_) HDivZeroCheck(second, dex_pc);
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001450 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +00001451 current_block_->AddInstruction(second);
1452 temps.Add(current_block_->GetLastInstruction());
1453 }
1454
Calin Juravlebacfec32014-11-14 15:54:36 +00001455 if (isDiv) {
1456 current_block_->AddInstruction(new (arena_) HDiv(type, first, second, dex_pc));
1457 } else {
1458 current_block_->AddInstruction(new (arena_) HRem(type, first, second, dex_pc));
1459 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001460 UpdateLocal(out_vreg, current_block_->GetLastInstruction(), dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001461}
1462
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001463void HGraphBuilder::BuildArrayAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001464 uint32_t dex_pc,
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001465 bool is_put,
1466 Primitive::Type anticipated_type) {
1467 uint8_t source_or_dest_reg = instruction.VRegA_23x();
1468 uint8_t array_reg = instruction.VRegB_23x();
1469 uint8_t index_reg = instruction.VRegC_23x();
1470
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001471 // We need one temporary for the null check, one for the index, and one for the length.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001472 Temporaries temps(graph_);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001473
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001474 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001475 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001476 current_block_->AddInstruction(object);
1477 temps.Add(object);
1478
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001479 HInstruction* length = new (arena_) HArrayLength(object, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001480 current_block_->AddInstruction(length);
1481 temps.Add(length);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001482 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001483 index = new (arena_) HBoundsCheck(index, length, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001484 current_block_->AddInstruction(index);
1485 temps.Add(index);
1486 if (is_put) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001487 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001488 // TODO: Insert a type check node if the type is Object.
Nicolas Geoffray39468442014-09-02 15:17:15 +01001489 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001490 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001491 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001492 current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type, dex_pc));
1493 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001494 }
Mark Mendell1152c922015-04-24 17:06:35 -04001495 graph_->SetHasBoundsChecks(true);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001496}
1497
Calin Juravle225ff812014-11-13 16:46:39 +00001498void HGraphBuilder::BuildFilledNewArray(uint32_t dex_pc,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001499 uint32_t type_index,
1500 uint32_t number_of_vreg_arguments,
1501 bool is_range,
1502 uint32_t* args,
1503 uint32_t register_index) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001504 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments, dex_pc);
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001505 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
1506 ? kQuickAllocArrayWithAccessCheck
1507 : kQuickAllocArray;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01001508 HInstruction* object = new (arena_) HNewArray(length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01001509 graph_->GetCurrentMethod(),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01001510 dex_pc,
1511 type_index,
1512 *dex_compilation_unit_->GetDexFile(),
1513 entrypoint);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001514 current_block_->AddInstruction(object);
1515
1516 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1517 DCHECK_EQ(descriptor[0], '[') << descriptor;
1518 char primitive = descriptor[1];
1519 DCHECK(primitive == 'I'
1520 || primitive == 'L'
1521 || primitive == '[') << descriptor;
1522 bool is_reference_array = (primitive == 'L') || (primitive == '[');
1523 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
1524
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001525 Temporaries temps(graph_);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001526 temps.Add(object);
1527 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001528 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type, dex_pc);
1529 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001530 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001531 new (arena_) HArraySet(object, index, value, type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001532 }
1533 latest_result_ = object;
1534}
1535
1536template <typename T>
1537void HGraphBuilder::BuildFillArrayData(HInstruction* object,
1538 const T* data,
1539 uint32_t element_count,
1540 Primitive::Type anticipated_type,
Calin Juravle225ff812014-11-13 16:46:39 +00001541 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001542 for (uint32_t i = 0; i < element_count; ++i) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001543 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1544 HInstruction* value = graph_->GetIntConstant(data[i], dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001545 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001546 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001547 }
1548}
1549
Calin Juravle225ff812014-11-13 16:46:39 +00001550void HGraphBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001551 Temporaries temps(graph_);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001552 HInstruction* array = LoadLocal(instruction.VRegA_31t(), Primitive::kPrimNot, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001553 HNullCheck* null_check = new (arena_) HNullCheck(array, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001554 current_block_->AddInstruction(null_check);
1555 temps.Add(null_check);
1556
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001557 HInstruction* length = new (arena_) HArrayLength(null_check, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001558 current_block_->AddInstruction(length);
1559
Calin Juravle225ff812014-11-13 16:46:39 +00001560 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
Calin Juravled0d48522014-11-04 16:40:20 +00001561 const Instruction::ArrayDataPayload* payload =
1562 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_start_ + payload_offset);
1563 const uint8_t* data = payload->data;
1564 uint32_t element_count = payload->element_count;
1565
1566 // Implementation of this DEX instruction seems to be that the bounds check is
1567 // done before doing any stores.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001568 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001569 current_block_->AddInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
Calin Juravled0d48522014-11-04 16:40:20 +00001570
1571 switch (payload->element_width) {
1572 case 1:
1573 BuildFillArrayData(null_check,
1574 reinterpret_cast<const int8_t*>(data),
1575 element_count,
1576 Primitive::kPrimByte,
Calin Juravle225ff812014-11-13 16:46:39 +00001577 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001578 break;
1579 case 2:
1580 BuildFillArrayData(null_check,
1581 reinterpret_cast<const int16_t*>(data),
1582 element_count,
1583 Primitive::kPrimShort,
Calin Juravle225ff812014-11-13 16:46:39 +00001584 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001585 break;
1586 case 4:
1587 BuildFillArrayData(null_check,
1588 reinterpret_cast<const int32_t*>(data),
1589 element_count,
1590 Primitive::kPrimInt,
Calin Juravle225ff812014-11-13 16:46:39 +00001591 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001592 break;
1593 case 8:
1594 BuildFillWideArrayData(null_check,
1595 reinterpret_cast<const int64_t*>(data),
1596 element_count,
Calin Juravle225ff812014-11-13 16:46:39 +00001597 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001598 break;
1599 default:
1600 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1601 }
Mark Mendell1152c922015-04-24 17:06:35 -04001602 graph_->SetHasBoundsChecks(true);
Calin Juravled0d48522014-11-04 16:40:20 +00001603}
1604
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001605void HGraphBuilder::BuildFillWideArrayData(HInstruction* object,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +01001606 const int64_t* data,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001607 uint32_t element_count,
Calin Juravle225ff812014-11-13 16:46:39 +00001608 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001609 for (uint32_t i = 0; i < element_count; ++i) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001610 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1611 HInstruction* value = graph_->GetLongConstant(data[i], dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001612 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001613 object, index, value, Primitive::kPrimLong, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001614 }
1615}
1616
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001617static TypeCheckKind ComputeTypeCheckKind(Handle<mirror::Class> cls)
1618 SHARED_REQUIRES(Locks::mutator_lock_) {
1619 if (cls->IsInterface()) {
1620 return TypeCheckKind::kInterfaceCheck;
1621 } else if (cls->IsArrayClass()) {
1622 if (cls->GetComponentType()->IsObjectClass()) {
1623 return TypeCheckKind::kArrayObjectCheck;
1624 } else if (cls->CannotBeAssignedFromOtherTypes()) {
1625 return TypeCheckKind::kExactCheck;
1626 } else {
1627 return TypeCheckKind::kArrayCheck;
1628 }
1629 } else if (cls->IsFinal()) {
1630 return TypeCheckKind::kExactCheck;
1631 } else if (cls->IsAbstract()) {
1632 return TypeCheckKind::kAbstractClassCheck;
1633 } else {
1634 return TypeCheckKind::kClassHierarchyCheck;
1635 }
1636}
1637
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001638bool HGraphBuilder::BuildTypeCheck(const Instruction& instruction,
1639 uint8_t destination,
1640 uint8_t reference,
1641 uint16_t type_index,
Calin Juravle225ff812014-11-13 16:46:39 +00001642 uint32_t dex_pc) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001643 ScopedObjectAccess soa(Thread::Current());
1644 StackHandleScope<2> hs(soa.Self());
1645 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
1646 dex_compilation_unit_->GetClassLinker()->FindDexCache(
1647 soa.Self(), *dex_compilation_unit_->GetDexFile())));
1648 Handle<mirror::Class> resolved_class(hs.NewHandle(dex_cache->GetResolvedType(type_index)));
1649
1650 if ((resolved_class.Get() == nullptr) ||
1651 // TODO: Remove this check once the compiler actually knows which
1652 // ArtMethod it is compiling.
1653 (GetCompilingClass() == nullptr) ||
1654 !GetCompilingClass()->CanAccess(resolved_class.Get())) {
Calin Juravle48c2b032014-12-09 18:11:36 +00001655 MaybeRecordStat(MethodCompilationStat::kNotCompiledCantAccesType);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001656 return false;
1657 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001658
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001659 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot, dex_pc);
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001660 HLoadClass* cls = new (arena_) HLoadClass(
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001661 graph_->GetCurrentMethod(),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01001662 type_index,
1663 *dex_compilation_unit_->GetDexFile(),
1664 IsOutermostCompilingClass(type_index),
1665 dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001666 current_block_->AddInstruction(cls);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001667
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001668 // The class needs a temporary before being used by the type check.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001669 Temporaries temps(graph_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001670 temps.Add(cls);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001671
1672 TypeCheckKind check_kind = ComputeTypeCheckKind(resolved_class);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001673 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001674 current_block_->AddInstruction(new (arena_) HInstanceOf(object, cls, check_kind, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001675 UpdateLocal(destination, current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001676 } else {
1677 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001678 current_block_->AddInstruction(new (arena_) HCheckCast(object, cls, check_kind, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001679 }
1680 return true;
1681}
1682
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001683bool HGraphBuilder::NeedsAccessCheck(uint32_t type_index) const {
1684 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
1685 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index);
1686}
1687
Mark Mendellfe57faa2015-09-18 09:26:15 -04001688void HGraphBuilder::BuildSwitchJumpTable(const SwitchTable& table,
1689 const Instruction& instruction,
1690 HInstruction* value,
1691 uint32_t dex_pc) {
1692 // Add the successor blocks to the current block.
1693 uint16_t num_entries = table.GetNumEntries();
1694 for (size_t i = 1; i <= num_entries; i++) {
1695 int32_t target_offset = table.GetEntryAt(i);
1696 HBasicBlock* case_target = FindBlockStartingAt(dex_pc + target_offset);
1697 DCHECK(case_target != nullptr);
1698
1699 // Add the target block as a successor.
1700 current_block_->AddSuccessor(case_target);
1701 }
1702
1703 // Add the default target block as the last successor.
1704 HBasicBlock* default_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
1705 DCHECK(default_target != nullptr);
1706 current_block_->AddSuccessor(default_target);
1707
1708 // Now add the Switch instruction.
1709 int32_t starting_key = table.GetEntryAt(0);
1710 current_block_->AddInstruction(
1711 new (arena_) HPackedSwitch(starting_key, num_entries, value, dex_pc));
1712 // This block ends with control flow.
1713 current_block_ = nullptr;
1714}
1715
Calin Juravle48c2b032014-12-09 18:11:36 +00001716void HGraphBuilder::BuildPackedSwitch(const Instruction& instruction, uint32_t dex_pc) {
David Brazdil2ef645b2015-06-17 18:20:52 +01001717 // Verifier guarantees that the payload for PackedSwitch contains:
1718 // (a) number of entries (may be zero)
1719 // (b) first and lowest switch case value (entry 0, always present)
1720 // (c) list of target pcs (entries 1 <= i <= N)
Andreas Gamped881df52014-11-24 23:28:39 -08001721 SwitchTable table(instruction, dex_pc, false);
1722
1723 // Value to test against.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001724 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt, dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08001725
Mark Mendellfe57faa2015-09-18 09:26:15 -04001726 // Starting key value.
1727 int32_t starting_key = table.GetEntryAt(0);
1728
David Brazdil2ef645b2015-06-17 18:20:52 +01001729 // Retrieve number of entries.
Andreas Gampee4d4d322014-12-04 09:09:57 -08001730 uint16_t num_entries = table.GetNumEntries();
David Brazdil2ef645b2015-06-17 18:20:52 +01001731 if (num_entries == 0) {
1732 return;
1733 }
Andreas Gampee4d4d322014-12-04 09:09:57 -08001734
Mark Mendellfe57faa2015-09-18 09:26:15 -04001735 // Don't use a packed switch if there are very few entries.
1736 if (num_entries > kSmallSwitchThreshold) {
1737 BuildSwitchJumpTable(table, instruction, value, dex_pc);
1738 } else {
1739 // Chained cmp-and-branch, starting from starting_key.
1740 for (size_t i = 1; i <= num_entries; i++) {
1741 BuildSwitchCaseHelper(instruction, i, i == num_entries, table, value,
1742 starting_key + i - 1, table.GetEntryAt(i), dex_pc);
1743 }
Andreas Gamped881df52014-11-24 23:28:39 -08001744 }
Andreas Gamped881df52014-11-24 23:28:39 -08001745}
1746
Calin Juravle48c2b032014-12-09 18:11:36 +00001747void HGraphBuilder::BuildSparseSwitch(const Instruction& instruction, uint32_t dex_pc) {
David Brazdil2ef645b2015-06-17 18:20:52 +01001748 // Verifier guarantees that the payload for SparseSwitch contains:
1749 // (a) number of entries (may be zero)
1750 // (b) sorted key values (entries 0 <= i < N)
1751 // (c) target pcs corresponding to the switch values (entries N <= i < 2*N)
Andreas Gampee4d4d322014-12-04 09:09:57 -08001752 SwitchTable table(instruction, dex_pc, true);
1753
1754 // Value to test against.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001755 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001756
1757 uint16_t num_entries = table.GetNumEntries();
Andreas Gampee4d4d322014-12-04 09:09:57 -08001758
1759 for (size_t i = 0; i < num_entries; i++) {
1760 BuildSwitchCaseHelper(instruction, i, i == static_cast<size_t>(num_entries) - 1, table, value,
1761 table.GetEntryAt(i), table.GetEntryAt(i + num_entries), dex_pc);
1762 }
Andreas Gampee4d4d322014-12-04 09:09:57 -08001763}
1764
1765void HGraphBuilder::BuildSwitchCaseHelper(const Instruction& instruction, size_t index,
1766 bool is_last_case, const SwitchTable& table,
1767 HInstruction* value, int32_t case_value_int,
1768 int32_t target_offset, uint32_t dex_pc) {
David Brazdil852eaff2015-02-02 15:23:05 +00001769 HBasicBlock* case_target = FindBlockStartingAt(dex_pc + target_offset);
1770 DCHECK(case_target != nullptr);
1771 PotentiallyAddSuspendCheck(case_target, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001772
1773 // The current case's value.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001774 HInstruction* this_case_value = graph_->GetIntConstant(case_value_int, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001775
1776 // Compare value and this_case_value.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001777 HEqual* comparison = new (arena_) HEqual(value, this_case_value, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001778 current_block_->AddInstruction(comparison);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001779 HInstruction* ifinst = new (arena_) HIf(comparison, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001780 current_block_->AddInstruction(ifinst);
1781
1782 // Case hit: use the target offset to determine where to go.
Andreas Gampee4d4d322014-12-04 09:09:57 -08001783 current_block_->AddSuccessor(case_target);
1784
1785 // Case miss: go to the next case (or default fall-through).
1786 // When there is a next case, we use the block stored with the table offset representing this
1787 // case (that is where we registered them in ComputeBranchTargets).
1788 // When there is no next case, we use the following instruction.
1789 // TODO: Find a good way to peel the last iteration to avoid conditional, but still have re-use.
1790 if (!is_last_case) {
1791 HBasicBlock* next_case_target = FindBlockStartingAt(table.GetDexPcForIndex(index));
1792 DCHECK(next_case_target != nullptr);
1793 current_block_->AddSuccessor(next_case_target);
1794
1795 // Need to manually add the block, as there is no dex-pc transition for the cases.
1796 graph_->AddBlock(next_case_target);
1797
1798 current_block_ = next_case_target;
1799 } else {
1800 HBasicBlock* default_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
1801 DCHECK(default_target != nullptr);
1802 current_block_->AddSuccessor(default_target);
1803 current_block_ = nullptr;
1804 }
1805}
1806
David Brazdil852eaff2015-02-02 15:23:05 +00001807void HGraphBuilder::PotentiallyAddSuspendCheck(HBasicBlock* target, uint32_t dex_pc) {
1808 int32_t target_offset = target->GetDexPc() - dex_pc;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001809 if (target_offset <= 0) {
David Brazdil852eaff2015-02-02 15:23:05 +00001810 // DX generates back edges to the first encountered return. We can save
1811 // time of later passes by not adding redundant suspend checks.
David Brazdil2fd6aa52015-02-02 18:58:27 +00001812 HInstruction* last_in_target = target->GetLastInstruction();
1813 if (last_in_target != nullptr &&
1814 (last_in_target->IsReturn() || last_in_target->IsReturnVoid())) {
1815 return;
David Brazdil852eaff2015-02-02 15:23:05 +00001816 }
1817
1818 // Add a suspend check to backward branches which may potentially loop. We
1819 // can remove them after we recognize loops in the graph.
Calin Juravle225ff812014-11-13 16:46:39 +00001820 current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_pc));
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001821 }
1822}
1823
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001824bool HGraphBuilder::CanDecodeQuickenedInfo() const {
1825 return interpreter_metadata_ != nullptr;
1826}
1827
1828uint16_t HGraphBuilder::LookupQuickenedInfo(uint32_t dex_pc) {
1829 DCHECK(interpreter_metadata_ != nullptr);
1830 uint32_t dex_pc_in_map = DecodeUnsignedLeb128(&interpreter_metadata_);
1831 DCHECK_EQ(dex_pc, dex_pc_in_map);
1832 return DecodeUnsignedLeb128(&interpreter_metadata_);
1833}
1834
Calin Juravle225ff812014-11-13 16:46:39 +00001835bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001836 if (current_block_ == nullptr) {
1837 return true; // Dead code
1838 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001839
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001840 switch (instruction.Opcode()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001841 case Instruction::CONST_4: {
1842 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001843 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n(), dex_pc);
1844 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001845 break;
1846 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001847
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001848 case Instruction::CONST_16: {
1849 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001850 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s(), dex_pc);
1851 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001852 break;
1853 }
1854
Dave Allison20dfc792014-06-16 20:44:29 -07001855 case Instruction::CONST: {
1856 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001857 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i(), dex_pc);
1858 UpdateLocal(register_index, constant, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001859 break;
1860 }
1861
1862 case Instruction::CONST_HIGH16: {
1863 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001864 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16, dex_pc);
1865 UpdateLocal(register_index, constant, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001866 break;
1867 }
1868
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001869 case Instruction::CONST_WIDE_16: {
1870 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001871 // Get 16 bits of constant value, sign extended to 64 bits.
1872 int64_t value = instruction.VRegB_21s();
1873 value <<= 48;
1874 value >>= 48;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001875 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1876 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001877 break;
1878 }
1879
1880 case Instruction::CONST_WIDE_32: {
1881 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001882 // Get 32 bits of constant value, sign extended to 64 bits.
1883 int64_t value = instruction.VRegB_31i();
1884 value <<= 32;
1885 value >>= 32;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001886 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1887 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001888 break;
1889 }
1890
1891 case Instruction::CONST_WIDE: {
1892 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001893 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l(), dex_pc);
1894 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001895 break;
1896 }
1897
Dave Allison20dfc792014-06-16 20:44:29 -07001898 case Instruction::CONST_WIDE_HIGH16: {
1899 int32_t register_index = instruction.VRegA();
1900 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001901 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1902 UpdateLocal(register_index, constant, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001903 break;
1904 }
1905
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001906 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001907 case Instruction::MOVE:
1908 case Instruction::MOVE_FROM16:
1909 case Instruction::MOVE_16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001910 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
1911 UpdateLocal(instruction.VRegA(), value, dex_pc);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001912 break;
1913 }
1914
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001915 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001916 case Instruction::MOVE_WIDE:
1917 case Instruction::MOVE_WIDE_FROM16:
1918 case Instruction::MOVE_WIDE_16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001919 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong, dex_pc);
1920 UpdateLocal(instruction.VRegA(), value, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001921 break;
1922 }
1923
1924 case Instruction::MOVE_OBJECT:
1925 case Instruction::MOVE_OBJECT_16:
1926 case Instruction::MOVE_OBJECT_FROM16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001927 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot, dex_pc);
1928 UpdateLocal(instruction.VRegA(), value, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001929 break;
1930 }
1931
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001932 case Instruction::RETURN_VOID_NO_BARRIER:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001933 case Instruction::RETURN_VOID: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001934 BuildReturn(instruction, Primitive::kPrimVoid, dex_pc);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001935 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001936 }
1937
Dave Allison20dfc792014-06-16 20:44:29 -07001938#define IF_XX(comparison, cond) \
Calin Juravle225ff812014-11-13 16:46:39 +00001939 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
1940 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001941
Dave Allison20dfc792014-06-16 20:44:29 -07001942 IF_XX(HEqual, EQ);
1943 IF_XX(HNotEqual, NE);
1944 IF_XX(HLessThan, LT);
1945 IF_XX(HLessThanOrEqual, LE);
1946 IF_XX(HGreaterThan, GT);
1947 IF_XX(HGreaterThanOrEqual, GE);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001948
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001949 case Instruction::GOTO:
1950 case Instruction::GOTO_16:
1951 case Instruction::GOTO_32: {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001952 int32_t offset = instruction.GetTargetOffset();
Calin Juravle225ff812014-11-13 16:46:39 +00001953 HBasicBlock* target = FindBlockStartingAt(offset + dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001954 DCHECK(target != nullptr);
David Brazdil852eaff2015-02-02 15:23:05 +00001955 PotentiallyAddSuspendCheck(target, dex_pc);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001956 current_block_->AddInstruction(new (arena_) HGoto(dex_pc));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001957 current_block_->AddSuccessor(target);
1958 current_block_ = nullptr;
1959 break;
1960 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001961
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001962 case Instruction::RETURN: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001963 BuildReturn(instruction, return_type_, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001964 break;
1965 }
1966
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001967 case Instruction::RETURN_OBJECT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001968 BuildReturn(instruction, return_type_, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001969 break;
1970 }
1971
1972 case Instruction::RETURN_WIDE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001973 BuildReturn(instruction, return_type_, dex_pc);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001974 break;
1975 }
1976
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001977 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001978 case Instruction::INVOKE_INTERFACE:
1979 case Instruction::INVOKE_STATIC:
1980 case Instruction::INVOKE_SUPER:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001981 case Instruction::INVOKE_VIRTUAL:
1982 case Instruction::INVOKE_VIRTUAL_QUICK: {
1983 uint16_t method_idx;
1984 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_QUICK) {
1985 if (!CanDecodeQuickenedInfo()) {
1986 return false;
1987 }
1988 method_idx = LookupQuickenedInfo(dex_pc);
1989 } else {
1990 method_idx = instruction.VRegB_35c();
1991 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001992 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001993 uint32_t args[5];
Ian Rogers29a26482014-05-02 15:27:29 -07001994 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00001995 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001996 number_of_vreg_arguments, false, args, -1)) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001997 return false;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001998 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001999 break;
2000 }
2001
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002002 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00002003 case Instruction::INVOKE_INTERFACE_RANGE:
2004 case Instruction::INVOKE_STATIC_RANGE:
2005 case Instruction::INVOKE_SUPER_RANGE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002006 case Instruction::INVOKE_VIRTUAL_RANGE:
2007 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2008 uint16_t method_idx;
2009 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK) {
2010 if (!CanDecodeQuickenedInfo()) {
2011 return false;
2012 }
2013 method_idx = LookupQuickenedInfo(dex_pc);
2014 } else {
2015 method_idx = instruction.VRegB_3rc();
2016 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002017 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
2018 uint32_t register_index = instruction.VRegC();
Calin Juravle225ff812014-11-13 16:46:39 +00002019 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002020 number_of_vreg_arguments, true, nullptr, register_index)) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002021 return false;
2022 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002023 break;
2024 }
2025
Roland Levillain88cb1752014-10-20 16:36:47 +01002026 case Instruction::NEG_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002027 Unop_12x<HNeg>(instruction, Primitive::kPrimInt, dex_pc);
Roland Levillain88cb1752014-10-20 16:36:47 +01002028 break;
2029 }
2030
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002031 case Instruction::NEG_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002032 Unop_12x<HNeg>(instruction, Primitive::kPrimLong, dex_pc);
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002033 break;
2034 }
2035
Roland Levillain3dbcb382014-10-28 17:30:07 +00002036 case Instruction::NEG_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002037 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat, dex_pc);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002038 break;
2039 }
2040
2041 case Instruction::NEG_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002042 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble, dex_pc);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002043 break;
2044 }
2045
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002046 case Instruction::NOT_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002047 Unop_12x<HNot>(instruction, Primitive::kPrimInt, dex_pc);
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002048 break;
2049 }
2050
Roland Levillain70566432014-10-24 16:20:17 +01002051 case Instruction::NOT_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002052 Unop_12x<HNot>(instruction, Primitive::kPrimLong, dex_pc);
Roland Levillain70566432014-10-24 16:20:17 +01002053 break;
2054 }
2055
Roland Levillaindff1f282014-11-05 14:15:05 +00002056 case Instruction::INT_TO_LONG: {
Roland Levillain624279f2014-12-04 11:54:28 +00002057 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong, dex_pc);
Roland Levillaindff1f282014-11-05 14:15:05 +00002058 break;
2059 }
2060
Roland Levillaincff13742014-11-17 14:32:17 +00002061 case Instruction::INT_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00002062 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00002063 break;
2064 }
2065
2066 case Instruction::INT_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00002067 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00002068 break;
2069 }
2070
Roland Levillain946e1432014-11-11 17:35:19 +00002071 case Instruction::LONG_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00002072 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt, dex_pc);
Roland Levillain946e1432014-11-11 17:35:19 +00002073 break;
2074 }
2075
Roland Levillain6d0e4832014-11-27 18:31:21 +00002076 case Instruction::LONG_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00002077 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimFloat, dex_pc);
Roland Levillain6d0e4832014-11-27 18:31:21 +00002078 break;
2079 }
2080
Roland Levillain647b9ed2014-11-27 12:06:00 +00002081 case Instruction::LONG_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00002082 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble, dex_pc);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002083 break;
2084 }
2085
Roland Levillain3f8f9362014-12-02 17:45:01 +00002086 case Instruction::FLOAT_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00002087 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimInt, dex_pc);
2088 break;
2089 }
2090
2091 case Instruction::FLOAT_TO_LONG: {
2092 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimLong, dex_pc);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002093 break;
2094 }
2095
Roland Levillain8964e2b2014-12-04 12:10:50 +00002096 case Instruction::FLOAT_TO_DOUBLE: {
2097 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimDouble, dex_pc);
2098 break;
2099 }
2100
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002101 case Instruction::DOUBLE_TO_INT: {
2102 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimInt, dex_pc);
2103 break;
2104 }
2105
2106 case Instruction::DOUBLE_TO_LONG: {
2107 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimLong, dex_pc);
2108 break;
2109 }
2110
Roland Levillain8964e2b2014-12-04 12:10:50 +00002111 case Instruction::DOUBLE_TO_FLOAT: {
2112 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimFloat, dex_pc);
2113 break;
2114 }
2115
Roland Levillain51d3fc42014-11-13 14:11:42 +00002116 case Instruction::INT_TO_BYTE: {
Roland Levillain624279f2014-12-04 11:54:28 +00002117 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte, dex_pc);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002118 break;
2119 }
2120
Roland Levillain01a8d712014-11-14 16:27:39 +00002121 case Instruction::INT_TO_SHORT: {
Roland Levillain624279f2014-12-04 11:54:28 +00002122 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort, dex_pc);
Roland Levillain01a8d712014-11-14 16:27:39 +00002123 break;
2124 }
2125
Roland Levillain981e4542014-11-14 11:47:14 +00002126 case Instruction::INT_TO_CHAR: {
Roland Levillain624279f2014-12-04 11:54:28 +00002127 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar, dex_pc);
Roland Levillain981e4542014-11-14 11:47:14 +00002128 break;
2129 }
2130
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002131 case Instruction::ADD_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002132 Binop_23x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002133 break;
2134 }
2135
2136 case Instruction::ADD_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002137 Binop_23x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002138 break;
2139 }
2140
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002141 case Instruction::ADD_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002142 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002143 break;
2144 }
2145
2146 case Instruction::ADD_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002147 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002148 break;
2149 }
2150
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002151 case Instruction::SUB_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002152 Binop_23x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002153 break;
2154 }
2155
2156 case Instruction::SUB_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002157 Binop_23x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002158 break;
2159 }
2160
Calin Juravle096cc022014-10-23 17:01:13 +01002161 case Instruction::SUB_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002162 Binop_23x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01002163 break;
2164 }
2165
2166 case Instruction::SUB_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002167 Binop_23x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01002168 break;
2169 }
2170
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002171 case Instruction::ADD_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002172 Binop_12x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002173 break;
2174 }
2175
Calin Juravle34bacdf2014-10-07 20:23:36 +01002176 case Instruction::MUL_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002177 Binop_23x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002178 break;
2179 }
2180
2181 case Instruction::MUL_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002182 Binop_23x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002183 break;
2184 }
2185
Calin Juravleb5bfa962014-10-21 18:02:24 +01002186 case Instruction::MUL_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002187 Binop_23x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01002188 break;
2189 }
2190
2191 case Instruction::MUL_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002192 Binop_23x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01002193 break;
2194 }
2195
Calin Juravled0d48522014-11-04 16:40:20 +00002196 case Instruction::DIV_INT: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002197 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2198 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravled0d48522014-11-04 16:40:20 +00002199 break;
2200 }
2201
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002202 case Instruction::DIV_LONG: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002203 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2204 dex_pc, Primitive::kPrimLong, false, true);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002205 break;
2206 }
2207
Calin Juravle7c4954d2014-10-28 16:57:40 +00002208 case Instruction::DIV_FLOAT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002209 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002210 break;
2211 }
2212
2213 case Instruction::DIV_DOUBLE: {
Calin Juravle225ff812014-11-13 16:46:39 +00002214 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002215 break;
2216 }
2217
Calin Juravlebacfec32014-11-14 15:54:36 +00002218 case Instruction::REM_INT: {
2219 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2220 dex_pc, Primitive::kPrimInt, false, false);
2221 break;
2222 }
2223
2224 case Instruction::REM_LONG: {
2225 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2226 dex_pc, Primitive::kPrimLong, false, false);
2227 break;
2228 }
2229
Calin Juravled2ec87d2014-12-08 14:24:46 +00002230 case Instruction::REM_FLOAT: {
2231 Binop_23x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2232 break;
2233 }
2234
2235 case Instruction::REM_DOUBLE: {
2236 Binop_23x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2237 break;
2238 }
2239
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002240 case Instruction::AND_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002241 Binop_23x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002242 break;
2243 }
2244
2245 case Instruction::AND_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002246 Binop_23x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002247 break;
2248 }
2249
Calin Juravle9aec02f2014-11-18 23:06:35 +00002250 case Instruction::SHL_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002251 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002252 break;
2253 }
2254
2255 case Instruction::SHL_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002256 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002257 break;
2258 }
2259
2260 case Instruction::SHR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002261 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002262 break;
2263 }
2264
2265 case Instruction::SHR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002266 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002267 break;
2268 }
2269
2270 case Instruction::USHR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002271 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002272 break;
2273 }
2274
2275 case Instruction::USHR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002276 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002277 break;
2278 }
2279
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002280 case Instruction::OR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002281 Binop_23x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002282 break;
2283 }
2284
2285 case Instruction::OR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002286 Binop_23x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002287 break;
2288 }
2289
2290 case Instruction::XOR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002291 Binop_23x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002292 break;
2293 }
2294
2295 case Instruction::XOR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002296 Binop_23x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002297 break;
2298 }
2299
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002300 case Instruction::ADD_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002301 Binop_12x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002302 break;
2303 }
2304
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002305 case Instruction::ADD_DOUBLE_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002306 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002307 break;
2308 }
2309
2310 case Instruction::ADD_FLOAT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002311 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002312 break;
2313 }
2314
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002315 case Instruction::SUB_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002316 Binop_12x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002317 break;
2318 }
2319
2320 case Instruction::SUB_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002321 Binop_12x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002322 break;
2323 }
2324
Calin Juravle096cc022014-10-23 17:01:13 +01002325 case Instruction::SUB_FLOAT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002326 Binop_12x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01002327 break;
2328 }
2329
2330 case Instruction::SUB_DOUBLE_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002331 Binop_12x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01002332 break;
2333 }
2334
Calin Juravle34bacdf2014-10-07 20:23:36 +01002335 case Instruction::MUL_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002336 Binop_12x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002337 break;
2338 }
2339
2340 case Instruction::MUL_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002341 Binop_12x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002342 break;
2343 }
2344
Calin Juravleb5bfa962014-10-21 18:02:24 +01002345 case Instruction::MUL_FLOAT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002346 Binop_12x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01002347 break;
2348 }
2349
2350 case Instruction::MUL_DOUBLE_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002351 Binop_12x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01002352 break;
2353 }
2354
Calin Juravle865fc882014-11-06 17:09:03 +00002355 case Instruction::DIV_INT_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002356 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2357 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravle865fc882014-11-06 17:09:03 +00002358 break;
2359 }
2360
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002361 case Instruction::DIV_LONG_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002362 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2363 dex_pc, Primitive::kPrimLong, false, true);
2364 break;
2365 }
2366
2367 case Instruction::REM_INT_2ADDR: {
2368 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2369 dex_pc, Primitive::kPrimInt, false, false);
2370 break;
2371 }
2372
2373 case Instruction::REM_LONG_2ADDR: {
2374 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2375 dex_pc, Primitive::kPrimLong, false, false);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002376 break;
2377 }
2378
Calin Juravled2ec87d2014-12-08 14:24:46 +00002379 case Instruction::REM_FLOAT_2ADDR: {
2380 Binop_12x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2381 break;
2382 }
2383
2384 case Instruction::REM_DOUBLE_2ADDR: {
2385 Binop_12x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2386 break;
2387 }
2388
Calin Juravle9aec02f2014-11-18 23:06:35 +00002389 case Instruction::SHL_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002390 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002391 break;
2392 }
2393
2394 case Instruction::SHL_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002395 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002396 break;
2397 }
2398
2399 case Instruction::SHR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002400 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002401 break;
2402 }
2403
2404 case Instruction::SHR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002405 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002406 break;
2407 }
2408
2409 case Instruction::USHR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002410 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002411 break;
2412 }
2413
2414 case Instruction::USHR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002415 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002416 break;
2417 }
2418
Calin Juravle7c4954d2014-10-28 16:57:40 +00002419 case Instruction::DIV_FLOAT_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00002420 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002421 break;
2422 }
2423
2424 case Instruction::DIV_DOUBLE_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00002425 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002426 break;
2427 }
2428
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002429 case Instruction::AND_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002430 Binop_12x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002431 break;
2432 }
2433
2434 case Instruction::AND_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002435 Binop_12x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002436 break;
2437 }
2438
2439 case Instruction::OR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002440 Binop_12x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002441 break;
2442 }
2443
2444 case Instruction::OR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002445 Binop_12x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002446 break;
2447 }
2448
2449 case Instruction::XOR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002450 Binop_12x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002451 break;
2452 }
2453
2454 case Instruction::XOR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002455 Binop_12x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002456 break;
2457 }
2458
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002459 case Instruction::ADD_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002460 Binop_22s<HAdd>(instruction, false, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002461 break;
2462 }
2463
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002464 case Instruction::AND_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002465 Binop_22s<HAnd>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002466 break;
2467 }
2468
2469 case Instruction::OR_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002470 Binop_22s<HOr>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002471 break;
2472 }
2473
2474 case Instruction::XOR_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002475 Binop_22s<HXor>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002476 break;
2477 }
2478
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002479 case Instruction::RSUB_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002480 Binop_22s<HSub>(instruction, true, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002481 break;
2482 }
2483
Calin Juravle34bacdf2014-10-07 20:23:36 +01002484 case Instruction::MUL_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002485 Binop_22s<HMul>(instruction, false, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002486 break;
2487 }
2488
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002489 case Instruction::ADD_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002490 Binop_22b<HAdd>(instruction, false, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002491 break;
2492 }
2493
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002494 case Instruction::AND_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002495 Binop_22b<HAnd>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002496 break;
2497 }
2498
2499 case Instruction::OR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002500 Binop_22b<HOr>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002501 break;
2502 }
2503
2504 case Instruction::XOR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002505 Binop_22b<HXor>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002506 break;
2507 }
2508
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002509 case Instruction::RSUB_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002510 Binop_22b<HSub>(instruction, true, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002511 break;
2512 }
2513
Calin Juravle34bacdf2014-10-07 20:23:36 +01002514 case Instruction::MUL_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002515 Binop_22b<HMul>(instruction, false, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002516 break;
2517 }
2518
Calin Juravled0d48522014-11-04 16:40:20 +00002519 case Instruction::DIV_INT_LIT16:
2520 case Instruction::DIV_INT_LIT8: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002521 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2522 dex_pc, Primitive::kPrimInt, true, true);
2523 break;
2524 }
2525
2526 case Instruction::REM_INT_LIT16:
2527 case Instruction::REM_INT_LIT8: {
2528 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2529 dex_pc, Primitive::kPrimInt, true, false);
Calin Juravled0d48522014-11-04 16:40:20 +00002530 break;
2531 }
2532
Calin Juravle9aec02f2014-11-18 23:06:35 +00002533 case Instruction::SHL_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002534 Binop_22b<HShl>(instruction, false, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002535 break;
2536 }
2537
2538 case Instruction::SHR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002539 Binop_22b<HShr>(instruction, false, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002540 break;
2541 }
2542
2543 case Instruction::USHR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002544 Binop_22b<HUShr>(instruction, false, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002545 break;
2546 }
2547
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002548 case Instruction::NEW_INSTANCE: {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002549 uint16_t type_index = instruction.VRegB_21c();
Jeff Hao848f70a2014-01-15 13:49:50 -08002550 if (compiler_driver_->IsStringTypeIndex(type_index, dex_file_)) {
Jeff Hao848f70a2014-01-15 13:49:50 -08002551 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002552 HFakeString* fake_string = new (arena_) HFakeString(dex_pc);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002553 current_block_->AddInstruction(fake_string);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002554 UpdateLocal(register_index, fake_string, dex_pc);
Jeff Hao848f70a2014-01-15 13:49:50 -08002555 } else {
2556 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
2557 ? kQuickAllocObjectWithAccessCheck
2558 : kQuickAllocObject;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002559
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002560 current_block_->AddInstruction(new (arena_) HNewInstance(
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002561 graph_->GetCurrentMethod(),
2562 dex_pc,
2563 type_index,
2564 *dex_compilation_unit_->GetDexFile(),
2565 entrypoint));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002566 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Jeff Hao848f70a2014-01-15 13:49:50 -08002567 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002568 break;
2569 }
2570
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002571 case Instruction::NEW_ARRAY: {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002572 uint16_t type_index = instruction.VRegC_22c();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002573 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt, dex_pc);
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002574 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
2575 ? kQuickAllocArrayWithAccessCheck
2576 : kQuickAllocArray;
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002577 current_block_->AddInstruction(new (arena_) HNewArray(length,
2578 graph_->GetCurrentMethod(),
2579 dex_pc,
2580 type_index,
2581 *dex_compilation_unit_->GetDexFile(),
2582 entrypoint));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002583 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002584 break;
2585 }
2586
2587 case Instruction::FILLED_NEW_ARRAY: {
2588 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
2589 uint32_t type_index = instruction.VRegB_35c();
2590 uint32_t args[5];
2591 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00002592 BuildFilledNewArray(dex_pc, type_index, number_of_vreg_arguments, false, args, 0);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002593 break;
2594 }
2595
2596 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2597 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
2598 uint32_t type_index = instruction.VRegB_3rc();
2599 uint32_t register_index = instruction.VRegC_3rc();
2600 BuildFilledNewArray(
Calin Juravle225ff812014-11-13 16:46:39 +00002601 dex_pc, type_index, number_of_vreg_arguments, true, nullptr, register_index);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002602 break;
2603 }
2604
2605 case Instruction::FILL_ARRAY_DATA: {
Calin Juravle225ff812014-11-13 16:46:39 +00002606 BuildFillArrayData(instruction, dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002607 break;
2608 }
2609
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002610 case Instruction::MOVE_RESULT:
Dave Allison20dfc792014-06-16 20:44:29 -07002611 case Instruction::MOVE_RESULT_WIDE:
David Brazdilfc6a86a2015-06-26 10:33:45 +00002612 case Instruction::MOVE_RESULT_OBJECT: {
Nicolas Geoffray1efcc222015-06-24 12:41:20 +01002613 if (latest_result_ == nullptr) {
2614 // Only dead code can lead to this situation, where the verifier
2615 // does not reject the method.
2616 } else {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002617 // An Invoke/FilledNewArray and its MoveResult could have landed in
2618 // different blocks if there was a try/catch block boundary between
2619 // them. For Invoke, we insert a StoreLocal after the instruction. For
2620 // FilledNewArray, the local needs to be updated after the array was
2621 // filled, otherwise we might overwrite an input vreg.
2622 HStoreLocal* update_local =
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002623 new (arena_) HStoreLocal(GetLocalAt(instruction.VRegA()), latest_result_, dex_pc);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002624 HBasicBlock* block = latest_result_->GetBlock();
2625 if (block == current_block_) {
2626 // MoveResult and the previous instruction are in the same block.
2627 current_block_->AddInstruction(update_local);
2628 } else {
2629 // The two instructions are in different blocks. Insert the MoveResult
2630 // before the final control-flow instruction of the previous block.
2631 DCHECK(block->EndsWithControlFlowInstruction());
2632 DCHECK(current_block_->GetInstructions().IsEmpty());
2633 block->InsertInstructionBefore(update_local, block->GetLastInstruction());
2634 }
Nicolas Geoffray1efcc222015-06-24 12:41:20 +01002635 latest_result_ = nullptr;
2636 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002637 break;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002638 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002639
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002640 case Instruction::CMP_LONG: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002641 Binop_23x_cmp(instruction, Primitive::kPrimLong, ComparisonBias::kNoBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002642 break;
2643 }
2644
2645 case Instruction::CMPG_FLOAT: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002646 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kGtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002647 break;
2648 }
2649
2650 case Instruction::CMPG_DOUBLE: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002651 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kGtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002652 break;
2653 }
2654
2655 case Instruction::CMPL_FLOAT: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002656 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kLtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002657 break;
2658 }
2659
2660 case Instruction::CMPL_DOUBLE: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002661 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kLtBias, dex_pc);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002662 break;
2663 }
2664
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002665 case Instruction::NOP:
2666 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002667
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002668 case Instruction::IGET:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002669 case Instruction::IGET_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002670 case Instruction::IGET_WIDE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002671 case Instruction::IGET_WIDE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002672 case Instruction::IGET_OBJECT:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002673 case Instruction::IGET_OBJECT_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002674 case Instruction::IGET_BOOLEAN:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002675 case Instruction::IGET_BOOLEAN_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002676 case Instruction::IGET_BYTE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002677 case Instruction::IGET_BYTE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002678 case Instruction::IGET_CHAR:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002679 case Instruction::IGET_CHAR_QUICK:
2680 case Instruction::IGET_SHORT:
2681 case Instruction::IGET_SHORT_QUICK: {
Calin Juravle225ff812014-11-13 16:46:39 +00002682 if (!BuildInstanceFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002683 return false;
2684 }
2685 break;
2686 }
2687
2688 case Instruction::IPUT:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002689 case Instruction::IPUT_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002690 case Instruction::IPUT_WIDE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002691 case Instruction::IPUT_WIDE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002692 case Instruction::IPUT_OBJECT:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002693 case Instruction::IPUT_OBJECT_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002694 case Instruction::IPUT_BOOLEAN:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002695 case Instruction::IPUT_BOOLEAN_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002696 case Instruction::IPUT_BYTE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002697 case Instruction::IPUT_BYTE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002698 case Instruction::IPUT_CHAR:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002699 case Instruction::IPUT_CHAR_QUICK:
2700 case Instruction::IPUT_SHORT:
2701 case Instruction::IPUT_SHORT_QUICK: {
Calin Juravle225ff812014-11-13 16:46:39 +00002702 if (!BuildInstanceFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002703 return false;
2704 }
2705 break;
2706 }
2707
2708 case Instruction::SGET:
2709 case Instruction::SGET_WIDE:
2710 case Instruction::SGET_OBJECT:
2711 case Instruction::SGET_BOOLEAN:
2712 case Instruction::SGET_BYTE:
2713 case Instruction::SGET_CHAR:
2714 case Instruction::SGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002715 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002716 return false;
2717 }
2718 break;
2719 }
2720
2721 case Instruction::SPUT:
2722 case Instruction::SPUT_WIDE:
2723 case Instruction::SPUT_OBJECT:
2724 case Instruction::SPUT_BOOLEAN:
2725 case Instruction::SPUT_BYTE:
2726 case Instruction::SPUT_CHAR:
2727 case Instruction::SPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002728 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002729 return false;
2730 }
2731 break;
2732 }
2733
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002734#define ARRAY_XX(kind, anticipated_type) \
2735 case Instruction::AGET##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00002736 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002737 break; \
2738 } \
2739 case Instruction::APUT##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00002740 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002741 break; \
2742 }
2743
2744 ARRAY_XX(, Primitive::kPrimInt);
2745 ARRAY_XX(_WIDE, Primitive::kPrimLong);
2746 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
2747 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
2748 ARRAY_XX(_BYTE, Primitive::kPrimByte);
2749 ARRAY_XX(_CHAR, Primitive::kPrimChar);
2750 ARRAY_XX(_SHORT, Primitive::kPrimShort);
2751
Nicolas Geoffray39468442014-09-02 15:17:15 +01002752 case Instruction::ARRAY_LENGTH: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002753 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot, dex_pc);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002754 // No need for a temporary for the null check, it is the only input of the following
2755 // instruction.
Calin Juravle225ff812014-11-13 16:46:39 +00002756 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002757 current_block_->AddInstruction(object);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002758 current_block_->AddInstruction(new (arena_) HArrayLength(object, dex_pc));
2759 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002760 break;
2761 }
2762
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002763 case Instruction::CONST_STRING: {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002764 current_block_->AddInstruction(
2765 new (arena_) HLoadString(graph_->GetCurrentMethod(), instruction.VRegB_21c(), dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002766 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002767 break;
2768 }
2769
2770 case Instruction::CONST_STRING_JUMBO: {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002771 current_block_->AddInstruction(
2772 new (arena_) HLoadString(graph_->GetCurrentMethod(), instruction.VRegB_31c(), dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002773 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002774 break;
2775 }
2776
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002777 case Instruction::CONST_CLASS: {
2778 uint16_t type_index = instruction.VRegB_21c();
2779 bool type_known_final;
2780 bool type_known_abstract;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002781 bool dont_use_is_referrers_class;
2782 // `CanAccessTypeWithoutChecks` will tell whether the method being
2783 // built is trying to access its own class, so that the generated
2784 // code can optimize for this case. However, the optimization does not
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002785 // work for inlining, so we use `IsOutermostCompilingClass` instead.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002786 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
2787 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002788 &type_known_final, &type_known_abstract, &dont_use_is_referrers_class);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002789 if (!can_access) {
Calin Juravle48c2b032014-12-09 18:11:36 +00002790 MaybeRecordStat(MethodCompilationStat::kNotCompiledCantAccesType);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002791 return false;
2792 }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002793 current_block_->AddInstruction(new (arena_) HLoadClass(
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002794 graph_->GetCurrentMethod(),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002795 type_index,
2796 *dex_compilation_unit_->GetDexFile(),
2797 IsOutermostCompilingClass(type_index),
2798 dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002799 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002800 break;
2801 }
2802
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002803 case Instruction::MOVE_EXCEPTION: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002804 current_block_->AddInstruction(new (arena_) HLoadException(dex_pc));
2805 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction(), dex_pc);
2806 current_block_->AddInstruction(new (arena_) HClearException(dex_pc));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002807 break;
2808 }
2809
2810 case Instruction::THROW: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002811 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00002812 current_block_->AddInstruction(new (arena_) HThrow(exception, dex_pc));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002813 // A throw instruction must branch to the exit block.
2814 current_block_->AddSuccessor(exit_block_);
2815 // We finished building this block. Set the current block to null to avoid
2816 // adding dead instructions to it.
2817 current_block_ = nullptr;
2818 break;
2819 }
2820
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002821 case Instruction::INSTANCE_OF: {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002822 uint8_t destination = instruction.VRegA_22c();
2823 uint8_t reference = instruction.VRegB_22c();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002824 uint16_t type_index = instruction.VRegC_22c();
Calin Juravle225ff812014-11-13 16:46:39 +00002825 if (!BuildTypeCheck(instruction, destination, reference, type_index, dex_pc)) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002826 return false;
2827 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002828 break;
2829 }
2830
2831 case Instruction::CHECK_CAST: {
2832 uint8_t reference = instruction.VRegA_21c();
2833 uint16_t type_index = instruction.VRegB_21c();
Calin Juravle225ff812014-11-13 16:46:39 +00002834 if (!BuildTypeCheck(instruction, -1, reference, type_index, dex_pc)) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002835 return false;
2836 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002837 break;
2838 }
2839
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002840 case Instruction::MONITOR_ENTER: {
2841 current_block_->AddInstruction(new (arena_) HMonitorOperation(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002842 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot, dex_pc),
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002843 HMonitorOperation::kEnter,
Calin Juravle225ff812014-11-13 16:46:39 +00002844 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002845 break;
2846 }
2847
2848 case Instruction::MONITOR_EXIT: {
2849 current_block_->AddInstruction(new (arena_) HMonitorOperation(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002850 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot, dex_pc),
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002851 HMonitorOperation::kExit,
Calin Juravle225ff812014-11-13 16:46:39 +00002852 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002853 break;
2854 }
2855
Andreas Gamped881df52014-11-24 23:28:39 -08002856 case Instruction::PACKED_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002857 BuildPackedSwitch(instruction, dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08002858 break;
2859 }
2860
Andreas Gampee4d4d322014-12-04 09:09:57 -08002861 case Instruction::SPARSE_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002862 BuildSparseSwitch(instruction, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08002863 break;
2864 }
2865
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002866 default:
Calin Juravle48c2b032014-12-09 18:11:36 +00002867 VLOG(compiler) << "Did not compile "
2868 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
2869 << " because of unhandled instruction "
2870 << instruction.Name();
2871 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnhandledInstruction);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002872 return false;
2873 }
2874 return true;
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00002875} // NOLINT(readability/fn_size)
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002876
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002877HLocal* HGraphBuilder::GetLocalAt(int register_index) const {
2878 return locals_.Get(register_index);
2879}
2880
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002881void HGraphBuilder::UpdateLocal(int register_index,
2882 HInstruction* instruction,
2883 uint32_t dex_pc) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002884 HLocal* local = GetLocalAt(register_index);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002885 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction, dex_pc));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002886}
2887
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002888HInstruction* HGraphBuilder::LoadLocal(int register_index,
2889 Primitive::Type type,
2890 uint32_t dex_pc) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002891 HLocal* local = GetLocalAt(register_index);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002892 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type, dex_pc));
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002893 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002894}
2895
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002896} // namespace art