blob: cae3da357b9d263df91f1949adf513901c8a55bb [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 Marko9b688a02015-05-06 14:12:42 +010034#include "utils/dex_cache_arrays_layout-inl.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000035
36namespace art {
37
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010038/**
39 * Helper class to add HTemporary instructions. This class is used when
40 * converting a DEX instruction to multiple HInstruction, and where those
41 * instructions do not die at the following instruction, but instead spans
42 * multiple instructions.
43 */
44class Temporaries : public ValueObject {
45 public:
Calin Juravlef97f9fb2014-11-11 15:38:19 +000046 explicit Temporaries(HGraph* graph) : graph_(graph), index_(0) {}
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010047
48 void Add(HInstruction* instruction) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +000049 HInstruction* temp = new (graph_->GetArena()) HTemporary(index_);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010050 instruction->GetBlock()->AddInstruction(temp);
Calin Juravlef97f9fb2014-11-11 15:38:19 +000051
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010052 DCHECK(temp->GetPrevious() == instruction);
Calin Juravlef97f9fb2014-11-11 15:38:19 +000053
54 size_t offset;
55 if (instruction->GetType() == Primitive::kPrimLong
56 || instruction->GetType() == Primitive::kPrimDouble) {
57 offset = 2;
58 } else {
59 offset = 1;
60 }
61 index_ += offset;
62
63 graph_->UpdateTemporariesVRegSlots(index_);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010064 }
65
66 private:
67 HGraph* const graph_;
68
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010069 // Current index in the temporary stack, updated by `Add`.
70 size_t index_;
71};
72
Andreas Gamped881df52014-11-24 23:28:39 -080073class SwitchTable : public ValueObject {
74 public:
75 SwitchTable(const Instruction& instruction, uint32_t dex_pc, bool sparse)
76 : instruction_(instruction), dex_pc_(dex_pc), sparse_(sparse) {
77 int32_t table_offset = instruction.VRegB_31t();
78 const uint16_t* table = reinterpret_cast<const uint16_t*>(&instruction) + table_offset;
79 if (sparse) {
80 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
81 } else {
82 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
83 }
84 num_entries_ = table[1];
85 values_ = reinterpret_cast<const int32_t*>(&table[2]);
86 }
87
88 uint16_t GetNumEntries() const {
89 return num_entries_;
90 }
91
Andreas Gampee4d4d322014-12-04 09:09:57 -080092 void CheckIndex(size_t index) const {
93 if (sparse_) {
94 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
95 DCHECK_LT(index, 2 * static_cast<size_t>(num_entries_));
96 } else {
97 // In a packed table, we have the starting key and num_entries_ values.
98 DCHECK_LT(index, 1 + static_cast<size_t>(num_entries_));
99 }
100 }
101
Andreas Gamped881df52014-11-24 23:28:39 -0800102 int32_t GetEntryAt(size_t index) const {
Andreas Gampee4d4d322014-12-04 09:09:57 -0800103 CheckIndex(index);
Andreas Gamped881df52014-11-24 23:28:39 -0800104 return values_[index];
105 }
106
107 uint32_t GetDexPcForIndex(size_t index) const {
Andreas Gampee4d4d322014-12-04 09:09:57 -0800108 CheckIndex(index);
Andreas Gamped881df52014-11-24 23:28:39 -0800109 return dex_pc_ +
110 (reinterpret_cast<const int16_t*>(values_ + index) -
111 reinterpret_cast<const int16_t*>(&instruction_));
112 }
113
Andreas Gampee4d4d322014-12-04 09:09:57 -0800114 // Index of the first value in the table.
115 size_t GetFirstValueIndex() const {
116 if (sparse_) {
117 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
118 return num_entries_;
119 } else {
120 // In a packed table, we have the starting key and num_entries_ values.
121 return 1;
122 }
123 }
124
Andreas Gamped881df52014-11-24 23:28:39 -0800125 private:
126 const Instruction& instruction_;
127 const uint32_t dex_pc_;
128
129 // Whether this is a sparse-switch table (or a packed-switch one).
130 const bool sparse_;
131
132 // This can't be const as it needs to be computed off of the given instruction, and complicated
133 // expressions in the initializer list seemed very ugly.
134 uint16_t num_entries_;
135
136 const int32_t* values_;
137
138 DISALLOW_COPY_AND_ASSIGN(SwitchTable);
139};
140
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100141void HGraphBuilder::InitializeLocals(uint16_t count) {
142 graph_->SetNumberOfVRegs(count);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000143 locals_.SetSize(count);
144 for (int i = 0; i < count; i++) {
145 HLocal* local = new (arena_) HLocal(i);
146 entry_block_->AddInstruction(local);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000147 locals_.Put(i, local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000148 }
149}
150
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000151void HGraphBuilder::InitializeParameters(uint16_t number_of_parameters) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100152 // dex_compilation_unit_ is null only when unit testing.
153 if (dex_compilation_unit_ == nullptr) {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000154 return;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100155 }
156
157 graph_->SetNumberOfInVRegs(number_of_parameters);
158 const char* shorty = dex_compilation_unit_->GetShorty();
159 int locals_index = locals_.Size() - number_of_parameters;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100160 int parameter_index = 0;
161
162 if (!dex_compilation_unit_->IsStatic()) {
163 // Add the implicit 'this' argument, not expressed in the signature.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100164 HParameterValue* parameter =
Calin Juravle10e244f2015-01-26 18:54:32 +0000165 new (arena_) HParameterValue(parameter_index++, Primitive::kPrimNot, true);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100166 entry_block_->AddInstruction(parameter);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100167 HLocal* local = GetLocalAt(locals_index++);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100168 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100169 number_of_parameters--;
170 }
171
172 uint32_t pos = 1;
173 for (int i = 0; i < number_of_parameters; i++) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100174 HParameterValue* parameter =
175 new (arena_) HParameterValue(parameter_index++, Primitive::GetType(shorty[pos++]));
176 entry_block_->AddInstruction(parameter);
177 HLocal* local = GetLocalAt(locals_index++);
178 // Store the parameter value in the local that the dex code will use
179 // to reference that parameter.
180 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter));
181 bool is_wide = (parameter->GetType() == Primitive::kPrimLong)
182 || (parameter->GetType() == Primitive::kPrimDouble);
183 if (is_wide) {
184 i++;
185 locals_index++;
186 parameter_index++;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100187 }
188 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100189}
190
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100191template<typename T>
Calin Juravle225ff812014-11-13 16:46:39 +0000192void HGraphBuilder::If_22t(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000193 int32_t target_offset = instruction.GetTargetOffset();
David Brazdil852eaff2015-02-02 15:23:05 +0000194 HBasicBlock* branch_target = FindBlockStartingAt(dex_pc + target_offset);
195 HBasicBlock* fallthrough_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
196 DCHECK(branch_target != nullptr);
197 DCHECK(fallthrough_target != nullptr);
198 PotentiallyAddSuspendCheck(branch_target, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100199 HInstruction* first = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
200 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Dave Allison20dfc792014-06-16 20:44:29 -0700201 T* comparison = new (arena_) T(first, second);
202 current_block_->AddInstruction(comparison);
203 HInstruction* ifinst = new (arena_) HIf(comparison);
204 current_block_->AddInstruction(ifinst);
David Brazdil852eaff2015-02-02 15:23:05 +0000205 current_block_->AddSuccessor(branch_target);
206 current_block_->AddSuccessor(fallthrough_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700207 current_block_ = nullptr;
208}
209
210template<typename T>
Calin Juravle225ff812014-11-13 16:46:39 +0000211void HGraphBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000212 int32_t target_offset = instruction.GetTargetOffset();
David Brazdil852eaff2015-02-02 15:23:05 +0000213 HBasicBlock* branch_target = FindBlockStartingAt(dex_pc + target_offset);
214 HBasicBlock* fallthrough_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
215 DCHECK(branch_target != nullptr);
216 DCHECK(fallthrough_target != nullptr);
217 PotentiallyAddSuspendCheck(branch_target, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700218 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000219 T* comparison = new (arena_) T(value, graph_->GetIntConstant(0));
Dave Allison20dfc792014-06-16 20:44:29 -0700220 current_block_->AddInstruction(comparison);
221 HInstruction* ifinst = new (arena_) HIf(comparison);
222 current_block_->AddInstruction(ifinst);
David Brazdil852eaff2015-02-02 15:23:05 +0000223 current_block_->AddSuccessor(branch_target);
224 current_block_->AddSuccessor(fallthrough_target);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100225 current_block_ = nullptr;
226}
227
Calin Juravle48c2b032014-12-09 18:11:36 +0000228void HGraphBuilder::MaybeRecordStat(MethodCompilationStat compilation_stat) {
229 if (compilation_stats_ != nullptr) {
230 compilation_stats_->RecordStat(compilation_stat);
231 }
232}
233
David Brazdil1b498722015-03-31 11:37:18 +0100234bool HGraphBuilder::SkipCompilation(const DexFile::CodeItem& code_item,
Calin Juravle48c2b032014-12-09 18:11:36 +0000235 size_t number_of_branches) {
236 const CompilerOptions& compiler_options = compiler_driver_->GetCompilerOptions();
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000237 CompilerOptions::CompilerFilter compiler_filter = compiler_options.GetCompilerFilter();
238 if (compiler_filter == CompilerOptions::kEverything) {
239 return false;
240 }
241
David Brazdil1b498722015-03-31 11:37:18 +0100242 if (compiler_options.IsHugeMethod(code_item.insns_size_in_code_units_)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000243 VLOG(compiler) << "Skip compilation of huge method "
244 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
David Brazdil1b498722015-03-31 11:37:18 +0100245 << ": " << code_item.insns_size_in_code_units_ << " code units";
Calin Juravle48c2b032014-12-09 18:11:36 +0000246 MaybeRecordStat(MethodCompilationStat::kNotCompiledHugeMethod);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000247 return true;
248 }
249
250 // If it's large and contains no branches, it's likely to be machine generated initialization.
David Brazdil1b498722015-03-31 11:37:18 +0100251 if (compiler_options.IsLargeMethod(code_item.insns_size_in_code_units_)
252 && (number_of_branches == 0)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000253 VLOG(compiler) << "Skip compilation of large method with no branch "
254 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
David Brazdil1b498722015-03-31 11:37:18 +0100255 << ": " << code_item.insns_size_in_code_units_ << " code units";
Calin Juravle48c2b032014-12-09 18:11:36 +0000256 MaybeRecordStat(MethodCompilationStat::kNotCompiledLargeMethodNoBranches);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000257 return true;
258 }
259
260 return false;
261}
262
David Brazdilbff75032015-07-08 17:26:51 +0000263static const DexFile::TryItem* GetTryItem(HBasicBlock* block,
264 const DexFile::CodeItem& code_item,
265 const ArenaBitVector& can_block_throw) {
266 DCHECK(!block->IsSingleTryBoundary());
267
268 // Block does not contain throwing instructions. Even if it is covered by
269 // a TryItem, we will consider it not in a try block.
270 if (!can_block_throw.IsBitSet(block->GetBlockId())) {
271 return nullptr;
272 }
273
274 // Instructions in the block may throw. Find a TryItem covering this block.
275 int32_t try_item_idx = DexFile::FindTryItem(code_item, block->GetDexPc());
David Brazdil6cd788f2015-07-08 16:44:00 +0100276 return (try_item_idx == -1) ? nullptr : DexFile::GetTryItems(code_item, try_item_idx);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000277}
278
279void HGraphBuilder::CreateBlocksForTryCatch(const DexFile::CodeItem& code_item) {
280 if (code_item.tries_size_ == 0) {
281 return;
282 }
283
284 // Create branch targets at the start/end of the TryItem range. These are
285 // places where the program might fall through into/out of the a block and
286 // where TryBoundary instructions will be inserted later. Other edges which
287 // enter/exit the try blocks are a result of branches/switches.
288 for (size_t idx = 0; idx < code_item.tries_size_; ++idx) {
289 const DexFile::TryItem* try_item = DexFile::GetTryItems(code_item, idx);
290 uint32_t dex_pc_start = try_item->start_addr_;
291 uint32_t dex_pc_end = dex_pc_start + try_item->insn_count_;
292 FindOrCreateBlockStartingAt(dex_pc_start);
293 if (dex_pc_end < code_item.insns_size_in_code_units_) {
294 // TODO: Do not create block if the last instruction cannot fall through.
295 FindOrCreateBlockStartingAt(dex_pc_end);
296 } else {
297 // The TryItem spans until the very end of the CodeItem (or beyond if
298 // invalid) and therefore cannot have any code afterwards.
299 }
300 }
301
302 // Create branch targets for exception handlers.
303 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(code_item, 0);
304 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
305 for (uint32_t idx = 0; idx < handlers_size; ++idx) {
306 CatchHandlerIterator iterator(handlers_ptr);
307 for (; iterator.HasNext(); iterator.Next()) {
308 uint32_t address = iterator.GetHandlerAddress();
309 HBasicBlock* block = FindOrCreateBlockStartingAt(address);
310 block->SetIsCatchBlock();
311 }
312 handlers_ptr = iterator.EndDataPointer();
313 }
314}
315
David Brazdil56e1acc2015-06-30 15:41:36 +0100316void HGraphBuilder::SplitTryBoundaryEdge(HBasicBlock* predecessor,
317 HBasicBlock* successor,
318 HTryBoundary::BoundaryKind kind,
319 const DexFile::CodeItem& code_item,
320 const DexFile::TryItem& try_item) {
321 // Split the edge with a single TryBoundary instruction.
322 HTryBoundary* try_boundary = new (arena_) HTryBoundary(kind);
323 HBasicBlock* try_entry_block = graph_->SplitEdge(predecessor, successor);
324 try_entry_block->AddInstruction(try_boundary);
325
326 // Link the TryBoundary to the handlers of `try_item`.
327 for (CatchHandlerIterator it(code_item, try_item); it.HasNext(); it.Next()) {
328 try_boundary->AddExceptionHandler(FindBlockStartingAt(it.GetHandlerAddress()));
329 }
330}
331
David Brazdilfc6a86a2015-06-26 10:33:45 +0000332void HGraphBuilder::InsertTryBoundaryBlocks(const DexFile::CodeItem& code_item) {
333 if (code_item.tries_size_ == 0) {
334 return;
335 }
336
David Brazdil72783ff2015-07-09 14:36:05 +0100337 // Bit vector stores information on which blocks contain throwing instructions.
338 // Must be expandable because catch blocks may be split into two.
339 ArenaBitVector can_block_throw(arena_, graph_->GetBlocks().Size(), /* expandable */ true);
David Brazdilbff75032015-07-08 17:26:51 +0000340
341 // Scan blocks and mark those which contain throwing instructions.
David Brazdil72783ff2015-07-09 14:36:05 +0100342 for (size_t block_id = 0, e = graph_->GetBlocks().Size(); block_id < e; ++block_id) {
David Brazdilbff75032015-07-08 17:26:51 +0000343 HBasicBlock* block = graph_->GetBlocks().Get(block_id);
David Brazdil72783ff2015-07-09 14:36:05 +0100344 bool can_throw = false;
David Brazdilbff75032015-07-08 17:26:51 +0000345 for (HInstructionIterator insn(block->GetInstructions()); !insn.Done(); insn.Advance()) {
346 if (insn.Current()->CanThrow()) {
David Brazdil72783ff2015-07-09 14:36:05 +0100347 can_throw = true;
David Brazdilbff75032015-07-08 17:26:51 +0000348 break;
349 }
350 }
David Brazdil72783ff2015-07-09 14:36:05 +0100351
352 if (can_throw) {
353 if (block->IsCatchBlock()) {
354 // Catch blocks are always considered an entry point into the TryItem in
355 // order to avoid splitting exceptional edges. We split the block after
356 // the move-exception (if present) and mark the first part non-throwing.
357 // Later on, a TryBoundary will be inserted between the two blocks.
358 HInstruction* first_insn = block->GetFirstInstruction();
359 if (first_insn->IsLoadException()) {
360 // Catch block starts with a LoadException. Split the block after the
David Brazdilcb1c0552015-08-04 16:22:25 +0100361 // StoreLocal and ClearException which must come after the load.
David Brazdil72783ff2015-07-09 14:36:05 +0100362 DCHECK(first_insn->GetNext()->IsStoreLocal());
David Brazdilcb1c0552015-08-04 16:22:25 +0100363 DCHECK(first_insn->GetNext()->GetNext()->IsClearException());
364 block = block->SplitBefore(first_insn->GetNext()->GetNext()->GetNext());
David Brazdil72783ff2015-07-09 14:36:05 +0100365 } else {
366 // Catch block does not load the exception. Split at the beginning to
367 // create an empty catch block.
368 block = block->SplitBefore(first_insn);
369 }
370 }
371 can_block_throw.SetBit(block->GetBlockId());
372 }
David Brazdilbff75032015-07-08 17:26:51 +0000373 }
374
David Brazdil281a6322015-07-03 10:34:57 +0100375 // Iterate over all blocks, find those covered by some TryItem and:
376 // (a) split edges which enter/exit the try range,
377 // (b) create TryBoundary instructions in the new blocks,
378 // (c) link the new blocks to corresponding exception handlers.
379 // We cannot iterate only over blocks in `branch_targets_` because switch-case
380 // blocks share the same dex_pc.
David Brazdil72783ff2015-07-09 14:36:05 +0100381 for (size_t block_id = 0, e = graph_->GetBlocks().Size(); block_id < e; ++block_id) {
David Brazdil281a6322015-07-03 10:34:57 +0100382 HBasicBlock* try_block = graph_->GetBlocks().Get(block_id);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000383
David Brazdil281a6322015-07-03 10:34:57 +0100384 // TryBoundary blocks are added at the end of the list and not iterated over.
385 DCHECK(!try_block->IsSingleTryBoundary());
David Brazdilfc6a86a2015-06-26 10:33:45 +0000386
David Brazdil281a6322015-07-03 10:34:57 +0100387 // Find the TryItem for this block.
David Brazdilbff75032015-07-08 17:26:51 +0000388 const DexFile::TryItem* try_item = GetTryItem(try_block, code_item, can_block_throw);
389 if (try_item == nullptr) {
David Brazdil281a6322015-07-03 10:34:57 +0100390 continue;
391 }
David Brazdil281a6322015-07-03 10:34:57 +0100392
David Brazdil72783ff2015-07-09 14:36:05 +0100393 // Catch blocks were split earlier and cannot throw.
394 DCHECK(!try_block->IsCatchBlock());
395
396 // Find predecessors which are not covered by the same TryItem range. Such
397 // edges enter the try block and will have a TryBoundary inserted.
398 for (size_t i = 0; i < try_block->GetPredecessors().Size(); ++i) {
399 HBasicBlock* predecessor = try_block->GetPredecessors().Get(i);
400 if (predecessor->IsSingleTryBoundary()) {
401 // The edge was already split because of an exit from a neighbouring
402 // TryItem. We split it again and insert an entry point.
403 if (kIsDebugBuild) {
404 HTryBoundary* last_insn = predecessor->GetLastInstruction()->AsTryBoundary();
405 const DexFile::TryItem* predecessor_try_item =
406 GetTryItem(predecessor->GetSinglePredecessor(), code_item, can_block_throw);
407 DCHECK(!last_insn->IsEntry());
408 DCHECK_EQ(last_insn->GetNormalFlowSuccessor(), try_block);
409 DCHECK(try_block->IsFirstIndexOfPredecessor(predecessor, i));
410 DCHECK_NE(try_item, predecessor_try_item);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000411 }
David Brazdil72783ff2015-07-09 14:36:05 +0100412 } else if (GetTryItem(predecessor, code_item, can_block_throw) != try_item) {
413 // This is an entry point into the TryItem and the edge has not been
414 // split yet. That means that `predecessor` is not in a TryItem, or
415 // it is in a different TryItem and we happened to iterate over this
416 // block first. We split the edge and insert an entry point.
417 } else {
418 // Not an edge on the boundary of the try block.
419 continue;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000420 }
David Brazdil72783ff2015-07-09 14:36:05 +0100421 SplitTryBoundaryEdge(predecessor, try_block, HTryBoundary::kEntry, code_item, *try_item);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000422 }
David Brazdil281a6322015-07-03 10:34:57 +0100423
424 // Find successors which are not covered by the same TryItem range. Such
425 // edges exit the try block and will have a TryBoundary inserted.
426 for (size_t i = 0; i < try_block->GetSuccessors().Size(); ++i) {
427 HBasicBlock* successor = try_block->GetSuccessors().Get(i);
428 if (successor->IsCatchBlock()) {
429 // A catch block is always considered an entry point into its TryItem.
430 // We therefore assume this is an exit point, regardless of whether
431 // the catch block is in a different TryItem or not.
432 } else if (successor->IsSingleTryBoundary()) {
433 // The edge was already split because of an entry into a neighbouring
434 // TryItem. We split it again and insert an exit.
435 if (kIsDebugBuild) {
436 HTryBoundary* last_insn = successor->GetLastInstruction()->AsTryBoundary();
David Brazdilbff75032015-07-08 17:26:51 +0000437 const DexFile::TryItem* successor_try_item =
438 GetTryItem(last_insn->GetNormalFlowSuccessor(), code_item, can_block_throw);
David Brazdil281a6322015-07-03 10:34:57 +0100439 DCHECK_EQ(try_block, successor->GetSinglePredecessor());
440 DCHECK(last_insn->IsEntry());
David Brazdilbff75032015-07-08 17:26:51 +0000441 DCHECK_NE(try_item, successor_try_item);
David Brazdil281a6322015-07-03 10:34:57 +0100442 }
David Brazdilbff75032015-07-08 17:26:51 +0000443 } else if (GetTryItem(successor, code_item, can_block_throw) != try_item) {
David Brazdil281a6322015-07-03 10:34:57 +0100444 // This is an exit out of the TryItem and the edge has not been split
445 // yet. That means that either `successor` is not in a TryItem, or it
446 // is in a different TryItem and we happened to iterate over this
447 // block first. We split the edge and insert an exit.
448 HInstruction* last_instruction = try_block->GetLastInstruction();
449 if (last_instruction->IsReturn() || last_instruction->IsReturnVoid()) {
450 DCHECK_EQ(successor, exit_block_);
451 // Control flow exits the try block with a Return(Void). Because
452 // splitting the edge would invalidate the invariant that Return
453 // always jumps to Exit, we move the Return outside the try block.
454 successor = try_block->SplitBefore(last_instruction);
455 }
456 } else {
457 // Not an edge on the boundary of the try block.
458 continue;
459 }
David Brazdilbff75032015-07-08 17:26:51 +0000460 SplitTryBoundaryEdge(try_block, successor, HTryBoundary::kExit, code_item, *try_item);
David Brazdil281a6322015-07-03 10:34:57 +0100461 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000462 }
463}
464
David Brazdil5e8b1372015-01-23 14:39:08 +0000465bool HGraphBuilder::BuildGraph(const DexFile::CodeItem& code_item) {
466 DCHECK(graph_->GetBlocks().IsEmpty());
467
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000468 const uint16_t* code_ptr = code_item.insns_;
469 const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100470 code_start_ = code_ptr;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000471
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000472 // Setup the graph with the entry block and exit block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100473 entry_block_ = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000474 graph_->AddBlock(entry_block_);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100475 exit_block_ = new (arena_) HBasicBlock(graph_, kNoDexPc);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000476 graph_->SetEntryBlock(entry_block_);
477 graph_->SetExitBlock(exit_block_);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000478
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000479 InitializeLocals(code_item.registers_size_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000480 graph_->SetMaximumNumberOfOutVRegs(code_item.outs_size_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000481
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000482 // Compute the number of dex instructions, blocks, and branches. We will
483 // check these values against limits given to the compiler.
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000484 size_t number_of_branches = 0;
485
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000486 // To avoid splitting blocks, we compute ahead of time the instructions that
487 // start a new block, and create these blocks.
Calin Juravle702d2602015-04-30 19:28:21 +0100488 if (!ComputeBranchTargets(code_ptr, code_end, &number_of_branches)) {
489 MaybeRecordStat(MethodCompilationStat::kNotCompiledBranchOutsideMethodCode);
490 return false;
491 }
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000492
493 // Note that the compiler driver is null when unit testing.
David Brazdil1b498722015-03-31 11:37:18 +0100494 if ((compiler_driver_ != nullptr) && SkipCompilation(code_item, number_of_branches)) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000495 return false;
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000496 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000497
David Brazdilfc6a86a2015-06-26 10:33:45 +0000498 CreateBlocksForTryCatch(code_item);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000499
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000500 InitializeParameters(code_item.ins_size_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100501
Calin Juravle225ff812014-11-13 16:46:39 +0000502 size_t dex_pc = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000503 while (code_ptr < code_end) {
Calin Juravle225ff812014-11-13 16:46:39 +0000504 // Update the current block if dex_pc starts a new block.
505 MaybeUpdateCurrentBlock(dex_pc);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000506 const Instruction& instruction = *Instruction::At(code_ptr);
Calin Juravle48c2b032014-12-09 18:11:36 +0000507 if (!AnalyzeDexInstruction(instruction, dex_pc)) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000508 return false;
Calin Juravle48c2b032014-12-09 18:11:36 +0000509 }
Calin Juravle225ff812014-11-13 16:46:39 +0000510 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000511 code_ptr += instruction.SizeInCodeUnits();
512 }
513
David Brazdilfc6a86a2015-06-26 10:33:45 +0000514 // Add Exit to the exit block.
David Brazdil3e187382015-06-26 09:59:52 +0000515 exit_block_->AddInstruction(new (arena_) HExit());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000516 // Add the suspend check to the entry block.
517 entry_block_->AddInstruction(new (arena_) HSuspendCheck(0));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000518 entry_block_->AddInstruction(new (arena_) HGoto());
David Brazdilbff75032015-07-08 17:26:51 +0000519 // Add the exit block at the end.
520 graph_->AddBlock(exit_block_);
David Brazdil5e8b1372015-01-23 14:39:08 +0000521
David Brazdilfc6a86a2015-06-26 10:33:45 +0000522 // Iterate over blocks covered by TryItems and insert TryBoundaries at entry
523 // and exit points. This requires all control-flow instructions and
524 // non-exceptional edges to have been created.
525 InsertTryBoundaryBlocks(code_item);
526
David Brazdil5e8b1372015-01-23 14:39:08 +0000527 return true;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000528}
529
David Brazdilfc6a86a2015-06-26 10:33:45 +0000530void HGraphBuilder::MaybeUpdateCurrentBlock(size_t dex_pc) {
531 HBasicBlock* block = FindBlockStartingAt(dex_pc);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000532 if (block == nullptr) {
533 return;
534 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000535
536 if (current_block_ != nullptr) {
537 // Branching instructions clear current_block, so we know
538 // the last instruction of the current block is not a branching
539 // instruction. We add an unconditional goto to the found block.
540 current_block_->AddInstruction(new (arena_) HGoto());
541 current_block_->AddSuccessor(block);
542 }
543 graph_->AddBlock(block);
544 current_block_ = block;
545}
546
Calin Juravle702d2602015-04-30 19:28:21 +0100547bool HGraphBuilder::ComputeBranchTargets(const uint16_t* code_ptr,
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000548 const uint16_t* code_end,
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000549 size_t* number_of_branches) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000550 branch_targets_.SetSize(code_end - code_ptr);
551
552 // Create the first block for the dex instructions, single successor of the entry block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100553 HBasicBlock* block = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000554 branch_targets_.Put(0, block);
555 entry_block_->AddSuccessor(block);
556
557 // Iterate over all instructions and find branching instructions. Create blocks for
558 // the locations these instructions branch to.
Andreas Gamped881df52014-11-24 23:28:39 -0800559 uint32_t dex_pc = 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000560 while (code_ptr < code_end) {
561 const Instruction& instruction = *Instruction::At(code_ptr);
562 if (instruction.IsBranch()) {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000563 (*number_of_branches)++;
Calin Juravle225ff812014-11-13 16:46:39 +0000564 int32_t target = instruction.GetTargetOffset() + dex_pc;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000565 // Create a block for the target instruction.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000566 FindOrCreateBlockStartingAt(target);
567
Calin Juravle225ff812014-11-13 16:46:39 +0000568 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000569 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle702d2602015-04-30 19:28:21 +0100570
David Brazdilfe659462015-06-24 14:23:56 +0100571 if (instruction.CanFlowThrough()) {
572 if (code_ptr >= code_end) {
Calin Juravle702d2602015-04-30 19:28:21 +0100573 // In the normal case we should never hit this but someone can artificially forge a dex
574 // file to fall-through out the method code. In this case we bail out compilation.
575 return false;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000576 } else {
577 FindOrCreateBlockStartingAt(dex_pc);
Calin Juravle702d2602015-04-30 19:28:21 +0100578 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000579 }
Andreas Gampee4d4d322014-12-04 09:09:57 -0800580 } else if (instruction.IsSwitch()) {
581 SwitchTable table(instruction, dex_pc, instruction.Opcode() == Instruction::SPARSE_SWITCH);
Andreas Gamped881df52014-11-24 23:28:39 -0800582
583 uint16_t num_entries = table.GetNumEntries();
584
Andreas Gampee4d4d322014-12-04 09:09:57 -0800585 // In a packed-switch, the entry at index 0 is the starting key. In a sparse-switch, the
586 // entry at index 0 is the first key, and values are after *all* keys.
587 size_t offset = table.GetFirstValueIndex();
588
589 // Use a larger loop counter type to avoid overflow issues.
590 for (size_t i = 0; i < num_entries; ++i) {
Andreas Gamped881df52014-11-24 23:28:39 -0800591 // The target of the case.
Andreas Gampee4d4d322014-12-04 09:09:57 -0800592 uint32_t target = dex_pc + table.GetEntryAt(i + offset);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000593 FindOrCreateBlockStartingAt(target);
Andreas Gamped881df52014-11-24 23:28:39 -0800594
David Brazdil281a6322015-07-03 10:34:57 +0100595 // Create a block for the switch-case logic. The block gets the dex_pc
596 // of the SWITCH instruction because it is part of its semantics.
597 block = new (arena_) HBasicBlock(graph_, dex_pc);
598 branch_targets_.Put(table.GetDexPcForIndex(i), block);
Andreas Gamped881df52014-11-24 23:28:39 -0800599 }
600
601 // Fall-through. Add a block if there is more code afterwards.
602 dex_pc += instruction.SizeInCodeUnits();
603 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle702d2602015-04-30 19:28:21 +0100604 if (code_ptr >= code_end) {
605 // In the normal case we should never hit this but someone can artificially forge a dex
606 // file to fall-through out the method code. In this case we bail out compilation.
607 // (A switch can fall-through so we don't need to check CanFlowThrough().)
608 return false;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000609 } else {
610 FindOrCreateBlockStartingAt(dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -0800611 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000612 } else {
613 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle225ff812014-11-13 16:46:39 +0000614 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000615 }
616 }
Calin Juravle702d2602015-04-30 19:28:21 +0100617 return true;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000618}
619
David Brazdilfc6a86a2015-06-26 10:33:45 +0000620HBasicBlock* HGraphBuilder::FindBlockStartingAt(int32_t dex_pc) const {
621 DCHECK_GE(dex_pc, 0);
622 DCHECK_LT(static_cast<size_t>(dex_pc), branch_targets_.Size());
623 return branch_targets_.Get(dex_pc);
624}
625
626HBasicBlock* HGraphBuilder::FindOrCreateBlockStartingAt(int32_t dex_pc) {
627 HBasicBlock* block = FindBlockStartingAt(dex_pc);
628 if (block == nullptr) {
629 block = new (arena_) HBasicBlock(graph_, dex_pc);
630 branch_targets_.Put(dex_pc, block);
631 }
632 return block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000633}
634
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100635template<typename T>
Roland Levillain88cb1752014-10-20 16:36:47 +0100636void HGraphBuilder::Unop_12x(const Instruction& instruction, Primitive::Type type) {
637 HInstruction* first = LoadLocal(instruction.VRegB(), type);
638 current_block_->AddInstruction(new (arena_) T(type, first));
639 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
640}
641
Roland Levillaindff1f282014-11-05 14:15:05 +0000642void HGraphBuilder::Conversion_12x(const Instruction& instruction,
643 Primitive::Type input_type,
Roland Levillain624279f2014-12-04 11:54:28 +0000644 Primitive::Type result_type,
645 uint32_t dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +0000646 HInstruction* first = LoadLocal(instruction.VRegB(), input_type);
Roland Levillain624279f2014-12-04 11:54:28 +0000647 current_block_->AddInstruction(new (arena_) HTypeConversion(result_type, first, dex_pc));
Roland Levillaindff1f282014-11-05 14:15:05 +0000648 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
649}
650
Roland Levillain88cb1752014-10-20 16:36:47 +0100651template<typename T>
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100652void HGraphBuilder::Binop_23x(const Instruction& instruction, Primitive::Type type) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100653 HInstruction* first = LoadLocal(instruction.VRegB(), type);
654 HInstruction* second = LoadLocal(instruction.VRegC(), type);
655 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100656 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
657}
658
659template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000660void HGraphBuilder::Binop_23x(const Instruction& instruction,
661 Primitive::Type type,
662 uint32_t dex_pc) {
663 HInstruction* first = LoadLocal(instruction.VRegB(), type);
664 HInstruction* second = LoadLocal(instruction.VRegC(), type);
665 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
666 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
667}
668
669template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000670void HGraphBuilder::Binop_23x_shift(const Instruction& instruction,
671 Primitive::Type type) {
672 HInstruction* first = LoadLocal(instruction.VRegB(), type);
673 HInstruction* second = LoadLocal(instruction.VRegC(), Primitive::kPrimInt);
674 current_block_->AddInstruction(new (arena_) T(type, first, second));
675 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
676}
677
Calin Juravleddb7df22014-11-25 20:56:51 +0000678void HGraphBuilder::Binop_23x_cmp(const Instruction& instruction,
679 Primitive::Type type,
Mark Mendellc4701932015-04-10 13:18:51 -0400680 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700681 uint32_t dex_pc) {
Calin Juravleddb7df22014-11-25 20:56:51 +0000682 HInstruction* first = LoadLocal(instruction.VRegB(), type);
683 HInstruction* second = LoadLocal(instruction.VRegC(), type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700684 current_block_->AddInstruction(new (arena_) HCompare(type, first, second, bias, dex_pc));
Calin Juravleddb7df22014-11-25 20:56:51 +0000685 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
686}
687
Calin Juravle9aec02f2014-11-18 23:06:35 +0000688template<typename T>
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100689void HGraphBuilder::Binop_12x(const Instruction& instruction, Primitive::Type type) {
690 HInstruction* first = LoadLocal(instruction.VRegA(), type);
691 HInstruction* second = LoadLocal(instruction.VRegB(), type);
692 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100693 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
694}
695
696template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000697void HGraphBuilder::Binop_12x_shift(const Instruction& instruction, Primitive::Type type) {
698 HInstruction* first = LoadLocal(instruction.VRegA(), type);
699 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
700 current_block_->AddInstruction(new (arena_) T(type, first, second));
701 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
702}
703
704template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000705void HGraphBuilder::Binop_12x(const Instruction& instruction,
706 Primitive::Type type,
707 uint32_t dex_pc) {
708 HInstruction* first = LoadLocal(instruction.VRegA(), type);
709 HInstruction* second = LoadLocal(instruction.VRegB(), type);
710 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
711 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
712}
713
714template<typename T>
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100715void HGraphBuilder::Binop_22s(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100716 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000717 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100718 if (reverse) {
719 std::swap(first, second);
720 }
721 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
722 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
723}
724
725template<typename T>
726void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100727 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000728 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100729 if (reverse) {
730 std::swap(first, second);
731 }
732 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
733 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
734}
735
Calin Juravle0c25d102015-04-20 14:49:09 +0100736static bool RequiresConstructorBarrier(const DexCompilationUnit* cu, const CompilerDriver& driver) {
Calin Juravle27df7582015-04-17 19:12:31 +0100737 Thread* self = Thread::Current();
Calin Juravle0c25d102015-04-20 14:49:09 +0100738 return cu->IsConstructor()
739 && driver.RequiresConstructorBarrier(self, cu->GetDexFile(), cu->GetClassDefIndex());
Calin Juravle27df7582015-04-17 19:12:31 +0100740}
741
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100742void HGraphBuilder::BuildReturn(const Instruction& instruction, Primitive::Type type) {
743 if (type == Primitive::kPrimVoid) {
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100744 if (graph_->ShouldGenerateConstructorBarrier()) {
745 // The compilation unit is null during testing.
746 if (dex_compilation_unit_ != nullptr) {
747 DCHECK(RequiresConstructorBarrier(dex_compilation_unit_, *compiler_driver_))
748 << "Inconsistent use of ShouldGenerateConstructorBarrier. Should not generate a barrier.";
749 }
Calin Juravle27df7582015-04-17 19:12:31 +0100750 current_block_->AddInstruction(new (arena_) HMemoryBarrier(kStoreStore));
751 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100752 current_block_->AddInstruction(new (arena_) HReturnVoid());
753 } else {
754 HInstruction* value = LoadLocal(instruction.VRegA(), type);
755 current_block_->AddInstruction(new (arena_) HReturn(value));
756 }
757 current_block_->AddSuccessor(exit_block_);
758 current_block_ = nullptr;
759}
760
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100761void HGraphBuilder::PotentiallySimplifyFakeString(uint16_t original_dex_register,
762 uint32_t dex_pc,
763 HInvoke* actual_string) {
764 if (!graph_->IsDebuggable()) {
765 // Notify that we cannot compile with baseline. The dex registers aliasing
766 // with `original_dex_register` will be handled when we optimize
767 // (see HInstructionSimplifer::VisitFakeString).
768 can_use_baseline_for_string_init_ = false;
769 return;
770 }
771 const VerifiedMethod* verified_method =
772 compiler_driver_->GetVerifiedMethod(dex_file_, dex_compilation_unit_->GetDexMethodIndex());
773 if (verified_method != nullptr) {
774 UpdateLocal(original_dex_register, actual_string);
775 const SafeMap<uint32_t, std::set<uint32_t>>& string_init_map =
776 verified_method->GetStringInitPcRegMap();
777 auto map_it = string_init_map.find(dex_pc);
778 if (map_it != string_init_map.end()) {
779 std::set<uint32_t> reg_set = map_it->second;
780 for (auto set_it = reg_set.begin(); set_it != reg_set.end(); ++set_it) {
781 HInstruction* load_local = LoadLocal(original_dex_register, Primitive::kPrimNot);
782 UpdateLocal(*set_it, load_local);
783 }
784 }
785 } else {
786 can_use_baseline_for_string_init_ = false;
787 }
788}
789
Vladimir Marko9b688a02015-05-06 14:12:42 +0100790HInvokeStaticOrDirect::DispatchInfo HGraphBuilder::ComputeDispatchInfo(
791 bool is_string_init,
792 int32_t string_init_offset,
793 MethodReference target_method,
794 uintptr_t direct_method,
795 uintptr_t direct_code) {
796 HInvokeStaticOrDirect::MethodLoadKind method_load_kind;
797 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location;
798 uint64_t method_load_data = 0u;
799 uint64_t direct_code_ptr = 0u;
800
801 if (is_string_init) {
802 // TODO: Use direct_method and direct_code for the appropriate StringFactory method.
803 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kStringInit;
804 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
805 method_load_data = string_init_offset;
806 } else if (target_method.dex_file == outer_compilation_unit_->GetDexFile() &&
807 target_method.dex_method_index == outer_compilation_unit_->GetDexMethodIndex()) {
808 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kRecursive;
809 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallSelf;
810 } else {
811 if (direct_method != 0u) { // Should we use a direct pointer to the method?
812 if (direct_method != static_cast<uintptr_t>(-1)) { // Is the method pointer known now?
813 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress;
814 method_load_data = direct_method;
815 } else { // The direct pointer will be known at link time.
816 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup;
817 }
818 } else { // Use dex cache.
819 DCHECK(target_method.dex_file == dex_compilation_unit_->GetDexFile());
820 DexCacheArraysLayout layout =
821 compiler_driver_->GetDexCacheArraysLayout(target_method.dex_file);
822 if (layout.Valid()) { // Can we use PC-relative access to the dex cache arrays?
823 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative;
824 method_load_data = layout.MethodOffset(target_method.dex_method_index);
825 } else { // We must go through the ArtMethod's pointer to resolved methods.
826 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
827 }
828 }
829 if (direct_code != 0u) { // Should we use a direct pointer to the code?
830 if (direct_code != static_cast<uintptr_t>(-1)) { // Is the code pointer known now?
831 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallDirect;
832 direct_code_ptr = direct_code;
833 } else if (compiler_driver_->IsImage() ||
834 target_method.dex_file == dex_compilation_unit_->GetDexFile()) {
835 // Use PC-relative calls for invokes within a multi-dex oat file.
836 // TODO: Recognize when the target dex file is within the current oat file for
837 // app compilation. At the moment we recognize only the boot image as multi-dex.
838 // NOTE: This will require changing the ARM backend which currently falls
839 // through from kCallPCRelative to kDirectCodeFixup for different dex files.
840 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative;
841 } else { // The direct pointer will be known at link time.
842 // NOTE: This is used for app->boot calls when compiling an app against
843 // a relocatable but not yet relocated image.
844 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup;
845 }
846 } else { // We must use the code pointer from the ArtMethod.
847 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
848 }
849 }
850
851 if (graph_->IsDebuggable()) {
852 // For debuggable apps always use the code pointer from ArtMethod
853 // so that we don't circumvent instrumentation stubs if installed.
854 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
855 }
856
857 return HInvokeStaticOrDirect::DispatchInfo {
858 method_load_kind, code_ptr_location, method_load_data, direct_code_ptr };
859}
860
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100861bool HGraphBuilder::BuildInvoke(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000862 uint32_t dex_pc,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100863 uint32_t method_idx,
864 uint32_t number_of_vreg_arguments,
865 bool is_range,
866 uint32_t* args,
867 uint32_t register_index) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100868 Instruction::Code opcode = instruction.Opcode();
869 InvokeType invoke_type;
870 switch (opcode) {
871 case Instruction::INVOKE_STATIC:
872 case Instruction::INVOKE_STATIC_RANGE:
873 invoke_type = kStatic;
874 break;
875 case Instruction::INVOKE_DIRECT:
876 case Instruction::INVOKE_DIRECT_RANGE:
877 invoke_type = kDirect;
878 break;
879 case Instruction::INVOKE_VIRTUAL:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +0000880 case Instruction::INVOKE_VIRTUAL_QUICK:
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100881 case Instruction::INVOKE_VIRTUAL_RANGE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +0000882 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100883 invoke_type = kVirtual;
884 break;
885 case Instruction::INVOKE_INTERFACE:
886 case Instruction::INVOKE_INTERFACE_RANGE:
887 invoke_type = kInterface;
888 break;
889 case Instruction::INVOKE_SUPER_RANGE:
890 case Instruction::INVOKE_SUPER:
891 invoke_type = kSuper;
892 break;
893 default:
894 LOG(FATAL) << "Unexpected invoke op: " << opcode;
895 return false;
896 }
897
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100898 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
899 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(method_id.proto_idx_);
900 const char* descriptor = dex_file_->StringDataByIdx(proto_id.shorty_idx_);
901 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100902 bool is_instance_call = invoke_type != kStatic;
Nicolas Geoffray2e335252015-06-18 11:11:27 +0100903 // Remove the return type from the 'proto'.
904 size_t number_of_arguments = strlen(descriptor) - 1;
905 if (is_instance_call) {
906 // One extra argument for 'this'.
907 ++number_of_arguments;
908 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100909
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000910 MethodReference target_method(dex_file_, method_idx);
911 uintptr_t direct_code;
912 uintptr_t direct_method;
913 int table_index;
914 InvokeType optimized_invoke_type = invoke_type;
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000915
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000916 if (!compiler_driver_->ComputeInvokeInfo(dex_compilation_unit_, dex_pc, true, true,
917 &optimized_invoke_type, &target_method, &table_index,
918 &direct_code, &direct_method)) {
Nicolas Geoffray2e335252015-06-18 11:11:27 +0100919 VLOG(compiler) << "Did not compile "
920 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
Calin Juravle48c2b032014-12-09 18:11:36 +0000921 << " because a method call could not be resolved";
922 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedMethod);
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000923 return false;
924 }
925 DCHECK(optimized_invoke_type != kSuper);
926
Roland Levillain4c0eb422015-04-24 16:43:49 +0100927 // By default, consider that the called method implicitly requires
928 // an initialization check of its declaring method.
929 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement =
930 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
931 // Potential class initialization check, in the case of a static method call.
932 HClinitCheck* clinit_check = nullptr;
Jeff Hao848f70a2014-01-15 13:49:50 -0800933 // Replace calls to String.<init> with StringFactory.
934 int32_t string_init_offset = 0;
935 bool is_string_init = compiler_driver_->IsStringInit(method_idx, dex_file_, &string_init_offset);
936 if (is_string_init) {
937 return_type = Primitive::kPrimNot;
938 is_instance_call = false;
939 number_of_arguments--;
940 invoke_type = kStatic;
941 optimized_invoke_type = kStatic;
942 }
Roland Levillain4c0eb422015-04-24 16:43:49 +0100943
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000944 HInvoke* invoke = nullptr;
Roland Levillain4c0eb422015-04-24 16:43:49 +0100945
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000946 if (optimized_invoke_type == kVirtual) {
947 invoke = new (arena_) HInvokeVirtual(
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800948 arena_, number_of_arguments, return_type, dex_pc, method_idx, table_index);
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000949 } else if (optimized_invoke_type == kInterface) {
950 invoke = new (arena_) HInvokeInterface(
951 arena_, number_of_arguments, return_type, dex_pc, method_idx, table_index);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100952 } else {
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000953 DCHECK(optimized_invoke_type == kDirect || optimized_invoke_type == kStatic);
Roland Levillain4c0eb422015-04-24 16:43:49 +0100954
Jeff Haocad65422015-06-18 21:16:08 -0700955 if (optimized_invoke_type == kStatic && !is_string_init) {
Roland Levillain4c0eb422015-04-24 16:43:49 +0100956 ScopedObjectAccess soa(Thread::Current());
957 StackHandleScope<4> hs(soa.Self());
958 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
959 dex_compilation_unit_->GetClassLinker()->FindDexCache(
960 *dex_compilation_unit_->GetDexFile())));
961 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
962 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700963 ArtMethod* resolved_method = compiler_driver_->ResolveMethod(
964 soa, dex_cache, class_loader, dex_compilation_unit_, method_idx, optimized_invoke_type);
Roland Levillain4c0eb422015-04-24 16:43:49 +0100965
966 if (resolved_method == nullptr) {
967 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedMethod);
968 return false;
969 }
970
971 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
972 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
973 outer_compilation_unit_->GetClassLinker()->FindDexCache(outer_dex_file)));
Nicolas Geoffrayafd06412015-06-20 22:44:47 +0100974 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Roland Levillain4c0eb422015-04-24 16:43:49 +0100975
976 // The index at which the method's class is stored in the DexCache's type array.
977 uint32_t storage_index = DexFile::kDexNoIndex;
Nicolas Geoffrayafd06412015-06-20 22:44:47 +0100978 bool is_outer_class = (resolved_method->GetDeclaringClass() == outer_class.Get());
979 if (is_outer_class) {
980 storage_index = outer_class->GetDexTypeIndex();
Roland Levillain4c0eb422015-04-24 16:43:49 +0100981 } else if (outer_dex_cache.Get() == dex_cache.Get()) {
982 // Get `storage_index` from IsClassOfStaticMethodAvailableToReferrer.
983 compiler_driver_->IsClassOfStaticMethodAvailableToReferrer(outer_dex_cache.Get(),
Nicolas Geoffrayafd06412015-06-20 22:44:47 +0100984 GetCompilingClass(),
Roland Levillain4c0eb422015-04-24 16:43:49 +0100985 resolved_method,
986 method_idx,
987 &storage_index);
988 }
989
Nicolas Geoffrayb783b402015-06-22 11:06:43 +0100990 if (!outer_class->IsInterface()
991 && outer_class->IsSubClass(resolved_method->GetDeclaringClass())) {
Nicolas Geoffrayafd06412015-06-20 22:44:47 +0100992 // If the outer class is the declaring class or a subclass
Roland Levillain5f02c6c2015-04-24 19:14:22 +0100993 // of the declaring class, no class initialization is needed
994 // before the static method call.
Nicolas Geoffrayafd06412015-06-20 22:44:47 +0100995 // Note that in case of inlining, we do not need to add clinit checks
996 // to calls that satisfy this subclass check with any inlined methods. This
997 // will be detected by the optimization passes.
Roland Levillain4c0eb422015-04-24 16:43:49 +0100998 clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
999 } else if (storage_index != DexFile::kDexNoIndex) {
1000 // If the method's class type index is available, check
1001 // whether we should add an explicit class initialization
1002 // check for its declaring class before the static method call.
1003
1004 // TODO: find out why this check is needed.
1005 bool is_in_dex_cache = compiler_driver_->CanAssumeTypeIsPresentInDexCache(
1006 *outer_compilation_unit_->GetDexFile(), storage_index);
1007 bool is_initialized =
1008 resolved_method->GetDeclaringClass()->IsInitialized() && is_in_dex_cache;
1009
1010 if (is_initialized) {
1011 clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
1012 } else {
1013 clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01001014 HLoadClass* load_class = new (arena_) HLoadClass(
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001015 graph_->GetCurrentMethod(),
1016 storage_index,
1017 *dex_compilation_unit_->GetDexFile(),
Nicolas Geoffrayafd06412015-06-20 22:44:47 +01001018 is_outer_class,
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001019 dex_pc);
Roland Levillain4c0eb422015-04-24 16:43:49 +01001020 current_block_->AddInstruction(load_class);
1021 clinit_check = new (arena_) HClinitCheck(load_class, dex_pc);
1022 current_block_->AddInstruction(clinit_check);
Roland Levillain4c0eb422015-04-24 16:43:49 +01001023 }
1024 }
1025 }
1026
Vladimir Marko9b688a02015-05-06 14:12:42 +01001027 HInvokeStaticOrDirect::DispatchInfo dispatch_info = ComputeDispatchInfo(is_string_init,
1028 string_init_offset,
1029 target_method,
1030 direct_method,
1031 direct_code);
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +01001032 invoke = new (arena_) HInvokeStaticOrDirect(arena_,
1033 number_of_arguments,
1034 return_type,
1035 dex_pc,
Vladimir Marko9b688a02015-05-06 14:12:42 +01001036 method_idx,
1037 target_method,
1038 dispatch_info,
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +01001039 invoke_type,
1040 optimized_invoke_type,
1041 clinit_check_requirement);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001042 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001043
1044 size_t start_index = 0;
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001045 Temporaries temps(graph_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001046 if (is_instance_call) {
1047 HInstruction* arg = LoadLocal(is_range ? register_index : args[0], Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00001048 HNullCheck* null_check = new (arena_) HNullCheck(arg, dex_pc);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001049 current_block_->AddInstruction(null_check);
1050 temps.Add(null_check);
1051 invoke->SetArgumentAt(0, null_check);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001052 start_index = 1;
1053 }
1054
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001055 uint32_t descriptor_index = 1; // Skip the return type.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001056 uint32_t argument_index = start_index;
Jeff Hao848f70a2014-01-15 13:49:50 -08001057 if (is_string_init) {
1058 start_index = 1;
1059 }
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001060 for (size_t i = start_index;
1061 // Make sure we don't go over the expected arguments or over the number of
1062 // dex registers given. If the instruction was seen as dead by the verifier,
1063 // it hasn't been properly checked.
1064 (i < number_of_vreg_arguments) && (argument_index < number_of_arguments);
1065 i++, argument_index++) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001066 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001067 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001068 if (!is_range
1069 && is_wide
1070 && ((i + 1 == number_of_vreg_arguments) || (args[i] + 1 != args[i + 1]))) {
1071 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
1072 // reject any class where this is violated. However, the verifier only does these checks
1073 // on non trivially dead instructions, so we just bailout the compilation.
1074 VLOG(compiler) << "Did not compile "
1075 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
1076 << " because of non-sequential dex register pair in wide argument";
1077 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1078 return false;
1079 }
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001080 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
1081 invoke->SetArgumentAt(argument_index, arg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001082 if (is_wide) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001083 i++;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001084 }
1085 }
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001086
1087 if (argument_index != number_of_arguments) {
1088 VLOG(compiler) << "Did not compile "
1089 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
1090 << " because of wrong number of arguments in invoke instruction";
1091 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1092 return false;
1093 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001094
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001095 if (invoke->IsInvokeStaticOrDirect()) {
1096 invoke->SetArgumentAt(argument_index, graph_->GetCurrentMethod());
1097 argument_index++;
1098 }
1099
Roland Levillain4c0eb422015-04-24 16:43:49 +01001100 if (clinit_check_requirement == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit) {
1101 // Add the class initialization check as last input of `invoke`.
1102 DCHECK(clinit_check != nullptr);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001103 DCHECK(!is_string_init);
Roland Levillain3e3d7332015-04-28 11:00:54 +01001104 invoke->SetArgumentAt(argument_index, clinit_check);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001105 argument_index++;
Roland Levillain4c0eb422015-04-24 16:43:49 +01001106 }
1107
Jeff Hao848f70a2014-01-15 13:49:50 -08001108 // Add move-result for StringFactory method.
1109 if (is_string_init) {
1110 uint32_t orig_this_reg = is_range ? register_index : args[0];
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001111 HInstruction* fake_string = LoadLocal(orig_this_reg, Primitive::kPrimNot);
1112 invoke->SetArgumentAt(argument_index, fake_string);
1113 current_block_->AddInstruction(invoke);
1114 PotentiallySimplifyFakeString(orig_this_reg, dex_pc, invoke);
1115 } else {
1116 current_block_->AddInstruction(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001117 }
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001118 latest_result_ = invoke;
1119
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001120 return true;
1121}
1122
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001123bool HGraphBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001124 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001125 bool is_put) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001126 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1127 uint32_t obj_reg = instruction.VRegB_22c();
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001128 uint16_t field_index;
1129 if (instruction.IsQuickened()) {
1130 if (!CanDecodeQuickenedInfo()) {
1131 return false;
1132 }
1133 field_index = LookupQuickenedInfo(dex_pc);
1134 } else {
1135 field_index = instruction.VRegC_22c();
1136 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001137
1138 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierc7853442015-03-27 14:35:38 -07001139 ArtField* resolved_field =
1140 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001141
Mathieu Chartierc7853442015-03-27 14:35:38 -07001142 if (resolved_field == nullptr) {
Calin Juravle48c2b032014-12-09 18:11:36 +00001143 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedField);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001144 return false;
1145 }
Calin Juravle52c48962014-12-16 17:02:57 +00001146
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001147 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001148
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001149 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00001150 current_block_->AddInstruction(new (arena_) HNullCheck(object, dex_pc));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001151 if (is_put) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001152 Temporaries temps(graph_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001153 HInstruction* null_check = current_block_->GetLastInstruction();
1154 // We need one temporary for the null check.
1155 temps.Add(null_check);
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001156 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001157 current_block_->AddInstruction(new (arena_) HInstanceFieldSet(
1158 null_check,
1159 value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001160 field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00001161 resolved_field->GetOffset(),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001162 resolved_field->IsVolatile(),
1163 field_index,
1164 *dex_file_));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001165 } else {
1166 current_block_->AddInstruction(new (arena_) HInstanceFieldGet(
1167 current_block_->GetLastInstruction(),
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001168 field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00001169 resolved_field->GetOffset(),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001170 resolved_field->IsVolatile(),
1171 field_index,
1172 *dex_file_));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001173
1174 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1175 }
1176 return true;
1177}
1178
Nicolas Geoffray30451742015-06-19 13:32:41 +01001179static mirror::Class* GetClassFrom(CompilerDriver* driver,
1180 const DexCompilationUnit& compilation_unit) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001181 ScopedObjectAccess soa(Thread::Current());
1182 StackHandleScope<2> hs(soa.Self());
Nicolas Geoffray30451742015-06-19 13:32:41 +01001183 const DexFile& dex_file = *compilation_unit.GetDexFile();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001184 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Nicolas Geoffray30451742015-06-19 13:32:41 +01001185 soa.Decode<mirror::ClassLoader*>(compilation_unit.GetClassLoader())));
1186 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
1187 compilation_unit.GetClassLinker()->FindDexCache(dex_file)));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001188
Nicolas Geoffray30451742015-06-19 13:32:41 +01001189 return driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, &compilation_unit);
1190}
1191
1192mirror::Class* HGraphBuilder::GetOutermostCompilingClass() const {
1193 return GetClassFrom(compiler_driver_, *outer_compilation_unit_);
1194}
1195
1196mirror::Class* HGraphBuilder::GetCompilingClass() const {
1197 return GetClassFrom(compiler_driver_, *dex_compilation_unit_);
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001198}
1199
1200bool HGraphBuilder::IsOutermostCompilingClass(uint16_t type_index) const {
1201 ScopedObjectAccess soa(Thread::Current());
1202 StackHandleScope<4> hs(soa.Self());
1203 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
1204 dex_compilation_unit_->GetClassLinker()->FindDexCache(*dex_compilation_unit_->GetDexFile())));
1205 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1206 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
1207 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
1208 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
Nicolas Geoffrayafd06412015-06-20 22:44:47 +01001209 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001210
Nicolas Geoffrayafd06412015-06-20 22:44:47 +01001211 return outer_class.Get() == cls.Get();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001212}
1213
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001214bool HGraphBuilder::BuildStaticFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001215 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001216 bool is_put) {
1217 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1218 uint16_t field_index = instruction.VRegB_21c();
1219
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001220 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierc7853442015-03-27 14:35:38 -07001221 StackHandleScope<4> hs(soa.Self());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001222 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
1223 dex_compilation_unit_->GetClassLinker()->FindDexCache(*dex_compilation_unit_->GetDexFile())));
1224 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1225 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
Mathieu Chartierc7853442015-03-27 14:35:38 -07001226 ArtField* resolved_field = compiler_driver_->ResolveField(
1227 soa, dex_cache, class_loader, dex_compilation_unit_, field_index, true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001228
Mathieu Chartierc7853442015-03-27 14:35:38 -07001229 if (resolved_field == nullptr) {
Calin Juravle48c2b032014-12-09 18:11:36 +00001230 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedField);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001231 return false;
1232 }
1233
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001234 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
1235 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
1236 outer_compilation_unit_->GetClassLinker()->FindDexCache(outer_dex_file)));
Nicolas Geoffray30451742015-06-19 13:32:41 +01001237 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001238
1239 // The index at which the field's class is stored in the DexCache's type array.
1240 uint32_t storage_index;
Nicolas Geoffray30451742015-06-19 13:32:41 +01001241 bool is_outer_class = (outer_class.Get() == resolved_field->GetDeclaringClass());
1242 if (is_outer_class) {
1243 storage_index = outer_class->GetDexTypeIndex();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001244 } else if (outer_dex_cache.Get() != dex_cache.Get()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001245 // The compiler driver cannot currently understand multiple dex caches involved. Just bailout.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001246 return false;
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001247 } else {
1248 std::pair<bool, bool> pair = compiler_driver_->IsFastStaticField(
1249 outer_dex_cache.Get(),
Nicolas Geoffray30451742015-06-19 13:32:41 +01001250 GetCompilingClass(),
Mathieu Chartierc7853442015-03-27 14:35:38 -07001251 resolved_field,
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001252 field_index,
1253 &storage_index);
1254 bool can_easily_access = is_put ? pair.second : pair.first;
1255 if (!can_easily_access) {
1256 return false;
1257 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001258 }
1259
1260 // TODO: find out why this check is needed.
1261 bool is_in_dex_cache = compiler_driver_->CanAssumeTypeIsPresentInDexCache(
Nicolas Geoffray6a816cf2015-03-24 16:17:56 +00001262 *outer_compilation_unit_->GetDexFile(), storage_index);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001263 bool is_initialized = resolved_field->GetDeclaringClass()->IsInitialized() && is_in_dex_cache;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001264
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001265 HLoadClass* constant = new (arena_) HLoadClass(graph_->GetCurrentMethod(),
1266 storage_index,
1267 *dex_compilation_unit_->GetDexFile(),
Nicolas Geoffray30451742015-06-19 13:32:41 +01001268 is_outer_class,
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001269 dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001270 current_block_->AddInstruction(constant);
1271
1272 HInstruction* cls = constant;
Nicolas Geoffray30451742015-06-19 13:32:41 +01001273 if (!is_initialized && !is_outer_class) {
Calin Juravle225ff812014-11-13 16:46:39 +00001274 cls = new (arena_) HClinitCheck(constant, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001275 current_block_->AddInstruction(cls);
1276 }
1277
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001278 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001279 if (is_put) {
1280 // We need to keep the class alive before loading the value.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001281 Temporaries temps(graph_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001282 temps.Add(cls);
1283 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1284 DCHECK_EQ(value->GetType(), field_type);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001285 current_block_->AddInstruction(new (arena_) HStaticFieldSet(cls,
1286 value,
1287 field_type,
1288 resolved_field->GetOffset(),
1289 resolved_field->IsVolatile(),
1290 field_index,
1291 *dex_file_));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001292 } else {
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001293 current_block_->AddInstruction(new (arena_) HStaticFieldGet(cls,
1294 field_type,
1295 resolved_field->GetOffset(),
1296 resolved_field->IsVolatile(),
1297 field_index,
1298 *dex_file_));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001299 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1300 }
1301 return true;
1302}
1303
Calin Juravlebacfec32014-11-14 15:54:36 +00001304void HGraphBuilder::BuildCheckedDivRem(uint16_t out_vreg,
1305 uint16_t first_vreg,
1306 int64_t second_vreg_or_constant,
1307 uint32_t dex_pc,
1308 Primitive::Type type,
1309 bool second_is_constant,
1310 bool isDiv) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001311 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Calin Juravled0d48522014-11-04 16:40:20 +00001312
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001313 HInstruction* first = LoadLocal(first_vreg, type);
1314 HInstruction* second = nullptr;
1315 if (second_is_constant) {
1316 if (type == Primitive::kPrimInt) {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001317 second = graph_->GetIntConstant(second_vreg_or_constant);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001318 } else {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001319 second = graph_->GetLongConstant(second_vreg_or_constant);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001320 }
1321 } else {
1322 second = LoadLocal(second_vreg_or_constant, type);
1323 }
1324
1325 if (!second_is_constant
1326 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0)
1327 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) {
1328 second = new (arena_) HDivZeroCheck(second, dex_pc);
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001329 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +00001330 current_block_->AddInstruction(second);
1331 temps.Add(current_block_->GetLastInstruction());
1332 }
1333
Calin Juravlebacfec32014-11-14 15:54:36 +00001334 if (isDiv) {
1335 current_block_->AddInstruction(new (arena_) HDiv(type, first, second, dex_pc));
1336 } else {
1337 current_block_->AddInstruction(new (arena_) HRem(type, first, second, dex_pc));
1338 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001339 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
Calin Juravled0d48522014-11-04 16:40:20 +00001340}
1341
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001342void HGraphBuilder::BuildArrayAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001343 uint32_t dex_pc,
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001344 bool is_put,
1345 Primitive::Type anticipated_type) {
1346 uint8_t source_or_dest_reg = instruction.VRegA_23x();
1347 uint8_t array_reg = instruction.VRegB_23x();
1348 uint8_t index_reg = instruction.VRegC_23x();
1349
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001350 // We need one temporary for the null check, one for the index, and one for the length.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001351 Temporaries temps(graph_);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001352
1353 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00001354 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001355 current_block_->AddInstruction(object);
1356 temps.Add(object);
1357
1358 HInstruction* length = new (arena_) HArrayLength(object);
1359 current_block_->AddInstruction(length);
1360 temps.Add(length);
1361 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt);
Calin Juravle225ff812014-11-13 16:46:39 +00001362 index = new (arena_) HBoundsCheck(index, length, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001363 current_block_->AddInstruction(index);
1364 temps.Add(index);
1365 if (is_put) {
1366 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
1367 // TODO: Insert a type check node if the type is Object.
Nicolas Geoffray39468442014-09-02 15:17:15 +01001368 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001369 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001370 } else {
1371 current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type));
1372 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1373 }
Mark Mendell1152c922015-04-24 17:06:35 -04001374 graph_->SetHasBoundsChecks(true);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001375}
1376
Calin Juravle225ff812014-11-13 16:46:39 +00001377void HGraphBuilder::BuildFilledNewArray(uint32_t dex_pc,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001378 uint32_t type_index,
1379 uint32_t number_of_vreg_arguments,
1380 bool is_range,
1381 uint32_t* args,
1382 uint32_t register_index) {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001383 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments);
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001384 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
1385 ? kQuickAllocArrayWithAccessCheck
1386 : kQuickAllocArray;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01001387 HInstruction* object = new (arena_) HNewArray(length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01001388 graph_->GetCurrentMethod(),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01001389 dex_pc,
1390 type_index,
1391 *dex_compilation_unit_->GetDexFile(),
1392 entrypoint);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001393 current_block_->AddInstruction(object);
1394
1395 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1396 DCHECK_EQ(descriptor[0], '[') << descriptor;
1397 char primitive = descriptor[1];
1398 DCHECK(primitive == 'I'
1399 || primitive == 'L'
1400 || primitive == '[') << descriptor;
1401 bool is_reference_array = (primitive == 'L') || (primitive == '[');
1402 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
1403
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001404 Temporaries temps(graph_);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001405 temps.Add(object);
1406 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
1407 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
David Brazdil8d5b8b22015-03-24 10:51:52 +00001408 HInstruction* index = graph_->GetIntConstant(i);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001409 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001410 new (arena_) HArraySet(object, index, value, type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001411 }
1412 latest_result_ = object;
1413}
1414
1415template <typename T>
1416void HGraphBuilder::BuildFillArrayData(HInstruction* object,
1417 const T* data,
1418 uint32_t element_count,
1419 Primitive::Type anticipated_type,
Calin Juravle225ff812014-11-13 16:46:39 +00001420 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001421 for (uint32_t i = 0; i < element_count; ++i) {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001422 HInstruction* index = graph_->GetIntConstant(i);
1423 HInstruction* value = graph_->GetIntConstant(data[i]);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001424 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001425 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001426 }
1427}
1428
Calin Juravle225ff812014-11-13 16:46:39 +00001429void HGraphBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001430 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +00001431 HInstruction* array = LoadLocal(instruction.VRegA_31t(), Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00001432 HNullCheck* null_check = new (arena_) HNullCheck(array, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001433 current_block_->AddInstruction(null_check);
1434 temps.Add(null_check);
1435
1436 HInstruction* length = new (arena_) HArrayLength(null_check);
1437 current_block_->AddInstruction(length);
1438
Calin Juravle225ff812014-11-13 16:46:39 +00001439 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
Calin Juravled0d48522014-11-04 16:40:20 +00001440 const Instruction::ArrayDataPayload* payload =
1441 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_start_ + payload_offset);
1442 const uint8_t* data = payload->data;
1443 uint32_t element_count = payload->element_count;
1444
1445 // Implementation of this DEX instruction seems to be that the bounds check is
1446 // done before doing any stores.
David Brazdil8d5b8b22015-03-24 10:51:52 +00001447 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1);
Calin Juravle225ff812014-11-13 16:46:39 +00001448 current_block_->AddInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
Calin Juravled0d48522014-11-04 16:40:20 +00001449
1450 switch (payload->element_width) {
1451 case 1:
1452 BuildFillArrayData(null_check,
1453 reinterpret_cast<const int8_t*>(data),
1454 element_count,
1455 Primitive::kPrimByte,
Calin Juravle225ff812014-11-13 16:46:39 +00001456 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001457 break;
1458 case 2:
1459 BuildFillArrayData(null_check,
1460 reinterpret_cast<const int16_t*>(data),
1461 element_count,
1462 Primitive::kPrimShort,
Calin Juravle225ff812014-11-13 16:46:39 +00001463 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001464 break;
1465 case 4:
1466 BuildFillArrayData(null_check,
1467 reinterpret_cast<const int32_t*>(data),
1468 element_count,
1469 Primitive::kPrimInt,
Calin Juravle225ff812014-11-13 16:46:39 +00001470 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001471 break;
1472 case 8:
1473 BuildFillWideArrayData(null_check,
1474 reinterpret_cast<const int64_t*>(data),
1475 element_count,
Calin Juravle225ff812014-11-13 16:46:39 +00001476 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001477 break;
1478 default:
1479 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1480 }
Mark Mendell1152c922015-04-24 17:06:35 -04001481 graph_->SetHasBoundsChecks(true);
Calin Juravled0d48522014-11-04 16:40:20 +00001482}
1483
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001484void HGraphBuilder::BuildFillWideArrayData(HInstruction* object,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +01001485 const int64_t* data,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001486 uint32_t element_count,
Calin Juravle225ff812014-11-13 16:46:39 +00001487 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001488 for (uint32_t i = 0; i < element_count; ++i) {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001489 HInstruction* index = graph_->GetIntConstant(i);
1490 HInstruction* value = graph_->GetLongConstant(data[i]);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001491 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001492 object, index, value, Primitive::kPrimLong, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001493 }
1494}
1495
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001496bool HGraphBuilder::BuildTypeCheck(const Instruction& instruction,
1497 uint8_t destination,
1498 uint8_t reference,
1499 uint16_t type_index,
Calin Juravle225ff812014-11-13 16:46:39 +00001500 uint32_t dex_pc) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001501 bool type_known_final;
1502 bool type_known_abstract;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001503 // `CanAccessTypeWithoutChecks` will tell whether the method being
1504 // built is trying to access its own class, so that the generated
1505 // code can optimize for this case. However, the optimization does not
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001506 // work for inlining, so we use `IsOutermostCompilingClass` instead.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001507 bool dont_use_is_referrers_class;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001508 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
1509 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001510 &type_known_final, &type_known_abstract, &dont_use_is_referrers_class);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001511 if (!can_access) {
Calin Juravle48c2b032014-12-09 18:11:36 +00001512 MaybeRecordStat(MethodCompilationStat::kNotCompiledCantAccesType);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001513 return false;
1514 }
1515 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot);
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001516 HLoadClass* cls = new (arena_) HLoadClass(
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001517 graph_->GetCurrentMethod(),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01001518 type_index,
1519 *dex_compilation_unit_->GetDexFile(),
1520 IsOutermostCompilingClass(type_index),
1521 dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001522 current_block_->AddInstruction(cls);
1523 // The class needs a temporary before being used by the type check.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001524 Temporaries temps(graph_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001525 temps.Add(cls);
1526 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
1527 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001528 new (arena_) HInstanceOf(object, cls, type_known_final, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001529 UpdateLocal(destination, current_block_->GetLastInstruction());
1530 } else {
1531 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
1532 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001533 new (arena_) HCheckCast(object, cls, type_known_final, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001534 }
1535 return true;
1536}
1537
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001538bool HGraphBuilder::NeedsAccessCheck(uint32_t type_index) const {
1539 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
1540 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index);
1541}
1542
Calin Juravle48c2b032014-12-09 18:11:36 +00001543void HGraphBuilder::BuildPackedSwitch(const Instruction& instruction, uint32_t dex_pc) {
David Brazdil2ef645b2015-06-17 18:20:52 +01001544 // Verifier guarantees that the payload for PackedSwitch contains:
1545 // (a) number of entries (may be zero)
1546 // (b) first and lowest switch case value (entry 0, always present)
1547 // (c) list of target pcs (entries 1 <= i <= N)
Andreas Gamped881df52014-11-24 23:28:39 -08001548 SwitchTable table(instruction, dex_pc, false);
1549
1550 // Value to test against.
1551 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
1552
David Brazdil2ef645b2015-06-17 18:20:52 +01001553 // Retrieve number of entries.
Andreas Gampee4d4d322014-12-04 09:09:57 -08001554 uint16_t num_entries = table.GetNumEntries();
David Brazdil2ef645b2015-06-17 18:20:52 +01001555 if (num_entries == 0) {
1556 return;
1557 }
Andreas Gampee4d4d322014-12-04 09:09:57 -08001558
Andreas Gamped881df52014-11-24 23:28:39 -08001559 // Chained cmp-and-branch, starting from starting_key.
1560 int32_t starting_key = table.GetEntryAt(0);
1561
Andreas Gamped881df52014-11-24 23:28:39 -08001562 for (size_t i = 1; i <= num_entries; i++) {
Andreas Gampee4d4d322014-12-04 09:09:57 -08001563 BuildSwitchCaseHelper(instruction, i, i == num_entries, table, value, starting_key + i - 1,
1564 table.GetEntryAt(i), dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08001565 }
Andreas Gamped881df52014-11-24 23:28:39 -08001566}
1567
Calin Juravle48c2b032014-12-09 18:11:36 +00001568void HGraphBuilder::BuildSparseSwitch(const Instruction& instruction, uint32_t dex_pc) {
David Brazdil2ef645b2015-06-17 18:20:52 +01001569 // Verifier guarantees that the payload for SparseSwitch contains:
1570 // (a) number of entries (may be zero)
1571 // (b) sorted key values (entries 0 <= i < N)
1572 // (c) target pcs corresponding to the switch values (entries N <= i < 2*N)
Andreas Gampee4d4d322014-12-04 09:09:57 -08001573 SwitchTable table(instruction, dex_pc, true);
1574
1575 // Value to test against.
1576 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
1577
1578 uint16_t num_entries = table.GetNumEntries();
Andreas Gampee4d4d322014-12-04 09:09:57 -08001579
1580 for (size_t i = 0; i < num_entries; i++) {
1581 BuildSwitchCaseHelper(instruction, i, i == static_cast<size_t>(num_entries) - 1, table, value,
1582 table.GetEntryAt(i), table.GetEntryAt(i + num_entries), dex_pc);
1583 }
Andreas Gampee4d4d322014-12-04 09:09:57 -08001584}
1585
1586void HGraphBuilder::BuildSwitchCaseHelper(const Instruction& instruction, size_t index,
1587 bool is_last_case, const SwitchTable& table,
1588 HInstruction* value, int32_t case_value_int,
1589 int32_t target_offset, uint32_t dex_pc) {
David Brazdil852eaff2015-02-02 15:23:05 +00001590 HBasicBlock* case_target = FindBlockStartingAt(dex_pc + target_offset);
1591 DCHECK(case_target != nullptr);
1592 PotentiallyAddSuspendCheck(case_target, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001593
1594 // The current case's value.
David Brazdil8d5b8b22015-03-24 10:51:52 +00001595 HInstruction* this_case_value = graph_->GetIntConstant(case_value_int);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001596
1597 // Compare value and this_case_value.
1598 HEqual* comparison = new (arena_) HEqual(value, this_case_value);
1599 current_block_->AddInstruction(comparison);
1600 HInstruction* ifinst = new (arena_) HIf(comparison);
1601 current_block_->AddInstruction(ifinst);
1602
1603 // Case hit: use the target offset to determine where to go.
Andreas Gampee4d4d322014-12-04 09:09:57 -08001604 current_block_->AddSuccessor(case_target);
1605
1606 // Case miss: go to the next case (or default fall-through).
1607 // When there is a next case, we use the block stored with the table offset representing this
1608 // case (that is where we registered them in ComputeBranchTargets).
1609 // When there is no next case, we use the following instruction.
1610 // TODO: Find a good way to peel the last iteration to avoid conditional, but still have re-use.
1611 if (!is_last_case) {
1612 HBasicBlock* next_case_target = FindBlockStartingAt(table.GetDexPcForIndex(index));
1613 DCHECK(next_case_target != nullptr);
1614 current_block_->AddSuccessor(next_case_target);
1615
1616 // Need to manually add the block, as there is no dex-pc transition for the cases.
1617 graph_->AddBlock(next_case_target);
1618
1619 current_block_ = next_case_target;
1620 } else {
1621 HBasicBlock* default_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
1622 DCHECK(default_target != nullptr);
1623 current_block_->AddSuccessor(default_target);
1624 current_block_ = nullptr;
1625 }
1626}
1627
David Brazdil852eaff2015-02-02 15:23:05 +00001628void HGraphBuilder::PotentiallyAddSuspendCheck(HBasicBlock* target, uint32_t dex_pc) {
1629 int32_t target_offset = target->GetDexPc() - dex_pc;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001630 if (target_offset <= 0) {
David Brazdil852eaff2015-02-02 15:23:05 +00001631 // DX generates back edges to the first encountered return. We can save
1632 // time of later passes by not adding redundant suspend checks.
David Brazdil2fd6aa52015-02-02 18:58:27 +00001633 HInstruction* last_in_target = target->GetLastInstruction();
1634 if (last_in_target != nullptr &&
1635 (last_in_target->IsReturn() || last_in_target->IsReturnVoid())) {
1636 return;
David Brazdil852eaff2015-02-02 15:23:05 +00001637 }
1638
1639 // Add a suspend check to backward branches which may potentially loop. We
1640 // can remove them after we recognize loops in the graph.
Calin Juravle225ff812014-11-13 16:46:39 +00001641 current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_pc));
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001642 }
1643}
1644
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001645bool HGraphBuilder::CanDecodeQuickenedInfo() const {
1646 return interpreter_metadata_ != nullptr;
1647}
1648
1649uint16_t HGraphBuilder::LookupQuickenedInfo(uint32_t dex_pc) {
1650 DCHECK(interpreter_metadata_ != nullptr);
1651 uint32_t dex_pc_in_map = DecodeUnsignedLeb128(&interpreter_metadata_);
1652 DCHECK_EQ(dex_pc, dex_pc_in_map);
1653 return DecodeUnsignedLeb128(&interpreter_metadata_);
1654}
1655
Calin Juravle225ff812014-11-13 16:46:39 +00001656bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001657 if (current_block_ == nullptr) {
1658 return true; // Dead code
1659 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001660
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001661 switch (instruction.Opcode()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001662 case Instruction::CONST_4: {
1663 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001664 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001665 UpdateLocal(register_index, constant);
1666 break;
1667 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001668
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001669 case Instruction::CONST_16: {
1670 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001671 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001672 UpdateLocal(register_index, constant);
1673 break;
1674 }
1675
Dave Allison20dfc792014-06-16 20:44:29 -07001676 case Instruction::CONST: {
1677 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001678 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i());
Dave Allison20dfc792014-06-16 20:44:29 -07001679 UpdateLocal(register_index, constant);
1680 break;
1681 }
1682
1683 case Instruction::CONST_HIGH16: {
1684 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001685 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16);
Dave Allison20dfc792014-06-16 20:44:29 -07001686 UpdateLocal(register_index, constant);
1687 break;
1688 }
1689
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001690 case Instruction::CONST_WIDE_16: {
1691 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001692 // Get 16 bits of constant value, sign extended to 64 bits.
1693 int64_t value = instruction.VRegB_21s();
1694 value <<= 48;
1695 value >>= 48;
David Brazdil8d5b8b22015-03-24 10:51:52 +00001696 HLongConstant* constant = graph_->GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001697 UpdateLocal(register_index, constant);
1698 break;
1699 }
1700
1701 case Instruction::CONST_WIDE_32: {
1702 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001703 // Get 32 bits of constant value, sign extended to 64 bits.
1704 int64_t value = instruction.VRegB_31i();
1705 value <<= 32;
1706 value >>= 32;
David Brazdil8d5b8b22015-03-24 10:51:52 +00001707 HLongConstant* constant = graph_->GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001708 UpdateLocal(register_index, constant);
1709 break;
1710 }
1711
1712 case Instruction::CONST_WIDE: {
1713 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001714 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001715 UpdateLocal(register_index, constant);
1716 break;
1717 }
1718
Dave Allison20dfc792014-06-16 20:44:29 -07001719 case Instruction::CONST_WIDE_HIGH16: {
1720 int32_t register_index = instruction.VRegA();
1721 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
David Brazdil8d5b8b22015-03-24 10:51:52 +00001722 HLongConstant* constant = graph_->GetLongConstant(value);
Dave Allison20dfc792014-06-16 20:44:29 -07001723 UpdateLocal(register_index, constant);
1724 break;
1725 }
1726
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001727 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001728 case Instruction::MOVE:
1729 case Instruction::MOVE_FROM16:
1730 case Instruction::MOVE_16: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001731 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001732 UpdateLocal(instruction.VRegA(), value);
1733 break;
1734 }
1735
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001736 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001737 case Instruction::MOVE_WIDE:
1738 case Instruction::MOVE_WIDE_FROM16:
1739 case Instruction::MOVE_WIDE_16: {
1740 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong);
1741 UpdateLocal(instruction.VRegA(), value);
1742 break;
1743 }
1744
1745 case Instruction::MOVE_OBJECT:
1746 case Instruction::MOVE_OBJECT_16:
1747 case Instruction::MOVE_OBJECT_FROM16: {
1748 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot);
1749 UpdateLocal(instruction.VRegA(), value);
1750 break;
1751 }
1752
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001753 case Instruction::RETURN_VOID_NO_BARRIER:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001754 case Instruction::RETURN_VOID: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001755 BuildReturn(instruction, Primitive::kPrimVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001756 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001757 }
1758
Dave Allison20dfc792014-06-16 20:44:29 -07001759#define IF_XX(comparison, cond) \
Calin Juravle225ff812014-11-13 16:46:39 +00001760 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
1761 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001762
Dave Allison20dfc792014-06-16 20:44:29 -07001763 IF_XX(HEqual, EQ);
1764 IF_XX(HNotEqual, NE);
1765 IF_XX(HLessThan, LT);
1766 IF_XX(HLessThanOrEqual, LE);
1767 IF_XX(HGreaterThan, GT);
1768 IF_XX(HGreaterThanOrEqual, GE);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001769
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001770 case Instruction::GOTO:
1771 case Instruction::GOTO_16:
1772 case Instruction::GOTO_32: {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001773 int32_t offset = instruction.GetTargetOffset();
Calin Juravle225ff812014-11-13 16:46:39 +00001774 HBasicBlock* target = FindBlockStartingAt(offset + dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001775 DCHECK(target != nullptr);
David Brazdil852eaff2015-02-02 15:23:05 +00001776 PotentiallyAddSuspendCheck(target, dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001777 current_block_->AddInstruction(new (arena_) HGoto());
1778 current_block_->AddSuccessor(target);
1779 current_block_ = nullptr;
1780 break;
1781 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001782
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001783 case Instruction::RETURN: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001784 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001785 break;
1786 }
1787
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001788 case Instruction::RETURN_OBJECT: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001789 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001790 break;
1791 }
1792
1793 case Instruction::RETURN_WIDE: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001794 BuildReturn(instruction, return_type_);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001795 break;
1796 }
1797
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001798 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001799 case Instruction::INVOKE_INTERFACE:
1800 case Instruction::INVOKE_STATIC:
1801 case Instruction::INVOKE_SUPER:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001802 case Instruction::INVOKE_VIRTUAL:
1803 case Instruction::INVOKE_VIRTUAL_QUICK: {
1804 uint16_t method_idx;
1805 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_QUICK) {
1806 if (!CanDecodeQuickenedInfo()) {
1807 return false;
1808 }
1809 method_idx = LookupQuickenedInfo(dex_pc);
1810 } else {
1811 method_idx = instruction.VRegB_35c();
1812 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001813 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001814 uint32_t args[5];
Ian Rogers29a26482014-05-02 15:27:29 -07001815 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00001816 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001817 number_of_vreg_arguments, false, args, -1)) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001818 return false;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001819 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001820 break;
1821 }
1822
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001823 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001824 case Instruction::INVOKE_INTERFACE_RANGE:
1825 case Instruction::INVOKE_STATIC_RANGE:
1826 case Instruction::INVOKE_SUPER_RANGE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001827 case Instruction::INVOKE_VIRTUAL_RANGE:
1828 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
1829 uint16_t method_idx;
1830 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK) {
1831 if (!CanDecodeQuickenedInfo()) {
1832 return false;
1833 }
1834 method_idx = LookupQuickenedInfo(dex_pc);
1835 } else {
1836 method_idx = instruction.VRegB_3rc();
1837 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001838 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1839 uint32_t register_index = instruction.VRegC();
Calin Juravle225ff812014-11-13 16:46:39 +00001840 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001841 number_of_vreg_arguments, true, nullptr, register_index)) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001842 return false;
1843 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001844 break;
1845 }
1846
Roland Levillain88cb1752014-10-20 16:36:47 +01001847 case Instruction::NEG_INT: {
1848 Unop_12x<HNeg>(instruction, Primitive::kPrimInt);
1849 break;
1850 }
1851
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001852 case Instruction::NEG_LONG: {
1853 Unop_12x<HNeg>(instruction, Primitive::kPrimLong);
1854 break;
1855 }
1856
Roland Levillain3dbcb382014-10-28 17:30:07 +00001857 case Instruction::NEG_FLOAT: {
1858 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat);
1859 break;
1860 }
1861
1862 case Instruction::NEG_DOUBLE: {
1863 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble);
1864 break;
1865 }
1866
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001867 case Instruction::NOT_INT: {
1868 Unop_12x<HNot>(instruction, Primitive::kPrimInt);
1869 break;
1870 }
1871
Roland Levillain70566432014-10-24 16:20:17 +01001872 case Instruction::NOT_LONG: {
1873 Unop_12x<HNot>(instruction, Primitive::kPrimLong);
1874 break;
1875 }
1876
Roland Levillaindff1f282014-11-05 14:15:05 +00001877 case Instruction::INT_TO_LONG: {
Roland Levillain624279f2014-12-04 11:54:28 +00001878 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong, dex_pc);
Roland Levillaindff1f282014-11-05 14:15:05 +00001879 break;
1880 }
1881
Roland Levillaincff13742014-11-17 14:32:17 +00001882 case Instruction::INT_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001883 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00001884 break;
1885 }
1886
1887 case Instruction::INT_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001888 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00001889 break;
1890 }
1891
Roland Levillain946e1432014-11-11 17:35:19 +00001892 case Instruction::LONG_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001893 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt, dex_pc);
Roland Levillain946e1432014-11-11 17:35:19 +00001894 break;
1895 }
1896
Roland Levillain6d0e4832014-11-27 18:31:21 +00001897 case Instruction::LONG_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001898 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimFloat, dex_pc);
Roland Levillain6d0e4832014-11-27 18:31:21 +00001899 break;
1900 }
1901
Roland Levillain647b9ed2014-11-27 12:06:00 +00001902 case Instruction::LONG_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001903 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble, dex_pc);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001904 break;
1905 }
1906
Roland Levillain3f8f9362014-12-02 17:45:01 +00001907 case Instruction::FLOAT_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001908 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimInt, dex_pc);
1909 break;
1910 }
1911
1912 case Instruction::FLOAT_TO_LONG: {
1913 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimLong, dex_pc);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001914 break;
1915 }
1916
Roland Levillain8964e2b2014-12-04 12:10:50 +00001917 case Instruction::FLOAT_TO_DOUBLE: {
1918 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimDouble, dex_pc);
1919 break;
1920 }
1921
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001922 case Instruction::DOUBLE_TO_INT: {
1923 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimInt, dex_pc);
1924 break;
1925 }
1926
1927 case Instruction::DOUBLE_TO_LONG: {
1928 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimLong, dex_pc);
1929 break;
1930 }
1931
Roland Levillain8964e2b2014-12-04 12:10:50 +00001932 case Instruction::DOUBLE_TO_FLOAT: {
1933 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimFloat, dex_pc);
1934 break;
1935 }
1936
Roland Levillain51d3fc42014-11-13 14:11:42 +00001937 case Instruction::INT_TO_BYTE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001938 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte, dex_pc);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001939 break;
1940 }
1941
Roland Levillain01a8d712014-11-14 16:27:39 +00001942 case Instruction::INT_TO_SHORT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001943 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort, dex_pc);
Roland Levillain01a8d712014-11-14 16:27:39 +00001944 break;
1945 }
1946
Roland Levillain981e4542014-11-14 11:47:14 +00001947 case Instruction::INT_TO_CHAR: {
Roland Levillain624279f2014-12-04 11:54:28 +00001948 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar, dex_pc);
Roland Levillain981e4542014-11-14 11:47:14 +00001949 break;
1950 }
1951
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001952 case Instruction::ADD_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001953 Binop_23x<HAdd>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001954 break;
1955 }
1956
1957 case Instruction::ADD_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001958 Binop_23x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001959 break;
1960 }
1961
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001962 case Instruction::ADD_DOUBLE: {
1963 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble);
1964 break;
1965 }
1966
1967 case Instruction::ADD_FLOAT: {
1968 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat);
1969 break;
1970 }
1971
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001972 case Instruction::SUB_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001973 Binop_23x<HSub>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001974 break;
1975 }
1976
1977 case Instruction::SUB_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001978 Binop_23x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001979 break;
1980 }
1981
Calin Juravle096cc022014-10-23 17:01:13 +01001982 case Instruction::SUB_FLOAT: {
1983 Binop_23x<HSub>(instruction, Primitive::kPrimFloat);
1984 break;
1985 }
1986
1987 case Instruction::SUB_DOUBLE: {
1988 Binop_23x<HSub>(instruction, Primitive::kPrimDouble);
1989 break;
1990 }
1991
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001992 case Instruction::ADD_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001993 Binop_12x<HAdd>(instruction, Primitive::kPrimInt);
1994 break;
1995 }
1996
Calin Juravle34bacdf2014-10-07 20:23:36 +01001997 case Instruction::MUL_INT: {
1998 Binop_23x<HMul>(instruction, Primitive::kPrimInt);
1999 break;
2000 }
2001
2002 case Instruction::MUL_LONG: {
2003 Binop_23x<HMul>(instruction, Primitive::kPrimLong);
2004 break;
2005 }
2006
Calin Juravleb5bfa962014-10-21 18:02:24 +01002007 case Instruction::MUL_FLOAT: {
2008 Binop_23x<HMul>(instruction, Primitive::kPrimFloat);
2009 break;
2010 }
2011
2012 case Instruction::MUL_DOUBLE: {
2013 Binop_23x<HMul>(instruction, Primitive::kPrimDouble);
2014 break;
2015 }
2016
Calin Juravled0d48522014-11-04 16:40:20 +00002017 case Instruction::DIV_INT: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002018 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2019 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravled0d48522014-11-04 16:40:20 +00002020 break;
2021 }
2022
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002023 case Instruction::DIV_LONG: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002024 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2025 dex_pc, Primitive::kPrimLong, false, true);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002026 break;
2027 }
2028
Calin Juravle7c4954d2014-10-28 16:57:40 +00002029 case Instruction::DIV_FLOAT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002030 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002031 break;
2032 }
2033
2034 case Instruction::DIV_DOUBLE: {
Calin Juravle225ff812014-11-13 16:46:39 +00002035 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002036 break;
2037 }
2038
Calin Juravlebacfec32014-11-14 15:54:36 +00002039 case Instruction::REM_INT: {
2040 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2041 dex_pc, Primitive::kPrimInt, false, false);
2042 break;
2043 }
2044
2045 case Instruction::REM_LONG: {
2046 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2047 dex_pc, Primitive::kPrimLong, false, false);
2048 break;
2049 }
2050
Calin Juravled2ec87d2014-12-08 14:24:46 +00002051 case Instruction::REM_FLOAT: {
2052 Binop_23x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2053 break;
2054 }
2055
2056 case Instruction::REM_DOUBLE: {
2057 Binop_23x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2058 break;
2059 }
2060
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002061 case Instruction::AND_INT: {
2062 Binop_23x<HAnd>(instruction, Primitive::kPrimInt);
2063 break;
2064 }
2065
2066 case Instruction::AND_LONG: {
2067 Binop_23x<HAnd>(instruction, Primitive::kPrimLong);
2068 break;
2069 }
2070
Calin Juravle9aec02f2014-11-18 23:06:35 +00002071 case Instruction::SHL_INT: {
2072 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt);
2073 break;
2074 }
2075
2076 case Instruction::SHL_LONG: {
2077 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong);
2078 break;
2079 }
2080
2081 case Instruction::SHR_INT: {
2082 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt);
2083 break;
2084 }
2085
2086 case Instruction::SHR_LONG: {
2087 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong);
2088 break;
2089 }
2090
2091 case Instruction::USHR_INT: {
2092 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt);
2093 break;
2094 }
2095
2096 case Instruction::USHR_LONG: {
2097 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong);
2098 break;
2099 }
2100
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002101 case Instruction::OR_INT: {
2102 Binop_23x<HOr>(instruction, Primitive::kPrimInt);
2103 break;
2104 }
2105
2106 case Instruction::OR_LONG: {
2107 Binop_23x<HOr>(instruction, Primitive::kPrimLong);
2108 break;
2109 }
2110
2111 case Instruction::XOR_INT: {
2112 Binop_23x<HXor>(instruction, Primitive::kPrimInt);
2113 break;
2114 }
2115
2116 case Instruction::XOR_LONG: {
2117 Binop_23x<HXor>(instruction, Primitive::kPrimLong);
2118 break;
2119 }
2120
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002121 case Instruction::ADD_LONG_2ADDR: {
2122 Binop_12x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002123 break;
2124 }
2125
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002126 case Instruction::ADD_DOUBLE_2ADDR: {
2127 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble);
2128 break;
2129 }
2130
2131 case Instruction::ADD_FLOAT_2ADDR: {
2132 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat);
2133 break;
2134 }
2135
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002136 case Instruction::SUB_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002137 Binop_12x<HSub>(instruction, Primitive::kPrimInt);
2138 break;
2139 }
2140
2141 case Instruction::SUB_LONG_2ADDR: {
2142 Binop_12x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002143 break;
2144 }
2145
Calin Juravle096cc022014-10-23 17:01:13 +01002146 case Instruction::SUB_FLOAT_2ADDR: {
2147 Binop_12x<HSub>(instruction, Primitive::kPrimFloat);
2148 break;
2149 }
2150
2151 case Instruction::SUB_DOUBLE_2ADDR: {
2152 Binop_12x<HSub>(instruction, Primitive::kPrimDouble);
2153 break;
2154 }
2155
Calin Juravle34bacdf2014-10-07 20:23:36 +01002156 case Instruction::MUL_INT_2ADDR: {
2157 Binop_12x<HMul>(instruction, Primitive::kPrimInt);
2158 break;
2159 }
2160
2161 case Instruction::MUL_LONG_2ADDR: {
2162 Binop_12x<HMul>(instruction, Primitive::kPrimLong);
2163 break;
2164 }
2165
Calin Juravleb5bfa962014-10-21 18:02:24 +01002166 case Instruction::MUL_FLOAT_2ADDR: {
2167 Binop_12x<HMul>(instruction, Primitive::kPrimFloat);
2168 break;
2169 }
2170
2171 case Instruction::MUL_DOUBLE_2ADDR: {
2172 Binop_12x<HMul>(instruction, Primitive::kPrimDouble);
2173 break;
2174 }
2175
Calin Juravle865fc882014-11-06 17:09:03 +00002176 case Instruction::DIV_INT_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002177 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2178 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravle865fc882014-11-06 17:09:03 +00002179 break;
2180 }
2181
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002182 case Instruction::DIV_LONG_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002183 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2184 dex_pc, Primitive::kPrimLong, false, true);
2185 break;
2186 }
2187
2188 case Instruction::REM_INT_2ADDR: {
2189 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2190 dex_pc, Primitive::kPrimInt, false, false);
2191 break;
2192 }
2193
2194 case Instruction::REM_LONG_2ADDR: {
2195 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2196 dex_pc, Primitive::kPrimLong, false, false);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002197 break;
2198 }
2199
Calin Juravled2ec87d2014-12-08 14:24:46 +00002200 case Instruction::REM_FLOAT_2ADDR: {
2201 Binop_12x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2202 break;
2203 }
2204
2205 case Instruction::REM_DOUBLE_2ADDR: {
2206 Binop_12x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2207 break;
2208 }
2209
Calin Juravle9aec02f2014-11-18 23:06:35 +00002210 case Instruction::SHL_INT_2ADDR: {
2211 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt);
2212 break;
2213 }
2214
2215 case Instruction::SHL_LONG_2ADDR: {
2216 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong);
2217 break;
2218 }
2219
2220 case Instruction::SHR_INT_2ADDR: {
2221 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt);
2222 break;
2223 }
2224
2225 case Instruction::SHR_LONG_2ADDR: {
2226 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong);
2227 break;
2228 }
2229
2230 case Instruction::USHR_INT_2ADDR: {
2231 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt);
2232 break;
2233 }
2234
2235 case Instruction::USHR_LONG_2ADDR: {
2236 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong);
2237 break;
2238 }
2239
Calin Juravle7c4954d2014-10-28 16:57:40 +00002240 case Instruction::DIV_FLOAT_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00002241 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002242 break;
2243 }
2244
2245 case Instruction::DIV_DOUBLE_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00002246 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002247 break;
2248 }
2249
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002250 case Instruction::AND_INT_2ADDR: {
2251 Binop_12x<HAnd>(instruction, Primitive::kPrimInt);
2252 break;
2253 }
2254
2255 case Instruction::AND_LONG_2ADDR: {
2256 Binop_12x<HAnd>(instruction, Primitive::kPrimLong);
2257 break;
2258 }
2259
2260 case Instruction::OR_INT_2ADDR: {
2261 Binop_12x<HOr>(instruction, Primitive::kPrimInt);
2262 break;
2263 }
2264
2265 case Instruction::OR_LONG_2ADDR: {
2266 Binop_12x<HOr>(instruction, Primitive::kPrimLong);
2267 break;
2268 }
2269
2270 case Instruction::XOR_INT_2ADDR: {
2271 Binop_12x<HXor>(instruction, Primitive::kPrimInt);
2272 break;
2273 }
2274
2275 case Instruction::XOR_LONG_2ADDR: {
2276 Binop_12x<HXor>(instruction, Primitive::kPrimLong);
2277 break;
2278 }
2279
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002280 case Instruction::ADD_INT_LIT16: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002281 Binop_22s<HAdd>(instruction, false);
2282 break;
2283 }
2284
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002285 case Instruction::AND_INT_LIT16: {
2286 Binop_22s<HAnd>(instruction, false);
2287 break;
2288 }
2289
2290 case Instruction::OR_INT_LIT16: {
2291 Binop_22s<HOr>(instruction, false);
2292 break;
2293 }
2294
2295 case Instruction::XOR_INT_LIT16: {
2296 Binop_22s<HXor>(instruction, false);
2297 break;
2298 }
2299
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002300 case Instruction::RSUB_INT: {
2301 Binop_22s<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002302 break;
2303 }
2304
Calin Juravle34bacdf2014-10-07 20:23:36 +01002305 case Instruction::MUL_INT_LIT16: {
2306 Binop_22s<HMul>(instruction, false);
2307 break;
2308 }
2309
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002310 case Instruction::ADD_INT_LIT8: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002311 Binop_22b<HAdd>(instruction, false);
2312 break;
2313 }
2314
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002315 case Instruction::AND_INT_LIT8: {
2316 Binop_22b<HAnd>(instruction, false);
2317 break;
2318 }
2319
2320 case Instruction::OR_INT_LIT8: {
2321 Binop_22b<HOr>(instruction, false);
2322 break;
2323 }
2324
2325 case Instruction::XOR_INT_LIT8: {
2326 Binop_22b<HXor>(instruction, false);
2327 break;
2328 }
2329
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002330 case Instruction::RSUB_INT_LIT8: {
2331 Binop_22b<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002332 break;
2333 }
2334
Calin Juravle34bacdf2014-10-07 20:23:36 +01002335 case Instruction::MUL_INT_LIT8: {
2336 Binop_22b<HMul>(instruction, false);
2337 break;
2338 }
2339
Calin Juravled0d48522014-11-04 16:40:20 +00002340 case Instruction::DIV_INT_LIT16:
2341 case Instruction::DIV_INT_LIT8: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002342 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2343 dex_pc, Primitive::kPrimInt, true, true);
2344 break;
2345 }
2346
2347 case Instruction::REM_INT_LIT16:
2348 case Instruction::REM_INT_LIT8: {
2349 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2350 dex_pc, Primitive::kPrimInt, true, false);
Calin Juravled0d48522014-11-04 16:40:20 +00002351 break;
2352 }
2353
Calin Juravle9aec02f2014-11-18 23:06:35 +00002354 case Instruction::SHL_INT_LIT8: {
2355 Binop_22b<HShl>(instruction, false);
2356 break;
2357 }
2358
2359 case Instruction::SHR_INT_LIT8: {
2360 Binop_22b<HShr>(instruction, false);
2361 break;
2362 }
2363
2364 case Instruction::USHR_INT_LIT8: {
2365 Binop_22b<HUShr>(instruction, false);
2366 break;
2367 }
2368
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002369 case Instruction::NEW_INSTANCE: {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002370 uint16_t type_index = instruction.VRegB_21c();
Jeff Hao848f70a2014-01-15 13:49:50 -08002371 if (compiler_driver_->IsStringTypeIndex(type_index, dex_file_)) {
Jeff Hao848f70a2014-01-15 13:49:50 -08002372 int32_t register_index = instruction.VRegA();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002373 HFakeString* fake_string = new (arena_) HFakeString();
2374 current_block_->AddInstruction(fake_string);
2375 UpdateLocal(register_index, fake_string);
Jeff Hao848f70a2014-01-15 13:49:50 -08002376 } else {
2377 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
2378 ? kQuickAllocObjectWithAccessCheck
2379 : kQuickAllocObject;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002380
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002381 current_block_->AddInstruction(new (arena_) HNewInstance(
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002382 graph_->GetCurrentMethod(),
2383 dex_pc,
2384 type_index,
2385 *dex_compilation_unit_->GetDexFile(),
2386 entrypoint));
Jeff Hao848f70a2014-01-15 13:49:50 -08002387 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
2388 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002389 break;
2390 }
2391
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002392 case Instruction::NEW_ARRAY: {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002393 uint16_t type_index = instruction.VRegC_22c();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002394 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt);
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002395 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
2396 ? kQuickAllocArrayWithAccessCheck
2397 : kQuickAllocArray;
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002398 current_block_->AddInstruction(new (arena_) HNewArray(length,
2399 graph_->GetCurrentMethod(),
2400 dex_pc,
2401 type_index,
2402 *dex_compilation_unit_->GetDexFile(),
2403 entrypoint));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002404 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
2405 break;
2406 }
2407
2408 case Instruction::FILLED_NEW_ARRAY: {
2409 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
2410 uint32_t type_index = instruction.VRegB_35c();
2411 uint32_t args[5];
2412 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00002413 BuildFilledNewArray(dex_pc, type_index, number_of_vreg_arguments, false, args, 0);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002414 break;
2415 }
2416
2417 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2418 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
2419 uint32_t type_index = instruction.VRegB_3rc();
2420 uint32_t register_index = instruction.VRegC_3rc();
2421 BuildFilledNewArray(
Calin Juravle225ff812014-11-13 16:46:39 +00002422 dex_pc, type_index, number_of_vreg_arguments, true, nullptr, register_index);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002423 break;
2424 }
2425
2426 case Instruction::FILL_ARRAY_DATA: {
Calin Juravle225ff812014-11-13 16:46:39 +00002427 BuildFillArrayData(instruction, dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002428 break;
2429 }
2430
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002431 case Instruction::MOVE_RESULT:
Dave Allison20dfc792014-06-16 20:44:29 -07002432 case Instruction::MOVE_RESULT_WIDE:
David Brazdilfc6a86a2015-06-26 10:33:45 +00002433 case Instruction::MOVE_RESULT_OBJECT: {
Nicolas Geoffray1efcc222015-06-24 12:41:20 +01002434 if (latest_result_ == nullptr) {
2435 // Only dead code can lead to this situation, where the verifier
2436 // does not reject the method.
2437 } else {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002438 // An Invoke/FilledNewArray and its MoveResult could have landed in
2439 // different blocks if there was a try/catch block boundary between
2440 // them. For Invoke, we insert a StoreLocal after the instruction. For
2441 // FilledNewArray, the local needs to be updated after the array was
2442 // filled, otherwise we might overwrite an input vreg.
2443 HStoreLocal* update_local =
2444 new (arena_) HStoreLocal(GetLocalAt(instruction.VRegA()), latest_result_);
2445 HBasicBlock* block = latest_result_->GetBlock();
2446 if (block == current_block_) {
2447 // MoveResult and the previous instruction are in the same block.
2448 current_block_->AddInstruction(update_local);
2449 } else {
2450 // The two instructions are in different blocks. Insert the MoveResult
2451 // before the final control-flow instruction of the previous block.
2452 DCHECK(block->EndsWithControlFlowInstruction());
2453 DCHECK(current_block_->GetInstructions().IsEmpty());
2454 block->InsertInstructionBefore(update_local, block->GetLastInstruction());
2455 }
Nicolas Geoffray1efcc222015-06-24 12:41:20 +01002456 latest_result_ = nullptr;
2457 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002458 break;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002459 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002460
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002461 case Instruction::CMP_LONG: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002462 Binop_23x_cmp(instruction, Primitive::kPrimLong, ComparisonBias::kNoBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002463 break;
2464 }
2465
2466 case Instruction::CMPG_FLOAT: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002467 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kGtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002468 break;
2469 }
2470
2471 case Instruction::CMPG_DOUBLE: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002472 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kGtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002473 break;
2474 }
2475
2476 case Instruction::CMPL_FLOAT: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002477 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kLtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002478 break;
2479 }
2480
2481 case Instruction::CMPL_DOUBLE: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002482 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kLtBias, dex_pc);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002483 break;
2484 }
2485
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002486 case Instruction::NOP:
2487 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002488
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002489 case Instruction::IGET:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002490 case Instruction::IGET_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002491 case Instruction::IGET_WIDE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002492 case Instruction::IGET_WIDE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002493 case Instruction::IGET_OBJECT:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002494 case Instruction::IGET_OBJECT_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002495 case Instruction::IGET_BOOLEAN:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002496 case Instruction::IGET_BOOLEAN_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002497 case Instruction::IGET_BYTE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002498 case Instruction::IGET_BYTE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002499 case Instruction::IGET_CHAR:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002500 case Instruction::IGET_CHAR_QUICK:
2501 case Instruction::IGET_SHORT:
2502 case Instruction::IGET_SHORT_QUICK: {
Calin Juravle225ff812014-11-13 16:46:39 +00002503 if (!BuildInstanceFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002504 return false;
2505 }
2506 break;
2507 }
2508
2509 case Instruction::IPUT:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002510 case Instruction::IPUT_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002511 case Instruction::IPUT_WIDE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002512 case Instruction::IPUT_WIDE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002513 case Instruction::IPUT_OBJECT:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002514 case Instruction::IPUT_OBJECT_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002515 case Instruction::IPUT_BOOLEAN:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002516 case Instruction::IPUT_BOOLEAN_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002517 case Instruction::IPUT_BYTE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002518 case Instruction::IPUT_BYTE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002519 case Instruction::IPUT_CHAR:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002520 case Instruction::IPUT_CHAR_QUICK:
2521 case Instruction::IPUT_SHORT:
2522 case Instruction::IPUT_SHORT_QUICK: {
Calin Juravle225ff812014-11-13 16:46:39 +00002523 if (!BuildInstanceFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002524 return false;
2525 }
2526 break;
2527 }
2528
2529 case Instruction::SGET:
2530 case Instruction::SGET_WIDE:
2531 case Instruction::SGET_OBJECT:
2532 case Instruction::SGET_BOOLEAN:
2533 case Instruction::SGET_BYTE:
2534 case Instruction::SGET_CHAR:
2535 case Instruction::SGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002536 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002537 return false;
2538 }
2539 break;
2540 }
2541
2542 case Instruction::SPUT:
2543 case Instruction::SPUT_WIDE:
2544 case Instruction::SPUT_OBJECT:
2545 case Instruction::SPUT_BOOLEAN:
2546 case Instruction::SPUT_BYTE:
2547 case Instruction::SPUT_CHAR:
2548 case Instruction::SPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002549 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002550 return false;
2551 }
2552 break;
2553 }
2554
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002555#define ARRAY_XX(kind, anticipated_type) \
2556 case Instruction::AGET##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00002557 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002558 break; \
2559 } \
2560 case Instruction::APUT##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00002561 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002562 break; \
2563 }
2564
2565 ARRAY_XX(, Primitive::kPrimInt);
2566 ARRAY_XX(_WIDE, Primitive::kPrimLong);
2567 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
2568 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
2569 ARRAY_XX(_BYTE, Primitive::kPrimByte);
2570 ARRAY_XX(_CHAR, Primitive::kPrimChar);
2571 ARRAY_XX(_SHORT, Primitive::kPrimShort);
2572
Nicolas Geoffray39468442014-09-02 15:17:15 +01002573 case Instruction::ARRAY_LENGTH: {
2574 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002575 // No need for a temporary for the null check, it is the only input of the following
2576 // instruction.
Calin Juravle225ff812014-11-13 16:46:39 +00002577 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002578 current_block_->AddInstruction(object);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002579 current_block_->AddInstruction(new (arena_) HArrayLength(object));
2580 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
2581 break;
2582 }
2583
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002584 case Instruction::CONST_STRING: {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002585 current_block_->AddInstruction(
2586 new (arena_) HLoadString(graph_->GetCurrentMethod(), instruction.VRegB_21c(), dex_pc));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002587 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2588 break;
2589 }
2590
2591 case Instruction::CONST_STRING_JUMBO: {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002592 current_block_->AddInstruction(
2593 new (arena_) HLoadString(graph_->GetCurrentMethod(), instruction.VRegB_31c(), dex_pc));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002594 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
2595 break;
2596 }
2597
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002598 case Instruction::CONST_CLASS: {
2599 uint16_t type_index = instruction.VRegB_21c();
2600 bool type_known_final;
2601 bool type_known_abstract;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002602 bool dont_use_is_referrers_class;
2603 // `CanAccessTypeWithoutChecks` will tell whether the method being
2604 // built is trying to access its own class, so that the generated
2605 // code can optimize for this case. However, the optimization does not
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002606 // work for inlining, so we use `IsOutermostCompilingClass` instead.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002607 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
2608 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002609 &type_known_final, &type_known_abstract, &dont_use_is_referrers_class);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002610 if (!can_access) {
Calin Juravle48c2b032014-12-09 18:11:36 +00002611 MaybeRecordStat(MethodCompilationStat::kNotCompiledCantAccesType);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002612 return false;
2613 }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002614 current_block_->AddInstruction(new (arena_) HLoadClass(
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002615 graph_->GetCurrentMethod(),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002616 type_index,
2617 *dex_compilation_unit_->GetDexFile(),
2618 IsOutermostCompilingClass(type_index),
2619 dex_pc));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002620 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2621 break;
2622 }
2623
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002624 case Instruction::MOVE_EXCEPTION: {
2625 current_block_->AddInstruction(new (arena_) HLoadException());
2626 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
David Brazdilcb1c0552015-08-04 16:22:25 +01002627 current_block_->AddInstruction(new (arena_) HClearException());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002628 break;
2629 }
2630
2631 case Instruction::THROW: {
2632 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00002633 current_block_->AddInstruction(new (arena_) HThrow(exception, dex_pc));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002634 // A throw instruction must branch to the exit block.
2635 current_block_->AddSuccessor(exit_block_);
2636 // We finished building this block. Set the current block to null to avoid
2637 // adding dead instructions to it.
2638 current_block_ = nullptr;
2639 break;
2640 }
2641
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002642 case Instruction::INSTANCE_OF: {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002643 uint8_t destination = instruction.VRegA_22c();
2644 uint8_t reference = instruction.VRegB_22c();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002645 uint16_t type_index = instruction.VRegC_22c();
Calin Juravle225ff812014-11-13 16:46:39 +00002646 if (!BuildTypeCheck(instruction, destination, reference, type_index, dex_pc)) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002647 return false;
2648 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002649 break;
2650 }
2651
2652 case Instruction::CHECK_CAST: {
2653 uint8_t reference = instruction.VRegA_21c();
2654 uint16_t type_index = instruction.VRegB_21c();
Calin Juravle225ff812014-11-13 16:46:39 +00002655 if (!BuildTypeCheck(instruction, -1, reference, type_index, dex_pc)) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002656 return false;
2657 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002658 break;
2659 }
2660
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002661 case Instruction::MONITOR_ENTER: {
2662 current_block_->AddInstruction(new (arena_) HMonitorOperation(
2663 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2664 HMonitorOperation::kEnter,
Calin Juravle225ff812014-11-13 16:46:39 +00002665 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002666 break;
2667 }
2668
2669 case Instruction::MONITOR_EXIT: {
2670 current_block_->AddInstruction(new (arena_) HMonitorOperation(
2671 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2672 HMonitorOperation::kExit,
Calin Juravle225ff812014-11-13 16:46:39 +00002673 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002674 break;
2675 }
2676
Andreas Gamped881df52014-11-24 23:28:39 -08002677 case Instruction::PACKED_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002678 BuildPackedSwitch(instruction, dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08002679 break;
2680 }
2681
Andreas Gampee4d4d322014-12-04 09:09:57 -08002682 case Instruction::SPARSE_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002683 BuildSparseSwitch(instruction, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08002684 break;
2685 }
2686
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002687 default:
Calin Juravle48c2b032014-12-09 18:11:36 +00002688 VLOG(compiler) << "Did not compile "
2689 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
2690 << " because of unhandled instruction "
2691 << instruction.Name();
2692 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnhandledInstruction);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002693 return false;
2694 }
2695 return true;
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00002696} // NOLINT(readability/fn_size)
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002697
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002698HLocal* HGraphBuilder::GetLocalAt(int register_index) const {
2699 return locals_.Get(register_index);
2700}
2701
2702void HGraphBuilder::UpdateLocal(int register_index, HInstruction* instruction) const {
2703 HLocal* local = GetLocalAt(register_index);
2704 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction));
2705}
2706
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002707HInstruction* HGraphBuilder::LoadLocal(int register_index, Primitive::Type type) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002708 HLocal* local = GetLocalAt(register_index);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002709 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type));
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002710 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002711}
2712
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002713} // namespace art