blob: 7a3aa581496c4ce6c5311fb60ddb5b933e874a51 [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.
401 for (size_t i = 0; i < try_block->GetPredecessors().Size(); ++i) {
402 HBasicBlock* predecessor = try_block->GetPredecessors().Get(i);
403 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.
429 for (size_t i = 0; i < try_block->GetSuccessors().Size(); ++i) {
430 HBasicBlock* successor = try_block->GetSuccessors().Get(i);
431 if (successor->IsCatchBlock()) {
432 // A catch block is always considered an entry point into its TryItem.
433 // We therefore assume this is an exit point, regardless of whether
434 // the catch block is in a different TryItem or not.
435 } else if (successor->IsSingleTryBoundary()) {
436 // The edge was already split because of an entry into a neighbouring
437 // TryItem. We split it again and insert an exit.
438 if (kIsDebugBuild) {
439 HTryBoundary* last_insn = successor->GetLastInstruction()->AsTryBoundary();
David Brazdilbff75032015-07-08 17:26:51 +0000440 const DexFile::TryItem* successor_try_item =
441 GetTryItem(last_insn->GetNormalFlowSuccessor(), code_item, can_block_throw);
David Brazdil281a6322015-07-03 10:34:57 +0100442 DCHECK_EQ(try_block, successor->GetSinglePredecessor());
443 DCHECK(last_insn->IsEntry());
David Brazdilbff75032015-07-08 17:26:51 +0000444 DCHECK_NE(try_item, successor_try_item);
David Brazdil281a6322015-07-03 10:34:57 +0100445 }
David Brazdilbff75032015-07-08 17:26:51 +0000446 } else if (GetTryItem(successor, code_item, can_block_throw) != try_item) {
David Brazdil281a6322015-07-03 10:34:57 +0100447 // This is an exit out of the TryItem and the edge has not been split
448 // yet. That means that either `successor` is not in a TryItem, or it
449 // is in a different TryItem and we happened to iterate over this
450 // block first. We split the edge and insert an exit.
451 HInstruction* last_instruction = try_block->GetLastInstruction();
452 if (last_instruction->IsReturn() || last_instruction->IsReturnVoid()) {
453 DCHECK_EQ(successor, exit_block_);
454 // Control flow exits the try block with a Return(Void). Because
455 // splitting the edge would invalidate the invariant that Return
456 // always jumps to Exit, we move the Return outside the try block.
457 successor = try_block->SplitBefore(last_instruction);
458 }
459 } else {
460 // Not an edge on the boundary of the try block.
461 continue;
462 }
David Brazdilbff75032015-07-08 17:26:51 +0000463 SplitTryBoundaryEdge(try_block, successor, HTryBoundary::kExit, code_item, *try_item);
David Brazdil281a6322015-07-03 10:34:57 +0100464 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000465 }
466}
467
David Brazdil5e8b1372015-01-23 14:39:08 +0000468bool HGraphBuilder::BuildGraph(const DexFile::CodeItem& code_item) {
469 DCHECK(graph_->GetBlocks().IsEmpty());
470
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000471 const uint16_t* code_ptr = code_item.insns_;
472 const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100473 code_start_ = code_ptr;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000474
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000475 // Setup the graph with the entry block and exit block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100476 entry_block_ = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000477 graph_->AddBlock(entry_block_);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100478 exit_block_ = new (arena_) HBasicBlock(graph_, kNoDexPc);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000479 graph_->SetEntryBlock(entry_block_);
480 graph_->SetExitBlock(exit_block_);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000481
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000482 InitializeLocals(code_item.registers_size_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000483 graph_->SetMaximumNumberOfOutVRegs(code_item.outs_size_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000484
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000485 // Compute the number of dex instructions, blocks, and branches. We will
486 // check these values against limits given to the compiler.
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000487 size_t number_of_branches = 0;
488
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000489 // To avoid splitting blocks, we compute ahead of time the instructions that
490 // start a new block, and create these blocks.
Calin Juravle702d2602015-04-30 19:28:21 +0100491 if (!ComputeBranchTargets(code_ptr, code_end, &number_of_branches)) {
492 MaybeRecordStat(MethodCompilationStat::kNotCompiledBranchOutsideMethodCode);
493 return false;
494 }
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000495
496 // Note that the compiler driver is null when unit testing.
David Brazdil1b498722015-03-31 11:37:18 +0100497 if ((compiler_driver_ != nullptr) && SkipCompilation(code_item, number_of_branches)) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000498 return false;
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000499 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000500
David Brazdilfc6a86a2015-06-26 10:33:45 +0000501 CreateBlocksForTryCatch(code_item);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000502
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000503 InitializeParameters(code_item.ins_size_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100504
Calin Juravle225ff812014-11-13 16:46:39 +0000505 size_t dex_pc = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000506 while (code_ptr < code_end) {
Calin Juravle225ff812014-11-13 16:46:39 +0000507 // Update the current block if dex_pc starts a new block.
508 MaybeUpdateCurrentBlock(dex_pc);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000509 const Instruction& instruction = *Instruction::At(code_ptr);
Calin Juravle48c2b032014-12-09 18:11:36 +0000510 if (!AnalyzeDexInstruction(instruction, dex_pc)) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000511 return false;
Calin Juravle48c2b032014-12-09 18:11:36 +0000512 }
Calin Juravle225ff812014-11-13 16:46:39 +0000513 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000514 code_ptr += instruction.SizeInCodeUnits();
515 }
516
David Brazdilfc6a86a2015-06-26 10:33:45 +0000517 // Add Exit to the exit block.
David Brazdil3e187382015-06-26 09:59:52 +0000518 exit_block_->AddInstruction(new (arena_) HExit());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000519 // Add the suspend check to the entry block.
520 entry_block_->AddInstruction(new (arena_) HSuspendCheck(0));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000521 entry_block_->AddInstruction(new (arena_) HGoto());
David Brazdilbff75032015-07-08 17:26:51 +0000522 // Add the exit block at the end.
523 graph_->AddBlock(exit_block_);
David Brazdil5e8b1372015-01-23 14:39:08 +0000524
David Brazdilfc6a86a2015-06-26 10:33:45 +0000525 // Iterate over blocks covered by TryItems and insert TryBoundaries at entry
526 // and exit points. This requires all control-flow instructions and
527 // non-exceptional edges to have been created.
528 InsertTryBoundaryBlocks(code_item);
529
David Brazdil5e8b1372015-01-23 14:39:08 +0000530 return true;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000531}
532
David Brazdilfc6a86a2015-06-26 10:33:45 +0000533void HGraphBuilder::MaybeUpdateCurrentBlock(size_t dex_pc) {
534 HBasicBlock* block = FindBlockStartingAt(dex_pc);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000535 if (block == nullptr) {
536 return;
537 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000538
539 if (current_block_ != nullptr) {
540 // Branching instructions clear current_block, so we know
541 // the last instruction of the current block is not a branching
542 // instruction. We add an unconditional goto to the found block.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600543 current_block_->AddInstruction(new (arena_) HGoto(dex_pc));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000544 current_block_->AddSuccessor(block);
545 }
546 graph_->AddBlock(block);
547 current_block_ = block;
548}
549
Calin Juravle702d2602015-04-30 19:28:21 +0100550bool HGraphBuilder::ComputeBranchTargets(const uint16_t* code_ptr,
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000551 const uint16_t* code_end,
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000552 size_t* number_of_branches) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000553 branch_targets_.SetSize(code_end - code_ptr);
554
555 // Create the first block for the dex instructions, single successor of the entry block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100556 HBasicBlock* block = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000557 branch_targets_.Put(0, block);
558 entry_block_->AddSuccessor(block);
559
560 // Iterate over all instructions and find branching instructions. Create blocks for
561 // the locations these instructions branch to.
Andreas Gamped881df52014-11-24 23:28:39 -0800562 uint32_t dex_pc = 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000563 while (code_ptr < code_end) {
564 const Instruction& instruction = *Instruction::At(code_ptr);
565 if (instruction.IsBranch()) {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000566 (*number_of_branches)++;
Calin Juravle225ff812014-11-13 16:46:39 +0000567 int32_t target = instruction.GetTargetOffset() + dex_pc;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000568 // Create a block for the target instruction.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000569 FindOrCreateBlockStartingAt(target);
570
Calin Juravle225ff812014-11-13 16:46:39 +0000571 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000572 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle702d2602015-04-30 19:28:21 +0100573
David Brazdilfe659462015-06-24 14:23:56 +0100574 if (instruction.CanFlowThrough()) {
575 if (code_ptr >= code_end) {
Calin Juravle702d2602015-04-30 19:28:21 +0100576 // In the normal case we should never hit this but someone can artificially forge a dex
577 // file to fall-through out the method code. In this case we bail out compilation.
578 return false;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000579 } else {
580 FindOrCreateBlockStartingAt(dex_pc);
Calin Juravle702d2602015-04-30 19:28:21 +0100581 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000582 }
Andreas Gampee4d4d322014-12-04 09:09:57 -0800583 } else if (instruction.IsSwitch()) {
584 SwitchTable table(instruction, dex_pc, instruction.Opcode() == Instruction::SPARSE_SWITCH);
Andreas Gamped881df52014-11-24 23:28:39 -0800585
586 uint16_t num_entries = table.GetNumEntries();
587
Andreas Gampee4d4d322014-12-04 09:09:57 -0800588 // In a packed-switch, the entry at index 0 is the starting key. In a sparse-switch, the
589 // entry at index 0 is the first key, and values are after *all* keys.
590 size_t offset = table.GetFirstValueIndex();
591
592 // Use a larger loop counter type to avoid overflow issues.
593 for (size_t i = 0; i < num_entries; ++i) {
Andreas Gamped881df52014-11-24 23:28:39 -0800594 // The target of the case.
Andreas Gampee4d4d322014-12-04 09:09:57 -0800595 uint32_t target = dex_pc + table.GetEntryAt(i + offset);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000596 FindOrCreateBlockStartingAt(target);
Andreas Gamped881df52014-11-24 23:28:39 -0800597
David Brazdil281a6322015-07-03 10:34:57 +0100598 // Create a block for the switch-case logic. The block gets the dex_pc
599 // of the SWITCH instruction because it is part of its semantics.
600 block = new (arena_) HBasicBlock(graph_, dex_pc);
601 branch_targets_.Put(table.GetDexPcForIndex(i), block);
Andreas Gamped881df52014-11-24 23:28:39 -0800602 }
603
604 // Fall-through. Add a block if there is more code afterwards.
605 dex_pc += instruction.SizeInCodeUnits();
606 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle702d2602015-04-30 19:28:21 +0100607 if (code_ptr >= code_end) {
608 // In the normal case we should never hit this but someone can artificially forge a dex
609 // file to fall-through out the method code. In this case we bail out compilation.
610 // (A switch can fall-through so we don't need to check CanFlowThrough().)
611 return false;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000612 } else {
613 FindOrCreateBlockStartingAt(dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -0800614 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000615 } else {
616 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle225ff812014-11-13 16:46:39 +0000617 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000618 }
619 }
Calin Juravle702d2602015-04-30 19:28:21 +0100620 return true;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000621}
622
David Brazdilfc6a86a2015-06-26 10:33:45 +0000623HBasicBlock* HGraphBuilder::FindBlockStartingAt(int32_t dex_pc) const {
624 DCHECK_GE(dex_pc, 0);
625 DCHECK_LT(static_cast<size_t>(dex_pc), branch_targets_.Size());
626 return branch_targets_.Get(dex_pc);
627}
628
629HBasicBlock* HGraphBuilder::FindOrCreateBlockStartingAt(int32_t dex_pc) {
630 HBasicBlock* block = FindBlockStartingAt(dex_pc);
631 if (block == nullptr) {
632 block = new (arena_) HBasicBlock(graph_, dex_pc);
633 branch_targets_.Put(dex_pc, block);
634 }
635 return block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000636}
637
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100638template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600639void HGraphBuilder::Unop_12x(const Instruction& instruction,
640 Primitive::Type type,
641 uint32_t dex_pc) {
642 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
643 current_block_->AddInstruction(new (arena_) T(type, first, dex_pc));
644 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Roland Levillain88cb1752014-10-20 16:36:47 +0100645}
646
Roland Levillaindff1f282014-11-05 14:15:05 +0000647void HGraphBuilder::Conversion_12x(const Instruction& instruction,
648 Primitive::Type input_type,
Roland Levillain624279f2014-12-04 11:54:28 +0000649 Primitive::Type result_type,
650 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600651 HInstruction* first = LoadLocal(instruction.VRegB(), input_type, dex_pc);
Roland Levillain624279f2014-12-04 11:54:28 +0000652 current_block_->AddInstruction(new (arena_) HTypeConversion(result_type, first, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600653 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100654}
655
656template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000657void HGraphBuilder::Binop_23x(const Instruction& instruction,
658 Primitive::Type type,
659 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600660 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
661 HInstruction* second = LoadLocal(instruction.VRegC(), type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000662 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600663 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000664}
665
666template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000667void HGraphBuilder::Binop_23x_shift(const Instruction& instruction,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600668 Primitive::Type type,
669 uint32_t dex_pc) {
670 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
671 HInstruction* second = LoadLocal(instruction.VRegC(), Primitive::kPrimInt, dex_pc);
672 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
673 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000674}
675
Calin Juravleddb7df22014-11-25 20:56:51 +0000676void HGraphBuilder::Binop_23x_cmp(const Instruction& instruction,
677 Primitive::Type type,
Mark Mendellc4701932015-04-10 13:18:51 -0400678 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700679 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600680 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
681 HInstruction* second = LoadLocal(instruction.VRegC(), type, dex_pc);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700682 current_block_->AddInstruction(new (arena_) HCompare(type, first, second, bias, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600683 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +0000684}
685
Calin Juravle9aec02f2014-11-18 23:06:35 +0000686template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600687void HGraphBuilder::Binop_12x_shift(const Instruction& instruction, Primitive::Type type,
688 uint32_t dex_pc) {
689 HInstruction* first = LoadLocal(instruction.VRegA(), type, dex_pc);
690 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
691 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
692 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000693}
694
695template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000696void HGraphBuilder::Binop_12x(const Instruction& instruction,
697 Primitive::Type type,
698 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600699 HInstruction* first = LoadLocal(instruction.VRegA(), type, dex_pc);
700 HInstruction* second = LoadLocal(instruction.VRegB(), type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000701 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600702 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000703}
704
705template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600706void HGraphBuilder::Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
707 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
708 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100709 if (reverse) {
710 std::swap(first, second);
711 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600712 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second, dex_pc));
713 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100714}
715
716template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600717void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
718 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
719 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100720 if (reverse) {
721 std::swap(first, second);
722 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600723 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second, dex_pc));
724 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100725}
726
Calin Juravle0c25d102015-04-20 14:49:09 +0100727static bool RequiresConstructorBarrier(const DexCompilationUnit* cu, const CompilerDriver& driver) {
Calin Juravle27df7582015-04-17 19:12:31 +0100728 Thread* self = Thread::Current();
Calin Juravle0c25d102015-04-20 14:49:09 +0100729 return cu->IsConstructor()
730 && driver.RequiresConstructorBarrier(self, cu->GetDexFile(), cu->GetClassDefIndex());
Calin Juravle27df7582015-04-17 19:12:31 +0100731}
732
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600733void HGraphBuilder::BuildReturn(const Instruction& instruction,
734 Primitive::Type type,
735 uint32_t dex_pc) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100736 if (type == Primitive::kPrimVoid) {
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100737 if (graph_->ShouldGenerateConstructorBarrier()) {
738 // The compilation unit is null during testing.
739 if (dex_compilation_unit_ != nullptr) {
740 DCHECK(RequiresConstructorBarrier(dex_compilation_unit_, *compiler_driver_))
741 << "Inconsistent use of ShouldGenerateConstructorBarrier. Should not generate a barrier.";
742 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600743 current_block_->AddInstruction(new (arena_) HMemoryBarrier(kStoreStore, dex_pc));
Calin Juravle27df7582015-04-17 19:12:31 +0100744 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600745 current_block_->AddInstruction(new (arena_) HReturnVoid(dex_pc));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100746 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600747 HInstruction* value = LoadLocal(instruction.VRegA(), type, dex_pc);
748 current_block_->AddInstruction(new (arena_) HReturn(value, dex_pc));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100749 }
750 current_block_->AddSuccessor(exit_block_);
751 current_block_ = nullptr;
752}
753
Calin Juravle68ad6492015-08-18 17:08:12 +0100754static InvokeType GetInvokeTypeFromOpCode(Instruction::Code opcode) {
755 switch (opcode) {
756 case Instruction::INVOKE_STATIC:
757 case Instruction::INVOKE_STATIC_RANGE:
758 return kStatic;
759 case Instruction::INVOKE_DIRECT:
760 case Instruction::INVOKE_DIRECT_RANGE:
761 return kDirect;
762 case Instruction::INVOKE_VIRTUAL:
763 case Instruction::INVOKE_VIRTUAL_QUICK:
764 case Instruction::INVOKE_VIRTUAL_RANGE:
765 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
766 return kVirtual;
767 case Instruction::INVOKE_INTERFACE:
768 case Instruction::INVOKE_INTERFACE_RANGE:
769 return kInterface;
770 case Instruction::INVOKE_SUPER_RANGE:
771 case Instruction::INVOKE_SUPER:
772 return kSuper;
773 default:
774 LOG(FATAL) << "Unexpected invoke opcode: " << opcode;
775 UNREACHABLE();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100776 }
Calin Juravle68ad6492015-08-18 17:08:12 +0100777}
778
779bool HGraphBuilder::BuildInvoke(const Instruction& instruction,
780 uint32_t dex_pc,
781 uint32_t method_idx,
782 uint32_t number_of_vreg_arguments,
783 bool is_range,
784 uint32_t* args,
785 uint32_t register_index) {
786 InvokeType original_invoke_type = GetInvokeTypeFromOpCode(instruction.Opcode());
787 InvokeType optimized_invoke_type = original_invoke_type;
788 const char* descriptor = dex_file_->GetMethodShorty(method_idx);
789 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
790
791 // Remove the return type from the 'proto'.
792 size_t number_of_arguments = strlen(descriptor) - 1;
793 if (original_invoke_type != kStatic) { // instance call
794 // One extra argument for 'this'.
795 number_of_arguments++;
796 }
797
798 MethodReference target_method(dex_file_, method_idx);
799 int32_t table_index;
800 uintptr_t direct_code;
801 uintptr_t direct_method;
802
803 if (!compiler_driver_->ComputeInvokeInfo(dex_compilation_unit_,
804 dex_pc,
805 true /* update_stats */,
806 true /* enable_devirtualization */,
807 &optimized_invoke_type,
808 &target_method,
809 &table_index,
810 &direct_code,
811 &direct_method)) {
812 VLOG(compiler) << "Did not compile "
813 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
814 << " because a method call could not be resolved";
815 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedMethod);
816 return false;
817 }
818
819 DCHECK(optimized_invoke_type != kSuper);
820
821 // Special handling for string init.
822 int32_t string_init_offset = 0;
823 bool is_string_init = compiler_driver_->IsStringInit(method_idx, dex_file_,
824 &string_init_offset);
825
826 // Potential class initialization check, in the case of a static method call.
827 HClinitCheck* clinit_check = nullptr;
828 HInvoke* invoke = nullptr;
829
830 if (is_string_init
831 || optimized_invoke_type == kDirect
832 || optimized_invoke_type == kStatic) {
833 // By default, consider that the called method implicitly requires
834 // an initialization check of its declaring method.
835 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement
836 = HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
837 if (optimized_invoke_type == kStatic && !is_string_init) {
838 clinit_check = ProcessClinitCheckForInvoke(dex_pc, method_idx, &clinit_check_requirement);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100839 }
Calin Juravle68ad6492015-08-18 17:08:12 +0100840
841 // Replace calls to String.<init> with StringFactory.
842 if (is_string_init) {
843 return_type = Primitive::kPrimNot;
844 number_of_arguments--;
845 optimized_invoke_type = kStatic;
846 }
847
848 HInvokeStaticOrDirect::DispatchInfo dispatch_info = ComputeDispatchInfo(is_string_init,
849 string_init_offset,
850 target_method,
851 direct_method,
852 direct_code);
853 invoke = new (arena_) HInvokeStaticOrDirect(arena_,
854 number_of_arguments,
855 return_type,
856 dex_pc,
857 method_idx,
858 target_method,
859 dispatch_info,
860 original_invoke_type,
861 optimized_invoke_type,
862 clinit_check_requirement);
863 } else if (optimized_invoke_type == kVirtual) {
864 invoke = new (arena_) HInvokeVirtual(arena_,
865 number_of_arguments,
866 return_type,
867 dex_pc,
868 method_idx,
869 table_index);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100870 } else {
Calin Juravle68ad6492015-08-18 17:08:12 +0100871 DCHECK_EQ(optimized_invoke_type, kInterface);
872 invoke = new (arena_) HInvokeInterface(arena_,
873 number_of_arguments,
874 return_type,
875 dex_pc,
876 method_idx,
877 table_index);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100878 }
Calin Juravle68ad6492015-08-18 17:08:12 +0100879
Calin Juravle0eedd7e2015-08-20 14:48:00 +0100880 return SetupArgumentsAndAddInvoke(invoke,
881 number_of_vreg_arguments,
882 args,
883 register_index,
884 is_range,
885 descriptor,
Calin Juravle599262c2015-08-20 15:01:27 +0100886 clinit_check);
Calin Juravle68ad6492015-08-18 17:08:12 +0100887}
888
889HClinitCheck* HGraphBuilder::ProcessClinitCheckForInvoke(
890 uint32_t dex_pc,
891 uint32_t method_idx,
892 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
893 ScopedObjectAccess soa(Thread::Current());
894 StackHandleScope<4> hs(soa.Self());
895 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
896 dex_compilation_unit_->GetClassLinker()->FindDexCache(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700897 soa.Self(), *dex_compilation_unit_->GetDexFile())));
Calin Juravle68ad6492015-08-18 17:08:12 +0100898 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
899 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
900 ArtMethod* resolved_method = compiler_driver_->ResolveMethod(
901 soa, dex_cache, class_loader, dex_compilation_unit_, method_idx, InvokeType::kStatic);
902
903 DCHECK(resolved_method != nullptr);
904
905 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
906 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700907 outer_compilation_unit_->GetClassLinker()->FindDexCache(soa.Self(), outer_dex_file)));
Calin Juravle68ad6492015-08-18 17:08:12 +0100908 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
909
910 // The index at which the method's class is stored in the DexCache's type array.
911 uint32_t storage_index = DexFile::kDexNoIndex;
912 bool is_outer_class = (resolved_method->GetDeclaringClass() == outer_class.Get());
913 if (is_outer_class) {
914 storage_index = outer_class->GetDexTypeIndex();
915 } else if (outer_dex_cache.Get() == dex_cache.Get()) {
916 // Get `storage_index` from IsClassOfStaticMethodAvailableToReferrer.
917 compiler_driver_->IsClassOfStaticMethodAvailableToReferrer(outer_dex_cache.Get(),
918 GetCompilingClass(),
919 resolved_method,
920 method_idx,
921 &storage_index);
922 }
923
924 HClinitCheck* clinit_check = nullptr;
925
926 if (!outer_class->IsInterface()
927 && outer_class->IsSubClass(resolved_method->GetDeclaringClass())) {
928 // If the outer class is the declaring class or a subclass
929 // of the declaring class, no class initialization is needed
930 // before the static method call.
931 // Note that in case of inlining, we do not need to add clinit checks
932 // to calls that satisfy this subclass check with any inlined methods. This
933 // will be detected by the optimization passes.
934 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
935 } else if (storage_index != DexFile::kDexNoIndex) {
936 // If the method's class type index is available, check
937 // whether we should add an explicit class initialization
938 // check for its declaring class before the static method call.
939
940 // TODO: find out why this check is needed.
941 bool is_in_dex_cache = compiler_driver_->CanAssumeTypeIsPresentInDexCache(
942 *outer_compilation_unit_->GetDexFile(), storage_index);
943 bool is_initialized =
944 resolved_method->GetDeclaringClass()->IsInitialized() && is_in_dex_cache;
945
946 if (is_initialized) {
947 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
948 } else {
949 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
950 HLoadClass* load_class = new (arena_) HLoadClass(
951 graph_->GetCurrentMethod(),
952 storage_index,
953 *dex_compilation_unit_->GetDexFile(),
954 is_outer_class,
955 dex_pc);
956 current_block_->AddInstruction(load_class);
957 clinit_check = new (arena_) HClinitCheck(load_class, dex_pc);
958 current_block_->AddInstruction(clinit_check);
959 }
960 }
961 return clinit_check;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100962}
963
Vladimir Marko58155012015-08-19 12:49:41 +0000964HInvokeStaticOrDirect::DispatchInfo HGraphBuilder::ComputeDispatchInfo(
965 bool is_string_init,
966 int32_t string_init_offset,
967 MethodReference target_method,
968 uintptr_t direct_method,
969 uintptr_t direct_code) {
970 HInvokeStaticOrDirect::MethodLoadKind method_load_kind;
971 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location;
972 uint64_t method_load_data = 0u;
973 uint64_t direct_code_ptr = 0u;
974
975 if (is_string_init) {
976 // TODO: Use direct_method and direct_code for the appropriate StringFactory method.
977 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kStringInit;
978 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
979 method_load_data = string_init_offset;
980 } else if (target_method.dex_file == outer_compilation_unit_->GetDexFile() &&
981 target_method.dex_method_index == outer_compilation_unit_->GetDexMethodIndex()) {
982 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kRecursive;
983 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallSelf;
984 } else {
985 if (direct_method != 0u) { // Should we use a direct pointer to the method?
986 if (direct_method != static_cast<uintptr_t>(-1)) { // Is the method pointer known now?
987 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress;
988 method_load_data = direct_method;
989 } else { // The direct pointer will be known at link time.
990 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup;
991 }
992 } else { // Use dex cache.
993 DCHECK(target_method.dex_file == dex_compilation_unit_->GetDexFile());
994 DexCacheArraysLayout layout =
995 compiler_driver_->GetDexCacheArraysLayout(target_method.dex_file);
996 if (layout.Valid()) { // Can we use PC-relative access to the dex cache arrays?
997 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative;
998 method_load_data = layout.MethodOffset(target_method.dex_method_index);
999 } else { // We must go through the ArtMethod's pointer to resolved methods.
1000 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
1001 }
1002 }
1003 if (direct_code != 0u) { // Should we use a direct pointer to the code?
1004 if (direct_code != static_cast<uintptr_t>(-1)) { // Is the code pointer known now?
1005 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallDirect;
1006 direct_code_ptr = direct_code;
1007 } else if (compiler_driver_->IsImage() ||
1008 target_method.dex_file == dex_compilation_unit_->GetDexFile()) {
1009 // Use PC-relative calls for invokes within a multi-dex oat file.
1010 // TODO: Recognize when the target dex file is within the current oat file for
1011 // app compilation. At the moment we recognize only the boot image as multi-dex.
1012 // NOTE: This will require changing the ARM backend which currently falls
1013 // through from kCallPCRelative to kDirectCodeFixup for different dex files.
1014 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative;
1015 } else { // The direct pointer will be known at link time.
1016 // NOTE: This is used for app->boot calls when compiling an app against
1017 // a relocatable but not yet relocated image.
1018 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup;
1019 }
1020 } else { // We must use the code pointer from the ArtMethod.
1021 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
1022 }
1023 }
1024
1025 if (graph_->IsDebuggable()) {
1026 // For debuggable apps always use the code pointer from ArtMethod
1027 // so that we don't circumvent instrumentation stubs if installed.
1028 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
1029 }
1030
1031 return HInvokeStaticOrDirect::DispatchInfo {
1032 method_load_kind, code_ptr_location, method_load_data, direct_code_ptr };
1033}
1034
Calin Juravle0eedd7e2015-08-20 14:48:00 +01001035bool HGraphBuilder::SetupArgumentsAndAddInvoke(HInvoke* invoke,
1036 uint32_t number_of_vreg_arguments,
1037 uint32_t* args,
1038 uint32_t register_index,
1039 bool is_range,
1040 const char* descriptor,
1041 HClinitCheck* clinit_check) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001042 size_t start_index = 0;
Calin Juravle68ad6492015-08-18 17:08:12 +01001043 size_t argument_index = 0;
1044 uint32_t descriptor_index = 1; // Skip the return type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001045 uint32_t dex_pc = invoke->GetDexPc();
Calin Juravle68ad6492015-08-18 17:08:12 +01001046
1047 bool is_instance_call = invoke->GetOriginalInvokeType() != InvokeType::kStatic;
1048 bool is_string_init = invoke->IsInvokeStaticOrDirect()
1049 && invoke->AsInvokeStaticOrDirect()->IsStringInit();
1050
1051 if (is_string_init) {
1052 start_index = 1;
1053 argument_index = 0;
1054 } else if (is_instance_call) {
1055 Temporaries temps(graph_);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001056 HInstruction* arg = LoadLocal(is_range ? register_index : args[0], Primitive::kPrimNot, dex_pc);
Calin Juravle68ad6492015-08-18 17:08:12 +01001057 HNullCheck* null_check = new (arena_) HNullCheck(arg, invoke->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001058 current_block_->AddInstruction(null_check);
1059 temps.Add(null_check);
1060 invoke->SetArgumentAt(0, null_check);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001061 start_index = 1;
Calin Juravle68ad6492015-08-18 17:08:12 +01001062 argument_index = 1;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001063 }
1064
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001065 for (size_t i = start_index;
1066 // Make sure we don't go over the expected arguments or over the number of
1067 // dex registers given. If the instruction was seen as dead by the verifier,
1068 // it hasn't been properly checked.
Calin Juravle68ad6492015-08-18 17:08:12 +01001069 (i < number_of_vreg_arguments) && (argument_index < invoke->GetNumberOfArguments());
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001070 i++, argument_index++) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001071 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001072 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001073 if (!is_range
1074 && is_wide
1075 && ((i + 1 == number_of_vreg_arguments) || (args[i] + 1 != args[i + 1]))) {
1076 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
1077 // reject any class where this is violated. However, the verifier only does these checks
1078 // on non trivially dead instructions, so we just bailout the compilation.
1079 VLOG(compiler) << "Did not compile "
1080 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
1081 << " because of non-sequential dex register pair in wide argument";
1082 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1083 return false;
1084 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001085 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type, dex_pc);
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001086 invoke->SetArgumentAt(argument_index, arg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001087 if (is_wide) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001088 i++;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001089 }
1090 }
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001091
Calin Juravle68ad6492015-08-18 17:08:12 +01001092 if (argument_index != invoke->GetNumberOfArguments()) {
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001093 VLOG(compiler) << "Did not compile "
1094 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
1095 << " because of wrong number of arguments in invoke instruction";
1096 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1097 return false;
1098 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001099
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001100 if (invoke->IsInvokeStaticOrDirect()) {
1101 invoke->SetArgumentAt(argument_index, graph_->GetCurrentMethod());
1102 argument_index++;
1103 }
1104
Calin Juravle68ad6492015-08-18 17:08:12 +01001105 if (clinit_check != nullptr) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001106 // Add the class initialization check as last input of `invoke`.
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001107 DCHECK(!is_string_init);
Calin Juravle68ad6492015-08-18 17:08:12 +01001108 DCHECK(invoke->IsInvokeStaticOrDirect());
1109 DCHECK(invoke->AsInvokeStaticOrDirect()->GetClinitCheckRequirement()
1110 == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit);
Roland Levillain3e3d7332015-04-28 11:00:54 +01001111 invoke->SetArgumentAt(argument_index, clinit_check);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001112 argument_index++;
Roland Levillain4c0eb422015-04-24 16:43:49 +01001113 }
1114
Jeff Hao848f70a2014-01-15 13:49:50 -08001115 // Add move-result for StringFactory method.
1116 if (is_string_init) {
1117 uint32_t orig_this_reg = is_range ? register_index : args[0];
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001118 HInstruction* fake_string = LoadLocal(orig_this_reg, Primitive::kPrimNot, dex_pc);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001119 invoke->SetArgumentAt(argument_index, fake_string);
Calin Juravle0eedd7e2015-08-20 14:48:00 +01001120 current_block_->AddInstruction(invoke);
Calin Juravle68ad6492015-08-18 17:08:12 +01001121 PotentiallySimplifyFakeString(orig_this_reg, invoke->GetDexPc(), invoke);
Calin Juravle0eedd7e2015-08-20 14:48:00 +01001122 } else {
1123 current_block_->AddInstruction(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001124 }
Calin Juravle0eedd7e2015-08-20 14:48:00 +01001125
1126 latest_result_ = invoke;
1127
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001128 return true;
1129}
1130
Calin Juravle68ad6492015-08-18 17:08:12 +01001131void HGraphBuilder::PotentiallySimplifyFakeString(uint16_t original_dex_register,
1132 uint32_t dex_pc,
1133 HInvoke* actual_string) {
1134 if (!graph_->IsDebuggable()) {
1135 // Notify that we cannot compile with baseline. The dex registers aliasing
1136 // with `original_dex_register` will be handled when we optimize
1137 // (see HInstructionSimplifer::VisitFakeString).
1138 can_use_baseline_for_string_init_ = false;
1139 return;
1140 }
1141 const VerifiedMethod* verified_method =
1142 compiler_driver_->GetVerifiedMethod(dex_file_, dex_compilation_unit_->GetDexMethodIndex());
1143 if (verified_method != nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001144 UpdateLocal(original_dex_register, actual_string, dex_pc);
Calin Juravle68ad6492015-08-18 17:08:12 +01001145 const SafeMap<uint32_t, std::set<uint32_t>>& string_init_map =
1146 verified_method->GetStringInitPcRegMap();
1147 auto map_it = string_init_map.find(dex_pc);
1148 if (map_it != string_init_map.end()) {
1149 std::set<uint32_t> reg_set = map_it->second;
1150 for (auto set_it = reg_set.begin(); set_it != reg_set.end(); ++set_it) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001151 HInstruction* load_local = LoadLocal(original_dex_register, Primitive::kPrimNot, dex_pc);
1152 UpdateLocal(*set_it, load_local, dex_pc);
Calin Juravle68ad6492015-08-18 17:08:12 +01001153 }
1154 }
1155 } else {
1156 can_use_baseline_for_string_init_ = false;
1157 }
1158}
1159
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001160bool HGraphBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001161 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001162 bool is_put) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001163 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1164 uint32_t obj_reg = instruction.VRegB_22c();
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001165 uint16_t field_index;
1166 if (instruction.IsQuickened()) {
1167 if (!CanDecodeQuickenedInfo()) {
1168 return false;
1169 }
1170 field_index = LookupQuickenedInfo(dex_pc);
1171 } else {
1172 field_index = instruction.VRegC_22c();
1173 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001174
1175 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierc7853442015-03-27 14:35:38 -07001176 ArtField* resolved_field =
1177 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001178
Mathieu Chartierc7853442015-03-27 14:35:38 -07001179 if (resolved_field == nullptr) {
Calin Juravle48c2b032014-12-09 18:11:36 +00001180 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedField);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001181 return false;
1182 }
Calin Juravle52c48962014-12-16 17:02:57 +00001183
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001184 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001185
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001186 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001187 current_block_->AddInstruction(new (arena_) HNullCheck(object, dex_pc));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001188 if (is_put) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001189 Temporaries temps(graph_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001190 HInstruction* null_check = current_block_->GetLastInstruction();
1191 // We need one temporary for the null check.
1192 temps.Add(null_check);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001193 HInstruction* value = LoadLocal(source_or_dest_reg, field_type, dex_pc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001194 current_block_->AddInstruction(new (arena_) HInstanceFieldSet(
1195 null_check,
1196 value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001197 field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00001198 resolved_field->GetOffset(),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001199 resolved_field->IsVolatile(),
1200 field_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07001201 *dex_file_,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001202 dex_compilation_unit_->GetDexCache(),
1203 dex_pc));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001204 } else {
1205 current_block_->AddInstruction(new (arena_) HInstanceFieldGet(
1206 current_block_->GetLastInstruction(),
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001207 field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00001208 resolved_field->GetOffset(),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001209 resolved_field->IsVolatile(),
1210 field_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07001211 *dex_file_,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001212 dex_compilation_unit_->GetDexCache(),
1213 dex_pc));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001214
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001215 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001216 }
1217 return true;
1218}
1219
Nicolas Geoffray30451742015-06-19 13:32:41 +01001220static mirror::Class* GetClassFrom(CompilerDriver* driver,
1221 const DexCompilationUnit& compilation_unit) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001222 ScopedObjectAccess soa(Thread::Current());
1223 StackHandleScope<2> hs(soa.Self());
Nicolas Geoffray30451742015-06-19 13:32:41 +01001224 const DexFile& dex_file = *compilation_unit.GetDexFile();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001225 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Nicolas Geoffray30451742015-06-19 13:32:41 +01001226 soa.Decode<mirror::ClassLoader*>(compilation_unit.GetClassLoader())));
1227 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001228 compilation_unit.GetClassLinker()->FindDexCache(soa.Self(), dex_file)));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001229
Nicolas Geoffray30451742015-06-19 13:32:41 +01001230 return driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, &compilation_unit);
1231}
1232
1233mirror::Class* HGraphBuilder::GetOutermostCompilingClass() const {
1234 return GetClassFrom(compiler_driver_, *outer_compilation_unit_);
1235}
1236
1237mirror::Class* HGraphBuilder::GetCompilingClass() const {
1238 return GetClassFrom(compiler_driver_, *dex_compilation_unit_);
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001239}
1240
1241bool HGraphBuilder::IsOutermostCompilingClass(uint16_t type_index) const {
1242 ScopedObjectAccess soa(Thread::Current());
1243 StackHandleScope<4> hs(soa.Self());
1244 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001245 dex_compilation_unit_->GetClassLinker()->FindDexCache(
1246 soa.Self(), *dex_compilation_unit_->GetDexFile())));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001247 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1248 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
1249 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
1250 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
Nicolas Geoffrayafd06412015-06-20 22:44:47 +01001251 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001252
Nicolas Geoffrayafd06412015-06-20 22:44:47 +01001253 return outer_class.Get() == cls.Get();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001254}
1255
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001256bool HGraphBuilder::BuildStaticFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001257 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001258 bool is_put) {
1259 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1260 uint16_t field_index = instruction.VRegB_21c();
1261
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001262 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierc7853442015-03-27 14:35:38 -07001263 StackHandleScope<4> hs(soa.Self());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001264 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001265 dex_compilation_unit_->GetClassLinker()->FindDexCache(
1266 soa.Self(), *dex_compilation_unit_->GetDexFile())));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001267 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1268 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
Mathieu Chartierc7853442015-03-27 14:35:38 -07001269 ArtField* resolved_field = compiler_driver_->ResolveField(
1270 soa, dex_cache, class_loader, dex_compilation_unit_, field_index, true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001271
Mathieu Chartierc7853442015-03-27 14:35:38 -07001272 if (resolved_field == nullptr) {
Calin Juravle48c2b032014-12-09 18:11:36 +00001273 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedField);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001274 return false;
1275 }
1276
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001277 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
1278 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001279 outer_compilation_unit_->GetClassLinker()->FindDexCache(soa.Self(), outer_dex_file)));
Nicolas Geoffray30451742015-06-19 13:32:41 +01001280 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001281
1282 // The index at which the field's class is stored in the DexCache's type array.
1283 uint32_t storage_index;
Nicolas Geoffray30451742015-06-19 13:32:41 +01001284 bool is_outer_class = (outer_class.Get() == resolved_field->GetDeclaringClass());
1285 if (is_outer_class) {
1286 storage_index = outer_class->GetDexTypeIndex();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001287 } else if (outer_dex_cache.Get() != dex_cache.Get()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001288 // The compiler driver cannot currently understand multiple dex caches involved. Just bailout.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001289 return false;
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001290 } else {
1291 std::pair<bool, bool> pair = compiler_driver_->IsFastStaticField(
1292 outer_dex_cache.Get(),
Nicolas Geoffray30451742015-06-19 13:32:41 +01001293 GetCompilingClass(),
Mathieu Chartierc7853442015-03-27 14:35:38 -07001294 resolved_field,
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001295 field_index,
1296 &storage_index);
1297 bool can_easily_access = is_put ? pair.second : pair.first;
1298 if (!can_easily_access) {
1299 return false;
1300 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001301 }
1302
1303 // TODO: find out why this check is needed.
1304 bool is_in_dex_cache = compiler_driver_->CanAssumeTypeIsPresentInDexCache(
Nicolas Geoffray6a816cf2015-03-24 16:17:56 +00001305 *outer_compilation_unit_->GetDexFile(), storage_index);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001306 bool is_initialized = resolved_field->GetDeclaringClass()->IsInitialized() && is_in_dex_cache;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001307
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001308 HLoadClass* constant = new (arena_) HLoadClass(graph_->GetCurrentMethod(),
1309 storage_index,
1310 *dex_compilation_unit_->GetDexFile(),
Nicolas Geoffray30451742015-06-19 13:32:41 +01001311 is_outer_class,
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001312 dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001313 current_block_->AddInstruction(constant);
1314
1315 HInstruction* cls = constant;
Nicolas Geoffray30451742015-06-19 13:32:41 +01001316 if (!is_initialized && !is_outer_class) {
Calin Juravle225ff812014-11-13 16:46:39 +00001317 cls = new (arena_) HClinitCheck(constant, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001318 current_block_->AddInstruction(cls);
1319 }
1320
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001321 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001322 if (is_put) {
1323 // We need to keep the class alive before loading the value.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001324 Temporaries temps(graph_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001325 temps.Add(cls);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001326 HInstruction* value = LoadLocal(source_or_dest_reg, field_type, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001327 DCHECK_EQ(value->GetType(), field_type);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001328 current_block_->AddInstruction(new (arena_) HStaticFieldSet(cls,
1329 value,
1330 field_type,
1331 resolved_field->GetOffset(),
1332 resolved_field->IsVolatile(),
1333 field_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07001334 *dex_file_,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001335 dex_cache_,
1336 dex_pc));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001337 } else {
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001338 current_block_->AddInstruction(new (arena_) HStaticFieldGet(cls,
1339 field_type,
1340 resolved_field->GetOffset(),
1341 resolved_field->IsVolatile(),
1342 field_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07001343 *dex_file_,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001344 dex_cache_,
1345 dex_pc));
1346 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001347 }
1348 return true;
1349}
1350
Calin Juravlebacfec32014-11-14 15:54:36 +00001351void HGraphBuilder::BuildCheckedDivRem(uint16_t out_vreg,
1352 uint16_t first_vreg,
1353 int64_t second_vreg_or_constant,
1354 uint32_t dex_pc,
1355 Primitive::Type type,
1356 bool second_is_constant,
1357 bool isDiv) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001358 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Calin Juravled0d48522014-11-04 16:40:20 +00001359
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001360 HInstruction* first = LoadLocal(first_vreg, type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001361 HInstruction* second = nullptr;
1362 if (second_is_constant) {
1363 if (type == Primitive::kPrimInt) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001364 second = graph_->GetIntConstant(second_vreg_or_constant, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001365 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001366 second = graph_->GetLongConstant(second_vreg_or_constant, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001367 }
1368 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001369 second = LoadLocal(second_vreg_or_constant, type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001370 }
1371
1372 if (!second_is_constant
1373 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0)
1374 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) {
1375 second = new (arena_) HDivZeroCheck(second, dex_pc);
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001376 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +00001377 current_block_->AddInstruction(second);
1378 temps.Add(current_block_->GetLastInstruction());
1379 }
1380
Calin Juravlebacfec32014-11-14 15:54:36 +00001381 if (isDiv) {
1382 current_block_->AddInstruction(new (arena_) HDiv(type, first, second, dex_pc));
1383 } else {
1384 current_block_->AddInstruction(new (arena_) HRem(type, first, second, dex_pc));
1385 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001386 UpdateLocal(out_vreg, current_block_->GetLastInstruction(), dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001387}
1388
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001389void HGraphBuilder::BuildArrayAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001390 uint32_t dex_pc,
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001391 bool is_put,
1392 Primitive::Type anticipated_type) {
1393 uint8_t source_or_dest_reg = instruction.VRegA_23x();
1394 uint8_t array_reg = instruction.VRegB_23x();
1395 uint8_t index_reg = instruction.VRegC_23x();
1396
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001397 // We need one temporary for the null check, one for the index, and one for the length.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001398 Temporaries temps(graph_);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001399
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001400 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001401 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001402 current_block_->AddInstruction(object);
1403 temps.Add(object);
1404
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001405 HInstruction* length = new (arena_) HArrayLength(object, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001406 current_block_->AddInstruction(length);
1407 temps.Add(length);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001408 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001409 index = new (arena_) HBoundsCheck(index, length, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001410 current_block_->AddInstruction(index);
1411 temps.Add(index);
1412 if (is_put) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001413 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001414 // TODO: Insert a type check node if the type is Object.
Nicolas Geoffray39468442014-09-02 15:17:15 +01001415 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001416 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001417 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001418 current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type, dex_pc));
1419 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001420 }
Mark Mendell1152c922015-04-24 17:06:35 -04001421 graph_->SetHasBoundsChecks(true);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001422}
1423
Calin Juravle225ff812014-11-13 16:46:39 +00001424void HGraphBuilder::BuildFilledNewArray(uint32_t dex_pc,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001425 uint32_t type_index,
1426 uint32_t number_of_vreg_arguments,
1427 bool is_range,
1428 uint32_t* args,
1429 uint32_t register_index) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001430 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments, dex_pc);
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001431 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
1432 ? kQuickAllocArrayWithAccessCheck
1433 : kQuickAllocArray;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01001434 HInstruction* object = new (arena_) HNewArray(length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01001435 graph_->GetCurrentMethod(),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01001436 dex_pc,
1437 type_index,
1438 *dex_compilation_unit_->GetDexFile(),
1439 entrypoint);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001440 current_block_->AddInstruction(object);
1441
1442 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1443 DCHECK_EQ(descriptor[0], '[') << descriptor;
1444 char primitive = descriptor[1];
1445 DCHECK(primitive == 'I'
1446 || primitive == 'L'
1447 || primitive == '[') << descriptor;
1448 bool is_reference_array = (primitive == 'L') || (primitive == '[');
1449 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
1450
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001451 Temporaries temps(graph_);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001452 temps.Add(object);
1453 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001454 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type, dex_pc);
1455 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001456 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001457 new (arena_) HArraySet(object, index, value, type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001458 }
1459 latest_result_ = object;
1460}
1461
1462template <typename T>
1463void HGraphBuilder::BuildFillArrayData(HInstruction* object,
1464 const T* data,
1465 uint32_t element_count,
1466 Primitive::Type anticipated_type,
Calin Juravle225ff812014-11-13 16:46:39 +00001467 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001468 for (uint32_t i = 0; i < element_count; ++i) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001469 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1470 HInstruction* value = graph_->GetIntConstant(data[i], dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001471 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001472 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001473 }
1474}
1475
Calin Juravle225ff812014-11-13 16:46:39 +00001476void HGraphBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001477 Temporaries temps(graph_);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001478 HInstruction* array = LoadLocal(instruction.VRegA_31t(), Primitive::kPrimNot, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001479 HNullCheck* null_check = new (arena_) HNullCheck(array, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001480 current_block_->AddInstruction(null_check);
1481 temps.Add(null_check);
1482
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001483 HInstruction* length = new (arena_) HArrayLength(null_check, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001484 current_block_->AddInstruction(length);
1485
Calin Juravle225ff812014-11-13 16:46:39 +00001486 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
Calin Juravled0d48522014-11-04 16:40:20 +00001487 const Instruction::ArrayDataPayload* payload =
1488 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_start_ + payload_offset);
1489 const uint8_t* data = payload->data;
1490 uint32_t element_count = payload->element_count;
1491
1492 // Implementation of this DEX instruction seems to be that the bounds check is
1493 // done before doing any stores.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001494 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001495 current_block_->AddInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
Calin Juravled0d48522014-11-04 16:40:20 +00001496
1497 switch (payload->element_width) {
1498 case 1:
1499 BuildFillArrayData(null_check,
1500 reinterpret_cast<const int8_t*>(data),
1501 element_count,
1502 Primitive::kPrimByte,
Calin Juravle225ff812014-11-13 16:46:39 +00001503 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001504 break;
1505 case 2:
1506 BuildFillArrayData(null_check,
1507 reinterpret_cast<const int16_t*>(data),
1508 element_count,
1509 Primitive::kPrimShort,
Calin Juravle225ff812014-11-13 16:46:39 +00001510 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001511 break;
1512 case 4:
1513 BuildFillArrayData(null_check,
1514 reinterpret_cast<const int32_t*>(data),
1515 element_count,
1516 Primitive::kPrimInt,
Calin Juravle225ff812014-11-13 16:46:39 +00001517 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001518 break;
1519 case 8:
1520 BuildFillWideArrayData(null_check,
1521 reinterpret_cast<const int64_t*>(data),
1522 element_count,
Calin Juravle225ff812014-11-13 16:46:39 +00001523 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001524 break;
1525 default:
1526 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1527 }
Mark Mendell1152c922015-04-24 17:06:35 -04001528 graph_->SetHasBoundsChecks(true);
Calin Juravled0d48522014-11-04 16:40:20 +00001529}
1530
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001531void HGraphBuilder::BuildFillWideArrayData(HInstruction* object,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +01001532 const int64_t* data,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001533 uint32_t element_count,
Calin Juravle225ff812014-11-13 16:46:39 +00001534 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001535 for (uint32_t i = 0; i < element_count; ++i) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001536 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1537 HInstruction* value = graph_->GetLongConstant(data[i], dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001538 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001539 object, index, value, Primitive::kPrimLong, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001540 }
1541}
1542
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001543bool HGraphBuilder::BuildTypeCheck(const Instruction& instruction,
1544 uint8_t destination,
1545 uint8_t reference,
1546 uint16_t type_index,
Calin Juravle225ff812014-11-13 16:46:39 +00001547 uint32_t dex_pc) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001548 bool type_known_final;
1549 bool type_known_abstract;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001550 // `CanAccessTypeWithoutChecks` will tell whether the method being
1551 // built is trying to access its own class, so that the generated
1552 // code can optimize for this case. However, the optimization does not
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001553 // work for inlining, so we use `IsOutermostCompilingClass` instead.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001554 bool dont_use_is_referrers_class;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001555 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
1556 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001557 &type_known_final, &type_known_abstract, &dont_use_is_referrers_class);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001558 if (!can_access) {
Calin Juravle48c2b032014-12-09 18:11:36 +00001559 MaybeRecordStat(MethodCompilationStat::kNotCompiledCantAccesType);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001560 return false;
1561 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001562 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot, dex_pc);
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001563 HLoadClass* cls = new (arena_) HLoadClass(
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001564 graph_->GetCurrentMethod(),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01001565 type_index,
1566 *dex_compilation_unit_->GetDexFile(),
1567 IsOutermostCompilingClass(type_index),
1568 dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001569 current_block_->AddInstruction(cls);
1570 // The class needs a temporary before being used by the type check.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001571 Temporaries temps(graph_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001572 temps.Add(cls);
1573 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
1574 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001575 new (arena_) HInstanceOf(object, cls, type_known_final, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001576 UpdateLocal(destination, current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001577 } else {
1578 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
1579 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001580 new (arena_) HCheckCast(object, cls, type_known_final, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001581 }
1582 return true;
1583}
1584
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001585bool HGraphBuilder::NeedsAccessCheck(uint32_t type_index) const {
1586 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
1587 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index);
1588}
1589
Calin Juravle48c2b032014-12-09 18:11:36 +00001590void HGraphBuilder::BuildPackedSwitch(const Instruction& instruction, uint32_t dex_pc) {
David Brazdil2ef645b2015-06-17 18:20:52 +01001591 // Verifier guarantees that the payload for PackedSwitch contains:
1592 // (a) number of entries (may be zero)
1593 // (b) first and lowest switch case value (entry 0, always present)
1594 // (c) list of target pcs (entries 1 <= i <= N)
Andreas Gamped881df52014-11-24 23:28:39 -08001595 SwitchTable table(instruction, dex_pc, false);
1596
1597 // Value to test against.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001598 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt, dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08001599
David Brazdil2ef645b2015-06-17 18:20:52 +01001600 // Retrieve number of entries.
Andreas Gampee4d4d322014-12-04 09:09:57 -08001601 uint16_t num_entries = table.GetNumEntries();
David Brazdil2ef645b2015-06-17 18:20:52 +01001602 if (num_entries == 0) {
1603 return;
1604 }
Andreas Gampee4d4d322014-12-04 09:09:57 -08001605
Andreas Gamped881df52014-11-24 23:28:39 -08001606 // Chained cmp-and-branch, starting from starting_key.
1607 int32_t starting_key = table.GetEntryAt(0);
1608
Andreas Gamped881df52014-11-24 23:28:39 -08001609 for (size_t i = 1; i <= num_entries; i++) {
Andreas Gampee4d4d322014-12-04 09:09:57 -08001610 BuildSwitchCaseHelper(instruction, i, i == num_entries, table, value, starting_key + i - 1,
1611 table.GetEntryAt(i), dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08001612 }
Andreas Gamped881df52014-11-24 23:28:39 -08001613}
1614
Calin Juravle48c2b032014-12-09 18:11:36 +00001615void HGraphBuilder::BuildSparseSwitch(const Instruction& instruction, uint32_t dex_pc) {
David Brazdil2ef645b2015-06-17 18:20:52 +01001616 // Verifier guarantees that the payload for SparseSwitch contains:
1617 // (a) number of entries (may be zero)
1618 // (b) sorted key values (entries 0 <= i < N)
1619 // (c) target pcs corresponding to the switch values (entries N <= i < 2*N)
Andreas Gampee4d4d322014-12-04 09:09:57 -08001620 SwitchTable table(instruction, dex_pc, true);
1621
1622 // Value to test against.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001623 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001624
1625 uint16_t num_entries = table.GetNumEntries();
Andreas Gampee4d4d322014-12-04 09:09:57 -08001626
1627 for (size_t i = 0; i < num_entries; i++) {
1628 BuildSwitchCaseHelper(instruction, i, i == static_cast<size_t>(num_entries) - 1, table, value,
1629 table.GetEntryAt(i), table.GetEntryAt(i + num_entries), dex_pc);
1630 }
Andreas Gampee4d4d322014-12-04 09:09:57 -08001631}
1632
1633void HGraphBuilder::BuildSwitchCaseHelper(const Instruction& instruction, size_t index,
1634 bool is_last_case, const SwitchTable& table,
1635 HInstruction* value, int32_t case_value_int,
1636 int32_t target_offset, uint32_t dex_pc) {
David Brazdil852eaff2015-02-02 15:23:05 +00001637 HBasicBlock* case_target = FindBlockStartingAt(dex_pc + target_offset);
1638 DCHECK(case_target != nullptr);
1639 PotentiallyAddSuspendCheck(case_target, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001640
1641 // The current case's value.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001642 HInstruction* this_case_value = graph_->GetIntConstant(case_value_int, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001643
1644 // Compare value and this_case_value.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001645 HEqual* comparison = new (arena_) HEqual(value, this_case_value, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001646 current_block_->AddInstruction(comparison);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001647 HInstruction* ifinst = new (arena_) HIf(comparison, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001648 current_block_->AddInstruction(ifinst);
1649
1650 // Case hit: use the target offset to determine where to go.
Andreas Gampee4d4d322014-12-04 09:09:57 -08001651 current_block_->AddSuccessor(case_target);
1652
1653 // Case miss: go to the next case (or default fall-through).
1654 // When there is a next case, we use the block stored with the table offset representing this
1655 // case (that is where we registered them in ComputeBranchTargets).
1656 // When there is no next case, we use the following instruction.
1657 // TODO: Find a good way to peel the last iteration to avoid conditional, but still have re-use.
1658 if (!is_last_case) {
1659 HBasicBlock* next_case_target = FindBlockStartingAt(table.GetDexPcForIndex(index));
1660 DCHECK(next_case_target != nullptr);
1661 current_block_->AddSuccessor(next_case_target);
1662
1663 // Need to manually add the block, as there is no dex-pc transition for the cases.
1664 graph_->AddBlock(next_case_target);
1665
1666 current_block_ = next_case_target;
1667 } else {
1668 HBasicBlock* default_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
1669 DCHECK(default_target != nullptr);
1670 current_block_->AddSuccessor(default_target);
1671 current_block_ = nullptr;
1672 }
1673}
1674
David Brazdil852eaff2015-02-02 15:23:05 +00001675void HGraphBuilder::PotentiallyAddSuspendCheck(HBasicBlock* target, uint32_t dex_pc) {
1676 int32_t target_offset = target->GetDexPc() - dex_pc;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001677 if (target_offset <= 0) {
David Brazdil852eaff2015-02-02 15:23:05 +00001678 // DX generates back edges to the first encountered return. We can save
1679 // time of later passes by not adding redundant suspend checks.
David Brazdil2fd6aa52015-02-02 18:58:27 +00001680 HInstruction* last_in_target = target->GetLastInstruction();
1681 if (last_in_target != nullptr &&
1682 (last_in_target->IsReturn() || last_in_target->IsReturnVoid())) {
1683 return;
David Brazdil852eaff2015-02-02 15:23:05 +00001684 }
1685
1686 // Add a suspend check to backward branches which may potentially loop. We
1687 // can remove them after we recognize loops in the graph.
Calin Juravle225ff812014-11-13 16:46:39 +00001688 current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_pc));
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001689 }
1690}
1691
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001692bool HGraphBuilder::CanDecodeQuickenedInfo() const {
1693 return interpreter_metadata_ != nullptr;
1694}
1695
1696uint16_t HGraphBuilder::LookupQuickenedInfo(uint32_t dex_pc) {
1697 DCHECK(interpreter_metadata_ != nullptr);
1698 uint32_t dex_pc_in_map = DecodeUnsignedLeb128(&interpreter_metadata_);
1699 DCHECK_EQ(dex_pc, dex_pc_in_map);
1700 return DecodeUnsignedLeb128(&interpreter_metadata_);
1701}
1702
Calin Juravle225ff812014-11-13 16:46:39 +00001703bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001704 if (current_block_ == nullptr) {
1705 return true; // Dead code
1706 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001707
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001708 switch (instruction.Opcode()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001709 case Instruction::CONST_4: {
1710 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001711 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n(), dex_pc);
1712 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001713 break;
1714 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001715
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001716 case Instruction::CONST_16: {
1717 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001718 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s(), dex_pc);
1719 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001720 break;
1721 }
1722
Dave Allison20dfc792014-06-16 20:44:29 -07001723 case Instruction::CONST: {
1724 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001725 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i(), dex_pc);
1726 UpdateLocal(register_index, constant, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001727 break;
1728 }
1729
1730 case Instruction::CONST_HIGH16: {
1731 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001732 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16, dex_pc);
1733 UpdateLocal(register_index, constant, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001734 break;
1735 }
1736
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001737 case Instruction::CONST_WIDE_16: {
1738 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001739 // Get 16 bits of constant value, sign extended to 64 bits.
1740 int64_t value = instruction.VRegB_21s();
1741 value <<= 48;
1742 value >>= 48;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001743 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1744 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001745 break;
1746 }
1747
1748 case Instruction::CONST_WIDE_32: {
1749 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001750 // Get 32 bits of constant value, sign extended to 64 bits.
1751 int64_t value = instruction.VRegB_31i();
1752 value <<= 32;
1753 value >>= 32;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001754 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1755 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001756 break;
1757 }
1758
1759 case Instruction::CONST_WIDE: {
1760 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001761 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l(), dex_pc);
1762 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001763 break;
1764 }
1765
Dave Allison20dfc792014-06-16 20:44:29 -07001766 case Instruction::CONST_WIDE_HIGH16: {
1767 int32_t register_index = instruction.VRegA();
1768 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001769 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1770 UpdateLocal(register_index, constant, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001771 break;
1772 }
1773
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001774 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001775 case Instruction::MOVE:
1776 case Instruction::MOVE_FROM16:
1777 case Instruction::MOVE_16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001778 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
1779 UpdateLocal(instruction.VRegA(), value, dex_pc);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001780 break;
1781 }
1782
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001783 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001784 case Instruction::MOVE_WIDE:
1785 case Instruction::MOVE_WIDE_FROM16:
1786 case Instruction::MOVE_WIDE_16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001787 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong, dex_pc);
1788 UpdateLocal(instruction.VRegA(), value, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001789 break;
1790 }
1791
1792 case Instruction::MOVE_OBJECT:
1793 case Instruction::MOVE_OBJECT_16:
1794 case Instruction::MOVE_OBJECT_FROM16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001795 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot, dex_pc);
1796 UpdateLocal(instruction.VRegA(), value, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001797 break;
1798 }
1799
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001800 case Instruction::RETURN_VOID_NO_BARRIER:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001801 case Instruction::RETURN_VOID: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001802 BuildReturn(instruction, Primitive::kPrimVoid, dex_pc);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001803 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001804 }
1805
Dave Allison20dfc792014-06-16 20:44:29 -07001806#define IF_XX(comparison, cond) \
Calin Juravle225ff812014-11-13 16:46:39 +00001807 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
1808 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001809
Dave Allison20dfc792014-06-16 20:44:29 -07001810 IF_XX(HEqual, EQ);
1811 IF_XX(HNotEqual, NE);
1812 IF_XX(HLessThan, LT);
1813 IF_XX(HLessThanOrEqual, LE);
1814 IF_XX(HGreaterThan, GT);
1815 IF_XX(HGreaterThanOrEqual, GE);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001816
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001817 case Instruction::GOTO:
1818 case Instruction::GOTO_16:
1819 case Instruction::GOTO_32: {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001820 int32_t offset = instruction.GetTargetOffset();
Calin Juravle225ff812014-11-13 16:46:39 +00001821 HBasicBlock* target = FindBlockStartingAt(offset + dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001822 DCHECK(target != nullptr);
David Brazdil852eaff2015-02-02 15:23:05 +00001823 PotentiallyAddSuspendCheck(target, dex_pc);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001824 current_block_->AddInstruction(new (arena_) HGoto(dex_pc));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001825 current_block_->AddSuccessor(target);
1826 current_block_ = nullptr;
1827 break;
1828 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001829
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001830 case Instruction::RETURN: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001831 BuildReturn(instruction, return_type_, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001832 break;
1833 }
1834
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001835 case Instruction::RETURN_OBJECT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001836 BuildReturn(instruction, return_type_, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001837 break;
1838 }
1839
1840 case Instruction::RETURN_WIDE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001841 BuildReturn(instruction, return_type_, dex_pc);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001842 break;
1843 }
1844
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001845 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001846 case Instruction::INVOKE_INTERFACE:
1847 case Instruction::INVOKE_STATIC:
1848 case Instruction::INVOKE_SUPER:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001849 case Instruction::INVOKE_VIRTUAL:
1850 case Instruction::INVOKE_VIRTUAL_QUICK: {
1851 uint16_t method_idx;
1852 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_QUICK) {
1853 if (!CanDecodeQuickenedInfo()) {
1854 return false;
1855 }
1856 method_idx = LookupQuickenedInfo(dex_pc);
1857 } else {
1858 method_idx = instruction.VRegB_35c();
1859 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001860 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001861 uint32_t args[5];
Ian Rogers29a26482014-05-02 15:27:29 -07001862 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00001863 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001864 number_of_vreg_arguments, false, args, -1)) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001865 return false;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001866 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001867 break;
1868 }
1869
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001870 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001871 case Instruction::INVOKE_INTERFACE_RANGE:
1872 case Instruction::INVOKE_STATIC_RANGE:
1873 case Instruction::INVOKE_SUPER_RANGE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001874 case Instruction::INVOKE_VIRTUAL_RANGE:
1875 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
1876 uint16_t method_idx;
1877 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK) {
1878 if (!CanDecodeQuickenedInfo()) {
1879 return false;
1880 }
1881 method_idx = LookupQuickenedInfo(dex_pc);
1882 } else {
1883 method_idx = instruction.VRegB_3rc();
1884 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001885 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1886 uint32_t register_index = instruction.VRegC();
Calin Juravle225ff812014-11-13 16:46:39 +00001887 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001888 number_of_vreg_arguments, true, nullptr, register_index)) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001889 return false;
1890 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001891 break;
1892 }
1893
Roland Levillain88cb1752014-10-20 16:36:47 +01001894 case Instruction::NEG_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001895 Unop_12x<HNeg>(instruction, Primitive::kPrimInt, dex_pc);
Roland Levillain88cb1752014-10-20 16:36:47 +01001896 break;
1897 }
1898
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001899 case Instruction::NEG_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001900 Unop_12x<HNeg>(instruction, Primitive::kPrimLong, dex_pc);
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001901 break;
1902 }
1903
Roland Levillain3dbcb382014-10-28 17:30:07 +00001904 case Instruction::NEG_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001905 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat, dex_pc);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001906 break;
1907 }
1908
1909 case Instruction::NEG_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001910 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble, dex_pc);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001911 break;
1912 }
1913
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001914 case Instruction::NOT_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001915 Unop_12x<HNot>(instruction, Primitive::kPrimInt, dex_pc);
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001916 break;
1917 }
1918
Roland Levillain70566432014-10-24 16:20:17 +01001919 case Instruction::NOT_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001920 Unop_12x<HNot>(instruction, Primitive::kPrimLong, dex_pc);
Roland Levillain70566432014-10-24 16:20:17 +01001921 break;
1922 }
1923
Roland Levillaindff1f282014-11-05 14:15:05 +00001924 case Instruction::INT_TO_LONG: {
Roland Levillain624279f2014-12-04 11:54:28 +00001925 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong, dex_pc);
Roland Levillaindff1f282014-11-05 14:15:05 +00001926 break;
1927 }
1928
Roland Levillaincff13742014-11-17 14:32:17 +00001929 case Instruction::INT_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001930 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00001931 break;
1932 }
1933
1934 case Instruction::INT_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001935 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00001936 break;
1937 }
1938
Roland Levillain946e1432014-11-11 17:35:19 +00001939 case Instruction::LONG_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001940 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt, dex_pc);
Roland Levillain946e1432014-11-11 17:35:19 +00001941 break;
1942 }
1943
Roland Levillain6d0e4832014-11-27 18:31:21 +00001944 case Instruction::LONG_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001945 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimFloat, dex_pc);
Roland Levillain6d0e4832014-11-27 18:31:21 +00001946 break;
1947 }
1948
Roland Levillain647b9ed2014-11-27 12:06:00 +00001949 case Instruction::LONG_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001950 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble, dex_pc);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001951 break;
1952 }
1953
Roland Levillain3f8f9362014-12-02 17:45:01 +00001954 case Instruction::FLOAT_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001955 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimInt, dex_pc);
1956 break;
1957 }
1958
1959 case Instruction::FLOAT_TO_LONG: {
1960 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimLong, dex_pc);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001961 break;
1962 }
1963
Roland Levillain8964e2b2014-12-04 12:10:50 +00001964 case Instruction::FLOAT_TO_DOUBLE: {
1965 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimDouble, dex_pc);
1966 break;
1967 }
1968
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001969 case Instruction::DOUBLE_TO_INT: {
1970 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimInt, dex_pc);
1971 break;
1972 }
1973
1974 case Instruction::DOUBLE_TO_LONG: {
1975 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimLong, dex_pc);
1976 break;
1977 }
1978
Roland Levillain8964e2b2014-12-04 12:10:50 +00001979 case Instruction::DOUBLE_TO_FLOAT: {
1980 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimFloat, dex_pc);
1981 break;
1982 }
1983
Roland Levillain51d3fc42014-11-13 14:11:42 +00001984 case Instruction::INT_TO_BYTE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001985 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte, dex_pc);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001986 break;
1987 }
1988
Roland Levillain01a8d712014-11-14 16:27:39 +00001989 case Instruction::INT_TO_SHORT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001990 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort, dex_pc);
Roland Levillain01a8d712014-11-14 16:27:39 +00001991 break;
1992 }
1993
Roland Levillain981e4542014-11-14 11:47:14 +00001994 case Instruction::INT_TO_CHAR: {
Roland Levillain624279f2014-12-04 11:54:28 +00001995 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar, dex_pc);
Roland Levillain981e4542014-11-14 11:47:14 +00001996 break;
1997 }
1998
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001999 case Instruction::ADD_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002000 Binop_23x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002001 break;
2002 }
2003
2004 case Instruction::ADD_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002005 Binop_23x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002006 break;
2007 }
2008
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002009 case Instruction::ADD_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002010 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002011 break;
2012 }
2013
2014 case Instruction::ADD_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002015 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002016 break;
2017 }
2018
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002019 case Instruction::SUB_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002020 Binop_23x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002021 break;
2022 }
2023
2024 case Instruction::SUB_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002025 Binop_23x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002026 break;
2027 }
2028
Calin Juravle096cc022014-10-23 17:01:13 +01002029 case Instruction::SUB_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002030 Binop_23x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01002031 break;
2032 }
2033
2034 case Instruction::SUB_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002035 Binop_23x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01002036 break;
2037 }
2038
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002039 case Instruction::ADD_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002040 Binop_12x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002041 break;
2042 }
2043
Calin Juravle34bacdf2014-10-07 20:23:36 +01002044 case Instruction::MUL_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002045 Binop_23x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002046 break;
2047 }
2048
2049 case Instruction::MUL_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002050 Binop_23x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002051 break;
2052 }
2053
Calin Juravleb5bfa962014-10-21 18:02:24 +01002054 case Instruction::MUL_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002055 Binop_23x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01002056 break;
2057 }
2058
2059 case Instruction::MUL_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002060 Binop_23x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01002061 break;
2062 }
2063
Calin Juravled0d48522014-11-04 16:40:20 +00002064 case Instruction::DIV_INT: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002065 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2066 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravled0d48522014-11-04 16:40:20 +00002067 break;
2068 }
2069
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002070 case Instruction::DIV_LONG: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002071 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2072 dex_pc, Primitive::kPrimLong, false, true);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002073 break;
2074 }
2075
Calin Juravle7c4954d2014-10-28 16:57:40 +00002076 case Instruction::DIV_FLOAT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002077 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002078 break;
2079 }
2080
2081 case Instruction::DIV_DOUBLE: {
Calin Juravle225ff812014-11-13 16:46:39 +00002082 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002083 break;
2084 }
2085
Calin Juravlebacfec32014-11-14 15:54:36 +00002086 case Instruction::REM_INT: {
2087 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2088 dex_pc, Primitive::kPrimInt, false, false);
2089 break;
2090 }
2091
2092 case Instruction::REM_LONG: {
2093 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2094 dex_pc, Primitive::kPrimLong, false, false);
2095 break;
2096 }
2097
Calin Juravled2ec87d2014-12-08 14:24:46 +00002098 case Instruction::REM_FLOAT: {
2099 Binop_23x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2100 break;
2101 }
2102
2103 case Instruction::REM_DOUBLE: {
2104 Binop_23x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2105 break;
2106 }
2107
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002108 case Instruction::AND_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002109 Binop_23x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002110 break;
2111 }
2112
2113 case Instruction::AND_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002114 Binop_23x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002115 break;
2116 }
2117
Calin Juravle9aec02f2014-11-18 23:06:35 +00002118 case Instruction::SHL_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002119 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002120 break;
2121 }
2122
2123 case Instruction::SHL_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002124 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002125 break;
2126 }
2127
2128 case Instruction::SHR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002129 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002130 break;
2131 }
2132
2133 case Instruction::SHR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002134 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002135 break;
2136 }
2137
2138 case Instruction::USHR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002139 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002140 break;
2141 }
2142
2143 case Instruction::USHR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002144 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002145 break;
2146 }
2147
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002148 case Instruction::OR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002149 Binop_23x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002150 break;
2151 }
2152
2153 case Instruction::OR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002154 Binop_23x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002155 break;
2156 }
2157
2158 case Instruction::XOR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002159 Binop_23x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002160 break;
2161 }
2162
2163 case Instruction::XOR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002164 Binop_23x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002165 break;
2166 }
2167
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002168 case Instruction::ADD_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002169 Binop_12x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002170 break;
2171 }
2172
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002173 case Instruction::ADD_DOUBLE_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002174 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002175 break;
2176 }
2177
2178 case Instruction::ADD_FLOAT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002179 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002180 break;
2181 }
2182
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002183 case Instruction::SUB_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002184 Binop_12x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002185 break;
2186 }
2187
2188 case Instruction::SUB_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002189 Binop_12x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002190 break;
2191 }
2192
Calin Juravle096cc022014-10-23 17:01:13 +01002193 case Instruction::SUB_FLOAT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002194 Binop_12x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01002195 break;
2196 }
2197
2198 case Instruction::SUB_DOUBLE_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002199 Binop_12x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01002200 break;
2201 }
2202
Calin Juravle34bacdf2014-10-07 20:23:36 +01002203 case Instruction::MUL_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002204 Binop_12x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002205 break;
2206 }
2207
2208 case Instruction::MUL_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002209 Binop_12x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002210 break;
2211 }
2212
Calin Juravleb5bfa962014-10-21 18:02:24 +01002213 case Instruction::MUL_FLOAT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002214 Binop_12x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01002215 break;
2216 }
2217
2218 case Instruction::MUL_DOUBLE_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002219 Binop_12x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01002220 break;
2221 }
2222
Calin Juravle865fc882014-11-06 17:09:03 +00002223 case Instruction::DIV_INT_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002224 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2225 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravle865fc882014-11-06 17:09:03 +00002226 break;
2227 }
2228
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002229 case Instruction::DIV_LONG_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002230 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2231 dex_pc, Primitive::kPrimLong, false, true);
2232 break;
2233 }
2234
2235 case Instruction::REM_INT_2ADDR: {
2236 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2237 dex_pc, Primitive::kPrimInt, false, false);
2238 break;
2239 }
2240
2241 case Instruction::REM_LONG_2ADDR: {
2242 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2243 dex_pc, Primitive::kPrimLong, false, false);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002244 break;
2245 }
2246
Calin Juravled2ec87d2014-12-08 14:24:46 +00002247 case Instruction::REM_FLOAT_2ADDR: {
2248 Binop_12x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2249 break;
2250 }
2251
2252 case Instruction::REM_DOUBLE_2ADDR: {
2253 Binop_12x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2254 break;
2255 }
2256
Calin Juravle9aec02f2014-11-18 23:06:35 +00002257 case Instruction::SHL_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002258 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002259 break;
2260 }
2261
2262 case Instruction::SHL_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002263 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002264 break;
2265 }
2266
2267 case Instruction::SHR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002268 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002269 break;
2270 }
2271
2272 case Instruction::SHR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002273 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002274 break;
2275 }
2276
2277 case Instruction::USHR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002278 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002279 break;
2280 }
2281
2282 case Instruction::USHR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002283 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002284 break;
2285 }
2286
Calin Juravle7c4954d2014-10-28 16:57:40 +00002287 case Instruction::DIV_FLOAT_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00002288 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002289 break;
2290 }
2291
2292 case Instruction::DIV_DOUBLE_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00002293 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002294 break;
2295 }
2296
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002297 case Instruction::AND_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002298 Binop_12x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002299 break;
2300 }
2301
2302 case Instruction::AND_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002303 Binop_12x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002304 break;
2305 }
2306
2307 case Instruction::OR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002308 Binop_12x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002309 break;
2310 }
2311
2312 case Instruction::OR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002313 Binop_12x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002314 break;
2315 }
2316
2317 case Instruction::XOR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002318 Binop_12x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002319 break;
2320 }
2321
2322 case Instruction::XOR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002323 Binop_12x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002324 break;
2325 }
2326
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002327 case Instruction::ADD_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002328 Binop_22s<HAdd>(instruction, false, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002329 break;
2330 }
2331
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002332 case Instruction::AND_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002333 Binop_22s<HAnd>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002334 break;
2335 }
2336
2337 case Instruction::OR_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002338 Binop_22s<HOr>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002339 break;
2340 }
2341
2342 case Instruction::XOR_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002343 Binop_22s<HXor>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002344 break;
2345 }
2346
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002347 case Instruction::RSUB_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002348 Binop_22s<HSub>(instruction, true, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002349 break;
2350 }
2351
Calin Juravle34bacdf2014-10-07 20:23:36 +01002352 case Instruction::MUL_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002353 Binop_22s<HMul>(instruction, false, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002354 break;
2355 }
2356
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002357 case Instruction::ADD_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002358 Binop_22b<HAdd>(instruction, false, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002359 break;
2360 }
2361
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002362 case Instruction::AND_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002363 Binop_22b<HAnd>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002364 break;
2365 }
2366
2367 case Instruction::OR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002368 Binop_22b<HOr>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002369 break;
2370 }
2371
2372 case Instruction::XOR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002373 Binop_22b<HXor>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002374 break;
2375 }
2376
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002377 case Instruction::RSUB_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002378 Binop_22b<HSub>(instruction, true, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002379 break;
2380 }
2381
Calin Juravle34bacdf2014-10-07 20:23:36 +01002382 case Instruction::MUL_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002383 Binop_22b<HMul>(instruction, false, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002384 break;
2385 }
2386
Calin Juravled0d48522014-11-04 16:40:20 +00002387 case Instruction::DIV_INT_LIT16:
2388 case Instruction::DIV_INT_LIT8: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002389 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2390 dex_pc, Primitive::kPrimInt, true, true);
2391 break;
2392 }
2393
2394 case Instruction::REM_INT_LIT16:
2395 case Instruction::REM_INT_LIT8: {
2396 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2397 dex_pc, Primitive::kPrimInt, true, false);
Calin Juravled0d48522014-11-04 16:40:20 +00002398 break;
2399 }
2400
Calin Juravle9aec02f2014-11-18 23:06:35 +00002401 case Instruction::SHL_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002402 Binop_22b<HShl>(instruction, false, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002403 break;
2404 }
2405
2406 case Instruction::SHR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002407 Binop_22b<HShr>(instruction, false, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002408 break;
2409 }
2410
2411 case Instruction::USHR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002412 Binop_22b<HUShr>(instruction, false, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002413 break;
2414 }
2415
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002416 case Instruction::NEW_INSTANCE: {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002417 uint16_t type_index = instruction.VRegB_21c();
Jeff Hao848f70a2014-01-15 13:49:50 -08002418 if (compiler_driver_->IsStringTypeIndex(type_index, dex_file_)) {
Jeff Hao848f70a2014-01-15 13:49:50 -08002419 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002420 HFakeString* fake_string = new (arena_) HFakeString(dex_pc);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002421 current_block_->AddInstruction(fake_string);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002422 UpdateLocal(register_index, fake_string, dex_pc);
Jeff Hao848f70a2014-01-15 13:49:50 -08002423 } else {
2424 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
2425 ? kQuickAllocObjectWithAccessCheck
2426 : kQuickAllocObject;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002427
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002428 current_block_->AddInstruction(new (arena_) HNewInstance(
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002429 graph_->GetCurrentMethod(),
2430 dex_pc,
2431 type_index,
2432 *dex_compilation_unit_->GetDexFile(),
2433 entrypoint));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002434 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Jeff Hao848f70a2014-01-15 13:49:50 -08002435 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002436 break;
2437 }
2438
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002439 case Instruction::NEW_ARRAY: {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002440 uint16_t type_index = instruction.VRegC_22c();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002441 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt, dex_pc);
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002442 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
2443 ? kQuickAllocArrayWithAccessCheck
2444 : kQuickAllocArray;
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002445 current_block_->AddInstruction(new (arena_) HNewArray(length,
2446 graph_->GetCurrentMethod(),
2447 dex_pc,
2448 type_index,
2449 *dex_compilation_unit_->GetDexFile(),
2450 entrypoint));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002451 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002452 break;
2453 }
2454
2455 case Instruction::FILLED_NEW_ARRAY: {
2456 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
2457 uint32_t type_index = instruction.VRegB_35c();
2458 uint32_t args[5];
2459 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00002460 BuildFilledNewArray(dex_pc, type_index, number_of_vreg_arguments, false, args, 0);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002461 break;
2462 }
2463
2464 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2465 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
2466 uint32_t type_index = instruction.VRegB_3rc();
2467 uint32_t register_index = instruction.VRegC_3rc();
2468 BuildFilledNewArray(
Calin Juravle225ff812014-11-13 16:46:39 +00002469 dex_pc, type_index, number_of_vreg_arguments, true, nullptr, register_index);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002470 break;
2471 }
2472
2473 case Instruction::FILL_ARRAY_DATA: {
Calin Juravle225ff812014-11-13 16:46:39 +00002474 BuildFillArrayData(instruction, dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002475 break;
2476 }
2477
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002478 case Instruction::MOVE_RESULT:
Dave Allison20dfc792014-06-16 20:44:29 -07002479 case Instruction::MOVE_RESULT_WIDE:
David Brazdilfc6a86a2015-06-26 10:33:45 +00002480 case Instruction::MOVE_RESULT_OBJECT: {
Nicolas Geoffray1efcc222015-06-24 12:41:20 +01002481 if (latest_result_ == nullptr) {
2482 // Only dead code can lead to this situation, where the verifier
2483 // does not reject the method.
2484 } else {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002485 // An Invoke/FilledNewArray and its MoveResult could have landed in
2486 // different blocks if there was a try/catch block boundary between
2487 // them. For Invoke, we insert a StoreLocal after the instruction. For
2488 // FilledNewArray, the local needs to be updated after the array was
2489 // filled, otherwise we might overwrite an input vreg.
2490 HStoreLocal* update_local =
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002491 new (arena_) HStoreLocal(GetLocalAt(instruction.VRegA()), latest_result_, dex_pc);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002492 HBasicBlock* block = latest_result_->GetBlock();
2493 if (block == current_block_) {
2494 // MoveResult and the previous instruction are in the same block.
2495 current_block_->AddInstruction(update_local);
2496 } else {
2497 // The two instructions are in different blocks. Insert the MoveResult
2498 // before the final control-flow instruction of the previous block.
2499 DCHECK(block->EndsWithControlFlowInstruction());
2500 DCHECK(current_block_->GetInstructions().IsEmpty());
2501 block->InsertInstructionBefore(update_local, block->GetLastInstruction());
2502 }
Nicolas Geoffray1efcc222015-06-24 12:41:20 +01002503 latest_result_ = nullptr;
2504 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002505 break;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002506 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002507
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002508 case Instruction::CMP_LONG: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002509 Binop_23x_cmp(instruction, Primitive::kPrimLong, ComparisonBias::kNoBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002510 break;
2511 }
2512
2513 case Instruction::CMPG_FLOAT: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002514 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kGtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002515 break;
2516 }
2517
2518 case Instruction::CMPG_DOUBLE: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002519 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kGtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002520 break;
2521 }
2522
2523 case Instruction::CMPL_FLOAT: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002524 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kLtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002525 break;
2526 }
2527
2528 case Instruction::CMPL_DOUBLE: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002529 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kLtBias, dex_pc);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002530 break;
2531 }
2532
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002533 case Instruction::NOP:
2534 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002535
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002536 case Instruction::IGET:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002537 case Instruction::IGET_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002538 case Instruction::IGET_WIDE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002539 case Instruction::IGET_WIDE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002540 case Instruction::IGET_OBJECT:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002541 case Instruction::IGET_OBJECT_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002542 case Instruction::IGET_BOOLEAN:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002543 case Instruction::IGET_BOOLEAN_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002544 case Instruction::IGET_BYTE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002545 case Instruction::IGET_BYTE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002546 case Instruction::IGET_CHAR:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002547 case Instruction::IGET_CHAR_QUICK:
2548 case Instruction::IGET_SHORT:
2549 case Instruction::IGET_SHORT_QUICK: {
Calin Juravle225ff812014-11-13 16:46:39 +00002550 if (!BuildInstanceFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002551 return false;
2552 }
2553 break;
2554 }
2555
2556 case Instruction::IPUT:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002557 case Instruction::IPUT_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002558 case Instruction::IPUT_WIDE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002559 case Instruction::IPUT_WIDE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002560 case Instruction::IPUT_OBJECT:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002561 case Instruction::IPUT_OBJECT_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002562 case Instruction::IPUT_BOOLEAN:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002563 case Instruction::IPUT_BOOLEAN_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002564 case Instruction::IPUT_BYTE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002565 case Instruction::IPUT_BYTE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002566 case Instruction::IPUT_CHAR:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002567 case Instruction::IPUT_CHAR_QUICK:
2568 case Instruction::IPUT_SHORT:
2569 case Instruction::IPUT_SHORT_QUICK: {
Calin Juravle225ff812014-11-13 16:46:39 +00002570 if (!BuildInstanceFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002571 return false;
2572 }
2573 break;
2574 }
2575
2576 case Instruction::SGET:
2577 case Instruction::SGET_WIDE:
2578 case Instruction::SGET_OBJECT:
2579 case Instruction::SGET_BOOLEAN:
2580 case Instruction::SGET_BYTE:
2581 case Instruction::SGET_CHAR:
2582 case Instruction::SGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002583 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002584 return false;
2585 }
2586 break;
2587 }
2588
2589 case Instruction::SPUT:
2590 case Instruction::SPUT_WIDE:
2591 case Instruction::SPUT_OBJECT:
2592 case Instruction::SPUT_BOOLEAN:
2593 case Instruction::SPUT_BYTE:
2594 case Instruction::SPUT_CHAR:
2595 case Instruction::SPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002596 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002597 return false;
2598 }
2599 break;
2600 }
2601
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002602#define ARRAY_XX(kind, anticipated_type) \
2603 case Instruction::AGET##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00002604 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002605 break; \
2606 } \
2607 case Instruction::APUT##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00002608 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002609 break; \
2610 }
2611
2612 ARRAY_XX(, Primitive::kPrimInt);
2613 ARRAY_XX(_WIDE, Primitive::kPrimLong);
2614 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
2615 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
2616 ARRAY_XX(_BYTE, Primitive::kPrimByte);
2617 ARRAY_XX(_CHAR, Primitive::kPrimChar);
2618 ARRAY_XX(_SHORT, Primitive::kPrimShort);
2619
Nicolas Geoffray39468442014-09-02 15:17:15 +01002620 case Instruction::ARRAY_LENGTH: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002621 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot, dex_pc);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002622 // No need for a temporary for the null check, it is the only input of the following
2623 // instruction.
Calin Juravle225ff812014-11-13 16:46:39 +00002624 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002625 current_block_->AddInstruction(object);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002626 current_block_->AddInstruction(new (arena_) HArrayLength(object, dex_pc));
2627 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002628 break;
2629 }
2630
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002631 case Instruction::CONST_STRING: {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002632 current_block_->AddInstruction(
2633 new (arena_) HLoadString(graph_->GetCurrentMethod(), instruction.VRegB_21c(), dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002634 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002635 break;
2636 }
2637
2638 case Instruction::CONST_STRING_JUMBO: {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002639 current_block_->AddInstruction(
2640 new (arena_) HLoadString(graph_->GetCurrentMethod(), instruction.VRegB_31c(), dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002641 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002642 break;
2643 }
2644
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002645 case Instruction::CONST_CLASS: {
2646 uint16_t type_index = instruction.VRegB_21c();
2647 bool type_known_final;
2648 bool type_known_abstract;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002649 bool dont_use_is_referrers_class;
2650 // `CanAccessTypeWithoutChecks` will tell whether the method being
2651 // built is trying to access its own class, so that the generated
2652 // code can optimize for this case. However, the optimization does not
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002653 // work for inlining, so we use `IsOutermostCompilingClass` instead.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002654 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
2655 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002656 &type_known_final, &type_known_abstract, &dont_use_is_referrers_class);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002657 if (!can_access) {
Calin Juravle48c2b032014-12-09 18:11:36 +00002658 MaybeRecordStat(MethodCompilationStat::kNotCompiledCantAccesType);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002659 return false;
2660 }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002661 current_block_->AddInstruction(new (arena_) HLoadClass(
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002662 graph_->GetCurrentMethod(),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002663 type_index,
2664 *dex_compilation_unit_->GetDexFile(),
2665 IsOutermostCompilingClass(type_index),
2666 dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002667 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002668 break;
2669 }
2670
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002671 case Instruction::MOVE_EXCEPTION: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002672 current_block_->AddInstruction(new (arena_) HLoadException(dex_pc));
2673 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction(), dex_pc);
2674 current_block_->AddInstruction(new (arena_) HClearException(dex_pc));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002675 break;
2676 }
2677
2678 case Instruction::THROW: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002679 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00002680 current_block_->AddInstruction(new (arena_) HThrow(exception, dex_pc));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002681 // A throw instruction must branch to the exit block.
2682 current_block_->AddSuccessor(exit_block_);
2683 // We finished building this block. Set the current block to null to avoid
2684 // adding dead instructions to it.
2685 current_block_ = nullptr;
2686 break;
2687 }
2688
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002689 case Instruction::INSTANCE_OF: {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002690 uint8_t destination = instruction.VRegA_22c();
2691 uint8_t reference = instruction.VRegB_22c();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002692 uint16_t type_index = instruction.VRegC_22c();
Calin Juravle225ff812014-11-13 16:46:39 +00002693 if (!BuildTypeCheck(instruction, destination, reference, type_index, dex_pc)) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002694 return false;
2695 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002696 break;
2697 }
2698
2699 case Instruction::CHECK_CAST: {
2700 uint8_t reference = instruction.VRegA_21c();
2701 uint16_t type_index = instruction.VRegB_21c();
Calin Juravle225ff812014-11-13 16:46:39 +00002702 if (!BuildTypeCheck(instruction, -1, reference, type_index, dex_pc)) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002703 return false;
2704 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002705 break;
2706 }
2707
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002708 case Instruction::MONITOR_ENTER: {
2709 current_block_->AddInstruction(new (arena_) HMonitorOperation(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002710 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot, dex_pc),
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002711 HMonitorOperation::kEnter,
Calin Juravle225ff812014-11-13 16:46:39 +00002712 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002713 break;
2714 }
2715
2716 case Instruction::MONITOR_EXIT: {
2717 current_block_->AddInstruction(new (arena_) HMonitorOperation(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002718 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot, dex_pc),
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002719 HMonitorOperation::kExit,
Calin Juravle225ff812014-11-13 16:46:39 +00002720 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002721 break;
2722 }
2723
Andreas Gamped881df52014-11-24 23:28:39 -08002724 case Instruction::PACKED_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002725 BuildPackedSwitch(instruction, dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08002726 break;
2727 }
2728
Andreas Gampee4d4d322014-12-04 09:09:57 -08002729 case Instruction::SPARSE_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002730 BuildSparseSwitch(instruction, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08002731 break;
2732 }
2733
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002734 default:
Calin Juravle48c2b032014-12-09 18:11:36 +00002735 VLOG(compiler) << "Did not compile "
2736 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
2737 << " because of unhandled instruction "
2738 << instruction.Name();
2739 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnhandledInstruction);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002740 return false;
2741 }
2742 return true;
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00002743} // NOLINT(readability/fn_size)
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002744
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002745HLocal* HGraphBuilder::GetLocalAt(int register_index) const {
2746 return locals_.Get(register_index);
2747}
2748
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002749void HGraphBuilder::UpdateLocal(int register_index,
2750 HInstruction* instruction,
2751 uint32_t dex_pc) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002752 HLocal* local = GetLocalAt(register_index);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002753 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction, dex_pc));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002754}
2755
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002756HInstruction* HGraphBuilder::LoadLocal(int register_index,
2757 Primitive::Type type,
2758 uint32_t dex_pc) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002759 HLocal* local = GetLocalAt(register_index);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002760 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type, dex_pc));
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002761 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002762}
2763
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002764} // namespace art