blob: 9895953e1f8a92bae4cf1ab734a6ace937e11621 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Nicolas Geoffraye5038322014-07-04 09:41:32 +010017#include "builder.h"
18
Mathieu Chartierc7853442015-03-27 14:35:38 -070019#include "art_field-inl.h"
Andreas Gamped881df52014-11-24 23:28:39 -080020#include "base/logging.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010021#include "class_linker.h"
Jeff Hao848f70a2014-01-15 13:49:50 -080022#include "dex/verified_method.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000023#include "dex_file-inl.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000024#include "dex_instruction-inl.h"
Roland Levillain4c0eb422015-04-24 16:43:49 +010025#include "dex/verified_method.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010026#include "driver/compiler_driver-inl.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000027#include "driver/compiler_options.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010028#include "mirror/class_loader.h"
29#include "mirror/dex_cache.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000030#include "nodes.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000031#include "primitive.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010032#include "scoped_thread_state_change.h"
33#include "thread.h"
Vladimir Marko58155012015-08-19 12:49:41 +000034#include "utils/dex_cache_arrays_layout-inl.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000035
36namespace art {
37
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010038/**
39 * Helper class to add HTemporary instructions. This class is used when
40 * converting a DEX instruction to multiple HInstruction, and where those
41 * instructions do not die at the following instruction, but instead spans
42 * multiple instructions.
43 */
44class Temporaries : public ValueObject {
45 public:
Calin Juravlef97f9fb2014-11-11 15:38:19 +000046 explicit Temporaries(HGraph* graph) : graph_(graph), index_(0) {}
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010047
48 void Add(HInstruction* instruction) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060049 HInstruction* temp = new (graph_->GetArena()) HTemporary(index_, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010050 instruction->GetBlock()->AddInstruction(temp);
Calin Juravlef97f9fb2014-11-11 15:38:19 +000051
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010052 DCHECK(temp->GetPrevious() == instruction);
Calin Juravlef97f9fb2014-11-11 15:38:19 +000053
54 size_t offset;
55 if (instruction->GetType() == Primitive::kPrimLong
56 || instruction->GetType() == Primitive::kPrimDouble) {
57 offset = 2;
58 } else {
59 offset = 1;
60 }
61 index_ += offset;
62
63 graph_->UpdateTemporariesVRegSlots(index_);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010064 }
65
66 private:
67 HGraph* const graph_;
68
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010069 // Current index in the temporary stack, updated by `Add`.
70 size_t index_;
71};
72
Andreas Gamped881df52014-11-24 23:28:39 -080073class SwitchTable : public ValueObject {
74 public:
75 SwitchTable(const Instruction& instruction, uint32_t dex_pc, bool sparse)
76 : instruction_(instruction), dex_pc_(dex_pc), sparse_(sparse) {
77 int32_t table_offset = instruction.VRegB_31t();
78 const uint16_t* table = reinterpret_cast<const uint16_t*>(&instruction) + table_offset;
79 if (sparse) {
80 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
81 } else {
82 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
83 }
84 num_entries_ = table[1];
85 values_ = reinterpret_cast<const int32_t*>(&table[2]);
86 }
87
88 uint16_t GetNumEntries() const {
89 return num_entries_;
90 }
91
Andreas Gampee4d4d322014-12-04 09:09:57 -080092 void CheckIndex(size_t index) const {
93 if (sparse_) {
94 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
95 DCHECK_LT(index, 2 * static_cast<size_t>(num_entries_));
96 } else {
97 // In a packed table, we have the starting key and num_entries_ values.
98 DCHECK_LT(index, 1 + static_cast<size_t>(num_entries_));
99 }
100 }
101
Andreas Gamped881df52014-11-24 23:28:39 -0800102 int32_t GetEntryAt(size_t index) const {
Andreas Gampee4d4d322014-12-04 09:09:57 -0800103 CheckIndex(index);
Andreas Gamped881df52014-11-24 23:28:39 -0800104 return values_[index];
105 }
106
107 uint32_t GetDexPcForIndex(size_t index) const {
Andreas Gampee4d4d322014-12-04 09:09:57 -0800108 CheckIndex(index);
Andreas Gamped881df52014-11-24 23:28:39 -0800109 return dex_pc_ +
110 (reinterpret_cast<const int16_t*>(values_ + index) -
111 reinterpret_cast<const int16_t*>(&instruction_));
112 }
113
Andreas Gampee4d4d322014-12-04 09:09:57 -0800114 // Index of the first value in the table.
115 size_t GetFirstValueIndex() const {
116 if (sparse_) {
117 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
118 return num_entries_;
119 } else {
120 // In a packed table, we have the starting key and num_entries_ values.
121 return 1;
122 }
123 }
124
Andreas Gamped881df52014-11-24 23:28:39 -0800125 private:
126 const Instruction& instruction_;
127 const uint32_t dex_pc_;
128
129 // Whether this is a sparse-switch table (or a packed-switch one).
130 const bool sparse_;
131
132 // This can't be const as it needs to be computed off of the given instruction, and complicated
133 // expressions in the initializer list seemed very ugly.
134 uint16_t num_entries_;
135
136 const int32_t* values_;
137
138 DISALLOW_COPY_AND_ASSIGN(SwitchTable);
139};
140
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100141void HGraphBuilder::InitializeLocals(uint16_t count) {
142 graph_->SetNumberOfVRegs(count);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000143 locals_.SetSize(count);
144 for (int i = 0; i < count; i++) {
145 HLocal* local = new (arena_) HLocal(i);
146 entry_block_->AddInstruction(local);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000147 locals_.Put(i, local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000148 }
149}
150
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000151void HGraphBuilder::InitializeParameters(uint16_t number_of_parameters) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100152 // dex_compilation_unit_ is null only when unit testing.
153 if (dex_compilation_unit_ == nullptr) {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000154 return;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100155 }
156
157 graph_->SetNumberOfInVRegs(number_of_parameters);
158 const char* shorty = dex_compilation_unit_->GetShorty();
159 int locals_index = locals_.Size() - number_of_parameters;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100160 int parameter_index = 0;
161
162 if (!dex_compilation_unit_->IsStatic()) {
163 // Add the implicit 'this' argument, not expressed in the signature.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600164 HParameterValue* parameter = new (arena_) HParameterValue(parameter_index++,
165 Primitive::kPrimNot,
166 true);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100167 entry_block_->AddInstruction(parameter);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100168 HLocal* local = GetLocalAt(locals_index++);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600169 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter, local->GetDexPc()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100170 number_of_parameters--;
171 }
172
173 uint32_t pos = 1;
174 for (int i = 0; i < number_of_parameters; i++) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600175 HParameterValue* parameter = new (arena_) HParameterValue(parameter_index++,
176 Primitive::GetType(shorty[pos++]),
177 false);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100178 entry_block_->AddInstruction(parameter);
179 HLocal* local = GetLocalAt(locals_index++);
180 // Store the parameter value in the local that the dex code will use
181 // to reference that parameter.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600182 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter, local->GetDexPc()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100183 bool is_wide = (parameter->GetType() == Primitive::kPrimLong)
184 || (parameter->GetType() == Primitive::kPrimDouble);
185 if (is_wide) {
186 i++;
187 locals_index++;
188 parameter_index++;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100189 }
190 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100191}
192
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100193template<typename T>
Calin Juravle225ff812014-11-13 16:46:39 +0000194void HGraphBuilder::If_22t(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000195 int32_t target_offset = instruction.GetTargetOffset();
David Brazdil852eaff2015-02-02 15:23:05 +0000196 HBasicBlock* branch_target = FindBlockStartingAt(dex_pc + target_offset);
197 HBasicBlock* fallthrough_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
198 DCHECK(branch_target != nullptr);
199 DCHECK(fallthrough_target != nullptr);
200 PotentiallyAddSuspendCheck(branch_target, dex_pc);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600201 HInstruction* first = LoadLocal(instruction.VRegA(), Primitive::kPrimInt, dex_pc);
202 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
203 T* comparison = new (arena_) T(first, second, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700204 current_block_->AddInstruction(comparison);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600205 HInstruction* ifinst = new (arena_) HIf(comparison, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700206 current_block_->AddInstruction(ifinst);
David Brazdil852eaff2015-02-02 15:23:05 +0000207 current_block_->AddSuccessor(branch_target);
208 current_block_->AddSuccessor(fallthrough_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700209 current_block_ = nullptr;
210}
211
212template<typename T>
Calin Juravle225ff812014-11-13 16:46:39 +0000213void HGraphBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000214 int32_t target_offset = instruction.GetTargetOffset();
David Brazdil852eaff2015-02-02 15:23:05 +0000215 HBasicBlock* branch_target = FindBlockStartingAt(dex_pc + target_offset);
216 HBasicBlock* fallthrough_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
217 DCHECK(branch_target != nullptr);
218 DCHECK(fallthrough_target != nullptr);
219 PotentiallyAddSuspendCheck(branch_target, dex_pc);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600220 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt, dex_pc);
221 T* comparison = new (arena_) T(value, graph_->GetIntConstant(0, dex_pc), dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700222 current_block_->AddInstruction(comparison);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600223 HInstruction* ifinst = new (arena_) HIf(comparison, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700224 current_block_->AddInstruction(ifinst);
David Brazdil852eaff2015-02-02 15:23:05 +0000225 current_block_->AddSuccessor(branch_target);
226 current_block_->AddSuccessor(fallthrough_target);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100227 current_block_ = nullptr;
228}
229
Calin Juravle48c2b032014-12-09 18:11:36 +0000230void HGraphBuilder::MaybeRecordStat(MethodCompilationStat compilation_stat) {
231 if (compilation_stats_ != nullptr) {
232 compilation_stats_->RecordStat(compilation_stat);
233 }
234}
235
David Brazdil1b498722015-03-31 11:37:18 +0100236bool HGraphBuilder::SkipCompilation(const DexFile::CodeItem& code_item,
Calin Juravle48c2b032014-12-09 18:11:36 +0000237 size_t number_of_branches) {
238 const CompilerOptions& compiler_options = compiler_driver_->GetCompilerOptions();
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000239 CompilerOptions::CompilerFilter compiler_filter = compiler_options.GetCompilerFilter();
240 if (compiler_filter == CompilerOptions::kEverything) {
241 return false;
242 }
243
David Brazdil1b498722015-03-31 11:37:18 +0100244 if (compiler_options.IsHugeMethod(code_item.insns_size_in_code_units_)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000245 VLOG(compiler) << "Skip compilation of huge method "
246 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
David Brazdil1b498722015-03-31 11:37:18 +0100247 << ": " << code_item.insns_size_in_code_units_ << " code units";
Calin Juravle48c2b032014-12-09 18:11:36 +0000248 MaybeRecordStat(MethodCompilationStat::kNotCompiledHugeMethod);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000249 return true;
250 }
251
252 // If it's large and contains no branches, it's likely to be machine generated initialization.
David Brazdil1b498722015-03-31 11:37:18 +0100253 if (compiler_options.IsLargeMethod(code_item.insns_size_in_code_units_)
254 && (number_of_branches == 0)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000255 VLOG(compiler) << "Skip compilation of large method with no branch "
256 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
David Brazdil1b498722015-03-31 11:37:18 +0100257 << ": " << code_item.insns_size_in_code_units_ << " code units";
Calin Juravle48c2b032014-12-09 18:11:36 +0000258 MaybeRecordStat(MethodCompilationStat::kNotCompiledLargeMethodNoBranches);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000259 return true;
260 }
261
262 return false;
263}
264
David Brazdilbff75032015-07-08 17:26:51 +0000265static const DexFile::TryItem* GetTryItem(HBasicBlock* block,
266 const DexFile::CodeItem& code_item,
267 const ArenaBitVector& can_block_throw) {
268 DCHECK(!block->IsSingleTryBoundary());
269
270 // Block does not contain throwing instructions. Even if it is covered by
271 // a TryItem, we will consider it not in a try block.
272 if (!can_block_throw.IsBitSet(block->GetBlockId())) {
273 return nullptr;
274 }
275
276 // Instructions in the block may throw. Find a TryItem covering this block.
277 int32_t try_item_idx = DexFile::FindTryItem(code_item, block->GetDexPc());
David Brazdil6cd788f2015-07-08 16:44:00 +0100278 return (try_item_idx == -1) ? nullptr : DexFile::GetTryItems(code_item, try_item_idx);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000279}
280
281void HGraphBuilder::CreateBlocksForTryCatch(const DexFile::CodeItem& code_item) {
282 if (code_item.tries_size_ == 0) {
283 return;
284 }
285
286 // Create branch targets at the start/end of the TryItem range. These are
287 // places where the program might fall through into/out of the a block and
288 // where TryBoundary instructions will be inserted later. Other edges which
289 // enter/exit the try blocks are a result of branches/switches.
290 for (size_t idx = 0; idx < code_item.tries_size_; ++idx) {
291 const DexFile::TryItem* try_item = DexFile::GetTryItems(code_item, idx);
292 uint32_t dex_pc_start = try_item->start_addr_;
293 uint32_t dex_pc_end = dex_pc_start + try_item->insn_count_;
294 FindOrCreateBlockStartingAt(dex_pc_start);
295 if (dex_pc_end < code_item.insns_size_in_code_units_) {
296 // TODO: Do not create block if the last instruction cannot fall through.
297 FindOrCreateBlockStartingAt(dex_pc_end);
298 } else {
299 // The TryItem spans until the very end of the CodeItem (or beyond if
300 // invalid) and therefore cannot have any code afterwards.
301 }
302 }
303
304 // Create branch targets for exception handlers.
305 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(code_item, 0);
306 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
307 for (uint32_t idx = 0; idx < handlers_size; ++idx) {
308 CatchHandlerIterator iterator(handlers_ptr);
309 for (; iterator.HasNext(); iterator.Next()) {
310 uint32_t address = iterator.GetHandlerAddress();
311 HBasicBlock* block = FindOrCreateBlockStartingAt(address);
David Brazdilec16f792015-08-19 15:04:01 +0100312 block->SetTryCatchInformation(
313 new (arena_) TryCatchInformation(iterator.GetHandlerTypeIndex(), *dex_file_));
David Brazdilfc6a86a2015-06-26 10:33:45 +0000314 }
315 handlers_ptr = iterator.EndDataPointer();
316 }
317}
318
David Brazdil56e1acc2015-06-30 15:41:36 +0100319void HGraphBuilder::SplitTryBoundaryEdge(HBasicBlock* predecessor,
320 HBasicBlock* successor,
321 HTryBoundary::BoundaryKind kind,
322 const DexFile::CodeItem& code_item,
323 const DexFile::TryItem& try_item) {
324 // Split the edge with a single TryBoundary instruction.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600325 HTryBoundary* try_boundary = new (arena_) HTryBoundary(kind, successor->GetDexPc());
David Brazdil56e1acc2015-06-30 15:41:36 +0100326 HBasicBlock* try_entry_block = graph_->SplitEdge(predecessor, successor);
327 try_entry_block->AddInstruction(try_boundary);
328
329 // Link the TryBoundary to the handlers of `try_item`.
330 for (CatchHandlerIterator it(code_item, try_item); it.HasNext(); it.Next()) {
331 try_boundary->AddExceptionHandler(FindBlockStartingAt(it.GetHandlerAddress()));
332 }
333}
334
David Brazdilfc6a86a2015-06-26 10:33:45 +0000335void HGraphBuilder::InsertTryBoundaryBlocks(const DexFile::CodeItem& code_item) {
336 if (code_item.tries_size_ == 0) {
337 return;
338 }
339
David Brazdil72783ff2015-07-09 14:36:05 +0100340 // Bit vector stores information on which blocks contain throwing instructions.
341 // Must be expandable because catch blocks may be split into two.
342 ArenaBitVector can_block_throw(arena_, graph_->GetBlocks().Size(), /* expandable */ true);
David Brazdilbff75032015-07-08 17:26:51 +0000343
344 // Scan blocks and mark those which contain throwing instructions.
David Brazdil72783ff2015-07-09 14:36:05 +0100345 for (size_t block_id = 0, e = graph_->GetBlocks().Size(); block_id < e; ++block_id) {
David Brazdilbff75032015-07-08 17:26:51 +0000346 HBasicBlock* block = graph_->GetBlocks().Get(block_id);
David Brazdil72783ff2015-07-09 14:36:05 +0100347 bool can_throw = false;
David Brazdilbff75032015-07-08 17:26:51 +0000348 for (HInstructionIterator insn(block->GetInstructions()); !insn.Done(); insn.Advance()) {
349 if (insn.Current()->CanThrow()) {
David Brazdil72783ff2015-07-09 14:36:05 +0100350 can_throw = true;
David Brazdilbff75032015-07-08 17:26:51 +0000351 break;
352 }
353 }
David Brazdil72783ff2015-07-09 14:36:05 +0100354
355 if (can_throw) {
356 if (block->IsCatchBlock()) {
357 // Catch blocks are always considered an entry point into the TryItem in
358 // order to avoid splitting exceptional edges. We split the block after
359 // the move-exception (if present) and mark the first part non-throwing.
360 // Later on, a TryBoundary will be inserted between the two blocks.
361 HInstruction* first_insn = block->GetFirstInstruction();
362 if (first_insn->IsLoadException()) {
363 // Catch block starts with a LoadException. Split the block after the
David Brazdilcb1c0552015-08-04 16:22:25 +0100364 // StoreLocal and ClearException which must come after the load.
David Brazdil72783ff2015-07-09 14:36:05 +0100365 DCHECK(first_insn->GetNext()->IsStoreLocal());
David Brazdilcb1c0552015-08-04 16:22:25 +0100366 DCHECK(first_insn->GetNext()->GetNext()->IsClearException());
367 block = block->SplitBefore(first_insn->GetNext()->GetNext()->GetNext());
David Brazdil72783ff2015-07-09 14:36:05 +0100368 } else {
369 // Catch block does not load the exception. Split at the beginning to
370 // create an empty catch block.
371 block = block->SplitBefore(first_insn);
372 }
373 }
374 can_block_throw.SetBit(block->GetBlockId());
375 }
David Brazdilbff75032015-07-08 17:26:51 +0000376 }
377
David Brazdil281a6322015-07-03 10:34:57 +0100378 // Iterate over all blocks, find those covered by some TryItem and:
379 // (a) split edges which enter/exit the try range,
380 // (b) create TryBoundary instructions in the new blocks,
381 // (c) link the new blocks to corresponding exception handlers.
382 // We cannot iterate only over blocks in `branch_targets_` because switch-case
383 // blocks share the same dex_pc.
David Brazdil72783ff2015-07-09 14:36:05 +0100384 for (size_t block_id = 0, e = graph_->GetBlocks().Size(); block_id < e; ++block_id) {
David Brazdil281a6322015-07-03 10:34:57 +0100385 HBasicBlock* try_block = graph_->GetBlocks().Get(block_id);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000386
David Brazdil281a6322015-07-03 10:34:57 +0100387 // TryBoundary blocks are added at the end of the list and not iterated over.
388 DCHECK(!try_block->IsSingleTryBoundary());
David Brazdilfc6a86a2015-06-26 10:33:45 +0000389
David Brazdil281a6322015-07-03 10:34:57 +0100390 // Find the TryItem for this block.
David Brazdilbff75032015-07-08 17:26:51 +0000391 const DexFile::TryItem* try_item = GetTryItem(try_block, code_item, can_block_throw);
392 if (try_item == nullptr) {
David Brazdil281a6322015-07-03 10:34:57 +0100393 continue;
394 }
David Brazdil281a6322015-07-03 10:34:57 +0100395
David Brazdil72783ff2015-07-09 14:36:05 +0100396 // Catch blocks were split earlier and cannot throw.
397 DCHECK(!try_block->IsCatchBlock());
398
399 // Find predecessors which are not covered by the same TryItem range. Such
400 // edges enter the try block and will have a TryBoundary inserted.
Vladimir Marko60584552015-09-03 13:35:12 +0000401 for (size_t i = 0; i < try_block->GetPredecessors().size(); ++i) {
402 HBasicBlock* predecessor = try_block->GetPredecessor(i);
David Brazdil72783ff2015-07-09 14:36:05 +0100403 if (predecessor->IsSingleTryBoundary()) {
404 // The edge was already split because of an exit from a neighbouring
405 // TryItem. We split it again and insert an entry point.
406 if (kIsDebugBuild) {
407 HTryBoundary* last_insn = predecessor->GetLastInstruction()->AsTryBoundary();
408 const DexFile::TryItem* predecessor_try_item =
409 GetTryItem(predecessor->GetSinglePredecessor(), code_item, can_block_throw);
410 DCHECK(!last_insn->IsEntry());
411 DCHECK_EQ(last_insn->GetNormalFlowSuccessor(), try_block);
412 DCHECK(try_block->IsFirstIndexOfPredecessor(predecessor, i));
413 DCHECK_NE(try_item, predecessor_try_item);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000414 }
David Brazdil72783ff2015-07-09 14:36:05 +0100415 } else if (GetTryItem(predecessor, code_item, can_block_throw) != try_item) {
416 // This is an entry point into the TryItem and the edge has not been
417 // split yet. That means that `predecessor` is not in a TryItem, or
418 // it is in a different TryItem and we happened to iterate over this
419 // block first. We split the edge and insert an entry point.
420 } else {
421 // Not an edge on the boundary of the try block.
422 continue;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000423 }
David Brazdil72783ff2015-07-09 14:36:05 +0100424 SplitTryBoundaryEdge(predecessor, try_block, HTryBoundary::kEntry, code_item, *try_item);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000425 }
David Brazdil281a6322015-07-03 10:34:57 +0100426
427 // Find successors which are not covered by the same TryItem range. Such
428 // edges exit the try block and will have a TryBoundary inserted.
Vladimir Marko60584552015-09-03 13:35:12 +0000429 for (HBasicBlock* successor : try_block->GetSuccessors()) {
David Brazdil281a6322015-07-03 10:34:57 +0100430 if (successor->IsCatchBlock()) {
431 // A catch block is always considered an entry point into its TryItem.
432 // We therefore assume this is an exit point, regardless of whether
433 // the catch block is in a different TryItem or not.
434 } else if (successor->IsSingleTryBoundary()) {
435 // The edge was already split because of an entry into a neighbouring
436 // TryItem. We split it again and insert an exit.
437 if (kIsDebugBuild) {
438 HTryBoundary* last_insn = successor->GetLastInstruction()->AsTryBoundary();
David Brazdilbff75032015-07-08 17:26:51 +0000439 const DexFile::TryItem* successor_try_item =
440 GetTryItem(last_insn->GetNormalFlowSuccessor(), code_item, can_block_throw);
David Brazdil281a6322015-07-03 10:34:57 +0100441 DCHECK_EQ(try_block, successor->GetSinglePredecessor());
442 DCHECK(last_insn->IsEntry());
David Brazdilbff75032015-07-08 17:26:51 +0000443 DCHECK_NE(try_item, successor_try_item);
David Brazdil281a6322015-07-03 10:34:57 +0100444 }
David Brazdilbff75032015-07-08 17:26:51 +0000445 } else if (GetTryItem(successor, code_item, can_block_throw) != try_item) {
David Brazdil281a6322015-07-03 10:34:57 +0100446 // This is an exit out of the TryItem and the edge has not been split
447 // yet. That means that either `successor` is not in a TryItem, or it
448 // is in a different TryItem and we happened to iterate over this
449 // block first. We split the edge and insert an exit.
450 HInstruction* last_instruction = try_block->GetLastInstruction();
451 if (last_instruction->IsReturn() || last_instruction->IsReturnVoid()) {
452 DCHECK_EQ(successor, exit_block_);
453 // Control flow exits the try block with a Return(Void). Because
454 // splitting the edge would invalidate the invariant that Return
455 // always jumps to Exit, we move the Return outside the try block.
456 successor = try_block->SplitBefore(last_instruction);
457 }
458 } else {
459 // Not an edge on the boundary of the try block.
460 continue;
461 }
David Brazdilbff75032015-07-08 17:26:51 +0000462 SplitTryBoundaryEdge(try_block, successor, HTryBoundary::kExit, code_item, *try_item);
David Brazdil281a6322015-07-03 10:34:57 +0100463 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000464 }
465}
466
David Brazdil5e8b1372015-01-23 14:39:08 +0000467bool HGraphBuilder::BuildGraph(const DexFile::CodeItem& code_item) {
468 DCHECK(graph_->GetBlocks().IsEmpty());
469
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000470 const uint16_t* code_ptr = code_item.insns_;
471 const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100472 code_start_ = code_ptr;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000473
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000474 // Setup the graph with the entry block and exit block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100475 entry_block_ = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000476 graph_->AddBlock(entry_block_);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100477 exit_block_ = new (arena_) HBasicBlock(graph_, kNoDexPc);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000478 graph_->SetEntryBlock(entry_block_);
479 graph_->SetExitBlock(exit_block_);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000480
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000481 InitializeLocals(code_item.registers_size_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000482 graph_->SetMaximumNumberOfOutVRegs(code_item.outs_size_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000483
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000484 // Compute the number of dex instructions, blocks, and branches. We will
485 // check these values against limits given to the compiler.
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000486 size_t number_of_branches = 0;
487
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000488 // To avoid splitting blocks, we compute ahead of time the instructions that
489 // start a new block, and create these blocks.
Calin Juravle702d2602015-04-30 19:28:21 +0100490 if (!ComputeBranchTargets(code_ptr, code_end, &number_of_branches)) {
491 MaybeRecordStat(MethodCompilationStat::kNotCompiledBranchOutsideMethodCode);
492 return false;
493 }
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000494
495 // Note that the compiler driver is null when unit testing.
David Brazdil1b498722015-03-31 11:37:18 +0100496 if ((compiler_driver_ != nullptr) && SkipCompilation(code_item, number_of_branches)) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000497 return false;
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000498 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000499
David Brazdilfc6a86a2015-06-26 10:33:45 +0000500 CreateBlocksForTryCatch(code_item);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000501
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000502 InitializeParameters(code_item.ins_size_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100503
Calin Juravle225ff812014-11-13 16:46:39 +0000504 size_t dex_pc = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000505 while (code_ptr < code_end) {
Calin Juravle225ff812014-11-13 16:46:39 +0000506 // Update the current block if dex_pc starts a new block.
507 MaybeUpdateCurrentBlock(dex_pc);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000508 const Instruction& instruction = *Instruction::At(code_ptr);
Calin Juravle48c2b032014-12-09 18:11:36 +0000509 if (!AnalyzeDexInstruction(instruction, dex_pc)) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000510 return false;
Calin Juravle48c2b032014-12-09 18:11:36 +0000511 }
Calin Juravle225ff812014-11-13 16:46:39 +0000512 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000513 code_ptr += instruction.SizeInCodeUnits();
514 }
515
David Brazdilfc6a86a2015-06-26 10:33:45 +0000516 // Add Exit to the exit block.
David Brazdil3e187382015-06-26 09:59:52 +0000517 exit_block_->AddInstruction(new (arena_) HExit());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000518 // Add the suspend check to the entry block.
519 entry_block_->AddInstruction(new (arena_) HSuspendCheck(0));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000520 entry_block_->AddInstruction(new (arena_) HGoto());
David Brazdilbff75032015-07-08 17:26:51 +0000521 // Add the exit block at the end.
522 graph_->AddBlock(exit_block_);
David Brazdil5e8b1372015-01-23 14:39:08 +0000523
David Brazdilfc6a86a2015-06-26 10:33:45 +0000524 // Iterate over blocks covered by TryItems and insert TryBoundaries at entry
525 // and exit points. This requires all control-flow instructions and
526 // non-exceptional edges to have been created.
527 InsertTryBoundaryBlocks(code_item);
528
David Brazdil5e8b1372015-01-23 14:39:08 +0000529 return true;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000530}
531
David Brazdilfc6a86a2015-06-26 10:33:45 +0000532void HGraphBuilder::MaybeUpdateCurrentBlock(size_t dex_pc) {
533 HBasicBlock* block = FindBlockStartingAt(dex_pc);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000534 if (block == nullptr) {
535 return;
536 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000537
538 if (current_block_ != nullptr) {
539 // Branching instructions clear current_block, so we know
540 // the last instruction of the current block is not a branching
541 // instruction. We add an unconditional goto to the found block.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600542 current_block_->AddInstruction(new (arena_) HGoto(dex_pc));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000543 current_block_->AddSuccessor(block);
544 }
545 graph_->AddBlock(block);
546 current_block_ = block;
547}
548
Calin Juravle702d2602015-04-30 19:28:21 +0100549bool HGraphBuilder::ComputeBranchTargets(const uint16_t* code_ptr,
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000550 const uint16_t* code_end,
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000551 size_t* number_of_branches) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000552 branch_targets_.SetSize(code_end - code_ptr);
553
554 // Create the first block for the dex instructions, single successor of the entry block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100555 HBasicBlock* block = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000556 branch_targets_.Put(0, block);
557 entry_block_->AddSuccessor(block);
558
559 // Iterate over all instructions and find branching instructions. Create blocks for
560 // the locations these instructions branch to.
Andreas Gamped881df52014-11-24 23:28:39 -0800561 uint32_t dex_pc = 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000562 while (code_ptr < code_end) {
563 const Instruction& instruction = *Instruction::At(code_ptr);
564 if (instruction.IsBranch()) {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000565 (*number_of_branches)++;
Calin Juravle225ff812014-11-13 16:46:39 +0000566 int32_t target = instruction.GetTargetOffset() + dex_pc;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000567 // Create a block for the target instruction.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000568 FindOrCreateBlockStartingAt(target);
569
Calin Juravle225ff812014-11-13 16:46:39 +0000570 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000571 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle702d2602015-04-30 19:28:21 +0100572
David Brazdilfe659462015-06-24 14:23:56 +0100573 if (instruction.CanFlowThrough()) {
574 if (code_ptr >= code_end) {
Calin Juravle702d2602015-04-30 19:28:21 +0100575 // In the normal case we should never hit this but someone can artificially forge a dex
576 // file to fall-through out the method code. In this case we bail out compilation.
577 return false;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000578 } else {
579 FindOrCreateBlockStartingAt(dex_pc);
Calin Juravle702d2602015-04-30 19:28:21 +0100580 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000581 }
Andreas Gampee4d4d322014-12-04 09:09:57 -0800582 } else if (instruction.IsSwitch()) {
583 SwitchTable table(instruction, dex_pc, instruction.Opcode() == Instruction::SPARSE_SWITCH);
Andreas Gamped881df52014-11-24 23:28:39 -0800584
585 uint16_t num_entries = table.GetNumEntries();
586
Andreas Gampee4d4d322014-12-04 09:09:57 -0800587 // In a packed-switch, the entry at index 0 is the starting key. In a sparse-switch, the
588 // entry at index 0 is the first key, and values are after *all* keys.
589 size_t offset = table.GetFirstValueIndex();
590
591 // Use a larger loop counter type to avoid overflow issues.
592 for (size_t i = 0; i < num_entries; ++i) {
Andreas Gamped881df52014-11-24 23:28:39 -0800593 // The target of the case.
Andreas Gampee4d4d322014-12-04 09:09:57 -0800594 uint32_t target = dex_pc + table.GetEntryAt(i + offset);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000595 FindOrCreateBlockStartingAt(target);
Andreas Gamped881df52014-11-24 23:28:39 -0800596
David Brazdil281a6322015-07-03 10:34:57 +0100597 // Create a block for the switch-case logic. The block gets the dex_pc
598 // of the SWITCH instruction because it is part of its semantics.
599 block = new (arena_) HBasicBlock(graph_, dex_pc);
600 branch_targets_.Put(table.GetDexPcForIndex(i), block);
Andreas Gamped881df52014-11-24 23:28:39 -0800601 }
602
603 // Fall-through. Add a block if there is more code afterwards.
604 dex_pc += instruction.SizeInCodeUnits();
605 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle702d2602015-04-30 19:28:21 +0100606 if (code_ptr >= code_end) {
607 // In the normal case we should never hit this but someone can artificially forge a dex
608 // file to fall-through out the method code. In this case we bail out compilation.
609 // (A switch can fall-through so we don't need to check CanFlowThrough().)
610 return false;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000611 } else {
612 FindOrCreateBlockStartingAt(dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -0800613 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000614 } else {
615 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle225ff812014-11-13 16:46:39 +0000616 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000617 }
618 }
Calin Juravle702d2602015-04-30 19:28:21 +0100619 return true;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000620}
621
David Brazdilfc6a86a2015-06-26 10:33:45 +0000622HBasicBlock* HGraphBuilder::FindBlockStartingAt(int32_t dex_pc) const {
623 DCHECK_GE(dex_pc, 0);
624 DCHECK_LT(static_cast<size_t>(dex_pc), branch_targets_.Size());
625 return branch_targets_.Get(dex_pc);
626}
627
628HBasicBlock* HGraphBuilder::FindOrCreateBlockStartingAt(int32_t dex_pc) {
629 HBasicBlock* block = FindBlockStartingAt(dex_pc);
630 if (block == nullptr) {
631 block = new (arena_) HBasicBlock(graph_, dex_pc);
632 branch_targets_.Put(dex_pc, block);
633 }
634 return block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000635}
636
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100637template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600638void HGraphBuilder::Unop_12x(const Instruction& instruction,
639 Primitive::Type type,
640 uint32_t dex_pc) {
641 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
642 current_block_->AddInstruction(new (arena_) T(type, first, dex_pc));
643 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Roland Levillain88cb1752014-10-20 16:36:47 +0100644}
645
Roland Levillaindff1f282014-11-05 14:15:05 +0000646void HGraphBuilder::Conversion_12x(const Instruction& instruction,
647 Primitive::Type input_type,
Roland Levillain624279f2014-12-04 11:54:28 +0000648 Primitive::Type result_type,
649 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600650 HInstruction* first = LoadLocal(instruction.VRegB(), input_type, dex_pc);
Roland Levillain624279f2014-12-04 11:54:28 +0000651 current_block_->AddInstruction(new (arena_) HTypeConversion(result_type, first, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600652 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100653}
654
655template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000656void HGraphBuilder::Binop_23x(const Instruction& instruction,
657 Primitive::Type type,
658 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600659 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
660 HInstruction* second = LoadLocal(instruction.VRegC(), type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000661 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600662 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000663}
664
665template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000666void HGraphBuilder::Binop_23x_shift(const Instruction& instruction,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600667 Primitive::Type type,
668 uint32_t dex_pc) {
669 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
670 HInstruction* second = LoadLocal(instruction.VRegC(), Primitive::kPrimInt, dex_pc);
671 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
672 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000673}
674
Calin Juravleddb7df22014-11-25 20:56:51 +0000675void HGraphBuilder::Binop_23x_cmp(const Instruction& instruction,
676 Primitive::Type type,
Mark Mendellc4701932015-04-10 13:18:51 -0400677 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700678 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600679 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
680 HInstruction* second = LoadLocal(instruction.VRegC(), type, dex_pc);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700681 current_block_->AddInstruction(new (arena_) HCompare(type, first, second, bias, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600682 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +0000683}
684
Calin Juravle9aec02f2014-11-18 23:06:35 +0000685template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600686void HGraphBuilder::Binop_12x_shift(const Instruction& instruction, Primitive::Type type,
687 uint32_t dex_pc) {
688 HInstruction* first = LoadLocal(instruction.VRegA(), type, dex_pc);
689 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
690 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
691 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000692}
693
694template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000695void HGraphBuilder::Binop_12x(const Instruction& instruction,
696 Primitive::Type type,
697 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600698 HInstruction* first = LoadLocal(instruction.VRegA(), type, dex_pc);
699 HInstruction* second = LoadLocal(instruction.VRegB(), type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000700 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600701 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000702}
703
704template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600705void HGraphBuilder::Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
706 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
707 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100708 if (reverse) {
709 std::swap(first, second);
710 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600711 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second, dex_pc));
712 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100713}
714
715template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600716void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
717 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
718 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100719 if (reverse) {
720 std::swap(first, second);
721 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600722 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second, dex_pc));
723 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100724}
725
Calin Juravle0c25d102015-04-20 14:49:09 +0100726static bool RequiresConstructorBarrier(const DexCompilationUnit* cu, const CompilerDriver& driver) {
Calin Juravle27df7582015-04-17 19:12:31 +0100727 Thread* self = Thread::Current();
Calin Juravle0c25d102015-04-20 14:49:09 +0100728 return cu->IsConstructor()
729 && driver.RequiresConstructorBarrier(self, cu->GetDexFile(), cu->GetClassDefIndex());
Calin Juravle27df7582015-04-17 19:12:31 +0100730}
731
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600732void HGraphBuilder::BuildReturn(const Instruction& instruction,
733 Primitive::Type type,
734 uint32_t dex_pc) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100735 if (type == Primitive::kPrimVoid) {
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100736 if (graph_->ShouldGenerateConstructorBarrier()) {
737 // The compilation unit is null during testing.
738 if (dex_compilation_unit_ != nullptr) {
739 DCHECK(RequiresConstructorBarrier(dex_compilation_unit_, *compiler_driver_))
740 << "Inconsistent use of ShouldGenerateConstructorBarrier. Should not generate a barrier.";
741 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600742 current_block_->AddInstruction(new (arena_) HMemoryBarrier(kStoreStore, dex_pc));
Calin Juravle27df7582015-04-17 19:12:31 +0100743 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600744 current_block_->AddInstruction(new (arena_) HReturnVoid(dex_pc));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100745 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600746 HInstruction* value = LoadLocal(instruction.VRegA(), type, dex_pc);
747 current_block_->AddInstruction(new (arena_) HReturn(value, dex_pc));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100748 }
749 current_block_->AddSuccessor(exit_block_);
750 current_block_ = nullptr;
751}
752
Calin Juravle68ad6492015-08-18 17:08:12 +0100753static InvokeType GetInvokeTypeFromOpCode(Instruction::Code opcode) {
754 switch (opcode) {
755 case Instruction::INVOKE_STATIC:
756 case Instruction::INVOKE_STATIC_RANGE:
757 return kStatic;
758 case Instruction::INVOKE_DIRECT:
759 case Instruction::INVOKE_DIRECT_RANGE:
760 return kDirect;
761 case Instruction::INVOKE_VIRTUAL:
762 case Instruction::INVOKE_VIRTUAL_QUICK:
763 case Instruction::INVOKE_VIRTUAL_RANGE:
764 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
765 return kVirtual;
766 case Instruction::INVOKE_INTERFACE:
767 case Instruction::INVOKE_INTERFACE_RANGE:
768 return kInterface;
769 case Instruction::INVOKE_SUPER_RANGE:
770 case Instruction::INVOKE_SUPER:
771 return kSuper;
772 default:
773 LOG(FATAL) << "Unexpected invoke opcode: " << opcode;
774 UNREACHABLE();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100775 }
Calin Juravle68ad6492015-08-18 17:08:12 +0100776}
777
778bool HGraphBuilder::BuildInvoke(const Instruction& instruction,
779 uint32_t dex_pc,
780 uint32_t method_idx,
781 uint32_t number_of_vreg_arguments,
782 bool is_range,
783 uint32_t* args,
784 uint32_t register_index) {
785 InvokeType original_invoke_type = GetInvokeTypeFromOpCode(instruction.Opcode());
786 InvokeType optimized_invoke_type = original_invoke_type;
787 const char* descriptor = dex_file_->GetMethodShorty(method_idx);
788 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
789
790 // Remove the return type from the 'proto'.
791 size_t number_of_arguments = strlen(descriptor) - 1;
792 if (original_invoke_type != kStatic) { // instance call
793 // One extra argument for 'this'.
794 number_of_arguments++;
795 }
796
797 MethodReference target_method(dex_file_, method_idx);
798 int32_t table_index;
799 uintptr_t direct_code;
800 uintptr_t direct_method;
801
802 if (!compiler_driver_->ComputeInvokeInfo(dex_compilation_unit_,
803 dex_pc,
804 true /* update_stats */,
805 true /* enable_devirtualization */,
806 &optimized_invoke_type,
807 &target_method,
808 &table_index,
809 &direct_code,
810 &direct_method)) {
811 VLOG(compiler) << "Did not compile "
812 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
813 << " because a method call could not be resolved";
814 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedMethod);
815 return false;
816 }
817
818 DCHECK(optimized_invoke_type != kSuper);
819
820 // Special handling for string init.
821 int32_t string_init_offset = 0;
822 bool is_string_init = compiler_driver_->IsStringInit(method_idx, dex_file_,
823 &string_init_offset);
824
825 // Potential class initialization check, in the case of a static method call.
826 HClinitCheck* clinit_check = nullptr;
827 HInvoke* invoke = nullptr;
828
829 if (is_string_init
830 || optimized_invoke_type == kDirect
831 || optimized_invoke_type == kStatic) {
832 // By default, consider that the called method implicitly requires
833 // an initialization check of its declaring method.
834 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement
835 = HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
836 if (optimized_invoke_type == kStatic && !is_string_init) {
837 clinit_check = ProcessClinitCheckForInvoke(dex_pc, method_idx, &clinit_check_requirement);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100838 }
Calin Juravle68ad6492015-08-18 17:08:12 +0100839
840 // Replace calls to String.<init> with StringFactory.
841 if (is_string_init) {
842 return_type = Primitive::kPrimNot;
843 number_of_arguments--;
844 optimized_invoke_type = kStatic;
845 }
846
847 HInvokeStaticOrDirect::DispatchInfo dispatch_info = ComputeDispatchInfo(is_string_init,
848 string_init_offset,
849 target_method,
850 direct_method,
851 direct_code);
852 invoke = new (arena_) HInvokeStaticOrDirect(arena_,
853 number_of_arguments,
854 return_type,
855 dex_pc,
856 method_idx,
857 target_method,
858 dispatch_info,
859 original_invoke_type,
860 optimized_invoke_type,
861 clinit_check_requirement);
862 } else if (optimized_invoke_type == kVirtual) {
863 invoke = new (arena_) HInvokeVirtual(arena_,
864 number_of_arguments,
865 return_type,
866 dex_pc,
867 method_idx,
868 table_index);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100869 } else {
Calin Juravle68ad6492015-08-18 17:08:12 +0100870 DCHECK_EQ(optimized_invoke_type, kInterface);
871 invoke = new (arena_) HInvokeInterface(arena_,
872 number_of_arguments,
873 return_type,
874 dex_pc,
875 method_idx,
876 table_index);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100877 }
Calin Juravle68ad6492015-08-18 17:08:12 +0100878
Calin Juravle0eedd7e2015-08-20 14:48:00 +0100879 return SetupArgumentsAndAddInvoke(invoke,
880 number_of_vreg_arguments,
881 args,
882 register_index,
883 is_range,
884 descriptor,
Calin Juravle599262c2015-08-20 15:01:27 +0100885 clinit_check);
Calin Juravle68ad6492015-08-18 17:08:12 +0100886}
887
888HClinitCheck* HGraphBuilder::ProcessClinitCheckForInvoke(
889 uint32_t dex_pc,
890 uint32_t method_idx,
891 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
892 ScopedObjectAccess soa(Thread::Current());
893 StackHandleScope<4> hs(soa.Self());
894 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
895 dex_compilation_unit_->GetClassLinker()->FindDexCache(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700896 soa.Self(), *dex_compilation_unit_->GetDexFile())));
Calin Juravle68ad6492015-08-18 17:08:12 +0100897 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
898 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
899 ArtMethod* resolved_method = compiler_driver_->ResolveMethod(
900 soa, dex_cache, class_loader, dex_compilation_unit_, method_idx, InvokeType::kStatic);
901
902 DCHECK(resolved_method != nullptr);
903
904 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
905 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700906 outer_compilation_unit_->GetClassLinker()->FindDexCache(soa.Self(), outer_dex_file)));
Calin Juravle68ad6492015-08-18 17:08:12 +0100907 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
908
909 // The index at which the method's class is stored in the DexCache's type array.
910 uint32_t storage_index = DexFile::kDexNoIndex;
911 bool is_outer_class = (resolved_method->GetDeclaringClass() == outer_class.Get());
912 if (is_outer_class) {
913 storage_index = outer_class->GetDexTypeIndex();
914 } else if (outer_dex_cache.Get() == dex_cache.Get()) {
915 // Get `storage_index` from IsClassOfStaticMethodAvailableToReferrer.
916 compiler_driver_->IsClassOfStaticMethodAvailableToReferrer(outer_dex_cache.Get(),
917 GetCompilingClass(),
918 resolved_method,
919 method_idx,
920 &storage_index);
921 }
922
923 HClinitCheck* clinit_check = nullptr;
924
925 if (!outer_class->IsInterface()
926 && outer_class->IsSubClass(resolved_method->GetDeclaringClass())) {
927 // If the outer class is the declaring class or a subclass
928 // of the declaring class, no class initialization is needed
929 // before the static method call.
930 // Note that in case of inlining, we do not need to add clinit checks
931 // to calls that satisfy this subclass check with any inlined methods. This
932 // will be detected by the optimization passes.
933 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
934 } else if (storage_index != DexFile::kDexNoIndex) {
935 // If the method's class type index is available, check
936 // whether we should add an explicit class initialization
937 // check for its declaring class before the static method call.
938
939 // TODO: find out why this check is needed.
940 bool is_in_dex_cache = compiler_driver_->CanAssumeTypeIsPresentInDexCache(
941 *outer_compilation_unit_->GetDexFile(), storage_index);
942 bool is_initialized =
943 resolved_method->GetDeclaringClass()->IsInitialized() && is_in_dex_cache;
944
945 if (is_initialized) {
946 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
947 } else {
948 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
949 HLoadClass* load_class = new (arena_) HLoadClass(
950 graph_->GetCurrentMethod(),
951 storage_index,
952 *dex_compilation_unit_->GetDexFile(),
953 is_outer_class,
954 dex_pc);
955 current_block_->AddInstruction(load_class);
956 clinit_check = new (arena_) HClinitCheck(load_class, dex_pc);
957 current_block_->AddInstruction(clinit_check);
958 }
959 }
960 return clinit_check;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100961}
962
Vladimir Marko58155012015-08-19 12:49:41 +0000963HInvokeStaticOrDirect::DispatchInfo HGraphBuilder::ComputeDispatchInfo(
964 bool is_string_init,
965 int32_t string_init_offset,
966 MethodReference target_method,
967 uintptr_t direct_method,
968 uintptr_t direct_code) {
969 HInvokeStaticOrDirect::MethodLoadKind method_load_kind;
970 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location;
971 uint64_t method_load_data = 0u;
972 uint64_t direct_code_ptr = 0u;
973
974 if (is_string_init) {
975 // TODO: Use direct_method and direct_code for the appropriate StringFactory method.
976 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kStringInit;
977 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
978 method_load_data = string_init_offset;
979 } else if (target_method.dex_file == outer_compilation_unit_->GetDexFile() &&
980 target_method.dex_method_index == outer_compilation_unit_->GetDexMethodIndex()) {
981 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kRecursive;
982 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallSelf;
983 } else {
984 if (direct_method != 0u) { // Should we use a direct pointer to the method?
985 if (direct_method != static_cast<uintptr_t>(-1)) { // Is the method pointer known now?
986 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress;
987 method_load_data = direct_method;
988 } else { // The direct pointer will be known at link time.
989 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup;
990 }
991 } else { // Use dex cache.
992 DCHECK(target_method.dex_file == dex_compilation_unit_->GetDexFile());
993 DexCacheArraysLayout layout =
994 compiler_driver_->GetDexCacheArraysLayout(target_method.dex_file);
995 if (layout.Valid()) { // Can we use PC-relative access to the dex cache arrays?
996 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative;
997 method_load_data = layout.MethodOffset(target_method.dex_method_index);
998 } else { // We must go through the ArtMethod's pointer to resolved methods.
999 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
1000 }
1001 }
1002 if (direct_code != 0u) { // Should we use a direct pointer to the code?
1003 if (direct_code != static_cast<uintptr_t>(-1)) { // Is the code pointer known now?
1004 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallDirect;
1005 direct_code_ptr = direct_code;
1006 } else if (compiler_driver_->IsImage() ||
1007 target_method.dex_file == dex_compilation_unit_->GetDexFile()) {
1008 // Use PC-relative calls for invokes within a multi-dex oat file.
1009 // TODO: Recognize when the target dex file is within the current oat file for
1010 // app compilation. At the moment we recognize only the boot image as multi-dex.
1011 // NOTE: This will require changing the ARM backend which currently falls
1012 // through from kCallPCRelative to kDirectCodeFixup for different dex files.
1013 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative;
1014 } else { // The direct pointer will be known at link time.
1015 // NOTE: This is used for app->boot calls when compiling an app against
1016 // a relocatable but not yet relocated image.
1017 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup;
1018 }
1019 } else { // We must use the code pointer from the ArtMethod.
1020 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
1021 }
1022 }
1023
1024 if (graph_->IsDebuggable()) {
1025 // For debuggable apps always use the code pointer from ArtMethod
1026 // so that we don't circumvent instrumentation stubs if installed.
1027 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
1028 }
1029
1030 return HInvokeStaticOrDirect::DispatchInfo {
1031 method_load_kind, code_ptr_location, method_load_data, direct_code_ptr };
1032}
1033
Calin Juravle0eedd7e2015-08-20 14:48:00 +01001034bool HGraphBuilder::SetupArgumentsAndAddInvoke(HInvoke* invoke,
1035 uint32_t number_of_vreg_arguments,
1036 uint32_t* args,
1037 uint32_t register_index,
1038 bool is_range,
1039 const char* descriptor,
1040 HClinitCheck* clinit_check) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001041 size_t start_index = 0;
Calin Juravle68ad6492015-08-18 17:08:12 +01001042 size_t argument_index = 0;
1043 uint32_t descriptor_index = 1; // Skip the return type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001044 uint32_t dex_pc = invoke->GetDexPc();
Calin Juravle68ad6492015-08-18 17:08:12 +01001045
1046 bool is_instance_call = invoke->GetOriginalInvokeType() != InvokeType::kStatic;
1047 bool is_string_init = invoke->IsInvokeStaticOrDirect()
1048 && invoke->AsInvokeStaticOrDirect()->IsStringInit();
1049
1050 if (is_string_init) {
1051 start_index = 1;
1052 argument_index = 0;
1053 } else if (is_instance_call) {
1054 Temporaries temps(graph_);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001055 HInstruction* arg = LoadLocal(is_range ? register_index : args[0], Primitive::kPrimNot, dex_pc);
Calin Juravle68ad6492015-08-18 17:08:12 +01001056 HNullCheck* null_check = new (arena_) HNullCheck(arg, invoke->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001057 current_block_->AddInstruction(null_check);
1058 temps.Add(null_check);
1059 invoke->SetArgumentAt(0, null_check);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001060 start_index = 1;
Calin Juravle68ad6492015-08-18 17:08:12 +01001061 argument_index = 1;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001062 }
1063
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001064 for (size_t i = start_index;
1065 // Make sure we don't go over the expected arguments or over the number of
1066 // dex registers given. If the instruction was seen as dead by the verifier,
1067 // it hasn't been properly checked.
Calin Juravle68ad6492015-08-18 17:08:12 +01001068 (i < number_of_vreg_arguments) && (argument_index < invoke->GetNumberOfArguments());
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001069 i++, argument_index++) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001070 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001071 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001072 if (!is_range
1073 && is_wide
1074 && ((i + 1 == number_of_vreg_arguments) || (args[i] + 1 != args[i + 1]))) {
1075 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
1076 // reject any class where this is violated. However, the verifier only does these checks
1077 // on non trivially dead instructions, so we just bailout the compilation.
1078 VLOG(compiler) << "Did not compile "
1079 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
1080 << " because of non-sequential dex register pair in wide argument";
1081 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1082 return false;
1083 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001084 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type, dex_pc);
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001085 invoke->SetArgumentAt(argument_index, arg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001086 if (is_wide) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001087 i++;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001088 }
1089 }
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001090
Calin Juravle68ad6492015-08-18 17:08:12 +01001091 if (argument_index != invoke->GetNumberOfArguments()) {
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001092 VLOG(compiler) << "Did not compile "
1093 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
1094 << " because of wrong number of arguments in invoke instruction";
1095 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1096 return false;
1097 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001098
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001099 if (invoke->IsInvokeStaticOrDirect()) {
1100 invoke->SetArgumentAt(argument_index, graph_->GetCurrentMethod());
1101 argument_index++;
1102 }
1103
Calin Juravle68ad6492015-08-18 17:08:12 +01001104 if (clinit_check != nullptr) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001105 // Add the class initialization check as last input of `invoke`.
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001106 DCHECK(!is_string_init);
Calin Juravle68ad6492015-08-18 17:08:12 +01001107 DCHECK(invoke->IsInvokeStaticOrDirect());
1108 DCHECK(invoke->AsInvokeStaticOrDirect()->GetClinitCheckRequirement()
1109 == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit);
Roland Levillain3e3d7332015-04-28 11:00:54 +01001110 invoke->SetArgumentAt(argument_index, clinit_check);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001111 argument_index++;
Roland Levillain4c0eb422015-04-24 16:43:49 +01001112 }
1113
Jeff Hao848f70a2014-01-15 13:49:50 -08001114 // Add move-result for StringFactory method.
1115 if (is_string_init) {
1116 uint32_t orig_this_reg = is_range ? register_index : args[0];
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001117 HInstruction* fake_string = LoadLocal(orig_this_reg, Primitive::kPrimNot, dex_pc);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001118 invoke->SetArgumentAt(argument_index, fake_string);
Calin Juravle0eedd7e2015-08-20 14:48:00 +01001119 current_block_->AddInstruction(invoke);
Calin Juravle68ad6492015-08-18 17:08:12 +01001120 PotentiallySimplifyFakeString(orig_this_reg, invoke->GetDexPc(), invoke);
Calin Juravle0eedd7e2015-08-20 14:48:00 +01001121 } else {
1122 current_block_->AddInstruction(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001123 }
Calin Juravle0eedd7e2015-08-20 14:48:00 +01001124
1125 latest_result_ = invoke;
1126
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001127 return true;
1128}
1129
Calin Juravle68ad6492015-08-18 17:08:12 +01001130void HGraphBuilder::PotentiallySimplifyFakeString(uint16_t original_dex_register,
1131 uint32_t dex_pc,
1132 HInvoke* actual_string) {
1133 if (!graph_->IsDebuggable()) {
1134 // Notify that we cannot compile with baseline. The dex registers aliasing
1135 // with `original_dex_register` will be handled when we optimize
1136 // (see HInstructionSimplifer::VisitFakeString).
1137 can_use_baseline_for_string_init_ = false;
1138 return;
1139 }
1140 const VerifiedMethod* verified_method =
1141 compiler_driver_->GetVerifiedMethod(dex_file_, dex_compilation_unit_->GetDexMethodIndex());
1142 if (verified_method != nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001143 UpdateLocal(original_dex_register, actual_string, dex_pc);
Calin Juravle68ad6492015-08-18 17:08:12 +01001144 const SafeMap<uint32_t, std::set<uint32_t>>& string_init_map =
1145 verified_method->GetStringInitPcRegMap();
1146 auto map_it = string_init_map.find(dex_pc);
1147 if (map_it != string_init_map.end()) {
1148 std::set<uint32_t> reg_set = map_it->second;
1149 for (auto set_it = reg_set.begin(); set_it != reg_set.end(); ++set_it) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001150 HInstruction* load_local = LoadLocal(original_dex_register, Primitive::kPrimNot, dex_pc);
1151 UpdateLocal(*set_it, load_local, dex_pc);
Calin Juravle68ad6492015-08-18 17:08:12 +01001152 }
1153 }
1154 } else {
1155 can_use_baseline_for_string_init_ = false;
1156 }
1157}
1158
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001159bool HGraphBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001160 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001161 bool is_put) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001162 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1163 uint32_t obj_reg = instruction.VRegB_22c();
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001164 uint16_t field_index;
1165 if (instruction.IsQuickened()) {
1166 if (!CanDecodeQuickenedInfo()) {
1167 return false;
1168 }
1169 field_index = LookupQuickenedInfo(dex_pc);
1170 } else {
1171 field_index = instruction.VRegC_22c();
1172 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001173
1174 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierc7853442015-03-27 14:35:38 -07001175 ArtField* resolved_field =
1176 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001177
Mathieu Chartierc7853442015-03-27 14:35:38 -07001178 if (resolved_field == nullptr) {
Calin Juravle48c2b032014-12-09 18:11:36 +00001179 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedField);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001180 return false;
1181 }
Calin Juravle52c48962014-12-16 17:02:57 +00001182
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001183 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001184
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001185 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001186 current_block_->AddInstruction(new (arena_) HNullCheck(object, dex_pc));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001187 if (is_put) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001188 Temporaries temps(graph_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001189 HInstruction* null_check = current_block_->GetLastInstruction();
1190 // We need one temporary for the null check.
1191 temps.Add(null_check);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001192 HInstruction* value = LoadLocal(source_or_dest_reg, field_type, dex_pc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001193 current_block_->AddInstruction(new (arena_) HInstanceFieldSet(
1194 null_check,
1195 value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001196 field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00001197 resolved_field->GetOffset(),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001198 resolved_field->IsVolatile(),
1199 field_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07001200 *dex_file_,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001201 dex_compilation_unit_->GetDexCache(),
1202 dex_pc));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001203 } else {
1204 current_block_->AddInstruction(new (arena_) HInstanceFieldGet(
1205 current_block_->GetLastInstruction(),
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001206 field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00001207 resolved_field->GetOffset(),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001208 resolved_field->IsVolatile(),
1209 field_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07001210 *dex_file_,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001211 dex_compilation_unit_->GetDexCache(),
1212 dex_pc));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001213
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001214 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001215 }
1216 return true;
1217}
1218
Nicolas Geoffray30451742015-06-19 13:32:41 +01001219static mirror::Class* GetClassFrom(CompilerDriver* driver,
1220 const DexCompilationUnit& compilation_unit) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001221 ScopedObjectAccess soa(Thread::Current());
1222 StackHandleScope<2> hs(soa.Self());
Nicolas Geoffray30451742015-06-19 13:32:41 +01001223 const DexFile& dex_file = *compilation_unit.GetDexFile();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001224 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Nicolas Geoffray30451742015-06-19 13:32:41 +01001225 soa.Decode<mirror::ClassLoader*>(compilation_unit.GetClassLoader())));
1226 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001227 compilation_unit.GetClassLinker()->FindDexCache(soa.Self(), dex_file)));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001228
Nicolas Geoffray30451742015-06-19 13:32:41 +01001229 return driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, &compilation_unit);
1230}
1231
1232mirror::Class* HGraphBuilder::GetOutermostCompilingClass() const {
1233 return GetClassFrom(compiler_driver_, *outer_compilation_unit_);
1234}
1235
1236mirror::Class* HGraphBuilder::GetCompilingClass() const {
1237 return GetClassFrom(compiler_driver_, *dex_compilation_unit_);
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001238}
1239
1240bool HGraphBuilder::IsOutermostCompilingClass(uint16_t type_index) const {
1241 ScopedObjectAccess soa(Thread::Current());
1242 StackHandleScope<4> hs(soa.Self());
1243 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001244 dex_compilation_unit_->GetClassLinker()->FindDexCache(
1245 soa.Self(), *dex_compilation_unit_->GetDexFile())));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001246 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1247 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
1248 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
1249 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
Nicolas Geoffrayafd06412015-06-20 22:44:47 +01001250 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001251
Nicolas Geoffrayafd06412015-06-20 22:44:47 +01001252 return outer_class.Get() == cls.Get();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001253}
1254
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001255bool HGraphBuilder::BuildStaticFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001256 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001257 bool is_put) {
1258 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1259 uint16_t field_index = instruction.VRegB_21c();
1260
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001261 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierc7853442015-03-27 14:35:38 -07001262 StackHandleScope<4> hs(soa.Self());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001263 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001264 dex_compilation_unit_->GetClassLinker()->FindDexCache(
1265 soa.Self(), *dex_compilation_unit_->GetDexFile())));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001266 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1267 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
Mathieu Chartierc7853442015-03-27 14:35:38 -07001268 ArtField* resolved_field = compiler_driver_->ResolveField(
1269 soa, dex_cache, class_loader, dex_compilation_unit_, field_index, true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001270
Mathieu Chartierc7853442015-03-27 14:35:38 -07001271 if (resolved_field == nullptr) {
Calin Juravle48c2b032014-12-09 18:11:36 +00001272 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedField);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001273 return false;
1274 }
1275
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001276 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
1277 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001278 outer_compilation_unit_->GetClassLinker()->FindDexCache(soa.Self(), outer_dex_file)));
Nicolas Geoffray30451742015-06-19 13:32:41 +01001279 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001280
1281 // The index at which the field's class is stored in the DexCache's type array.
1282 uint32_t storage_index;
Nicolas Geoffray30451742015-06-19 13:32:41 +01001283 bool is_outer_class = (outer_class.Get() == resolved_field->GetDeclaringClass());
1284 if (is_outer_class) {
1285 storage_index = outer_class->GetDexTypeIndex();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001286 } else if (outer_dex_cache.Get() != dex_cache.Get()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001287 // The compiler driver cannot currently understand multiple dex caches involved. Just bailout.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001288 return false;
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001289 } else {
1290 std::pair<bool, bool> pair = compiler_driver_->IsFastStaticField(
1291 outer_dex_cache.Get(),
Nicolas Geoffray30451742015-06-19 13:32:41 +01001292 GetCompilingClass(),
Mathieu Chartierc7853442015-03-27 14:35:38 -07001293 resolved_field,
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001294 field_index,
1295 &storage_index);
1296 bool can_easily_access = is_put ? pair.second : pair.first;
1297 if (!can_easily_access) {
1298 return false;
1299 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001300 }
1301
1302 // TODO: find out why this check is needed.
1303 bool is_in_dex_cache = compiler_driver_->CanAssumeTypeIsPresentInDexCache(
Nicolas Geoffray6a816cf2015-03-24 16:17:56 +00001304 *outer_compilation_unit_->GetDexFile(), storage_index);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001305 bool is_initialized = resolved_field->GetDeclaringClass()->IsInitialized() && is_in_dex_cache;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001306
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001307 HLoadClass* constant = new (arena_) HLoadClass(graph_->GetCurrentMethod(),
1308 storage_index,
1309 *dex_compilation_unit_->GetDexFile(),
Nicolas Geoffray30451742015-06-19 13:32:41 +01001310 is_outer_class,
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001311 dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001312 current_block_->AddInstruction(constant);
1313
1314 HInstruction* cls = constant;
Nicolas Geoffray30451742015-06-19 13:32:41 +01001315 if (!is_initialized && !is_outer_class) {
Calin Juravle225ff812014-11-13 16:46:39 +00001316 cls = new (arena_) HClinitCheck(constant, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001317 current_block_->AddInstruction(cls);
1318 }
1319
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001320 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001321 if (is_put) {
1322 // We need to keep the class alive before loading the value.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001323 Temporaries temps(graph_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001324 temps.Add(cls);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001325 HInstruction* value = LoadLocal(source_or_dest_reg, field_type, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001326 DCHECK_EQ(value->GetType(), field_type);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001327 current_block_->AddInstruction(new (arena_) HStaticFieldSet(cls,
1328 value,
1329 field_type,
1330 resolved_field->GetOffset(),
1331 resolved_field->IsVolatile(),
1332 field_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07001333 *dex_file_,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001334 dex_cache_,
1335 dex_pc));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001336 } else {
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001337 current_block_->AddInstruction(new (arena_) HStaticFieldGet(cls,
1338 field_type,
1339 resolved_field->GetOffset(),
1340 resolved_field->IsVolatile(),
1341 field_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07001342 *dex_file_,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001343 dex_cache_,
1344 dex_pc));
1345 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001346 }
1347 return true;
1348}
1349
Calin Juravlebacfec32014-11-14 15:54:36 +00001350void HGraphBuilder::BuildCheckedDivRem(uint16_t out_vreg,
1351 uint16_t first_vreg,
1352 int64_t second_vreg_or_constant,
1353 uint32_t dex_pc,
1354 Primitive::Type type,
1355 bool second_is_constant,
1356 bool isDiv) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001357 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Calin Juravled0d48522014-11-04 16:40:20 +00001358
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001359 HInstruction* first = LoadLocal(first_vreg, type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001360 HInstruction* second = nullptr;
1361 if (second_is_constant) {
1362 if (type == Primitive::kPrimInt) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001363 second = graph_->GetIntConstant(second_vreg_or_constant, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001364 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001365 second = graph_->GetLongConstant(second_vreg_or_constant, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001366 }
1367 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001368 second = LoadLocal(second_vreg_or_constant, type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001369 }
1370
1371 if (!second_is_constant
1372 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0)
1373 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) {
1374 second = new (arena_) HDivZeroCheck(second, dex_pc);
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001375 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +00001376 current_block_->AddInstruction(second);
1377 temps.Add(current_block_->GetLastInstruction());
1378 }
1379
Calin Juravlebacfec32014-11-14 15:54:36 +00001380 if (isDiv) {
1381 current_block_->AddInstruction(new (arena_) HDiv(type, first, second, dex_pc));
1382 } else {
1383 current_block_->AddInstruction(new (arena_) HRem(type, first, second, dex_pc));
1384 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001385 UpdateLocal(out_vreg, current_block_->GetLastInstruction(), dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001386}
1387
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001388void HGraphBuilder::BuildArrayAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001389 uint32_t dex_pc,
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001390 bool is_put,
1391 Primitive::Type anticipated_type) {
1392 uint8_t source_or_dest_reg = instruction.VRegA_23x();
1393 uint8_t array_reg = instruction.VRegB_23x();
1394 uint8_t index_reg = instruction.VRegC_23x();
1395
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001396 // We need one temporary for the null check, one for the index, and one for the length.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001397 Temporaries temps(graph_);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001398
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001399 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001400 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001401 current_block_->AddInstruction(object);
1402 temps.Add(object);
1403
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001404 HInstruction* length = new (arena_) HArrayLength(object, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001405 current_block_->AddInstruction(length);
1406 temps.Add(length);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001407 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001408 index = new (arena_) HBoundsCheck(index, length, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001409 current_block_->AddInstruction(index);
1410 temps.Add(index);
1411 if (is_put) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001412 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001413 // TODO: Insert a type check node if the type is Object.
Nicolas Geoffray39468442014-09-02 15:17:15 +01001414 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001415 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001416 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001417 current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type, dex_pc));
1418 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001419 }
Mark Mendell1152c922015-04-24 17:06:35 -04001420 graph_->SetHasBoundsChecks(true);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001421}
1422
Calin Juravle225ff812014-11-13 16:46:39 +00001423void HGraphBuilder::BuildFilledNewArray(uint32_t dex_pc,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001424 uint32_t type_index,
1425 uint32_t number_of_vreg_arguments,
1426 bool is_range,
1427 uint32_t* args,
1428 uint32_t register_index) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001429 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments, dex_pc);
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001430 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
1431 ? kQuickAllocArrayWithAccessCheck
1432 : kQuickAllocArray;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01001433 HInstruction* object = new (arena_) HNewArray(length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01001434 graph_->GetCurrentMethod(),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01001435 dex_pc,
1436 type_index,
1437 *dex_compilation_unit_->GetDexFile(),
1438 entrypoint);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001439 current_block_->AddInstruction(object);
1440
1441 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1442 DCHECK_EQ(descriptor[0], '[') << descriptor;
1443 char primitive = descriptor[1];
1444 DCHECK(primitive == 'I'
1445 || primitive == 'L'
1446 || primitive == '[') << descriptor;
1447 bool is_reference_array = (primitive == 'L') || (primitive == '[');
1448 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
1449
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001450 Temporaries temps(graph_);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001451 temps.Add(object);
1452 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001453 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type, dex_pc);
1454 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001455 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001456 new (arena_) HArraySet(object, index, value, type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001457 }
1458 latest_result_ = object;
1459}
1460
1461template <typename T>
1462void HGraphBuilder::BuildFillArrayData(HInstruction* object,
1463 const T* data,
1464 uint32_t element_count,
1465 Primitive::Type anticipated_type,
Calin Juravle225ff812014-11-13 16:46:39 +00001466 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001467 for (uint32_t i = 0; i < element_count; ++i) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001468 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1469 HInstruction* value = graph_->GetIntConstant(data[i], dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001470 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001471 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001472 }
1473}
1474
Calin Juravle225ff812014-11-13 16:46:39 +00001475void HGraphBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001476 Temporaries temps(graph_);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001477 HInstruction* array = LoadLocal(instruction.VRegA_31t(), Primitive::kPrimNot, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001478 HNullCheck* null_check = new (arena_) HNullCheck(array, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001479 current_block_->AddInstruction(null_check);
1480 temps.Add(null_check);
1481
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001482 HInstruction* length = new (arena_) HArrayLength(null_check, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001483 current_block_->AddInstruction(length);
1484
Calin Juravle225ff812014-11-13 16:46:39 +00001485 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
Calin Juravled0d48522014-11-04 16:40:20 +00001486 const Instruction::ArrayDataPayload* payload =
1487 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_start_ + payload_offset);
1488 const uint8_t* data = payload->data;
1489 uint32_t element_count = payload->element_count;
1490
1491 // Implementation of this DEX instruction seems to be that the bounds check is
1492 // done before doing any stores.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001493 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001494 current_block_->AddInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
Calin Juravled0d48522014-11-04 16:40:20 +00001495
1496 switch (payload->element_width) {
1497 case 1:
1498 BuildFillArrayData(null_check,
1499 reinterpret_cast<const int8_t*>(data),
1500 element_count,
1501 Primitive::kPrimByte,
Calin Juravle225ff812014-11-13 16:46:39 +00001502 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001503 break;
1504 case 2:
1505 BuildFillArrayData(null_check,
1506 reinterpret_cast<const int16_t*>(data),
1507 element_count,
1508 Primitive::kPrimShort,
Calin Juravle225ff812014-11-13 16:46:39 +00001509 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001510 break;
1511 case 4:
1512 BuildFillArrayData(null_check,
1513 reinterpret_cast<const int32_t*>(data),
1514 element_count,
1515 Primitive::kPrimInt,
Calin Juravle225ff812014-11-13 16:46:39 +00001516 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001517 break;
1518 case 8:
1519 BuildFillWideArrayData(null_check,
1520 reinterpret_cast<const int64_t*>(data),
1521 element_count,
Calin Juravle225ff812014-11-13 16:46:39 +00001522 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001523 break;
1524 default:
1525 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1526 }
Mark Mendell1152c922015-04-24 17:06:35 -04001527 graph_->SetHasBoundsChecks(true);
Calin Juravled0d48522014-11-04 16:40:20 +00001528}
1529
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001530void HGraphBuilder::BuildFillWideArrayData(HInstruction* object,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +01001531 const int64_t* data,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001532 uint32_t element_count,
Calin Juravle225ff812014-11-13 16:46:39 +00001533 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001534 for (uint32_t i = 0; i < element_count; ++i) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001535 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1536 HInstruction* value = graph_->GetLongConstant(data[i], dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001537 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001538 object, index, value, Primitive::kPrimLong, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001539 }
1540}
1541
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001542bool HGraphBuilder::BuildTypeCheck(const Instruction& instruction,
1543 uint8_t destination,
1544 uint8_t reference,
1545 uint16_t type_index,
Calin Juravle225ff812014-11-13 16:46:39 +00001546 uint32_t dex_pc) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001547 bool type_known_final;
1548 bool type_known_abstract;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001549 // `CanAccessTypeWithoutChecks` will tell whether the method being
1550 // built is trying to access its own class, so that the generated
1551 // code can optimize for this case. However, the optimization does not
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001552 // work for inlining, so we use `IsOutermostCompilingClass` instead.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001553 bool dont_use_is_referrers_class;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001554 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
1555 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001556 &type_known_final, &type_known_abstract, &dont_use_is_referrers_class);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001557 if (!can_access) {
Calin Juravle48c2b032014-12-09 18:11:36 +00001558 MaybeRecordStat(MethodCompilationStat::kNotCompiledCantAccesType);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001559 return false;
1560 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001561 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot, dex_pc);
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001562 HLoadClass* cls = new (arena_) HLoadClass(
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001563 graph_->GetCurrentMethod(),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01001564 type_index,
1565 *dex_compilation_unit_->GetDexFile(),
1566 IsOutermostCompilingClass(type_index),
1567 dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001568 current_block_->AddInstruction(cls);
1569 // The class needs a temporary before being used by the type check.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001570 Temporaries temps(graph_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001571 temps.Add(cls);
1572 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
1573 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001574 new (arena_) HInstanceOf(object, cls, type_known_final, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001575 UpdateLocal(destination, current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001576 } else {
1577 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
1578 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001579 new (arena_) HCheckCast(object, cls, type_known_final, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001580 }
1581 return true;
1582}
1583
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001584bool HGraphBuilder::NeedsAccessCheck(uint32_t type_index) const {
1585 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
1586 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index);
1587}
1588
Calin Juravle48c2b032014-12-09 18:11:36 +00001589void HGraphBuilder::BuildPackedSwitch(const Instruction& instruction, uint32_t dex_pc) {
David Brazdil2ef645b2015-06-17 18:20:52 +01001590 // Verifier guarantees that the payload for PackedSwitch contains:
1591 // (a) number of entries (may be zero)
1592 // (b) first and lowest switch case value (entry 0, always present)
1593 // (c) list of target pcs (entries 1 <= i <= N)
Andreas Gamped881df52014-11-24 23:28:39 -08001594 SwitchTable table(instruction, dex_pc, false);
1595
1596 // Value to test against.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001597 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt, dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08001598
David Brazdil2ef645b2015-06-17 18:20:52 +01001599 // Retrieve number of entries.
Andreas Gampee4d4d322014-12-04 09:09:57 -08001600 uint16_t num_entries = table.GetNumEntries();
David Brazdil2ef645b2015-06-17 18:20:52 +01001601 if (num_entries == 0) {
1602 return;
1603 }
Andreas Gampee4d4d322014-12-04 09:09:57 -08001604
Andreas Gamped881df52014-11-24 23:28:39 -08001605 // Chained cmp-and-branch, starting from starting_key.
1606 int32_t starting_key = table.GetEntryAt(0);
1607
Andreas Gamped881df52014-11-24 23:28:39 -08001608 for (size_t i = 1; i <= num_entries; i++) {
Andreas Gampee4d4d322014-12-04 09:09:57 -08001609 BuildSwitchCaseHelper(instruction, i, i == num_entries, table, value, starting_key + i - 1,
1610 table.GetEntryAt(i), dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08001611 }
Andreas Gamped881df52014-11-24 23:28:39 -08001612}
1613
Calin Juravle48c2b032014-12-09 18:11:36 +00001614void HGraphBuilder::BuildSparseSwitch(const Instruction& instruction, uint32_t dex_pc) {
David Brazdil2ef645b2015-06-17 18:20:52 +01001615 // Verifier guarantees that the payload for SparseSwitch contains:
1616 // (a) number of entries (may be zero)
1617 // (b) sorted key values (entries 0 <= i < N)
1618 // (c) target pcs corresponding to the switch values (entries N <= i < 2*N)
Andreas Gampee4d4d322014-12-04 09:09:57 -08001619 SwitchTable table(instruction, dex_pc, true);
1620
1621 // Value to test against.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001622 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001623
1624 uint16_t num_entries = table.GetNumEntries();
Andreas Gampee4d4d322014-12-04 09:09:57 -08001625
1626 for (size_t i = 0; i < num_entries; i++) {
1627 BuildSwitchCaseHelper(instruction, i, i == static_cast<size_t>(num_entries) - 1, table, value,
1628 table.GetEntryAt(i), table.GetEntryAt(i + num_entries), dex_pc);
1629 }
Andreas Gampee4d4d322014-12-04 09:09:57 -08001630}
1631
1632void HGraphBuilder::BuildSwitchCaseHelper(const Instruction& instruction, size_t index,
1633 bool is_last_case, const SwitchTable& table,
1634 HInstruction* value, int32_t case_value_int,
1635 int32_t target_offset, uint32_t dex_pc) {
David Brazdil852eaff2015-02-02 15:23:05 +00001636 HBasicBlock* case_target = FindBlockStartingAt(dex_pc + target_offset);
1637 DCHECK(case_target != nullptr);
1638 PotentiallyAddSuspendCheck(case_target, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001639
1640 // The current case's value.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001641 HInstruction* this_case_value = graph_->GetIntConstant(case_value_int, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001642
1643 // Compare value and this_case_value.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001644 HEqual* comparison = new (arena_) HEqual(value, this_case_value, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001645 current_block_->AddInstruction(comparison);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001646 HInstruction* ifinst = new (arena_) HIf(comparison, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001647 current_block_->AddInstruction(ifinst);
1648
1649 // Case hit: use the target offset to determine where to go.
Andreas Gampee4d4d322014-12-04 09:09:57 -08001650 current_block_->AddSuccessor(case_target);
1651
1652 // Case miss: go to the next case (or default fall-through).
1653 // When there is a next case, we use the block stored with the table offset representing this
1654 // case (that is where we registered them in ComputeBranchTargets).
1655 // When there is no next case, we use the following instruction.
1656 // TODO: Find a good way to peel the last iteration to avoid conditional, but still have re-use.
1657 if (!is_last_case) {
1658 HBasicBlock* next_case_target = FindBlockStartingAt(table.GetDexPcForIndex(index));
1659 DCHECK(next_case_target != nullptr);
1660 current_block_->AddSuccessor(next_case_target);
1661
1662 // Need to manually add the block, as there is no dex-pc transition for the cases.
1663 graph_->AddBlock(next_case_target);
1664
1665 current_block_ = next_case_target;
1666 } else {
1667 HBasicBlock* default_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
1668 DCHECK(default_target != nullptr);
1669 current_block_->AddSuccessor(default_target);
1670 current_block_ = nullptr;
1671 }
1672}
1673
David Brazdil852eaff2015-02-02 15:23:05 +00001674void HGraphBuilder::PotentiallyAddSuspendCheck(HBasicBlock* target, uint32_t dex_pc) {
1675 int32_t target_offset = target->GetDexPc() - dex_pc;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001676 if (target_offset <= 0) {
David Brazdil852eaff2015-02-02 15:23:05 +00001677 // DX generates back edges to the first encountered return. We can save
1678 // time of later passes by not adding redundant suspend checks.
David Brazdil2fd6aa52015-02-02 18:58:27 +00001679 HInstruction* last_in_target = target->GetLastInstruction();
1680 if (last_in_target != nullptr &&
1681 (last_in_target->IsReturn() || last_in_target->IsReturnVoid())) {
1682 return;
David Brazdil852eaff2015-02-02 15:23:05 +00001683 }
1684
1685 // Add a suspend check to backward branches which may potentially loop. We
1686 // can remove them after we recognize loops in the graph.
Calin Juravle225ff812014-11-13 16:46:39 +00001687 current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_pc));
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001688 }
1689}
1690
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001691bool HGraphBuilder::CanDecodeQuickenedInfo() const {
1692 return interpreter_metadata_ != nullptr;
1693}
1694
1695uint16_t HGraphBuilder::LookupQuickenedInfo(uint32_t dex_pc) {
1696 DCHECK(interpreter_metadata_ != nullptr);
1697 uint32_t dex_pc_in_map = DecodeUnsignedLeb128(&interpreter_metadata_);
1698 DCHECK_EQ(dex_pc, dex_pc_in_map);
1699 return DecodeUnsignedLeb128(&interpreter_metadata_);
1700}
1701
Calin Juravle225ff812014-11-13 16:46:39 +00001702bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001703 if (current_block_ == nullptr) {
1704 return true; // Dead code
1705 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001706
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001707 switch (instruction.Opcode()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001708 case Instruction::CONST_4: {
1709 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001710 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n(), dex_pc);
1711 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001712 break;
1713 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001714
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001715 case Instruction::CONST_16: {
1716 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001717 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s(), dex_pc);
1718 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001719 break;
1720 }
1721
Dave Allison20dfc792014-06-16 20:44:29 -07001722 case Instruction::CONST: {
1723 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001724 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i(), dex_pc);
1725 UpdateLocal(register_index, constant, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001726 break;
1727 }
1728
1729 case Instruction::CONST_HIGH16: {
1730 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001731 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16, dex_pc);
1732 UpdateLocal(register_index, constant, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001733 break;
1734 }
1735
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001736 case Instruction::CONST_WIDE_16: {
1737 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001738 // Get 16 bits of constant value, sign extended to 64 bits.
1739 int64_t value = instruction.VRegB_21s();
1740 value <<= 48;
1741 value >>= 48;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001742 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1743 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001744 break;
1745 }
1746
1747 case Instruction::CONST_WIDE_32: {
1748 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001749 // Get 32 bits of constant value, sign extended to 64 bits.
1750 int64_t value = instruction.VRegB_31i();
1751 value <<= 32;
1752 value >>= 32;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001753 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1754 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001755 break;
1756 }
1757
1758 case Instruction::CONST_WIDE: {
1759 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001760 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l(), dex_pc);
1761 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001762 break;
1763 }
1764
Dave Allison20dfc792014-06-16 20:44:29 -07001765 case Instruction::CONST_WIDE_HIGH16: {
1766 int32_t register_index = instruction.VRegA();
1767 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001768 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1769 UpdateLocal(register_index, constant, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001770 break;
1771 }
1772
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001773 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001774 case Instruction::MOVE:
1775 case Instruction::MOVE_FROM16:
1776 case Instruction::MOVE_16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001777 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
1778 UpdateLocal(instruction.VRegA(), value, dex_pc);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001779 break;
1780 }
1781
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001782 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001783 case Instruction::MOVE_WIDE:
1784 case Instruction::MOVE_WIDE_FROM16:
1785 case Instruction::MOVE_WIDE_16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001786 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong, dex_pc);
1787 UpdateLocal(instruction.VRegA(), value, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001788 break;
1789 }
1790
1791 case Instruction::MOVE_OBJECT:
1792 case Instruction::MOVE_OBJECT_16:
1793 case Instruction::MOVE_OBJECT_FROM16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001794 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot, dex_pc);
1795 UpdateLocal(instruction.VRegA(), value, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001796 break;
1797 }
1798
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001799 case Instruction::RETURN_VOID_NO_BARRIER:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001800 case Instruction::RETURN_VOID: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001801 BuildReturn(instruction, Primitive::kPrimVoid, dex_pc);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001802 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001803 }
1804
Dave Allison20dfc792014-06-16 20:44:29 -07001805#define IF_XX(comparison, cond) \
Calin Juravle225ff812014-11-13 16:46:39 +00001806 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
1807 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001808
Dave Allison20dfc792014-06-16 20:44:29 -07001809 IF_XX(HEqual, EQ);
1810 IF_XX(HNotEqual, NE);
1811 IF_XX(HLessThan, LT);
1812 IF_XX(HLessThanOrEqual, LE);
1813 IF_XX(HGreaterThan, GT);
1814 IF_XX(HGreaterThanOrEqual, GE);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001815
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001816 case Instruction::GOTO:
1817 case Instruction::GOTO_16:
1818 case Instruction::GOTO_32: {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001819 int32_t offset = instruction.GetTargetOffset();
Calin Juravle225ff812014-11-13 16:46:39 +00001820 HBasicBlock* target = FindBlockStartingAt(offset + dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001821 DCHECK(target != nullptr);
David Brazdil852eaff2015-02-02 15:23:05 +00001822 PotentiallyAddSuspendCheck(target, dex_pc);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001823 current_block_->AddInstruction(new (arena_) HGoto(dex_pc));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001824 current_block_->AddSuccessor(target);
1825 current_block_ = nullptr;
1826 break;
1827 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001828
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001829 case Instruction::RETURN: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001830 BuildReturn(instruction, return_type_, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001831 break;
1832 }
1833
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001834 case Instruction::RETURN_OBJECT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001835 BuildReturn(instruction, return_type_, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001836 break;
1837 }
1838
1839 case Instruction::RETURN_WIDE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001840 BuildReturn(instruction, return_type_, dex_pc);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001841 break;
1842 }
1843
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001844 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001845 case Instruction::INVOKE_INTERFACE:
1846 case Instruction::INVOKE_STATIC:
1847 case Instruction::INVOKE_SUPER:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001848 case Instruction::INVOKE_VIRTUAL:
1849 case Instruction::INVOKE_VIRTUAL_QUICK: {
1850 uint16_t method_idx;
1851 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_QUICK) {
1852 if (!CanDecodeQuickenedInfo()) {
1853 return false;
1854 }
1855 method_idx = LookupQuickenedInfo(dex_pc);
1856 } else {
1857 method_idx = instruction.VRegB_35c();
1858 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001859 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001860 uint32_t args[5];
Ian Rogers29a26482014-05-02 15:27:29 -07001861 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00001862 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001863 number_of_vreg_arguments, false, args, -1)) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001864 return false;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001865 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001866 break;
1867 }
1868
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001869 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001870 case Instruction::INVOKE_INTERFACE_RANGE:
1871 case Instruction::INVOKE_STATIC_RANGE:
1872 case Instruction::INVOKE_SUPER_RANGE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001873 case Instruction::INVOKE_VIRTUAL_RANGE:
1874 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
1875 uint16_t method_idx;
1876 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK) {
1877 if (!CanDecodeQuickenedInfo()) {
1878 return false;
1879 }
1880 method_idx = LookupQuickenedInfo(dex_pc);
1881 } else {
1882 method_idx = instruction.VRegB_3rc();
1883 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001884 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1885 uint32_t register_index = instruction.VRegC();
Calin Juravle225ff812014-11-13 16:46:39 +00001886 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001887 number_of_vreg_arguments, true, nullptr, register_index)) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001888 return false;
1889 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001890 break;
1891 }
1892
Roland Levillain88cb1752014-10-20 16:36:47 +01001893 case Instruction::NEG_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001894 Unop_12x<HNeg>(instruction, Primitive::kPrimInt, dex_pc);
Roland Levillain88cb1752014-10-20 16:36:47 +01001895 break;
1896 }
1897
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001898 case Instruction::NEG_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001899 Unop_12x<HNeg>(instruction, Primitive::kPrimLong, dex_pc);
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001900 break;
1901 }
1902
Roland Levillain3dbcb382014-10-28 17:30:07 +00001903 case Instruction::NEG_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001904 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat, dex_pc);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001905 break;
1906 }
1907
1908 case Instruction::NEG_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001909 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble, dex_pc);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001910 break;
1911 }
1912
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001913 case Instruction::NOT_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001914 Unop_12x<HNot>(instruction, Primitive::kPrimInt, dex_pc);
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001915 break;
1916 }
1917
Roland Levillain70566432014-10-24 16:20:17 +01001918 case Instruction::NOT_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001919 Unop_12x<HNot>(instruction, Primitive::kPrimLong, dex_pc);
Roland Levillain70566432014-10-24 16:20:17 +01001920 break;
1921 }
1922
Roland Levillaindff1f282014-11-05 14:15:05 +00001923 case Instruction::INT_TO_LONG: {
Roland Levillain624279f2014-12-04 11:54:28 +00001924 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong, dex_pc);
Roland Levillaindff1f282014-11-05 14:15:05 +00001925 break;
1926 }
1927
Roland Levillaincff13742014-11-17 14:32:17 +00001928 case Instruction::INT_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001929 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00001930 break;
1931 }
1932
1933 case Instruction::INT_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001934 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00001935 break;
1936 }
1937
Roland Levillain946e1432014-11-11 17:35:19 +00001938 case Instruction::LONG_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001939 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt, dex_pc);
Roland Levillain946e1432014-11-11 17:35:19 +00001940 break;
1941 }
1942
Roland Levillain6d0e4832014-11-27 18:31:21 +00001943 case Instruction::LONG_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001944 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimFloat, dex_pc);
Roland Levillain6d0e4832014-11-27 18:31:21 +00001945 break;
1946 }
1947
Roland Levillain647b9ed2014-11-27 12:06:00 +00001948 case Instruction::LONG_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001949 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble, dex_pc);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001950 break;
1951 }
1952
Roland Levillain3f8f9362014-12-02 17:45:01 +00001953 case Instruction::FLOAT_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001954 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimInt, dex_pc);
1955 break;
1956 }
1957
1958 case Instruction::FLOAT_TO_LONG: {
1959 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimLong, dex_pc);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001960 break;
1961 }
1962
Roland Levillain8964e2b2014-12-04 12:10:50 +00001963 case Instruction::FLOAT_TO_DOUBLE: {
1964 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimDouble, dex_pc);
1965 break;
1966 }
1967
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001968 case Instruction::DOUBLE_TO_INT: {
1969 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimInt, dex_pc);
1970 break;
1971 }
1972
1973 case Instruction::DOUBLE_TO_LONG: {
1974 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimLong, dex_pc);
1975 break;
1976 }
1977
Roland Levillain8964e2b2014-12-04 12:10:50 +00001978 case Instruction::DOUBLE_TO_FLOAT: {
1979 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimFloat, dex_pc);
1980 break;
1981 }
1982
Roland Levillain51d3fc42014-11-13 14:11:42 +00001983 case Instruction::INT_TO_BYTE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001984 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte, dex_pc);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001985 break;
1986 }
1987
Roland Levillain01a8d712014-11-14 16:27:39 +00001988 case Instruction::INT_TO_SHORT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001989 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort, dex_pc);
Roland Levillain01a8d712014-11-14 16:27:39 +00001990 break;
1991 }
1992
Roland Levillain981e4542014-11-14 11:47:14 +00001993 case Instruction::INT_TO_CHAR: {
Roland Levillain624279f2014-12-04 11:54:28 +00001994 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar, dex_pc);
Roland Levillain981e4542014-11-14 11:47:14 +00001995 break;
1996 }
1997
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001998 case Instruction::ADD_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001999 Binop_23x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002000 break;
2001 }
2002
2003 case Instruction::ADD_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002004 Binop_23x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002005 break;
2006 }
2007
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002008 case Instruction::ADD_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002009 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002010 break;
2011 }
2012
2013 case Instruction::ADD_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002014 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002015 break;
2016 }
2017
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002018 case Instruction::SUB_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002019 Binop_23x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002020 break;
2021 }
2022
2023 case Instruction::SUB_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002024 Binop_23x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002025 break;
2026 }
2027
Calin Juravle096cc022014-10-23 17:01:13 +01002028 case Instruction::SUB_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002029 Binop_23x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01002030 break;
2031 }
2032
2033 case Instruction::SUB_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002034 Binop_23x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01002035 break;
2036 }
2037
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002038 case Instruction::ADD_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002039 Binop_12x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002040 break;
2041 }
2042
Calin Juravle34bacdf2014-10-07 20:23:36 +01002043 case Instruction::MUL_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002044 Binop_23x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002045 break;
2046 }
2047
2048 case Instruction::MUL_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002049 Binop_23x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002050 break;
2051 }
2052
Calin Juravleb5bfa962014-10-21 18:02:24 +01002053 case Instruction::MUL_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002054 Binop_23x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01002055 break;
2056 }
2057
2058 case Instruction::MUL_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002059 Binop_23x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01002060 break;
2061 }
2062
Calin Juravled0d48522014-11-04 16:40:20 +00002063 case Instruction::DIV_INT: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002064 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2065 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravled0d48522014-11-04 16:40:20 +00002066 break;
2067 }
2068
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002069 case Instruction::DIV_LONG: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002070 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2071 dex_pc, Primitive::kPrimLong, false, true);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002072 break;
2073 }
2074
Calin Juravle7c4954d2014-10-28 16:57:40 +00002075 case Instruction::DIV_FLOAT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002076 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002077 break;
2078 }
2079
2080 case Instruction::DIV_DOUBLE: {
Calin Juravle225ff812014-11-13 16:46:39 +00002081 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002082 break;
2083 }
2084
Calin Juravlebacfec32014-11-14 15:54:36 +00002085 case Instruction::REM_INT: {
2086 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2087 dex_pc, Primitive::kPrimInt, false, false);
2088 break;
2089 }
2090
2091 case Instruction::REM_LONG: {
2092 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2093 dex_pc, Primitive::kPrimLong, false, false);
2094 break;
2095 }
2096
Calin Juravled2ec87d2014-12-08 14:24:46 +00002097 case Instruction::REM_FLOAT: {
2098 Binop_23x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2099 break;
2100 }
2101
2102 case Instruction::REM_DOUBLE: {
2103 Binop_23x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2104 break;
2105 }
2106
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002107 case Instruction::AND_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002108 Binop_23x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002109 break;
2110 }
2111
2112 case Instruction::AND_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002113 Binop_23x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002114 break;
2115 }
2116
Calin Juravle9aec02f2014-11-18 23:06:35 +00002117 case Instruction::SHL_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002118 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002119 break;
2120 }
2121
2122 case Instruction::SHL_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002123 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002124 break;
2125 }
2126
2127 case Instruction::SHR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002128 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002129 break;
2130 }
2131
2132 case Instruction::SHR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002133 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002134 break;
2135 }
2136
2137 case Instruction::USHR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002138 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002139 break;
2140 }
2141
2142 case Instruction::USHR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002143 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002144 break;
2145 }
2146
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002147 case Instruction::OR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002148 Binop_23x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002149 break;
2150 }
2151
2152 case Instruction::OR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002153 Binop_23x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002154 break;
2155 }
2156
2157 case Instruction::XOR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002158 Binop_23x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002159 break;
2160 }
2161
2162 case Instruction::XOR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002163 Binop_23x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002164 break;
2165 }
2166
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002167 case Instruction::ADD_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002168 Binop_12x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002169 break;
2170 }
2171
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002172 case Instruction::ADD_DOUBLE_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002173 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002174 break;
2175 }
2176
2177 case Instruction::ADD_FLOAT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002178 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002179 break;
2180 }
2181
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002182 case Instruction::SUB_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002183 Binop_12x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002184 break;
2185 }
2186
2187 case Instruction::SUB_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002188 Binop_12x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002189 break;
2190 }
2191
Calin Juravle096cc022014-10-23 17:01:13 +01002192 case Instruction::SUB_FLOAT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002193 Binop_12x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01002194 break;
2195 }
2196
2197 case Instruction::SUB_DOUBLE_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002198 Binop_12x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01002199 break;
2200 }
2201
Calin Juravle34bacdf2014-10-07 20:23:36 +01002202 case Instruction::MUL_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002203 Binop_12x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002204 break;
2205 }
2206
2207 case Instruction::MUL_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002208 Binop_12x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002209 break;
2210 }
2211
Calin Juravleb5bfa962014-10-21 18:02:24 +01002212 case Instruction::MUL_FLOAT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002213 Binop_12x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01002214 break;
2215 }
2216
2217 case Instruction::MUL_DOUBLE_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002218 Binop_12x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01002219 break;
2220 }
2221
Calin Juravle865fc882014-11-06 17:09:03 +00002222 case Instruction::DIV_INT_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002223 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2224 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravle865fc882014-11-06 17:09:03 +00002225 break;
2226 }
2227
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002228 case Instruction::DIV_LONG_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002229 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2230 dex_pc, Primitive::kPrimLong, false, true);
2231 break;
2232 }
2233
2234 case Instruction::REM_INT_2ADDR: {
2235 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2236 dex_pc, Primitive::kPrimInt, false, false);
2237 break;
2238 }
2239
2240 case Instruction::REM_LONG_2ADDR: {
2241 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2242 dex_pc, Primitive::kPrimLong, false, false);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002243 break;
2244 }
2245
Calin Juravled2ec87d2014-12-08 14:24:46 +00002246 case Instruction::REM_FLOAT_2ADDR: {
2247 Binop_12x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2248 break;
2249 }
2250
2251 case Instruction::REM_DOUBLE_2ADDR: {
2252 Binop_12x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2253 break;
2254 }
2255
Calin Juravle9aec02f2014-11-18 23:06:35 +00002256 case Instruction::SHL_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002257 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002258 break;
2259 }
2260
2261 case Instruction::SHL_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002262 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002263 break;
2264 }
2265
2266 case Instruction::SHR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002267 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002268 break;
2269 }
2270
2271 case Instruction::SHR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002272 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002273 break;
2274 }
2275
2276 case Instruction::USHR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002277 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002278 break;
2279 }
2280
2281 case Instruction::USHR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002282 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002283 break;
2284 }
2285
Calin Juravle7c4954d2014-10-28 16:57:40 +00002286 case Instruction::DIV_FLOAT_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00002287 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002288 break;
2289 }
2290
2291 case Instruction::DIV_DOUBLE_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00002292 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002293 break;
2294 }
2295
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002296 case Instruction::AND_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002297 Binop_12x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002298 break;
2299 }
2300
2301 case Instruction::AND_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002302 Binop_12x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002303 break;
2304 }
2305
2306 case Instruction::OR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002307 Binop_12x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002308 break;
2309 }
2310
2311 case Instruction::OR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002312 Binop_12x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002313 break;
2314 }
2315
2316 case Instruction::XOR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002317 Binop_12x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002318 break;
2319 }
2320
2321 case Instruction::XOR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002322 Binop_12x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002323 break;
2324 }
2325
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002326 case Instruction::ADD_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002327 Binop_22s<HAdd>(instruction, false, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002328 break;
2329 }
2330
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002331 case Instruction::AND_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002332 Binop_22s<HAnd>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002333 break;
2334 }
2335
2336 case Instruction::OR_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002337 Binop_22s<HOr>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002338 break;
2339 }
2340
2341 case Instruction::XOR_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002342 Binop_22s<HXor>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002343 break;
2344 }
2345
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002346 case Instruction::RSUB_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002347 Binop_22s<HSub>(instruction, true, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002348 break;
2349 }
2350
Calin Juravle34bacdf2014-10-07 20:23:36 +01002351 case Instruction::MUL_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002352 Binop_22s<HMul>(instruction, false, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002353 break;
2354 }
2355
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002356 case Instruction::ADD_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002357 Binop_22b<HAdd>(instruction, false, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002358 break;
2359 }
2360
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002361 case Instruction::AND_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002362 Binop_22b<HAnd>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002363 break;
2364 }
2365
2366 case Instruction::OR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002367 Binop_22b<HOr>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002368 break;
2369 }
2370
2371 case Instruction::XOR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002372 Binop_22b<HXor>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002373 break;
2374 }
2375
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002376 case Instruction::RSUB_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002377 Binop_22b<HSub>(instruction, true, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002378 break;
2379 }
2380
Calin Juravle34bacdf2014-10-07 20:23:36 +01002381 case Instruction::MUL_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002382 Binop_22b<HMul>(instruction, false, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002383 break;
2384 }
2385
Calin Juravled0d48522014-11-04 16:40:20 +00002386 case Instruction::DIV_INT_LIT16:
2387 case Instruction::DIV_INT_LIT8: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002388 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2389 dex_pc, Primitive::kPrimInt, true, true);
2390 break;
2391 }
2392
2393 case Instruction::REM_INT_LIT16:
2394 case Instruction::REM_INT_LIT8: {
2395 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2396 dex_pc, Primitive::kPrimInt, true, false);
Calin Juravled0d48522014-11-04 16:40:20 +00002397 break;
2398 }
2399
Calin Juravle9aec02f2014-11-18 23:06:35 +00002400 case Instruction::SHL_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002401 Binop_22b<HShl>(instruction, false, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002402 break;
2403 }
2404
2405 case Instruction::SHR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002406 Binop_22b<HShr>(instruction, false, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002407 break;
2408 }
2409
2410 case Instruction::USHR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002411 Binop_22b<HUShr>(instruction, false, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002412 break;
2413 }
2414
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002415 case Instruction::NEW_INSTANCE: {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002416 uint16_t type_index = instruction.VRegB_21c();
Jeff Hao848f70a2014-01-15 13:49:50 -08002417 if (compiler_driver_->IsStringTypeIndex(type_index, dex_file_)) {
Jeff Hao848f70a2014-01-15 13:49:50 -08002418 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002419 HFakeString* fake_string = new (arena_) HFakeString(dex_pc);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002420 current_block_->AddInstruction(fake_string);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002421 UpdateLocal(register_index, fake_string, dex_pc);
Jeff Hao848f70a2014-01-15 13:49:50 -08002422 } else {
2423 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
2424 ? kQuickAllocObjectWithAccessCheck
2425 : kQuickAllocObject;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002426
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002427 current_block_->AddInstruction(new (arena_) HNewInstance(
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002428 graph_->GetCurrentMethod(),
2429 dex_pc,
2430 type_index,
2431 *dex_compilation_unit_->GetDexFile(),
2432 entrypoint));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002433 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Jeff Hao848f70a2014-01-15 13:49:50 -08002434 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002435 break;
2436 }
2437
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002438 case Instruction::NEW_ARRAY: {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002439 uint16_t type_index = instruction.VRegC_22c();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002440 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt, dex_pc);
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002441 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
2442 ? kQuickAllocArrayWithAccessCheck
2443 : kQuickAllocArray;
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002444 current_block_->AddInstruction(new (arena_) HNewArray(length,
2445 graph_->GetCurrentMethod(),
2446 dex_pc,
2447 type_index,
2448 *dex_compilation_unit_->GetDexFile(),
2449 entrypoint));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002450 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002451 break;
2452 }
2453
2454 case Instruction::FILLED_NEW_ARRAY: {
2455 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
2456 uint32_t type_index = instruction.VRegB_35c();
2457 uint32_t args[5];
2458 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00002459 BuildFilledNewArray(dex_pc, type_index, number_of_vreg_arguments, false, args, 0);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002460 break;
2461 }
2462
2463 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2464 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
2465 uint32_t type_index = instruction.VRegB_3rc();
2466 uint32_t register_index = instruction.VRegC_3rc();
2467 BuildFilledNewArray(
Calin Juravle225ff812014-11-13 16:46:39 +00002468 dex_pc, type_index, number_of_vreg_arguments, true, nullptr, register_index);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002469 break;
2470 }
2471
2472 case Instruction::FILL_ARRAY_DATA: {
Calin Juravle225ff812014-11-13 16:46:39 +00002473 BuildFillArrayData(instruction, dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002474 break;
2475 }
2476
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002477 case Instruction::MOVE_RESULT:
Dave Allison20dfc792014-06-16 20:44:29 -07002478 case Instruction::MOVE_RESULT_WIDE:
David Brazdilfc6a86a2015-06-26 10:33:45 +00002479 case Instruction::MOVE_RESULT_OBJECT: {
Nicolas Geoffray1efcc222015-06-24 12:41:20 +01002480 if (latest_result_ == nullptr) {
2481 // Only dead code can lead to this situation, where the verifier
2482 // does not reject the method.
2483 } else {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002484 // An Invoke/FilledNewArray and its MoveResult could have landed in
2485 // different blocks if there was a try/catch block boundary between
2486 // them. For Invoke, we insert a StoreLocal after the instruction. For
2487 // FilledNewArray, the local needs to be updated after the array was
2488 // filled, otherwise we might overwrite an input vreg.
2489 HStoreLocal* update_local =
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002490 new (arena_) HStoreLocal(GetLocalAt(instruction.VRegA()), latest_result_, dex_pc);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002491 HBasicBlock* block = latest_result_->GetBlock();
2492 if (block == current_block_) {
2493 // MoveResult and the previous instruction are in the same block.
2494 current_block_->AddInstruction(update_local);
2495 } else {
2496 // The two instructions are in different blocks. Insert the MoveResult
2497 // before the final control-flow instruction of the previous block.
2498 DCHECK(block->EndsWithControlFlowInstruction());
2499 DCHECK(current_block_->GetInstructions().IsEmpty());
2500 block->InsertInstructionBefore(update_local, block->GetLastInstruction());
2501 }
Nicolas Geoffray1efcc222015-06-24 12:41:20 +01002502 latest_result_ = nullptr;
2503 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002504 break;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002505 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002506
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002507 case Instruction::CMP_LONG: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002508 Binop_23x_cmp(instruction, Primitive::kPrimLong, ComparisonBias::kNoBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002509 break;
2510 }
2511
2512 case Instruction::CMPG_FLOAT: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002513 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kGtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002514 break;
2515 }
2516
2517 case Instruction::CMPG_DOUBLE: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002518 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kGtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002519 break;
2520 }
2521
2522 case Instruction::CMPL_FLOAT: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002523 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kLtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002524 break;
2525 }
2526
2527 case Instruction::CMPL_DOUBLE: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002528 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kLtBias, dex_pc);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002529 break;
2530 }
2531
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002532 case Instruction::NOP:
2533 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002534
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002535 case Instruction::IGET:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002536 case Instruction::IGET_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002537 case Instruction::IGET_WIDE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002538 case Instruction::IGET_WIDE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002539 case Instruction::IGET_OBJECT:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002540 case Instruction::IGET_OBJECT_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002541 case Instruction::IGET_BOOLEAN:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002542 case Instruction::IGET_BOOLEAN_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002543 case Instruction::IGET_BYTE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002544 case Instruction::IGET_BYTE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002545 case Instruction::IGET_CHAR:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002546 case Instruction::IGET_CHAR_QUICK:
2547 case Instruction::IGET_SHORT:
2548 case Instruction::IGET_SHORT_QUICK: {
Calin Juravle225ff812014-11-13 16:46:39 +00002549 if (!BuildInstanceFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002550 return false;
2551 }
2552 break;
2553 }
2554
2555 case Instruction::IPUT:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002556 case Instruction::IPUT_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002557 case Instruction::IPUT_WIDE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002558 case Instruction::IPUT_WIDE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002559 case Instruction::IPUT_OBJECT:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002560 case Instruction::IPUT_OBJECT_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002561 case Instruction::IPUT_BOOLEAN:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002562 case Instruction::IPUT_BOOLEAN_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002563 case Instruction::IPUT_BYTE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002564 case Instruction::IPUT_BYTE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002565 case Instruction::IPUT_CHAR:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002566 case Instruction::IPUT_CHAR_QUICK:
2567 case Instruction::IPUT_SHORT:
2568 case Instruction::IPUT_SHORT_QUICK: {
Calin Juravle225ff812014-11-13 16:46:39 +00002569 if (!BuildInstanceFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002570 return false;
2571 }
2572 break;
2573 }
2574
2575 case Instruction::SGET:
2576 case Instruction::SGET_WIDE:
2577 case Instruction::SGET_OBJECT:
2578 case Instruction::SGET_BOOLEAN:
2579 case Instruction::SGET_BYTE:
2580 case Instruction::SGET_CHAR:
2581 case Instruction::SGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002582 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002583 return false;
2584 }
2585 break;
2586 }
2587
2588 case Instruction::SPUT:
2589 case Instruction::SPUT_WIDE:
2590 case Instruction::SPUT_OBJECT:
2591 case Instruction::SPUT_BOOLEAN:
2592 case Instruction::SPUT_BYTE:
2593 case Instruction::SPUT_CHAR:
2594 case Instruction::SPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002595 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002596 return false;
2597 }
2598 break;
2599 }
2600
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002601#define ARRAY_XX(kind, anticipated_type) \
2602 case Instruction::AGET##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00002603 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002604 break; \
2605 } \
2606 case Instruction::APUT##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00002607 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002608 break; \
2609 }
2610
2611 ARRAY_XX(, Primitive::kPrimInt);
2612 ARRAY_XX(_WIDE, Primitive::kPrimLong);
2613 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
2614 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
2615 ARRAY_XX(_BYTE, Primitive::kPrimByte);
2616 ARRAY_XX(_CHAR, Primitive::kPrimChar);
2617 ARRAY_XX(_SHORT, Primitive::kPrimShort);
2618
Nicolas Geoffray39468442014-09-02 15:17:15 +01002619 case Instruction::ARRAY_LENGTH: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002620 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot, dex_pc);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002621 // No need for a temporary for the null check, it is the only input of the following
2622 // instruction.
Calin Juravle225ff812014-11-13 16:46:39 +00002623 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002624 current_block_->AddInstruction(object);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002625 current_block_->AddInstruction(new (arena_) HArrayLength(object, dex_pc));
2626 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002627 break;
2628 }
2629
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002630 case Instruction::CONST_STRING: {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002631 current_block_->AddInstruction(
2632 new (arena_) HLoadString(graph_->GetCurrentMethod(), instruction.VRegB_21c(), dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002633 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002634 break;
2635 }
2636
2637 case Instruction::CONST_STRING_JUMBO: {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002638 current_block_->AddInstruction(
2639 new (arena_) HLoadString(graph_->GetCurrentMethod(), instruction.VRegB_31c(), dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002640 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002641 break;
2642 }
2643
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002644 case Instruction::CONST_CLASS: {
2645 uint16_t type_index = instruction.VRegB_21c();
2646 bool type_known_final;
2647 bool type_known_abstract;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002648 bool dont_use_is_referrers_class;
2649 // `CanAccessTypeWithoutChecks` will tell whether the method being
2650 // built is trying to access its own class, so that the generated
2651 // code can optimize for this case. However, the optimization does not
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002652 // work for inlining, so we use `IsOutermostCompilingClass` instead.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002653 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
2654 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002655 &type_known_final, &type_known_abstract, &dont_use_is_referrers_class);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002656 if (!can_access) {
Calin Juravle48c2b032014-12-09 18:11:36 +00002657 MaybeRecordStat(MethodCompilationStat::kNotCompiledCantAccesType);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002658 return false;
2659 }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002660 current_block_->AddInstruction(new (arena_) HLoadClass(
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002661 graph_->GetCurrentMethod(),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002662 type_index,
2663 *dex_compilation_unit_->GetDexFile(),
2664 IsOutermostCompilingClass(type_index),
2665 dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002666 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002667 break;
2668 }
2669
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002670 case Instruction::MOVE_EXCEPTION: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002671 current_block_->AddInstruction(new (arena_) HLoadException(dex_pc));
2672 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction(), dex_pc);
2673 current_block_->AddInstruction(new (arena_) HClearException(dex_pc));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002674 break;
2675 }
2676
2677 case Instruction::THROW: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002678 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00002679 current_block_->AddInstruction(new (arena_) HThrow(exception, dex_pc));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002680 // A throw instruction must branch to the exit block.
2681 current_block_->AddSuccessor(exit_block_);
2682 // We finished building this block. Set the current block to null to avoid
2683 // adding dead instructions to it.
2684 current_block_ = nullptr;
2685 break;
2686 }
2687
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002688 case Instruction::INSTANCE_OF: {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002689 uint8_t destination = instruction.VRegA_22c();
2690 uint8_t reference = instruction.VRegB_22c();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002691 uint16_t type_index = instruction.VRegC_22c();
Calin Juravle225ff812014-11-13 16:46:39 +00002692 if (!BuildTypeCheck(instruction, destination, reference, type_index, dex_pc)) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002693 return false;
2694 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002695 break;
2696 }
2697
2698 case Instruction::CHECK_CAST: {
2699 uint8_t reference = instruction.VRegA_21c();
2700 uint16_t type_index = instruction.VRegB_21c();
Calin Juravle225ff812014-11-13 16:46:39 +00002701 if (!BuildTypeCheck(instruction, -1, reference, type_index, dex_pc)) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002702 return false;
2703 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002704 break;
2705 }
2706
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002707 case Instruction::MONITOR_ENTER: {
2708 current_block_->AddInstruction(new (arena_) HMonitorOperation(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002709 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot, dex_pc),
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002710 HMonitorOperation::kEnter,
Calin Juravle225ff812014-11-13 16:46:39 +00002711 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002712 break;
2713 }
2714
2715 case Instruction::MONITOR_EXIT: {
2716 current_block_->AddInstruction(new (arena_) HMonitorOperation(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002717 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot, dex_pc),
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002718 HMonitorOperation::kExit,
Calin Juravle225ff812014-11-13 16:46:39 +00002719 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002720 break;
2721 }
2722
Andreas Gamped881df52014-11-24 23:28:39 -08002723 case Instruction::PACKED_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002724 BuildPackedSwitch(instruction, dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08002725 break;
2726 }
2727
Andreas Gampee4d4d322014-12-04 09:09:57 -08002728 case Instruction::SPARSE_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002729 BuildSparseSwitch(instruction, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08002730 break;
2731 }
2732
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002733 default:
Calin Juravle48c2b032014-12-09 18:11:36 +00002734 VLOG(compiler) << "Did not compile "
2735 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
2736 << " because of unhandled instruction "
2737 << instruction.Name();
2738 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnhandledInstruction);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002739 return false;
2740 }
2741 return true;
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00002742} // NOLINT(readability/fn_size)
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002743
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002744HLocal* HGraphBuilder::GetLocalAt(int register_index) const {
2745 return locals_.Get(register_index);
2746}
2747
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002748void HGraphBuilder::UpdateLocal(int register_index,
2749 HInstruction* instruction,
2750 uint32_t dex_pc) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002751 HLocal* local = GetLocalAt(register_index);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002752 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction, dex_pc));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002753}
2754
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002755HInstruction* HGraphBuilder::LoadLocal(int register_index,
2756 Primitive::Type type,
2757 uint32_t dex_pc) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002758 HLocal* local = GetLocalAt(register_index);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002759 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type, dex_pc));
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002760 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002761}
2762
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002763} // namespace art