blob: c7430e7eb694e90a41ab6cc95804d09d7f7781a0 [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"
David Srbecky0cf44932015-12-09 14:09:59 +000020#include "base/arena_bit_vector.h"
21#include "base/bit_vector-inl.h"
Andreas Gamped881df52014-11-24 23:28:39 -080022#include "base/logging.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010023#include "class_linker.h"
Jeff Hao848f70a2014-01-15 13:49:50 -080024#include "dex/verified_method.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000025#include "dex_file-inl.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000026#include "dex_instruction-inl.h"
Roland Levillain4c0eb422015-04-24 16:43:49 +010027#include "dex/verified_method.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010028#include "driver/compiler_driver-inl.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000029#include "driver/compiler_options.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010030#include "mirror/class_loader.h"
31#include "mirror/dex_cache.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000032#include "nodes.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000033#include "primitive.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010034#include "scoped_thread_state_change.h"
35#include "thread.h"
Vladimir Marko58155012015-08-19 12:49:41 +000036#include "utils/dex_cache_arrays_layout-inl.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000037
38namespace art {
39
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010040/**
41 * Helper class to add HTemporary instructions. This class is used when
42 * converting a DEX instruction to multiple HInstruction, and where those
43 * instructions do not die at the following instruction, but instead spans
44 * multiple instructions.
45 */
46class Temporaries : public ValueObject {
47 public:
Calin Juravlef97f9fb2014-11-11 15:38:19 +000048 explicit Temporaries(HGraph* graph) : graph_(graph), index_(0) {}
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010049
50 void Add(HInstruction* instruction) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060051 HInstruction* temp = new (graph_->GetArena()) HTemporary(index_, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010052 instruction->GetBlock()->AddInstruction(temp);
Calin Juravlef97f9fb2014-11-11 15:38:19 +000053
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010054 DCHECK(temp->GetPrevious() == instruction);
Calin Juravlef97f9fb2014-11-11 15:38:19 +000055
56 size_t offset;
57 if (instruction->GetType() == Primitive::kPrimLong
58 || instruction->GetType() == Primitive::kPrimDouble) {
59 offset = 2;
60 } else {
61 offset = 1;
62 }
63 index_ += offset;
64
65 graph_->UpdateTemporariesVRegSlots(index_);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010066 }
67
68 private:
69 HGraph* const graph_;
70
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010071 // Current index in the temporary stack, updated by `Add`.
72 size_t index_;
73};
74
Andreas Gamped881df52014-11-24 23:28:39 -080075class SwitchTable : public ValueObject {
76 public:
77 SwitchTable(const Instruction& instruction, uint32_t dex_pc, bool sparse)
78 : instruction_(instruction), dex_pc_(dex_pc), sparse_(sparse) {
79 int32_t table_offset = instruction.VRegB_31t();
80 const uint16_t* table = reinterpret_cast<const uint16_t*>(&instruction) + table_offset;
81 if (sparse) {
82 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
83 } else {
84 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
85 }
86 num_entries_ = table[1];
87 values_ = reinterpret_cast<const int32_t*>(&table[2]);
88 }
89
90 uint16_t GetNumEntries() const {
91 return num_entries_;
92 }
93
Andreas Gampee4d4d322014-12-04 09:09:57 -080094 void CheckIndex(size_t index) const {
95 if (sparse_) {
96 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
97 DCHECK_LT(index, 2 * static_cast<size_t>(num_entries_));
98 } else {
99 // In a packed table, we have the starting key and num_entries_ values.
100 DCHECK_LT(index, 1 + static_cast<size_t>(num_entries_));
101 }
102 }
103
Andreas Gamped881df52014-11-24 23:28:39 -0800104 int32_t GetEntryAt(size_t index) const {
Andreas Gampee4d4d322014-12-04 09:09:57 -0800105 CheckIndex(index);
Andreas Gamped881df52014-11-24 23:28:39 -0800106 return values_[index];
107 }
108
109 uint32_t GetDexPcForIndex(size_t index) const {
Andreas Gampee4d4d322014-12-04 09:09:57 -0800110 CheckIndex(index);
Andreas Gamped881df52014-11-24 23:28:39 -0800111 return dex_pc_ +
112 (reinterpret_cast<const int16_t*>(values_ + index) -
113 reinterpret_cast<const int16_t*>(&instruction_));
114 }
115
Andreas Gampee4d4d322014-12-04 09:09:57 -0800116 // Index of the first value in the table.
117 size_t GetFirstValueIndex() const {
118 if (sparse_) {
119 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
120 return num_entries_;
121 } else {
122 // In a packed table, we have the starting key and num_entries_ values.
123 return 1;
124 }
125 }
126
Andreas Gamped881df52014-11-24 23:28:39 -0800127 private:
128 const Instruction& instruction_;
129 const uint32_t dex_pc_;
130
131 // Whether this is a sparse-switch table (or a packed-switch one).
132 const bool sparse_;
133
134 // This can't be const as it needs to be computed off of the given instruction, and complicated
135 // expressions in the initializer list seemed very ugly.
136 uint16_t num_entries_;
137
138 const int32_t* values_;
139
140 DISALLOW_COPY_AND_ASSIGN(SwitchTable);
141};
142
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100143void HGraphBuilder::InitializeLocals(uint16_t count) {
144 graph_->SetNumberOfVRegs(count);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100145 locals_.resize(count);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000146 for (int i = 0; i < count; i++) {
147 HLocal* local = new (arena_) HLocal(i);
148 entry_block_->AddInstruction(local);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100149 locals_[i] = local;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000150 }
151}
152
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000153void HGraphBuilder::InitializeParameters(uint16_t number_of_parameters) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100154 // dex_compilation_unit_ is null only when unit testing.
155 if (dex_compilation_unit_ == nullptr) {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000156 return;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100157 }
158
159 graph_->SetNumberOfInVRegs(number_of_parameters);
160 const char* shorty = dex_compilation_unit_->GetShorty();
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100161 int locals_index = locals_.size() - number_of_parameters;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100162 int parameter_index = 0;
163
Calin Juravlee6e3bea2015-10-14 13:53:10 +0000164 const DexFile::MethodId& referrer_method_id =
165 dex_file_->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100166 if (!dex_compilation_unit_->IsStatic()) {
167 // Add the implicit 'this' argument, not expressed in the signature.
Calin Juravlee6e3bea2015-10-14 13:53:10 +0000168 HParameterValue* parameter = new (arena_) HParameterValue(*dex_file_,
169 referrer_method_id.class_idx_,
170 parameter_index++,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600171 Primitive::kPrimNot,
172 true);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100173 entry_block_->AddInstruction(parameter);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100174 HLocal* local = GetLocalAt(locals_index++);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600175 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter, local->GetDexPc()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100176 number_of_parameters--;
177 }
178
Calin Juravlee6e3bea2015-10-14 13:53:10 +0000179 const DexFile::ProtoId& proto = dex_file_->GetMethodPrototype(referrer_method_id);
180 const DexFile::TypeList* arg_types = dex_file_->GetProtoParameters(proto);
181 for (int i = 0, shorty_pos = 1; i < number_of_parameters; i++) {
182 HParameterValue* parameter = new (arena_) HParameterValue(
183 *dex_file_,
184 arg_types->GetTypeItem(shorty_pos - 1).type_idx_,
185 parameter_index++,
186 Primitive::GetType(shorty[shorty_pos]),
187 false);
188 ++shorty_pos;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100189 entry_block_->AddInstruction(parameter);
190 HLocal* local = GetLocalAt(locals_index++);
191 // Store the parameter value in the local that the dex code will use
192 // to reference that parameter.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600193 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter, local->GetDexPc()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100194 bool is_wide = (parameter->GetType() == Primitive::kPrimLong)
195 || (parameter->GetType() == Primitive::kPrimDouble);
196 if (is_wide) {
197 i++;
198 locals_index++;
199 parameter_index++;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100200 }
201 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100202}
203
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100204template<typename T>
Calin Juravle225ff812014-11-13 16:46:39 +0000205void HGraphBuilder::If_22t(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000206 int32_t target_offset = instruction.GetTargetOffset();
David Brazdil852eaff2015-02-02 15:23:05 +0000207 HBasicBlock* branch_target = FindBlockStartingAt(dex_pc + target_offset);
208 HBasicBlock* fallthrough_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
209 DCHECK(branch_target != nullptr);
210 DCHECK(fallthrough_target != nullptr);
211 PotentiallyAddSuspendCheck(branch_target, dex_pc);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600212 HInstruction* first = LoadLocal(instruction.VRegA(), Primitive::kPrimInt, dex_pc);
213 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
214 T* comparison = new (arena_) T(first, second, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700215 current_block_->AddInstruction(comparison);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600216 HInstruction* ifinst = new (arena_) HIf(comparison, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700217 current_block_->AddInstruction(ifinst);
David Brazdil852eaff2015-02-02 15:23:05 +0000218 current_block_->AddSuccessor(branch_target);
219 current_block_->AddSuccessor(fallthrough_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700220 current_block_ = nullptr;
221}
222
223template<typename T>
Calin Juravle225ff812014-11-13 16:46:39 +0000224void HGraphBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000225 int32_t target_offset = instruction.GetTargetOffset();
David Brazdil852eaff2015-02-02 15:23:05 +0000226 HBasicBlock* branch_target = FindBlockStartingAt(dex_pc + target_offset);
227 HBasicBlock* fallthrough_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
228 DCHECK(branch_target != nullptr);
229 DCHECK(fallthrough_target != nullptr);
230 PotentiallyAddSuspendCheck(branch_target, dex_pc);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600231 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt, dex_pc);
232 T* comparison = new (arena_) T(value, graph_->GetIntConstant(0, dex_pc), dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700233 current_block_->AddInstruction(comparison);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600234 HInstruction* ifinst = new (arena_) HIf(comparison, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700235 current_block_->AddInstruction(ifinst);
David Brazdil852eaff2015-02-02 15:23:05 +0000236 current_block_->AddSuccessor(branch_target);
237 current_block_->AddSuccessor(fallthrough_target);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100238 current_block_ = nullptr;
239}
240
Calin Juravle48c2b032014-12-09 18:11:36 +0000241void HGraphBuilder::MaybeRecordStat(MethodCompilationStat compilation_stat) {
242 if (compilation_stats_ != nullptr) {
243 compilation_stats_->RecordStat(compilation_stat);
244 }
245}
246
David Brazdil1b498722015-03-31 11:37:18 +0100247bool HGraphBuilder::SkipCompilation(const DexFile::CodeItem& code_item,
Calin Juravle48c2b032014-12-09 18:11:36 +0000248 size_t number_of_branches) {
249 const CompilerOptions& compiler_options = compiler_driver_->GetCompilerOptions();
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000250 CompilerOptions::CompilerFilter compiler_filter = compiler_options.GetCompilerFilter();
251 if (compiler_filter == CompilerOptions::kEverything) {
252 return false;
253 }
254
David Brazdil1b498722015-03-31 11:37:18 +0100255 if (compiler_options.IsHugeMethod(code_item.insns_size_in_code_units_)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000256 VLOG(compiler) << "Skip compilation of huge method "
257 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
David Brazdil1b498722015-03-31 11:37:18 +0100258 << ": " << code_item.insns_size_in_code_units_ << " code units";
Calin Juravle48c2b032014-12-09 18:11:36 +0000259 MaybeRecordStat(MethodCompilationStat::kNotCompiledHugeMethod);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000260 return true;
261 }
262
263 // If it's large and contains no branches, it's likely to be machine generated initialization.
David Brazdil1b498722015-03-31 11:37:18 +0100264 if (compiler_options.IsLargeMethod(code_item.insns_size_in_code_units_)
265 && (number_of_branches == 0)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000266 VLOG(compiler) << "Skip compilation of large method with no branch "
267 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
David Brazdil1b498722015-03-31 11:37:18 +0100268 << ": " << code_item.insns_size_in_code_units_ << " code units";
Calin Juravle48c2b032014-12-09 18:11:36 +0000269 MaybeRecordStat(MethodCompilationStat::kNotCompiledLargeMethodNoBranches);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000270 return true;
271 }
272
273 return false;
274}
275
David Brazdilfc6a86a2015-06-26 10:33:45 +0000276void HGraphBuilder::CreateBlocksForTryCatch(const DexFile::CodeItem& code_item) {
277 if (code_item.tries_size_ == 0) {
278 return;
279 }
280
281 // Create branch targets at the start/end of the TryItem range. These are
282 // places where the program might fall through into/out of the a block and
283 // where TryBoundary instructions will be inserted later. Other edges which
284 // enter/exit the try blocks are a result of branches/switches.
285 for (size_t idx = 0; idx < code_item.tries_size_; ++idx) {
286 const DexFile::TryItem* try_item = DexFile::GetTryItems(code_item, idx);
287 uint32_t dex_pc_start = try_item->start_addr_;
288 uint32_t dex_pc_end = dex_pc_start + try_item->insn_count_;
289 FindOrCreateBlockStartingAt(dex_pc_start);
290 if (dex_pc_end < code_item.insns_size_in_code_units_) {
291 // TODO: Do not create block if the last instruction cannot fall through.
292 FindOrCreateBlockStartingAt(dex_pc_end);
293 } else {
294 // The TryItem spans until the very end of the CodeItem (or beyond if
295 // invalid) and therefore cannot have any code afterwards.
296 }
297 }
298
299 // Create branch targets for exception handlers.
300 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(code_item, 0);
301 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
302 for (uint32_t idx = 0; idx < handlers_size; ++idx) {
303 CatchHandlerIterator iterator(handlers_ptr);
304 for (; iterator.HasNext(); iterator.Next()) {
305 uint32_t address = iterator.GetHandlerAddress();
306 HBasicBlock* block = FindOrCreateBlockStartingAt(address);
David Brazdilec16f792015-08-19 15:04:01 +0100307 block->SetTryCatchInformation(
308 new (arena_) TryCatchInformation(iterator.GetHandlerTypeIndex(), *dex_file_));
David Brazdilfc6a86a2015-06-26 10:33:45 +0000309 }
310 handlers_ptr = iterator.EndDataPointer();
311 }
312}
313
David Brazdild7558da2015-09-22 13:04:14 +0100314// Returns the TryItem stored for `block` or nullptr if there is no info for it.
315static const DexFile::TryItem* GetTryItem(
316 HBasicBlock* block,
317 const ArenaSafeMap<uint32_t, const DexFile::TryItem*>& try_block_info) {
318 auto iterator = try_block_info.find(block->GetBlockId());
319 return (iterator == try_block_info.end()) ? nullptr : iterator->second;
320}
David Brazdil56e1acc2015-06-30 15:41:36 +0100321
David Brazdild7558da2015-09-22 13:04:14 +0100322void HGraphBuilder::LinkToCatchBlocks(HTryBoundary* try_boundary,
323 const DexFile::CodeItem& code_item,
324 const DexFile::TryItem* try_item) {
325 for (CatchHandlerIterator it(code_item, *try_item); it.HasNext(); it.Next()) {
David Brazdil56e1acc2015-06-30 15:41:36 +0100326 try_boundary->AddExceptionHandler(FindBlockStartingAt(it.GetHandlerAddress()));
327 }
328}
329
David Brazdilfc6a86a2015-06-26 10:33:45 +0000330void HGraphBuilder::InsertTryBoundaryBlocks(const DexFile::CodeItem& code_item) {
331 if (code_item.tries_size_ == 0) {
332 return;
333 }
334
David Brazdild7558da2015-09-22 13:04:14 +0100335 // Keep a map of all try blocks and their respective TryItems. We do not use
336 // the block's pointer but rather its id to ensure deterministic iteration.
337 ArenaSafeMap<uint32_t, const DexFile::TryItem*> try_block_info(
Vladimir Marko5233f932015-09-29 19:01:15 +0100338 std::less<uint32_t>(), arena_->Adapter(kArenaAllocGraphBuilder));
David Brazdilbff75032015-07-08 17:26:51 +0000339
David Brazdild7558da2015-09-22 13:04:14 +0100340 // Obtain TryItem information for blocks with throwing instructions, and split
341 // blocks which are both try & catch to simplify the graph.
342 // NOTE: We are appending new blocks inside the loop, so we need to use index
343 // because iterators can be invalidated. We remember the initial size to avoid
344 // iterating over the new blocks which cannot throw.
345 for (size_t i = 0, e = graph_->GetBlocks().size(); i < e; ++i) {
346 HBasicBlock* block = graph_->GetBlocks()[i];
David Brazdil72783ff2015-07-09 14:36:05 +0100347
David Brazdild7558da2015-09-22 13:04:14 +0100348 // Do not bother creating exceptional edges for try blocks which have no
349 // throwing instructions. In that case we simply assume that the block is
350 // not covered by a TryItem. This prevents us from creating a throw-catch
351 // loop for synchronized blocks.
352 if (block->HasThrowingInstructions()) {
353 // Try to find a TryItem covering the block.
354 DCHECK_NE(block->GetDexPc(), kNoDexPc) << "Block must have a dec_pc to find its TryItem.";
355 const int32_t try_item_idx = DexFile::FindTryItem(code_item, block->GetDexPc());
356 if (try_item_idx != -1) {
357 // Block throwing and in a TryItem. Store the try block information.
358 HBasicBlock* throwing_block = block;
359 if (block->IsCatchBlock()) {
360 // Simplify blocks which are both try and catch, otherwise we would
361 // need a strategy for splitting exceptional edges. We split the block
362 // after the move-exception (if present) and mark the first part not
363 // throwing. The normal-flow edge between them will be split later.
David Brazdil9bc43612015-11-05 21:25:24 +0000364 throwing_block = block->SplitCatchBlockAfterMoveException();
365 // Move-exception does not throw and the block has throwing insructions
366 // so it must have been possible to split it.
367 DCHECK(throwing_block != nullptr);
David Brazdil72783ff2015-07-09 14:36:05 +0100368 }
David Brazdild7558da2015-09-22 13:04:14 +0100369
370 try_block_info.Put(throwing_block->GetBlockId(),
371 DexFile::GetTryItems(code_item, try_item_idx));
David Brazdil72783ff2015-07-09 14:36:05 +0100372 }
David Brazdil72783ff2015-07-09 14:36:05 +0100373 }
David Brazdilbff75032015-07-08 17:26:51 +0000374 }
375
David Brazdild7558da2015-09-22 13:04:14 +0100376 // Do a pass over the try blocks and insert entering TryBoundaries where at
377 // least one predecessor is not covered by the same TryItem as the try block.
378 // We do not split each edge separately, but rather create one boundary block
379 // that all predecessors are relinked to. This preserves loop headers (b/23895756).
380 for (auto entry : try_block_info) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100381 HBasicBlock* try_block = graph_->GetBlocks()[entry.first];
David Brazdild7558da2015-09-22 13:04:14 +0100382 for (HBasicBlock* predecessor : try_block->GetPredecessors()) {
383 if (GetTryItem(predecessor, try_block_info) != entry.second) {
384 // Found a predecessor not covered by the same TryItem. Insert entering
385 // boundary block.
386 HTryBoundary* try_entry =
387 new (arena_) HTryBoundary(HTryBoundary::kEntry, try_block->GetDexPc());
388 try_block->CreateImmediateDominator()->AddInstruction(try_entry);
389 LinkToCatchBlocks(try_entry, code_item, entry.second);
390 break;
391 }
David Brazdil281a6322015-07-03 10:34:57 +0100392 }
David Brazdild7558da2015-09-22 13:04:14 +0100393 }
David Brazdil281a6322015-07-03 10:34:57 +0100394
David Brazdild7558da2015-09-22 13:04:14 +0100395 // Do a second pass over the try blocks and insert exit TryBoundaries where
396 // the successor is not in the same TryItem.
397 for (auto entry : try_block_info) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100398 HBasicBlock* try_block = graph_->GetBlocks()[entry.first];
David Brazdild7558da2015-09-22 13:04:14 +0100399 // NOTE: Do not use iterators because SplitEdge would invalidate them.
400 for (size_t i = 0, e = try_block->GetSuccessors().size(); i < e; ++i) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100401 HBasicBlock* successor = try_block->GetSuccessors()[i];
David Brazdil72783ff2015-07-09 14:36:05 +0100402
David Brazdild7558da2015-09-22 13:04:14 +0100403 // If the successor is a try block, all of its predecessors must be
404 // covered by the same TryItem. Otherwise the previous pass would have
405 // created a non-throwing boundary block.
406 if (GetTryItem(successor, try_block_info) != nullptr) {
407 DCHECK_EQ(entry.second, GetTryItem(successor, try_block_info));
David Brazdil72783ff2015-07-09 14:36:05 +0100408 continue;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000409 }
David Brazdil281a6322015-07-03 10:34:57 +0100410
David Brazdild7558da2015-09-22 13:04:14 +0100411 // Preserve the invariant that Return(Void) always jumps to Exit by moving
412 // it outside the try block if necessary.
413 HInstruction* last_instruction = try_block->GetLastInstruction();
414 if (last_instruction->IsReturn() || last_instruction->IsReturnVoid()) {
415 DCHECK_EQ(successor, exit_block_);
416 successor = try_block->SplitBefore(last_instruction);
David Brazdil281a6322015-07-03 10:34:57 +0100417 }
David Brazdild7558da2015-09-22 13:04:14 +0100418
419 // Insert TryBoundary and link to catch blocks.
420 HTryBoundary* try_exit =
421 new (arena_) HTryBoundary(HTryBoundary::kExit, successor->GetDexPc());
422 graph_->SplitEdge(try_block, successor)->AddInstruction(try_exit);
423 LinkToCatchBlocks(try_exit, code_item, entry.second);
David Brazdil281a6322015-07-03 10:34:57 +0100424 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000425 }
426}
427
David Brazdil5e8b1372015-01-23 14:39:08 +0000428bool HGraphBuilder::BuildGraph(const DexFile::CodeItem& code_item) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100429 DCHECK(graph_->GetBlocks().empty());
David Brazdil5e8b1372015-01-23 14:39:08 +0000430
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000431 const uint16_t* code_ptr = code_item.insns_;
432 const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100433 code_start_ = code_ptr;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000434
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000435 // Setup the graph with the entry block and exit block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100436 entry_block_ = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000437 graph_->AddBlock(entry_block_);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100438 exit_block_ = new (arena_) HBasicBlock(graph_, kNoDexPc);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000439 graph_->SetEntryBlock(entry_block_);
440 graph_->SetExitBlock(exit_block_);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000441
David Brazdil77a48ae2015-09-15 12:34:04 +0000442 graph_->SetHasTryCatch(code_item.tries_size_ != 0);
443
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000444 InitializeLocals(code_item.registers_size_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000445 graph_->SetMaximumNumberOfOutVRegs(code_item.outs_size_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000446
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000447 // Compute the number of dex instructions, blocks, and branches. We will
448 // check these values against limits given to the compiler.
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000449 size_t number_of_branches = 0;
450
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000451 // To avoid splitting blocks, we compute ahead of time the instructions that
452 // start a new block, and create these blocks.
Calin Juravle702d2602015-04-30 19:28:21 +0100453 if (!ComputeBranchTargets(code_ptr, code_end, &number_of_branches)) {
454 MaybeRecordStat(MethodCompilationStat::kNotCompiledBranchOutsideMethodCode);
455 return false;
456 }
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000457
458 // Note that the compiler driver is null when unit testing.
David Brazdil1b498722015-03-31 11:37:18 +0100459 if ((compiler_driver_ != nullptr) && SkipCompilation(code_item, number_of_branches)) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000460 return false;
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000461 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000462
David Srbecky0cf44932015-12-09 14:09:59 +0000463 // Find locations where we want to generate extra stackmaps for native debugging.
464 // This allows us to generate the info only at interesting points (for example,
465 // at start of java statement) rather than before every dex instruction.
466 const bool native_debuggable = compiler_driver_ != nullptr &&
467 compiler_driver_->GetCompilerOptions().GetNativeDebuggable();
468 ArenaBitVector* native_debug_info_locations;
469 if (native_debuggable) {
470 const uint32_t num_instructions = code_item.insns_size_in_code_units_;
471 native_debug_info_locations = new (arena_) ArenaBitVector (arena_, num_instructions, false);
472 native_debug_info_locations->ClearAllBits();
473 FindNativeDebugInfoLocations(code_item, native_debug_info_locations);
474 }
475
David Brazdilfc6a86a2015-06-26 10:33:45 +0000476 CreateBlocksForTryCatch(code_item);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000477
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000478 InitializeParameters(code_item.ins_size_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100479
Calin Juravle225ff812014-11-13 16:46:39 +0000480 size_t dex_pc = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000481 while (code_ptr < code_end) {
Calin Juravle225ff812014-11-13 16:46:39 +0000482 // Update the current block if dex_pc starts a new block.
483 MaybeUpdateCurrentBlock(dex_pc);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000484 const Instruction& instruction = *Instruction::At(code_ptr);
David Srbecky0cf44932015-12-09 14:09:59 +0000485 if (native_debuggable && native_debug_info_locations->IsBitSet(dex_pc)) {
486 if (current_block_ != nullptr) {
487 current_block_->AddInstruction(new (arena_) HNativeDebugInfo(dex_pc));
488 }
489 }
Calin Juravle48c2b032014-12-09 18:11:36 +0000490 if (!AnalyzeDexInstruction(instruction, dex_pc)) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000491 return false;
Calin Juravle48c2b032014-12-09 18:11:36 +0000492 }
Calin Juravle225ff812014-11-13 16:46:39 +0000493 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000494 code_ptr += instruction.SizeInCodeUnits();
495 }
496
David Brazdilfc6a86a2015-06-26 10:33:45 +0000497 // Add Exit to the exit block.
David Brazdil3e187382015-06-26 09:59:52 +0000498 exit_block_->AddInstruction(new (arena_) HExit());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000499 // Add the suspend check to the entry block.
500 entry_block_->AddInstruction(new (arena_) HSuspendCheck(0));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000501 entry_block_->AddInstruction(new (arena_) HGoto());
David Brazdilbff75032015-07-08 17:26:51 +0000502 // Add the exit block at the end.
503 graph_->AddBlock(exit_block_);
David Brazdil5e8b1372015-01-23 14:39:08 +0000504
David Brazdilfc6a86a2015-06-26 10:33:45 +0000505 // Iterate over blocks covered by TryItems and insert TryBoundaries at entry
506 // and exit points. This requires all control-flow instructions and
507 // non-exceptional edges to have been created.
508 InsertTryBoundaryBlocks(code_item);
509
David Brazdil5e8b1372015-01-23 14:39:08 +0000510 return true;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000511}
512
David Brazdilfc6a86a2015-06-26 10:33:45 +0000513void HGraphBuilder::MaybeUpdateCurrentBlock(size_t dex_pc) {
514 HBasicBlock* block = FindBlockStartingAt(dex_pc);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000515 if (block == nullptr) {
516 return;
517 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000518
519 if (current_block_ != nullptr) {
520 // Branching instructions clear current_block, so we know
521 // the last instruction of the current block is not a branching
522 // instruction. We add an unconditional goto to the found block.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600523 current_block_->AddInstruction(new (arena_) HGoto(dex_pc));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000524 current_block_->AddSuccessor(block);
525 }
526 graph_->AddBlock(block);
527 current_block_ = block;
528}
529
David Srbecky0cf44932015-12-09 14:09:59 +0000530void HGraphBuilder::FindNativeDebugInfoLocations(const DexFile::CodeItem& code_item,
531 ArenaBitVector* locations) {
532 // The callback gets called when the line number changes.
533 // In other words, it marks the start of new java statement.
534 struct Callback {
535 static bool Position(void* ctx, const DexFile::PositionInfo& entry) {
536 static_cast<ArenaBitVector*>(ctx)->SetBit(entry.address_);
537 return false;
538 }
539 };
540 dex_file_->DecodeDebugPositionInfo(&code_item, Callback::Position, locations);
541 // Add native debug info at the start of every basic block.
542 for (uint32_t pc = 0; pc < code_item.insns_size_in_code_units_; pc++) {
543 if (FindBlockStartingAt(pc) != nullptr) {
544 locations->SetBit(pc);
545 }
546 }
547 // Instruction-specific tweaks.
548 const Instruction* const begin = Instruction::At(code_item.insns_);
549 const Instruction* const end = begin->RelativeAt(code_item.insns_size_in_code_units_);
550 for (const Instruction* inst = begin; inst < end; inst = inst->Next()) {
551 switch (inst->Opcode()) {
552 case Instruction::MOVE_EXCEPTION:
553 case Instruction::MOVE_RESULT:
554 case Instruction::MOVE_RESULT_WIDE:
555 case Instruction::MOVE_RESULT_OBJECT: {
556 // The compiler checks that there are no instructions before those.
557 // So generate HNativeDebugInfo after them instead.
558 locations->ClearBit(inst->GetDexPc(code_item.insns_));
559 const Instruction* next = inst->Next();
560 if (next < end) {
561 locations->SetBit(next->GetDexPc(code_item.insns_));
562 }
563 break;
564 }
565 default:
566 break;
567 }
568 }
569}
570
Calin Juravle702d2602015-04-30 19:28:21 +0100571bool HGraphBuilder::ComputeBranchTargets(const uint16_t* code_ptr,
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000572 const uint16_t* code_end,
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000573 size_t* number_of_branches) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100574 branch_targets_.resize(code_end - code_ptr, nullptr);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000575
576 // Create the first block for the dex instructions, single successor of the entry block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100577 HBasicBlock* block = new (arena_) HBasicBlock(graph_, 0);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100578 branch_targets_[0] = block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000579 entry_block_->AddSuccessor(block);
580
581 // Iterate over all instructions and find branching instructions. Create blocks for
582 // the locations these instructions branch to.
Andreas Gamped881df52014-11-24 23:28:39 -0800583 uint32_t dex_pc = 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000584 while (code_ptr < code_end) {
585 const Instruction& instruction = *Instruction::At(code_ptr);
586 if (instruction.IsBranch()) {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000587 (*number_of_branches)++;
Calin Juravle225ff812014-11-13 16:46:39 +0000588 int32_t target = instruction.GetTargetOffset() + dex_pc;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000589 // Create a block for the target instruction.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000590 FindOrCreateBlockStartingAt(target);
591
Calin Juravle225ff812014-11-13 16:46:39 +0000592 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000593 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle702d2602015-04-30 19:28:21 +0100594
David Brazdilfe659462015-06-24 14:23:56 +0100595 if (instruction.CanFlowThrough()) {
596 if (code_ptr >= code_end) {
Calin Juravle702d2602015-04-30 19:28:21 +0100597 // In the normal case we should never hit this but someone can artificially forge a dex
598 // file to fall-through out the method code. In this case we bail out compilation.
599 return false;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000600 } else {
601 FindOrCreateBlockStartingAt(dex_pc);
Calin Juravle702d2602015-04-30 19:28:21 +0100602 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000603 }
Andreas Gampee4d4d322014-12-04 09:09:57 -0800604 } else if (instruction.IsSwitch()) {
605 SwitchTable table(instruction, dex_pc, instruction.Opcode() == Instruction::SPARSE_SWITCH);
Andreas Gamped881df52014-11-24 23:28:39 -0800606
607 uint16_t num_entries = table.GetNumEntries();
608
Andreas Gampee4d4d322014-12-04 09:09:57 -0800609 // In a packed-switch, the entry at index 0 is the starting key. In a sparse-switch, the
610 // entry at index 0 is the first key, and values are after *all* keys.
611 size_t offset = table.GetFirstValueIndex();
612
613 // Use a larger loop counter type to avoid overflow issues.
614 for (size_t i = 0; i < num_entries; ++i) {
Andreas Gamped881df52014-11-24 23:28:39 -0800615 // The target of the case.
Andreas Gampee4d4d322014-12-04 09:09:57 -0800616 uint32_t target = dex_pc + table.GetEntryAt(i + offset);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000617 FindOrCreateBlockStartingAt(target);
Andreas Gamped881df52014-11-24 23:28:39 -0800618
David Brazdil281a6322015-07-03 10:34:57 +0100619 // Create a block for the switch-case logic. The block gets the dex_pc
620 // of the SWITCH instruction because it is part of its semantics.
621 block = new (arena_) HBasicBlock(graph_, dex_pc);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100622 branch_targets_[table.GetDexPcForIndex(i)] = block;
Andreas Gamped881df52014-11-24 23:28:39 -0800623 }
624
625 // Fall-through. Add a block if there is more code afterwards.
626 dex_pc += instruction.SizeInCodeUnits();
627 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle702d2602015-04-30 19:28:21 +0100628 if (code_ptr >= code_end) {
629 // In the normal case we should never hit this but someone can artificially forge a dex
630 // file to fall-through out the method code. In this case we bail out compilation.
631 // (A switch can fall-through so we don't need to check CanFlowThrough().)
632 return false;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000633 } else {
634 FindOrCreateBlockStartingAt(dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -0800635 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000636 } else {
637 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle225ff812014-11-13 16:46:39 +0000638 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000639 }
640 }
Calin Juravle702d2602015-04-30 19:28:21 +0100641 return true;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000642}
643
David Brazdilfc6a86a2015-06-26 10:33:45 +0000644HBasicBlock* HGraphBuilder::FindBlockStartingAt(int32_t dex_pc) const {
645 DCHECK_GE(dex_pc, 0);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100646 return branch_targets_[dex_pc];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000647}
648
649HBasicBlock* HGraphBuilder::FindOrCreateBlockStartingAt(int32_t dex_pc) {
650 HBasicBlock* block = FindBlockStartingAt(dex_pc);
651 if (block == nullptr) {
652 block = new (arena_) HBasicBlock(graph_, dex_pc);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100653 branch_targets_[dex_pc] = block;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000654 }
655 return block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000656}
657
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100658template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600659void HGraphBuilder::Unop_12x(const Instruction& instruction,
660 Primitive::Type type,
661 uint32_t dex_pc) {
662 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
663 current_block_->AddInstruction(new (arena_) T(type, first, dex_pc));
664 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Roland Levillain88cb1752014-10-20 16:36:47 +0100665}
666
Roland Levillaindff1f282014-11-05 14:15:05 +0000667void HGraphBuilder::Conversion_12x(const Instruction& instruction,
668 Primitive::Type input_type,
Roland Levillain624279f2014-12-04 11:54:28 +0000669 Primitive::Type result_type,
670 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600671 HInstruction* first = LoadLocal(instruction.VRegB(), input_type, dex_pc);
Roland Levillain624279f2014-12-04 11:54:28 +0000672 current_block_->AddInstruction(new (arena_) HTypeConversion(result_type, first, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600673 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100674}
675
676template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000677void HGraphBuilder::Binop_23x(const Instruction& instruction,
678 Primitive::Type type,
679 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);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000682 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600683 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000684}
685
686template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000687void HGraphBuilder::Binop_23x_shift(const Instruction& instruction,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600688 Primitive::Type type,
689 uint32_t dex_pc) {
690 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
691 HInstruction* second = LoadLocal(instruction.VRegC(), Primitive::kPrimInt, dex_pc);
692 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
693 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000694}
695
Calin Juravleddb7df22014-11-25 20:56:51 +0000696void HGraphBuilder::Binop_23x_cmp(const Instruction& instruction,
697 Primitive::Type type,
Mark Mendellc4701932015-04-10 13:18:51 -0400698 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700699 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600700 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
701 HInstruction* second = LoadLocal(instruction.VRegC(), type, dex_pc);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700702 current_block_->AddInstruction(new (arena_) HCompare(type, first, second, bias, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600703 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +0000704}
705
Calin Juravle9aec02f2014-11-18 23:06:35 +0000706template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600707void HGraphBuilder::Binop_12x_shift(const Instruction& instruction, Primitive::Type type,
708 uint32_t dex_pc) {
709 HInstruction* first = LoadLocal(instruction.VRegA(), type, dex_pc);
710 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
711 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
712 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000713}
714
715template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000716void HGraphBuilder::Binop_12x(const Instruction& instruction,
717 Primitive::Type type,
718 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600719 HInstruction* first = LoadLocal(instruction.VRegA(), type, dex_pc);
720 HInstruction* second = LoadLocal(instruction.VRegB(), type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000721 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600722 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000723}
724
725template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600726void HGraphBuilder::Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
727 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
728 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100729 if (reverse) {
730 std::swap(first, second);
731 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600732 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second, dex_pc));
733 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100734}
735
736template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600737void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
738 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
739 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100740 if (reverse) {
741 std::swap(first, second);
742 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600743 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second, dex_pc));
744 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100745}
746
Calin Juravle0c25d102015-04-20 14:49:09 +0100747static bool RequiresConstructorBarrier(const DexCompilationUnit* cu, const CompilerDriver& driver) {
Calin Juravle27df7582015-04-17 19:12:31 +0100748 Thread* self = Thread::Current();
Calin Juravle0c25d102015-04-20 14:49:09 +0100749 return cu->IsConstructor()
750 && driver.RequiresConstructorBarrier(self, cu->GetDexFile(), cu->GetClassDefIndex());
Calin Juravle27df7582015-04-17 19:12:31 +0100751}
752
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600753void HGraphBuilder::BuildReturn(const Instruction& instruction,
754 Primitive::Type type,
755 uint32_t dex_pc) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100756 if (type == Primitive::kPrimVoid) {
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100757 if (graph_->ShouldGenerateConstructorBarrier()) {
758 // The compilation unit is null during testing.
759 if (dex_compilation_unit_ != nullptr) {
760 DCHECK(RequiresConstructorBarrier(dex_compilation_unit_, *compiler_driver_))
761 << "Inconsistent use of ShouldGenerateConstructorBarrier. Should not generate a barrier.";
762 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600763 current_block_->AddInstruction(new (arena_) HMemoryBarrier(kStoreStore, dex_pc));
Calin Juravle27df7582015-04-17 19:12:31 +0100764 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600765 current_block_->AddInstruction(new (arena_) HReturnVoid(dex_pc));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100766 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600767 HInstruction* value = LoadLocal(instruction.VRegA(), type, dex_pc);
768 current_block_->AddInstruction(new (arena_) HReturn(value, dex_pc));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100769 }
770 current_block_->AddSuccessor(exit_block_);
771 current_block_ = nullptr;
772}
773
Calin Juravle68ad6492015-08-18 17:08:12 +0100774static InvokeType GetInvokeTypeFromOpCode(Instruction::Code opcode) {
775 switch (opcode) {
776 case Instruction::INVOKE_STATIC:
777 case Instruction::INVOKE_STATIC_RANGE:
778 return kStatic;
779 case Instruction::INVOKE_DIRECT:
780 case Instruction::INVOKE_DIRECT_RANGE:
781 return kDirect;
782 case Instruction::INVOKE_VIRTUAL:
783 case Instruction::INVOKE_VIRTUAL_QUICK:
784 case Instruction::INVOKE_VIRTUAL_RANGE:
785 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
786 return kVirtual;
787 case Instruction::INVOKE_INTERFACE:
788 case Instruction::INVOKE_INTERFACE_RANGE:
789 return kInterface;
790 case Instruction::INVOKE_SUPER_RANGE:
791 case Instruction::INVOKE_SUPER:
792 return kSuper;
793 default:
794 LOG(FATAL) << "Unexpected invoke opcode: " << opcode;
795 UNREACHABLE();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100796 }
Calin Juravle68ad6492015-08-18 17:08:12 +0100797}
798
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000799ArtMethod* HGraphBuilder::ResolveMethod(uint16_t method_idx, InvokeType invoke_type) {
800 ScopedObjectAccess soa(Thread::Current());
Alex Lightfedd91d2016-01-07 14:49:16 -0800801 StackHandleScope<3> hs(soa.Self());
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000802
803 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
804 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
805 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
806 Handle<mirror::Class> compiling_class(hs.NewHandle(GetCompilingClass()));
807
Andreas Gampedae24142015-12-03 17:27:32 -0800808 ArtMethod* resolved_method = class_linker->ResolveMethod<ClassLinker::kForceICCECheck>(
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000809 *dex_compilation_unit_->GetDexFile(),
810 method_idx,
811 dex_compilation_unit_->GetDexCache(),
812 class_loader,
813 /* referrer */ nullptr,
814 invoke_type);
815
816 if (UNLIKELY(resolved_method == nullptr)) {
817 // Clean up any exception left by type resolution.
818 soa.Self()->ClearException();
819 return nullptr;
820 }
821
822 // Check access. The class linker has a fast path for looking into the dex cache
823 // and does not check the access if it hits it.
824 if (compiling_class.Get() == nullptr) {
825 if (!resolved_method->IsPublic()) {
826 return nullptr;
827 }
828 } else if (!compiling_class->CanAccessResolvedMethod(resolved_method->GetDeclaringClass(),
829 resolved_method,
830 dex_compilation_unit_->GetDexCache().Get(),
831 method_idx)) {
832 return nullptr;
833 }
834
835 // We have to special case the invoke-super case, as ClassLinker::ResolveMethod does not.
Alex Lightfedd91d2016-01-07 14:49:16 -0800836 // We need to look at the referrer's super class vtable. We need to do this to know if we need to
837 // make this an invoke-unresolved to handle cross-dex invokes or abstract super methods, both of
838 // which require runtime handling.
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000839 if (invoke_type == kSuper) {
840 if (compiling_class.Get() == nullptr) {
Alex Lightfedd91d2016-01-07 14:49:16 -0800841 // We could not determine the method's class we need to wait until runtime.
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000842 DCHECK(Runtime::Current()->IsAotCompiler());
843 return nullptr;
844 }
Alex Lightfedd91d2016-01-07 14:49:16 -0800845 ArtMethod* current_method = graph_->GetArtMethod();
846 DCHECK(current_method != nullptr);
847 Handle<mirror::Class> methods_class(hs.NewHandle(
848 dex_compilation_unit_->GetClassLinker()->ResolveReferencedClassOfMethod(Thread::Current(),
849 method_idx,
850 current_method)));
851 if (methods_class.Get() == nullptr) {
852 // Invoking a super method requires knowing the actual super class. If we did not resolve
853 // the compiling method's declaring class (which only happens for ahead of time
854 // compilation), bail out.
855 DCHECK(Runtime::Current()->IsAotCompiler());
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000856 return nullptr;
Alex Lightfedd91d2016-01-07 14:49:16 -0800857 } else {
858 ArtMethod* actual_method;
859 if (methods_class->IsInterface()) {
860 actual_method = methods_class->FindVirtualMethodForInterfaceSuper(
861 resolved_method, class_linker->GetImagePointerSize());
862 } else {
863 uint16_t vtable_index = resolved_method->GetMethodIndex();
864 actual_method = compiling_class->GetSuperClass()->GetVTableEntry(
865 vtable_index, class_linker->GetImagePointerSize());
866 }
867 if (actual_method != resolved_method &&
868 !IsSameDexFile(*actual_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
869 // The back-end code generator relies on this check in order to ensure that it will not
870 // attempt to read the dex_cache with a dex_method_index that is not from the correct
871 // dex_file. If we didn't do this check then the dex_method_index will not be updated in the
872 // builder, which means that the code-generator (and compiler driver during sharpening and
873 // inliner, maybe) might invoke an incorrect method.
874 // TODO: The actual method could still be referenced in the current dex file, so we
875 // could try locating it.
876 // TODO: Remove the dex_file restriction.
877 return nullptr;
878 }
879 if (!actual_method->IsInvokable()) {
880 // Fail if the actual method cannot be invoked. Otherwise, the runtime resolution stub
881 // could resolve the callee to the wrong method.
882 return nullptr;
883 }
884 resolved_method = actual_method;
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000885 }
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000886 }
887
888 // Check for incompatible class changes. The class linker has a fast path for
889 // looking into the dex cache and does not check incompatible class changes if it hits it.
890 if (resolved_method->CheckIncompatibleClassChange(invoke_type)) {
891 return nullptr;
892 }
893
894 return resolved_method;
895}
896
Calin Juravle68ad6492015-08-18 17:08:12 +0100897bool HGraphBuilder::BuildInvoke(const Instruction& instruction,
898 uint32_t dex_pc,
899 uint32_t method_idx,
900 uint32_t number_of_vreg_arguments,
901 bool is_range,
902 uint32_t* args,
903 uint32_t register_index) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000904 InvokeType invoke_type = GetInvokeTypeFromOpCode(instruction.Opcode());
Calin Juravle68ad6492015-08-18 17:08:12 +0100905 const char* descriptor = dex_file_->GetMethodShorty(method_idx);
906 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
907
908 // Remove the return type from the 'proto'.
909 size_t number_of_arguments = strlen(descriptor) - 1;
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000910 if (invoke_type != kStatic) { // instance call
Calin Juravle68ad6492015-08-18 17:08:12 +0100911 // One extra argument for 'this'.
912 number_of_arguments++;
913 }
914
915 MethodReference target_method(dex_file_, method_idx);
Calin Juravle68ad6492015-08-18 17:08:12 +0100916
Calin Juravle5d01db12015-08-25 15:02:42 +0100917 // Special handling for string init.
918 int32_t string_init_offset = 0;
919 bool is_string_init = compiler_driver_->IsStringInit(method_idx,
920 dex_file_,
921 &string_init_offset);
922 // Replace calls to String.<init> with StringFactory.
923 if (is_string_init) {
Vladimir Markodc151b22015-10-15 18:02:30 +0100924 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
925 HInvokeStaticOrDirect::MethodLoadKind::kStringInit,
926 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
927 dchecked_integral_cast<uint64_t>(string_init_offset),
928 0U
929 };
Calin Juravle5d01db12015-08-25 15:02:42 +0100930 HInvoke* invoke = new (arena_) HInvokeStaticOrDirect(
931 arena_,
932 number_of_arguments - 1,
933 Primitive::kPrimNot /*return_type */,
934 dex_pc,
935 method_idx,
936 target_method,
937 dispatch_info,
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000938 invoke_type,
Calin Juravle5d01db12015-08-25 15:02:42 +0100939 kStatic /* optimized_invoke_type */,
940 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit);
941 return HandleStringInit(invoke,
942 number_of_vreg_arguments,
943 args,
944 register_index,
945 is_range,
946 descriptor);
947 }
948
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000949 ArtMethod* resolved_method = ResolveMethod(method_idx, invoke_type);
950
Alex Lightfedd91d2016-01-07 14:49:16 -0800951 if (UNLIKELY(resolved_method == nullptr)) {
Calin Juravle175dc732015-08-25 15:42:32 +0100952 MaybeRecordStat(MethodCompilationStat::kUnresolvedMethod);
953 HInvoke* invoke = new (arena_) HInvokeUnresolved(arena_,
954 number_of_arguments,
955 return_type,
956 dex_pc,
957 method_idx,
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000958 invoke_type);
Calin Juravle175dc732015-08-25 15:42:32 +0100959 return HandleInvoke(invoke,
960 number_of_vreg_arguments,
961 args,
962 register_index,
963 is_range,
964 descriptor,
965 nullptr /* clinit_check */);
Calin Juravle68ad6492015-08-18 17:08:12 +0100966 }
967
Calin Juravle68ad6492015-08-18 17:08:12 +0100968 // Potential class initialization check, in the case of a static method call.
969 HClinitCheck* clinit_check = nullptr;
970 HInvoke* invoke = nullptr;
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000971 if (invoke_type == kDirect || invoke_type == kStatic || invoke_type == kSuper) {
Calin Juravle68ad6492015-08-18 17:08:12 +0100972 // By default, consider that the called method implicitly requires
973 // an initialization check of its declaring method.
974 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement
975 = HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000976 ScopedObjectAccess soa(Thread::Current());
977 if (invoke_type == kStatic) {
978 clinit_check = ProcessClinitCheckForInvoke(
979 dex_pc, resolved_method, method_idx, &clinit_check_requirement);
980 } else if (invoke_type == kSuper) {
981 if (IsSameDexFile(*resolved_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
982 // Update the target method to the one resolved. Note that this may be a no-op if
983 // we resolved to the method referenced by the instruction.
984 method_idx = resolved_method->GetDexMethodIndex();
985 target_method = MethodReference(dex_file_, method_idx);
986 }
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100987 }
Calin Juravle68ad6492015-08-18 17:08:12 +0100988
Vladimir Markodc151b22015-10-15 18:02:30 +0100989 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
990 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
991 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
992 0u,
993 0U
994 };
Calin Juravle68ad6492015-08-18 17:08:12 +0100995 invoke = new (arena_) HInvokeStaticOrDirect(arena_,
996 number_of_arguments,
997 return_type,
998 dex_pc,
999 method_idx,
1000 target_method,
1001 dispatch_info,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001002 invoke_type,
1003 invoke_type,
Calin Juravle68ad6492015-08-18 17:08:12 +01001004 clinit_check_requirement);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001005 } else if (invoke_type == kVirtual) {
1006 ScopedObjectAccess soa(Thread::Current()); // Needed for the method index
Calin Juravle68ad6492015-08-18 17:08:12 +01001007 invoke = new (arena_) HInvokeVirtual(arena_,
1008 number_of_arguments,
1009 return_type,
1010 dex_pc,
1011 method_idx,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001012 resolved_method->GetMethodIndex());
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001013 } else {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001014 DCHECK_EQ(invoke_type, kInterface);
1015 ScopedObjectAccess soa(Thread::Current()); // Needed for the method index
Calin Juravle68ad6492015-08-18 17:08:12 +01001016 invoke = new (arena_) HInvokeInterface(arena_,
1017 number_of_arguments,
1018 return_type,
1019 dex_pc,
1020 method_idx,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001021 resolved_method->GetDexMethodIndex());
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001022 }
Calin Juravle68ad6492015-08-18 17:08:12 +01001023
Calin Juravle5d01db12015-08-25 15:02:42 +01001024 return HandleInvoke(invoke,
1025 number_of_vreg_arguments,
1026 args,
1027 register_index,
1028 is_range,
1029 descriptor,
1030 clinit_check);
Calin Juravle68ad6492015-08-18 17:08:12 +01001031}
1032
Nicolas Geoffray729645a2015-11-19 13:29:02 +00001033bool HGraphBuilder::BuildNewInstance(uint16_t type_index, uint32_t dex_pc) {
1034 bool finalizable;
1035 bool can_throw = NeedsAccessCheck(type_index, &finalizable);
1036
1037 // Only the non-resolved entrypoint handles the finalizable class case. If we
1038 // need access checks, then we haven't resolved the method and the class may
1039 // again be finalizable.
1040 QuickEntrypointEnum entrypoint = (finalizable || can_throw)
1041 ? kQuickAllocObject
1042 : kQuickAllocObjectInitialized;
1043
1044 ScopedObjectAccess soa(Thread::Current());
1045 StackHandleScope<3> hs(soa.Self());
1046 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
1047 dex_compilation_unit_->GetClassLinker()->FindDexCache(
1048 soa.Self(), *dex_compilation_unit_->GetDexFile())));
1049 Handle<mirror::Class> resolved_class(hs.NewHandle(dex_cache->GetResolvedType(type_index)));
1050 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
1051 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
1052 outer_compilation_unit_->GetClassLinker()->FindDexCache(soa.Self(), outer_dex_file)));
1053
1054 if (outer_dex_cache.Get() != dex_cache.Get()) {
1055 // We currently do not support inlining allocations across dex files.
1056 return false;
1057 }
1058
1059 HLoadClass* load_class = new (arena_) HLoadClass(
1060 graph_->GetCurrentMethod(),
1061 type_index,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001062 outer_dex_file,
Nicolas Geoffray729645a2015-11-19 13:29:02 +00001063 IsOutermostCompilingClass(type_index),
1064 dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001065 /*needs_access_check*/ can_throw,
1066 compiler_driver_->CanAssumeTypeIsPresentInDexCache(outer_dex_file, type_index));
Nicolas Geoffray729645a2015-11-19 13:29:02 +00001067
1068 current_block_->AddInstruction(load_class);
1069 HInstruction* cls = load_class;
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +00001070 if (!IsInitialized(resolved_class)) {
Nicolas Geoffray729645a2015-11-19 13:29:02 +00001071 cls = new (arena_) HClinitCheck(load_class, dex_pc);
1072 current_block_->AddInstruction(cls);
1073 }
1074
1075 current_block_->AddInstruction(new (arena_) HNewInstance(
1076 cls,
1077 graph_->GetCurrentMethod(),
1078 dex_pc,
1079 type_index,
1080 *dex_compilation_unit_->GetDexFile(),
1081 can_throw,
1082 finalizable,
1083 entrypoint));
1084 return true;
1085}
1086
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +00001087static bool IsSubClass(mirror::Class* to_test, mirror::Class* super_class)
1088 SHARED_REQUIRES(Locks::mutator_lock_) {
1089 return to_test != nullptr && !to_test->IsInterface() && to_test->IsSubClass(super_class);
1090}
1091
1092bool HGraphBuilder::IsInitialized(Handle<mirror::Class> cls) const {
Nicolas Geoffray729645a2015-11-19 13:29:02 +00001093 if (cls.Get() == nullptr) {
1094 return false;
1095 }
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +00001096
1097 // `CanAssumeClassIsLoaded` will return true if we're JITting, or will
1098 // check whether the class is in an image for the AOT compilation.
1099 if (cls->IsInitialized() &&
1100 compiler_driver_->CanAssumeClassIsLoaded(cls.Get())) {
Nicolas Geoffray729645a2015-11-19 13:29:02 +00001101 return true;
1102 }
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +00001103
1104 if (IsSubClass(GetOutermostCompilingClass(), cls.Get())) {
1105 return true;
1106 }
1107
1108 // TODO: We should walk over the inlined methods, but we don't pass
1109 // that information to the builder.
1110 if (IsSubClass(GetCompilingClass(), cls.Get())) {
1111 return true;
1112 }
1113
1114 return false;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00001115}
1116
Calin Juravle68ad6492015-08-18 17:08:12 +01001117HClinitCheck* HGraphBuilder::ProcessClinitCheckForInvoke(
1118 uint32_t dex_pc,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001119 ArtMethod* resolved_method,
Calin Juravle68ad6492015-08-18 17:08:12 +01001120 uint32_t method_idx,
1121 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001122 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
1123 Thread* self = Thread::Current();
1124 StackHandleScope<4> hs(self);
Calin Juravle68ad6492015-08-18 17:08:12 +01001125 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
1126 dex_compilation_unit_->GetClassLinker()->FindDexCache(
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001127 self, *dex_compilation_unit_->GetDexFile())));
Calin Juravle68ad6492015-08-18 17:08:12 +01001128 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001129 outer_compilation_unit_->GetClassLinker()->FindDexCache(
1130 self, outer_dex_file)));
Calin Juravle68ad6492015-08-18 17:08:12 +01001131 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +00001132 Handle<mirror::Class> resolved_method_class(hs.NewHandle(resolved_method->GetDeclaringClass()));
Calin Juravle68ad6492015-08-18 17:08:12 +01001133
1134 // The index at which the method's class is stored in the DexCache's type array.
1135 uint32_t storage_index = DexFile::kDexNoIndex;
1136 bool is_outer_class = (resolved_method->GetDeclaringClass() == outer_class.Get());
1137 if (is_outer_class) {
1138 storage_index = outer_class->GetDexTypeIndex();
1139 } else if (outer_dex_cache.Get() == dex_cache.Get()) {
1140 // Get `storage_index` from IsClassOfStaticMethodAvailableToReferrer.
1141 compiler_driver_->IsClassOfStaticMethodAvailableToReferrer(outer_dex_cache.Get(),
1142 GetCompilingClass(),
1143 resolved_method,
1144 method_idx,
1145 &storage_index);
1146 }
1147
1148 HClinitCheck* clinit_check = nullptr;
1149
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +00001150 if (IsInitialized(resolved_method_class)) {
Calin Juravle68ad6492015-08-18 17:08:12 +01001151 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
1152 } else if (storage_index != DexFile::kDexNoIndex) {
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +00001153 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
1154 HLoadClass* load_class = new (arena_) HLoadClass(
1155 graph_->GetCurrentMethod(),
1156 storage_index,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001157 outer_dex_file,
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +00001158 is_outer_class,
1159 dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001160 /*needs_access_check*/ false,
1161 compiler_driver_->CanAssumeTypeIsPresentInDexCache(outer_dex_file, storage_index));
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +00001162 current_block_->AddInstruction(load_class);
1163 clinit_check = new (arena_) HClinitCheck(load_class, dex_pc);
1164 current_block_->AddInstruction(clinit_check);
Calin Juravle68ad6492015-08-18 17:08:12 +01001165 }
1166 return clinit_check;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001167}
1168
Calin Juravle5d01db12015-08-25 15:02:42 +01001169bool HGraphBuilder::SetupInvokeArguments(HInvoke* invoke,
1170 uint32_t number_of_vreg_arguments,
1171 uint32_t* args,
1172 uint32_t register_index,
1173 bool is_range,
1174 const char* descriptor,
1175 size_t start_index,
1176 size_t* argument_index) {
Calin Juravle68ad6492015-08-18 17:08:12 +01001177 uint32_t descriptor_index = 1; // Skip the return type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001178 uint32_t dex_pc = invoke->GetDexPc();
Calin Juravle68ad6492015-08-18 17:08:12 +01001179
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001180 for (size_t i = start_index;
1181 // Make sure we don't go over the expected arguments or over the number of
1182 // dex registers given. If the instruction was seen as dead by the verifier,
1183 // it hasn't been properly checked.
Calin Juravle5d01db12015-08-25 15:02:42 +01001184 (i < number_of_vreg_arguments) && (*argument_index < invoke->GetNumberOfArguments());
1185 i++, (*argument_index)++) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001186 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001187 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001188 if (!is_range
1189 && is_wide
1190 && ((i + 1 == number_of_vreg_arguments) || (args[i] + 1 != args[i + 1]))) {
1191 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
1192 // reject any class where this is violated. However, the verifier only does these checks
1193 // on non trivially dead instructions, so we just bailout the compilation.
1194 VLOG(compiler) << "Did not compile "
1195 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
1196 << " because of non-sequential dex register pair in wide argument";
1197 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1198 return false;
1199 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001200 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type, dex_pc);
Calin Juravle5d01db12015-08-25 15:02:42 +01001201 invoke->SetArgumentAt(*argument_index, arg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001202 if (is_wide) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001203 i++;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001204 }
1205 }
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001206
Calin Juravle5d01db12015-08-25 15:02:42 +01001207 if (*argument_index != invoke->GetNumberOfArguments()) {
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001208 VLOG(compiler) << "Did not compile "
1209 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
1210 << " because of wrong number of arguments in invoke instruction";
1211 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1212 return false;
1213 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001214
Vladimir Markob554b5a2015-11-06 12:57:55 +00001215 if (invoke->IsInvokeStaticOrDirect() &&
1216 HInvokeStaticOrDirect::NeedsCurrentMethodInput(
1217 invoke->AsInvokeStaticOrDirect()->GetMethodLoadKind())) {
Calin Juravle5d01db12015-08-25 15:02:42 +01001218 invoke->SetArgumentAt(*argument_index, graph_->GetCurrentMethod());
1219 (*argument_index)++;
1220 }
1221
1222 return true;
1223}
1224
1225bool HGraphBuilder::HandleInvoke(HInvoke* invoke,
1226 uint32_t number_of_vreg_arguments,
1227 uint32_t* args,
1228 uint32_t register_index,
1229 bool is_range,
1230 const char* descriptor,
1231 HClinitCheck* clinit_check) {
1232 DCHECK(!invoke->IsInvokeStaticOrDirect() || !invoke->AsInvokeStaticOrDirect()->IsStringInit());
1233
1234 size_t start_index = 0;
1235 size_t argument_index = 0;
1236 if (invoke->GetOriginalInvokeType() != InvokeType::kStatic) { // Instance call.
1237 Temporaries temps(graph_);
1238 HInstruction* arg = LoadLocal(
1239 is_range ? register_index : args[0], Primitive::kPrimNot, invoke->GetDexPc());
1240 HNullCheck* null_check = new (arena_) HNullCheck(arg, invoke->GetDexPc());
1241 current_block_->AddInstruction(null_check);
1242 temps.Add(null_check);
1243 invoke->SetArgumentAt(0, null_check);
1244 start_index = 1;
1245 argument_index = 1;
1246 }
1247
1248 if (!SetupInvokeArguments(invoke,
1249 number_of_vreg_arguments,
1250 args,
1251 register_index,
1252 is_range,
1253 descriptor,
1254 start_index,
1255 &argument_index)) {
1256 return false;
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001257 }
1258
Calin Juravle68ad6492015-08-18 17:08:12 +01001259 if (clinit_check != nullptr) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001260 // Add the class initialization check as last input of `invoke`.
Calin Juravle68ad6492015-08-18 17:08:12 +01001261 DCHECK(invoke->IsInvokeStaticOrDirect());
1262 DCHECK(invoke->AsInvokeStaticOrDirect()->GetClinitCheckRequirement()
1263 == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit);
Roland Levillain3e3d7332015-04-28 11:00:54 +01001264 invoke->SetArgumentAt(argument_index, clinit_check);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001265 argument_index++;
Roland Levillain4c0eb422015-04-24 16:43:49 +01001266 }
1267
Calin Juravle5d01db12015-08-25 15:02:42 +01001268 current_block_->AddInstruction(invoke);
1269 latest_result_ = invoke;
1270
1271 return true;
1272}
1273
1274bool HGraphBuilder::HandleStringInit(HInvoke* invoke,
1275 uint32_t number_of_vreg_arguments,
1276 uint32_t* args,
1277 uint32_t register_index,
1278 bool is_range,
1279 const char* descriptor) {
1280 DCHECK(invoke->IsInvokeStaticOrDirect());
1281 DCHECK(invoke->AsInvokeStaticOrDirect()->IsStringInit());
1282
1283 size_t start_index = 1;
1284 size_t argument_index = 0;
1285 if (!SetupInvokeArguments(invoke,
1286 number_of_vreg_arguments,
1287 args,
1288 register_index,
1289 is_range,
1290 descriptor,
1291 start_index,
1292 &argument_index)) {
1293 return false;
Jeff Hao848f70a2014-01-15 13:49:50 -08001294 }
Calin Juravle0eedd7e2015-08-20 14:48:00 +01001295
Calin Juravle5d01db12015-08-25 15:02:42 +01001296 // Add move-result for StringFactory method.
1297 uint32_t orig_this_reg = is_range ? register_index : args[0];
David Brazdil6de19382016-01-08 17:37:10 +00001298 HInstruction* new_instance = LoadLocal(orig_this_reg, Primitive::kPrimNot, invoke->GetDexPc());
1299 invoke->SetArgumentAt(argument_index, new_instance);
Calin Juravle5d01db12015-08-25 15:02:42 +01001300 current_block_->AddInstruction(invoke);
Calin Juravle5d01db12015-08-25 15:02:42 +01001301
Calin Juravle0eedd7e2015-08-20 14:48:00 +01001302 latest_result_ = invoke;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001303 return true;
1304}
1305
Calin Juravlee460d1d2015-09-29 04:52:17 +01001306static Primitive::Type GetFieldAccessType(const DexFile& dex_file, uint16_t field_index) {
1307 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
1308 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
1309 return Primitive::GetType(type[0]);
1310}
1311
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001312bool HGraphBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001313 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001314 bool is_put) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001315 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1316 uint32_t obj_reg = instruction.VRegB_22c();
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001317 uint16_t field_index;
1318 if (instruction.IsQuickened()) {
1319 if (!CanDecodeQuickenedInfo()) {
1320 return false;
1321 }
1322 field_index = LookupQuickenedInfo(dex_pc);
1323 } else {
1324 field_index = instruction.VRegC_22c();
1325 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001326
1327 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierc7853442015-03-27 14:35:38 -07001328 ArtField* resolved_field =
1329 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001330
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001331
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001332 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot, dex_pc);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001333 HInstruction* null_check = new (arena_) HNullCheck(object, dex_pc);
1334 current_block_->AddInstruction(null_check);
1335
1336 Primitive::Type field_type = (resolved_field == nullptr)
1337 ? GetFieldAccessType(*dex_file_, field_index)
1338 : resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001339 if (is_put) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001340 Temporaries temps(graph_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001341 // We need one temporary for the null check.
1342 temps.Add(null_check);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001343 HInstruction* value = LoadLocal(source_or_dest_reg, field_type, dex_pc);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001344 HInstruction* field_set = nullptr;
1345 if (resolved_field == nullptr) {
1346 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
1347 field_set = new (arena_) HUnresolvedInstanceFieldSet(null_check,
1348 value,
1349 field_type,
1350 field_index,
1351 dex_pc);
1352 } else {
Mingyao Yang8df69d42015-10-22 15:40:58 -07001353 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Calin Juravlee460d1d2015-09-29 04:52:17 +01001354 field_set = new (arena_) HInstanceFieldSet(null_check,
1355 value,
1356 field_type,
1357 resolved_field->GetOffset(),
1358 resolved_field->IsVolatile(),
1359 field_index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07001360 class_def_index,
Calin Juravlee460d1d2015-09-29 04:52:17 +01001361 *dex_file_,
1362 dex_compilation_unit_->GetDexCache(),
1363 dex_pc);
1364 }
1365 current_block_->AddInstruction(field_set);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001366 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001367 HInstruction* field_get = nullptr;
1368 if (resolved_field == nullptr) {
1369 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
1370 field_get = new (arena_) HUnresolvedInstanceFieldGet(null_check,
1371 field_type,
1372 field_index,
1373 dex_pc);
1374 } else {
Mingyao Yang8df69d42015-10-22 15:40:58 -07001375 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Calin Juravlee460d1d2015-09-29 04:52:17 +01001376 field_get = new (arena_) HInstanceFieldGet(null_check,
1377 field_type,
1378 resolved_field->GetOffset(),
1379 resolved_field->IsVolatile(),
1380 field_index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07001381 class_def_index,
Calin Juravlee460d1d2015-09-29 04:52:17 +01001382 *dex_file_,
1383 dex_compilation_unit_->GetDexCache(),
1384 dex_pc);
1385 }
1386 current_block_->AddInstruction(field_get);
1387 UpdateLocal(source_or_dest_reg, field_get, dex_pc);
Calin Juravlee6f49b42015-09-17 14:04:33 +00001388 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001389
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001390 return true;
1391}
1392
Nicolas Geoffray30451742015-06-19 13:32:41 +01001393static mirror::Class* GetClassFrom(CompilerDriver* driver,
1394 const DexCompilationUnit& compilation_unit) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001395 ScopedObjectAccess soa(Thread::Current());
1396 StackHandleScope<2> hs(soa.Self());
Nicolas Geoffray30451742015-06-19 13:32:41 +01001397 const DexFile& dex_file = *compilation_unit.GetDexFile();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001398 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Nicolas Geoffray30451742015-06-19 13:32:41 +01001399 soa.Decode<mirror::ClassLoader*>(compilation_unit.GetClassLoader())));
1400 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001401 compilation_unit.GetClassLinker()->FindDexCache(soa.Self(), dex_file)));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001402
Nicolas Geoffray30451742015-06-19 13:32:41 +01001403 return driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, &compilation_unit);
1404}
1405
1406mirror::Class* HGraphBuilder::GetOutermostCompilingClass() const {
1407 return GetClassFrom(compiler_driver_, *outer_compilation_unit_);
1408}
1409
1410mirror::Class* HGraphBuilder::GetCompilingClass() const {
1411 return GetClassFrom(compiler_driver_, *dex_compilation_unit_);
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001412}
1413
1414bool HGraphBuilder::IsOutermostCompilingClass(uint16_t type_index) const {
1415 ScopedObjectAccess soa(Thread::Current());
1416 StackHandleScope<4> hs(soa.Self());
1417 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001418 dex_compilation_unit_->GetClassLinker()->FindDexCache(
1419 soa.Self(), *dex_compilation_unit_->GetDexFile())));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001420 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1421 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
1422 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
1423 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
Nicolas Geoffrayafd06412015-06-20 22:44:47 +01001424 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001425
Calin Juravle4e2a5572015-10-07 18:55:43 +01001426 // GetOutermostCompilingClass returns null when the class is unresolved
1427 // (e.g. if it derives from an unresolved class). This is bogus knowing that
1428 // we are compiling it.
1429 // When this happens we cannot establish a direct relation between the current
1430 // class and the outer class, so we return false.
1431 // (Note that this is only used for optimizing invokes and field accesses)
1432 return (cls.Get() != nullptr) && (outer_class.Get() == cls.Get());
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001433}
1434
Calin Juravle07380a22015-09-17 14:15:12 +01001435void HGraphBuilder::BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
1436 uint32_t dex_pc,
1437 bool is_put,
1438 Primitive::Type field_type) {
1439 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1440 uint16_t field_index = instruction.VRegB_21c();
1441
1442 if (is_put) {
1443 HInstruction* value = LoadLocal(source_or_dest_reg, field_type, dex_pc);
1444 current_block_->AddInstruction(
1445 new (arena_) HUnresolvedStaticFieldSet(value, field_type, field_index, dex_pc));
1446 } else {
1447 current_block_->AddInstruction(
1448 new (arena_) HUnresolvedStaticFieldGet(field_type, field_index, dex_pc));
1449 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction(), dex_pc);
1450 }
1451}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001452bool HGraphBuilder::BuildStaticFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001453 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001454 bool is_put) {
1455 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1456 uint16_t field_index = instruction.VRegB_21c();
1457
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001458 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray729645a2015-11-19 13:29:02 +00001459 StackHandleScope<5> hs(soa.Self());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001460 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001461 dex_compilation_unit_->GetClassLinker()->FindDexCache(
1462 soa.Self(), *dex_compilation_unit_->GetDexFile())));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001463 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1464 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
Mathieu Chartierc7853442015-03-27 14:35:38 -07001465 ArtField* resolved_field = compiler_driver_->ResolveField(
1466 soa, dex_cache, class_loader, dex_compilation_unit_, field_index, true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001467
Mathieu Chartierc7853442015-03-27 14:35:38 -07001468 if (resolved_field == nullptr) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001469 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
1470 Primitive::Type field_type = GetFieldAccessType(*dex_file_, field_index);
Calin Juravle07380a22015-09-17 14:15:12 +01001471 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001472 return true;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001473 }
1474
Calin Juravle07380a22015-09-17 14:15:12 +01001475 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001476 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
1477 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001478 outer_compilation_unit_->GetClassLinker()->FindDexCache(soa.Self(), outer_dex_file)));
Nicolas Geoffray30451742015-06-19 13:32:41 +01001479 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001480
1481 // The index at which the field's class is stored in the DexCache's type array.
1482 uint32_t storage_index;
Nicolas Geoffray30451742015-06-19 13:32:41 +01001483 bool is_outer_class = (outer_class.Get() == resolved_field->GetDeclaringClass());
1484 if (is_outer_class) {
1485 storage_index = outer_class->GetDexTypeIndex();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001486 } else if (outer_dex_cache.Get() != dex_cache.Get()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001487 // The compiler driver cannot currently understand multiple dex caches involved. Just bailout.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001488 return false;
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001489 } else {
Calin Juravle07380a22015-09-17 14:15:12 +01001490 // TODO: This is rather expensive. Perf it and cache the results if needed.
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001491 std::pair<bool, bool> pair = compiler_driver_->IsFastStaticField(
1492 outer_dex_cache.Get(),
Nicolas Geoffray30451742015-06-19 13:32:41 +01001493 GetCompilingClass(),
Mathieu Chartierc7853442015-03-27 14:35:38 -07001494 resolved_field,
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001495 field_index,
1496 &storage_index);
1497 bool can_easily_access = is_put ? pair.second : pair.first;
1498 if (!can_easily_access) {
Calin Juravle07380a22015-09-17 14:15:12 +01001499 MaybeRecordStat(MethodCompilationStat::kUnresolvedFieldNotAFastAccess);
1500 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
1501 return true;
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001502 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001503 }
1504
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001505 bool is_in_cache =
1506 compiler_driver_->CanAssumeTypeIsPresentInDexCache(outer_dex_file, storage_index);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001507 HLoadClass* constant = new (arena_) HLoadClass(graph_->GetCurrentMethod(),
1508 storage_index,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001509 outer_dex_file,
Nicolas Geoffray30451742015-06-19 13:32:41 +01001510 is_outer_class,
Calin Juravle98893e12015-10-02 21:05:03 +01001511 dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001512 /*needs_access_check*/ false,
1513 is_in_cache);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001514 current_block_->AddInstruction(constant);
1515
1516 HInstruction* cls = constant;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00001517
1518 Handle<mirror::Class> klass(hs.NewHandle(resolved_field->GetDeclaringClass()));
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +00001519 if (!IsInitialized(klass)) {
Calin Juravle225ff812014-11-13 16:46:39 +00001520 cls = new (arena_) HClinitCheck(constant, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001521 current_block_->AddInstruction(cls);
1522 }
Mingyao Yang8df69d42015-10-22 15:40:58 -07001523
Nicolas Geoffray729645a2015-11-19 13:29:02 +00001524 uint16_t class_def_index = klass->GetDexClassDefIndex();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001525 if (is_put) {
1526 // We need to keep the class alive before loading the value.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001527 Temporaries temps(graph_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001528 temps.Add(cls);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001529 HInstruction* value = LoadLocal(source_or_dest_reg, field_type, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001530 DCHECK_EQ(value->GetType(), field_type);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001531 current_block_->AddInstruction(new (arena_) HStaticFieldSet(cls,
1532 value,
1533 field_type,
1534 resolved_field->GetOffset(),
1535 resolved_field->IsVolatile(),
1536 field_index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07001537 class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07001538 *dex_file_,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001539 dex_cache_,
1540 dex_pc));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001541 } else {
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001542 current_block_->AddInstruction(new (arena_) HStaticFieldGet(cls,
1543 field_type,
1544 resolved_field->GetOffset(),
1545 resolved_field->IsVolatile(),
1546 field_index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07001547 class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07001548 *dex_file_,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001549 dex_cache_,
1550 dex_pc));
1551 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001552 }
1553 return true;
1554}
1555
Calin Juravlebacfec32014-11-14 15:54:36 +00001556void HGraphBuilder::BuildCheckedDivRem(uint16_t out_vreg,
1557 uint16_t first_vreg,
1558 int64_t second_vreg_or_constant,
1559 uint32_t dex_pc,
1560 Primitive::Type type,
1561 bool second_is_constant,
1562 bool isDiv) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001563 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Calin Juravled0d48522014-11-04 16:40:20 +00001564
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001565 HInstruction* first = LoadLocal(first_vreg, type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001566 HInstruction* second = nullptr;
1567 if (second_is_constant) {
1568 if (type == Primitive::kPrimInt) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001569 second = graph_->GetIntConstant(second_vreg_or_constant, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001570 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001571 second = graph_->GetLongConstant(second_vreg_or_constant, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001572 }
1573 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001574 second = LoadLocal(second_vreg_or_constant, type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001575 }
1576
1577 if (!second_is_constant
1578 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0)
1579 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) {
1580 second = new (arena_) HDivZeroCheck(second, dex_pc);
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001581 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +00001582 current_block_->AddInstruction(second);
1583 temps.Add(current_block_->GetLastInstruction());
1584 }
1585
Calin Juravlebacfec32014-11-14 15:54:36 +00001586 if (isDiv) {
1587 current_block_->AddInstruction(new (arena_) HDiv(type, first, second, dex_pc));
1588 } else {
1589 current_block_->AddInstruction(new (arena_) HRem(type, first, second, dex_pc));
1590 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001591 UpdateLocal(out_vreg, current_block_->GetLastInstruction(), dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001592}
1593
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001594void HGraphBuilder::BuildArrayAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001595 uint32_t dex_pc,
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001596 bool is_put,
1597 Primitive::Type anticipated_type) {
1598 uint8_t source_or_dest_reg = instruction.VRegA_23x();
1599 uint8_t array_reg = instruction.VRegB_23x();
1600 uint8_t index_reg = instruction.VRegC_23x();
1601
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001602 // We need one temporary for the null check, one for the index, and one for the length.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001603 Temporaries temps(graph_);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001604
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001605 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001606 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001607 current_block_->AddInstruction(object);
1608 temps.Add(object);
1609
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001610 HInstruction* length = new (arena_) HArrayLength(object, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001611 current_block_->AddInstruction(length);
1612 temps.Add(length);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001613 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001614 index = new (arena_) HBoundsCheck(index, length, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001615 current_block_->AddInstruction(index);
1616 temps.Add(index);
1617 if (is_put) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001618 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001619 // TODO: Insert a type check node if the type is Object.
Nicolas Geoffray39468442014-09-02 15:17:15 +01001620 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001621 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001622 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001623 current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type, dex_pc));
1624 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001625 }
Mark Mendell1152c922015-04-24 17:06:35 -04001626 graph_->SetHasBoundsChecks(true);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001627}
1628
Calin Juravle225ff812014-11-13 16:46:39 +00001629void HGraphBuilder::BuildFilledNewArray(uint32_t dex_pc,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001630 uint32_t type_index,
1631 uint32_t number_of_vreg_arguments,
1632 bool is_range,
1633 uint32_t* args,
1634 uint32_t register_index) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001635 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments, dex_pc);
Mingyao Yangfb8464a2015-11-02 10:56:59 -08001636 bool finalizable;
1637 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index, &finalizable)
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001638 ? kQuickAllocArrayWithAccessCheck
1639 : kQuickAllocArray;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01001640 HInstruction* object = new (arena_) HNewArray(length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01001641 graph_->GetCurrentMethod(),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01001642 dex_pc,
1643 type_index,
1644 *dex_compilation_unit_->GetDexFile(),
1645 entrypoint);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001646 current_block_->AddInstruction(object);
1647
1648 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1649 DCHECK_EQ(descriptor[0], '[') << descriptor;
1650 char primitive = descriptor[1];
1651 DCHECK(primitive == 'I'
1652 || primitive == 'L'
1653 || primitive == '[') << descriptor;
1654 bool is_reference_array = (primitive == 'L') || (primitive == '[');
1655 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
1656
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001657 Temporaries temps(graph_);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001658 temps.Add(object);
1659 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001660 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type, dex_pc);
1661 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001662 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001663 new (arena_) HArraySet(object, index, value, type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001664 }
1665 latest_result_ = object;
1666}
1667
1668template <typename T>
1669void HGraphBuilder::BuildFillArrayData(HInstruction* object,
1670 const T* data,
1671 uint32_t element_count,
1672 Primitive::Type anticipated_type,
Calin Juravle225ff812014-11-13 16:46:39 +00001673 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001674 for (uint32_t i = 0; i < element_count; ++i) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001675 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1676 HInstruction* value = graph_->GetIntConstant(data[i], dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001677 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001678 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001679 }
1680}
1681
Calin Juravle225ff812014-11-13 16:46:39 +00001682void HGraphBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001683 Temporaries temps(graph_);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001684 HInstruction* array = LoadLocal(instruction.VRegA_31t(), Primitive::kPrimNot, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001685 HNullCheck* null_check = new (arena_) HNullCheck(array, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001686 current_block_->AddInstruction(null_check);
1687 temps.Add(null_check);
1688
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001689 HInstruction* length = new (arena_) HArrayLength(null_check, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001690 current_block_->AddInstruction(length);
1691
Calin Juravle225ff812014-11-13 16:46:39 +00001692 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
Calin Juravled0d48522014-11-04 16:40:20 +00001693 const Instruction::ArrayDataPayload* payload =
1694 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_start_ + payload_offset);
1695 const uint8_t* data = payload->data;
1696 uint32_t element_count = payload->element_count;
1697
1698 // Implementation of this DEX instruction seems to be that the bounds check is
1699 // done before doing any stores.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001700 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001701 current_block_->AddInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
Calin Juravled0d48522014-11-04 16:40:20 +00001702
1703 switch (payload->element_width) {
1704 case 1:
1705 BuildFillArrayData(null_check,
1706 reinterpret_cast<const int8_t*>(data),
1707 element_count,
1708 Primitive::kPrimByte,
Calin Juravle225ff812014-11-13 16:46:39 +00001709 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001710 break;
1711 case 2:
1712 BuildFillArrayData(null_check,
1713 reinterpret_cast<const int16_t*>(data),
1714 element_count,
1715 Primitive::kPrimShort,
Calin Juravle225ff812014-11-13 16:46:39 +00001716 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001717 break;
1718 case 4:
1719 BuildFillArrayData(null_check,
1720 reinterpret_cast<const int32_t*>(data),
1721 element_count,
1722 Primitive::kPrimInt,
Calin Juravle225ff812014-11-13 16:46:39 +00001723 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001724 break;
1725 case 8:
1726 BuildFillWideArrayData(null_check,
1727 reinterpret_cast<const int64_t*>(data),
1728 element_count,
Calin Juravle225ff812014-11-13 16:46:39 +00001729 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001730 break;
1731 default:
1732 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1733 }
Mark Mendell1152c922015-04-24 17:06:35 -04001734 graph_->SetHasBoundsChecks(true);
Calin Juravled0d48522014-11-04 16:40:20 +00001735}
1736
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001737void HGraphBuilder::BuildFillWideArrayData(HInstruction* object,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +01001738 const int64_t* data,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001739 uint32_t element_count,
Calin Juravle225ff812014-11-13 16:46:39 +00001740 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001741 for (uint32_t i = 0; i < element_count; ++i) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001742 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1743 HInstruction* value = graph_->GetLongConstant(data[i], dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001744 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001745 object, index, value, Primitive::kPrimLong, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001746 }
1747}
1748
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001749static TypeCheckKind ComputeTypeCheckKind(Handle<mirror::Class> cls)
1750 SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle98893e12015-10-02 21:05:03 +01001751 if (cls.Get() == nullptr) {
1752 return TypeCheckKind::kUnresolvedCheck;
1753 } else if (cls->IsInterface()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001754 return TypeCheckKind::kInterfaceCheck;
1755 } else if (cls->IsArrayClass()) {
1756 if (cls->GetComponentType()->IsObjectClass()) {
1757 return TypeCheckKind::kArrayObjectCheck;
1758 } else if (cls->CannotBeAssignedFromOtherTypes()) {
1759 return TypeCheckKind::kExactCheck;
1760 } else {
1761 return TypeCheckKind::kArrayCheck;
1762 }
1763 } else if (cls->IsFinal()) {
1764 return TypeCheckKind::kExactCheck;
1765 } else if (cls->IsAbstract()) {
1766 return TypeCheckKind::kAbstractClassCheck;
1767 } else {
1768 return TypeCheckKind::kClassHierarchyCheck;
1769 }
1770}
1771
Calin Juravle98893e12015-10-02 21:05:03 +01001772void HGraphBuilder::BuildTypeCheck(const Instruction& instruction,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001773 uint8_t destination,
1774 uint8_t reference,
1775 uint16_t type_index,
Calin Juravle225ff812014-11-13 16:46:39 +00001776 uint32_t dex_pc) {
Calin Juravle98893e12015-10-02 21:05:03 +01001777 bool type_known_final, type_known_abstract, use_declaring_class;
1778 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
1779 dex_compilation_unit_->GetDexMethodIndex(),
1780 *dex_compilation_unit_->GetDexFile(),
1781 type_index,
1782 &type_known_final,
1783 &type_known_abstract,
1784 &use_declaring_class);
1785
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001786 ScopedObjectAccess soa(Thread::Current());
1787 StackHandleScope<2> hs(soa.Self());
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001788 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001789 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001790 dex_compilation_unit_->GetClassLinker()->FindDexCache(soa.Self(), dex_file)));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001791 Handle<mirror::Class> resolved_class(hs.NewHandle(dex_cache->GetResolvedType(type_index)));
1792
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001793 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot, dex_pc);
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001794 HLoadClass* cls = new (arena_) HLoadClass(
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001795 graph_->GetCurrentMethod(),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01001796 type_index,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001797 dex_file,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01001798 IsOutermostCompilingClass(type_index),
Calin Juravle98893e12015-10-02 21:05:03 +01001799 dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001800 !can_access,
1801 compiler_driver_->CanAssumeTypeIsPresentInDexCache(dex_file, type_index));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001802 current_block_->AddInstruction(cls);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001803
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001804 // The class needs a temporary before being used by the type check.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001805 Temporaries temps(graph_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001806 temps.Add(cls);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001807
1808 TypeCheckKind check_kind = ComputeTypeCheckKind(resolved_class);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001809 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001810 current_block_->AddInstruction(new (arena_) HInstanceOf(object, cls, check_kind, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001811 UpdateLocal(destination, current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001812 } else {
1813 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
David Brazdilf5552582015-12-27 13:36:12 +00001814 // We emit a CheckCast followed by a BoundType. CheckCast is a statement
1815 // which may throw. If it succeeds BoundType sets the new type of `object`
1816 // for all subsequent uses.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001817 current_block_->AddInstruction(new (arena_) HCheckCast(object, cls, check_kind, dex_pc));
David Brazdilf5552582015-12-27 13:36:12 +00001818 current_block_->AddInstruction(new (arena_) HBoundType(object, dex_pc));
1819 UpdateLocal(reference, current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001820 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001821}
1822
Mingyao Yangfb8464a2015-11-02 10:56:59 -08001823bool HGraphBuilder::NeedsAccessCheck(uint32_t type_index, bool* finalizable) const {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001824 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
Mingyao Yangfb8464a2015-11-02 10:56:59 -08001825 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index, finalizable);
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001826}
1827
Mark Mendellfe57faa2015-09-18 09:26:15 -04001828void HGraphBuilder::BuildSwitchJumpTable(const SwitchTable& table,
1829 const Instruction& instruction,
1830 HInstruction* value,
1831 uint32_t dex_pc) {
1832 // Add the successor blocks to the current block.
1833 uint16_t num_entries = table.GetNumEntries();
1834 for (size_t i = 1; i <= num_entries; i++) {
1835 int32_t target_offset = table.GetEntryAt(i);
1836 HBasicBlock* case_target = FindBlockStartingAt(dex_pc + target_offset);
1837 DCHECK(case_target != nullptr);
1838
1839 // Add the target block as a successor.
1840 current_block_->AddSuccessor(case_target);
1841 }
1842
1843 // Add the default target block as the last successor.
1844 HBasicBlock* default_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
1845 DCHECK(default_target != nullptr);
1846 current_block_->AddSuccessor(default_target);
1847
1848 // Now add the Switch instruction.
1849 int32_t starting_key = table.GetEntryAt(0);
1850 current_block_->AddInstruction(
1851 new (arena_) HPackedSwitch(starting_key, num_entries, value, dex_pc));
1852 // This block ends with control flow.
1853 current_block_ = nullptr;
1854}
1855
Calin Juravle48c2b032014-12-09 18:11:36 +00001856void HGraphBuilder::BuildPackedSwitch(const Instruction& instruction, uint32_t dex_pc) {
David Brazdil2ef645b2015-06-17 18:20:52 +01001857 // Verifier guarantees that the payload for PackedSwitch contains:
1858 // (a) number of entries (may be zero)
1859 // (b) first and lowest switch case value (entry 0, always present)
1860 // (c) list of target pcs (entries 1 <= i <= N)
Andreas Gamped881df52014-11-24 23:28:39 -08001861 SwitchTable table(instruction, dex_pc, false);
1862
1863 // Value to test against.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001864 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt, dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08001865
Mark Mendellfe57faa2015-09-18 09:26:15 -04001866 // Starting key value.
1867 int32_t starting_key = table.GetEntryAt(0);
1868
David Brazdil2ef645b2015-06-17 18:20:52 +01001869 // Retrieve number of entries.
Andreas Gampee4d4d322014-12-04 09:09:57 -08001870 uint16_t num_entries = table.GetNumEntries();
David Brazdil2ef645b2015-06-17 18:20:52 +01001871 if (num_entries == 0) {
1872 return;
1873 }
Andreas Gampee4d4d322014-12-04 09:09:57 -08001874
Mark Mendellfe57faa2015-09-18 09:26:15 -04001875 // Don't use a packed switch if there are very few entries.
1876 if (num_entries > kSmallSwitchThreshold) {
1877 BuildSwitchJumpTable(table, instruction, value, dex_pc);
1878 } else {
1879 // Chained cmp-and-branch, starting from starting_key.
1880 for (size_t i = 1; i <= num_entries; i++) {
Mark Mendell3b9f3042015-09-24 08:43:40 -04001881 BuildSwitchCaseHelper(instruction,
1882 i,
1883 i == num_entries,
1884 table,
1885 value,
1886 starting_key + i - 1,
1887 table.GetEntryAt(i),
1888 dex_pc);
Mark Mendellfe57faa2015-09-18 09:26:15 -04001889 }
Andreas Gamped881df52014-11-24 23:28:39 -08001890 }
Andreas Gamped881df52014-11-24 23:28:39 -08001891}
1892
Calin Juravle48c2b032014-12-09 18:11:36 +00001893void HGraphBuilder::BuildSparseSwitch(const Instruction& instruction, uint32_t dex_pc) {
David Brazdil2ef645b2015-06-17 18:20:52 +01001894 // Verifier guarantees that the payload for SparseSwitch contains:
1895 // (a) number of entries (may be zero)
1896 // (b) sorted key values (entries 0 <= i < N)
1897 // (c) target pcs corresponding to the switch values (entries N <= i < 2*N)
Andreas Gampee4d4d322014-12-04 09:09:57 -08001898 SwitchTable table(instruction, dex_pc, true);
1899
1900 // Value to test against.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001901 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001902
1903 uint16_t num_entries = table.GetNumEntries();
Andreas Gampee4d4d322014-12-04 09:09:57 -08001904
1905 for (size_t i = 0; i < num_entries; i++) {
1906 BuildSwitchCaseHelper(instruction, i, i == static_cast<size_t>(num_entries) - 1, table, value,
1907 table.GetEntryAt(i), table.GetEntryAt(i + num_entries), dex_pc);
1908 }
Andreas Gampee4d4d322014-12-04 09:09:57 -08001909}
1910
1911void HGraphBuilder::BuildSwitchCaseHelper(const Instruction& instruction, size_t index,
1912 bool is_last_case, const SwitchTable& table,
1913 HInstruction* value, int32_t case_value_int,
1914 int32_t target_offset, uint32_t dex_pc) {
David Brazdil852eaff2015-02-02 15:23:05 +00001915 HBasicBlock* case_target = FindBlockStartingAt(dex_pc + target_offset);
1916 DCHECK(case_target != nullptr);
1917 PotentiallyAddSuspendCheck(case_target, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001918
1919 // The current case's value.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001920 HInstruction* this_case_value = graph_->GetIntConstant(case_value_int, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001921
1922 // Compare value and this_case_value.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001923 HEqual* comparison = new (arena_) HEqual(value, this_case_value, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001924 current_block_->AddInstruction(comparison);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001925 HInstruction* ifinst = new (arena_) HIf(comparison, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001926 current_block_->AddInstruction(ifinst);
1927
1928 // Case hit: use the target offset to determine where to go.
Andreas Gampee4d4d322014-12-04 09:09:57 -08001929 current_block_->AddSuccessor(case_target);
1930
1931 // Case miss: go to the next case (or default fall-through).
1932 // When there is a next case, we use the block stored with the table offset representing this
1933 // case (that is where we registered them in ComputeBranchTargets).
1934 // When there is no next case, we use the following instruction.
1935 // TODO: Find a good way to peel the last iteration to avoid conditional, but still have re-use.
1936 if (!is_last_case) {
1937 HBasicBlock* next_case_target = FindBlockStartingAt(table.GetDexPcForIndex(index));
1938 DCHECK(next_case_target != nullptr);
1939 current_block_->AddSuccessor(next_case_target);
1940
1941 // Need to manually add the block, as there is no dex-pc transition for the cases.
1942 graph_->AddBlock(next_case_target);
1943
1944 current_block_ = next_case_target;
1945 } else {
1946 HBasicBlock* default_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
1947 DCHECK(default_target != nullptr);
1948 current_block_->AddSuccessor(default_target);
1949 current_block_ = nullptr;
1950 }
1951}
1952
David Brazdil852eaff2015-02-02 15:23:05 +00001953void HGraphBuilder::PotentiallyAddSuspendCheck(HBasicBlock* target, uint32_t dex_pc) {
1954 int32_t target_offset = target->GetDexPc() - dex_pc;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001955 if (target_offset <= 0) {
David Brazdil852eaff2015-02-02 15:23:05 +00001956 // DX generates back edges to the first encountered return. We can save
1957 // time of later passes by not adding redundant suspend checks.
David Brazdil2fd6aa52015-02-02 18:58:27 +00001958 HInstruction* last_in_target = target->GetLastInstruction();
1959 if (last_in_target != nullptr &&
1960 (last_in_target->IsReturn() || last_in_target->IsReturnVoid())) {
1961 return;
David Brazdil852eaff2015-02-02 15:23:05 +00001962 }
1963
1964 // Add a suspend check to backward branches which may potentially loop. We
1965 // can remove them after we recognize loops in the graph.
Calin Juravle225ff812014-11-13 16:46:39 +00001966 current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_pc));
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001967 }
1968}
1969
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001970bool HGraphBuilder::CanDecodeQuickenedInfo() const {
1971 return interpreter_metadata_ != nullptr;
1972}
1973
1974uint16_t HGraphBuilder::LookupQuickenedInfo(uint32_t dex_pc) {
1975 DCHECK(interpreter_metadata_ != nullptr);
1976 uint32_t dex_pc_in_map = DecodeUnsignedLeb128(&interpreter_metadata_);
1977 DCHECK_EQ(dex_pc, dex_pc_in_map);
1978 return DecodeUnsignedLeb128(&interpreter_metadata_);
1979}
1980
Calin Juravle225ff812014-11-13 16:46:39 +00001981bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001982 if (current_block_ == nullptr) {
1983 return true; // Dead code
1984 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001985
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001986 switch (instruction.Opcode()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001987 case Instruction::CONST_4: {
1988 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001989 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n(), dex_pc);
1990 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001991 break;
1992 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001993
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001994 case Instruction::CONST_16: {
1995 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001996 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s(), dex_pc);
1997 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001998 break;
1999 }
2000
Dave Allison20dfc792014-06-16 20:44:29 -07002001 case Instruction::CONST: {
2002 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002003 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i(), dex_pc);
2004 UpdateLocal(register_index, constant, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07002005 break;
2006 }
2007
2008 case Instruction::CONST_HIGH16: {
2009 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002010 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16, dex_pc);
2011 UpdateLocal(register_index, constant, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07002012 break;
2013 }
2014
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002015 case Instruction::CONST_WIDE_16: {
2016 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07002017 // Get 16 bits of constant value, sign extended to 64 bits.
2018 int64_t value = instruction.VRegB_21s();
2019 value <<= 48;
2020 value >>= 48;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002021 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
2022 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002023 break;
2024 }
2025
2026 case Instruction::CONST_WIDE_32: {
2027 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07002028 // Get 32 bits of constant value, sign extended to 64 bits.
2029 int64_t value = instruction.VRegB_31i();
2030 value <<= 32;
2031 value >>= 32;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002032 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
2033 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002034 break;
2035 }
2036
2037 case Instruction::CONST_WIDE: {
2038 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002039 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l(), dex_pc);
2040 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002041 break;
2042 }
2043
Dave Allison20dfc792014-06-16 20:44:29 -07002044 case Instruction::CONST_WIDE_HIGH16: {
2045 int32_t register_index = instruction.VRegA();
2046 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002047 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
2048 UpdateLocal(register_index, constant, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07002049 break;
2050 }
2051
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00002052 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07002053 case Instruction::MOVE:
2054 case Instruction::MOVE_FROM16:
2055 case Instruction::MOVE_16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002056 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
2057 UpdateLocal(instruction.VRegA(), value, dex_pc);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002058 break;
2059 }
2060
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00002061 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07002062 case Instruction::MOVE_WIDE:
2063 case Instruction::MOVE_WIDE_FROM16:
2064 case Instruction::MOVE_WIDE_16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002065 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong, dex_pc);
2066 UpdateLocal(instruction.VRegA(), value, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07002067 break;
2068 }
2069
2070 case Instruction::MOVE_OBJECT:
2071 case Instruction::MOVE_OBJECT_16:
2072 case Instruction::MOVE_OBJECT_FROM16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002073 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot, dex_pc);
2074 UpdateLocal(instruction.VRegA(), value, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07002075 break;
2076 }
2077
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002078 case Instruction::RETURN_VOID_NO_BARRIER:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002079 case Instruction::RETURN_VOID: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002080 BuildReturn(instruction, Primitive::kPrimVoid, dex_pc);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002081 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002082 }
2083
Dave Allison20dfc792014-06-16 20:44:29 -07002084#define IF_XX(comparison, cond) \
Calin Juravle225ff812014-11-13 16:46:39 +00002085 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
2086 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002087
Dave Allison20dfc792014-06-16 20:44:29 -07002088 IF_XX(HEqual, EQ);
2089 IF_XX(HNotEqual, NE);
2090 IF_XX(HLessThan, LT);
2091 IF_XX(HLessThanOrEqual, LE);
2092 IF_XX(HGreaterThan, GT);
2093 IF_XX(HGreaterThanOrEqual, GE);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002094
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002095 case Instruction::GOTO:
2096 case Instruction::GOTO_16:
2097 case Instruction::GOTO_32: {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002098 int32_t offset = instruction.GetTargetOffset();
Calin Juravle225ff812014-11-13 16:46:39 +00002099 HBasicBlock* target = FindBlockStartingAt(offset + dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002100 DCHECK(target != nullptr);
David Brazdil852eaff2015-02-02 15:23:05 +00002101 PotentiallyAddSuspendCheck(target, dex_pc);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002102 current_block_->AddInstruction(new (arena_) HGoto(dex_pc));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002103 current_block_->AddSuccessor(target);
2104 current_block_ = nullptr;
2105 break;
2106 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002107
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002108 case Instruction::RETURN: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002109 BuildReturn(instruction, return_type_, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002110 break;
2111 }
2112
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002113 case Instruction::RETURN_OBJECT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002114 BuildReturn(instruction, return_type_, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002115 break;
2116 }
2117
2118 case Instruction::RETURN_WIDE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002119 BuildReturn(instruction, return_type_, dex_pc);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002120 break;
2121 }
2122
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002123 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00002124 case Instruction::INVOKE_INTERFACE:
2125 case Instruction::INVOKE_STATIC:
2126 case Instruction::INVOKE_SUPER:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002127 case Instruction::INVOKE_VIRTUAL:
2128 case Instruction::INVOKE_VIRTUAL_QUICK: {
2129 uint16_t method_idx;
2130 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_QUICK) {
2131 if (!CanDecodeQuickenedInfo()) {
2132 return false;
2133 }
2134 method_idx = LookupQuickenedInfo(dex_pc);
2135 } else {
2136 method_idx = instruction.VRegB_35c();
2137 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002138 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002139 uint32_t args[5];
Ian Rogers29a26482014-05-02 15:27:29 -07002140 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00002141 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00002142 number_of_vreg_arguments, false, args, -1)) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002143 return false;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002144 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002145 break;
2146 }
2147
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002148 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00002149 case Instruction::INVOKE_INTERFACE_RANGE:
2150 case Instruction::INVOKE_STATIC_RANGE:
2151 case Instruction::INVOKE_SUPER_RANGE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002152 case Instruction::INVOKE_VIRTUAL_RANGE:
2153 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2154 uint16_t method_idx;
2155 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK) {
2156 if (!CanDecodeQuickenedInfo()) {
2157 return false;
2158 }
2159 method_idx = LookupQuickenedInfo(dex_pc);
2160 } else {
2161 method_idx = instruction.VRegB_3rc();
2162 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002163 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
2164 uint32_t register_index = instruction.VRegC();
Calin Juravle225ff812014-11-13 16:46:39 +00002165 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002166 number_of_vreg_arguments, true, nullptr, register_index)) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002167 return false;
2168 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002169 break;
2170 }
2171
Roland Levillain88cb1752014-10-20 16:36:47 +01002172 case Instruction::NEG_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002173 Unop_12x<HNeg>(instruction, Primitive::kPrimInt, dex_pc);
Roland Levillain88cb1752014-10-20 16:36:47 +01002174 break;
2175 }
2176
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002177 case Instruction::NEG_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002178 Unop_12x<HNeg>(instruction, Primitive::kPrimLong, dex_pc);
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002179 break;
2180 }
2181
Roland Levillain3dbcb382014-10-28 17:30:07 +00002182 case Instruction::NEG_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002183 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat, dex_pc);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002184 break;
2185 }
2186
2187 case Instruction::NEG_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002188 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble, dex_pc);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002189 break;
2190 }
2191
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002192 case Instruction::NOT_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002193 Unop_12x<HNot>(instruction, Primitive::kPrimInt, dex_pc);
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002194 break;
2195 }
2196
Roland Levillain70566432014-10-24 16:20:17 +01002197 case Instruction::NOT_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002198 Unop_12x<HNot>(instruction, Primitive::kPrimLong, dex_pc);
Roland Levillain70566432014-10-24 16:20:17 +01002199 break;
2200 }
2201
Roland Levillaindff1f282014-11-05 14:15:05 +00002202 case Instruction::INT_TO_LONG: {
Roland Levillain624279f2014-12-04 11:54:28 +00002203 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong, dex_pc);
Roland Levillaindff1f282014-11-05 14:15:05 +00002204 break;
2205 }
2206
Roland Levillaincff13742014-11-17 14:32:17 +00002207 case Instruction::INT_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00002208 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00002209 break;
2210 }
2211
2212 case Instruction::INT_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00002213 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00002214 break;
2215 }
2216
Roland Levillain946e1432014-11-11 17:35:19 +00002217 case Instruction::LONG_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00002218 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt, dex_pc);
Roland Levillain946e1432014-11-11 17:35:19 +00002219 break;
2220 }
2221
Roland Levillain6d0e4832014-11-27 18:31:21 +00002222 case Instruction::LONG_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00002223 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimFloat, dex_pc);
Roland Levillain6d0e4832014-11-27 18:31:21 +00002224 break;
2225 }
2226
Roland Levillain647b9ed2014-11-27 12:06:00 +00002227 case Instruction::LONG_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00002228 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble, dex_pc);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002229 break;
2230 }
2231
Roland Levillain3f8f9362014-12-02 17:45:01 +00002232 case Instruction::FLOAT_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00002233 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimInt, dex_pc);
2234 break;
2235 }
2236
2237 case Instruction::FLOAT_TO_LONG: {
2238 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimLong, dex_pc);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002239 break;
2240 }
2241
Roland Levillain8964e2b2014-12-04 12:10:50 +00002242 case Instruction::FLOAT_TO_DOUBLE: {
2243 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimDouble, dex_pc);
2244 break;
2245 }
2246
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002247 case Instruction::DOUBLE_TO_INT: {
2248 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimInt, dex_pc);
2249 break;
2250 }
2251
2252 case Instruction::DOUBLE_TO_LONG: {
2253 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimLong, dex_pc);
2254 break;
2255 }
2256
Roland Levillain8964e2b2014-12-04 12:10:50 +00002257 case Instruction::DOUBLE_TO_FLOAT: {
2258 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimFloat, dex_pc);
2259 break;
2260 }
2261
Roland Levillain51d3fc42014-11-13 14:11:42 +00002262 case Instruction::INT_TO_BYTE: {
Roland Levillain624279f2014-12-04 11:54:28 +00002263 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte, dex_pc);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002264 break;
2265 }
2266
Roland Levillain01a8d712014-11-14 16:27:39 +00002267 case Instruction::INT_TO_SHORT: {
Roland Levillain624279f2014-12-04 11:54:28 +00002268 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort, dex_pc);
Roland Levillain01a8d712014-11-14 16:27:39 +00002269 break;
2270 }
2271
Roland Levillain981e4542014-11-14 11:47:14 +00002272 case Instruction::INT_TO_CHAR: {
Roland Levillain624279f2014-12-04 11:54:28 +00002273 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar, dex_pc);
Roland Levillain981e4542014-11-14 11:47:14 +00002274 break;
2275 }
2276
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002277 case Instruction::ADD_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002278 Binop_23x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002279 break;
2280 }
2281
2282 case Instruction::ADD_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002283 Binop_23x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002284 break;
2285 }
2286
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002287 case Instruction::ADD_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002288 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002289 break;
2290 }
2291
2292 case Instruction::ADD_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002293 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002294 break;
2295 }
2296
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002297 case Instruction::SUB_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002298 Binop_23x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002299 break;
2300 }
2301
2302 case Instruction::SUB_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002303 Binop_23x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002304 break;
2305 }
2306
Calin Juravle096cc022014-10-23 17:01:13 +01002307 case Instruction::SUB_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002308 Binop_23x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01002309 break;
2310 }
2311
2312 case Instruction::SUB_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002313 Binop_23x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01002314 break;
2315 }
2316
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002317 case Instruction::ADD_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002318 Binop_12x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002319 break;
2320 }
2321
Calin Juravle34bacdf2014-10-07 20:23:36 +01002322 case Instruction::MUL_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002323 Binop_23x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002324 break;
2325 }
2326
2327 case Instruction::MUL_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002328 Binop_23x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002329 break;
2330 }
2331
Calin Juravleb5bfa962014-10-21 18:02:24 +01002332 case Instruction::MUL_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002333 Binop_23x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01002334 break;
2335 }
2336
2337 case Instruction::MUL_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002338 Binop_23x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01002339 break;
2340 }
2341
Calin Juravled0d48522014-11-04 16:40:20 +00002342 case Instruction::DIV_INT: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002343 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2344 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravled0d48522014-11-04 16:40:20 +00002345 break;
2346 }
2347
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002348 case Instruction::DIV_LONG: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002349 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2350 dex_pc, Primitive::kPrimLong, false, true);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002351 break;
2352 }
2353
Calin Juravle7c4954d2014-10-28 16:57:40 +00002354 case Instruction::DIV_FLOAT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002355 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002356 break;
2357 }
2358
2359 case Instruction::DIV_DOUBLE: {
Calin Juravle225ff812014-11-13 16:46:39 +00002360 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002361 break;
2362 }
2363
Calin Juravlebacfec32014-11-14 15:54:36 +00002364 case Instruction::REM_INT: {
2365 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2366 dex_pc, Primitive::kPrimInt, false, false);
2367 break;
2368 }
2369
2370 case Instruction::REM_LONG: {
2371 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2372 dex_pc, Primitive::kPrimLong, false, false);
2373 break;
2374 }
2375
Calin Juravled2ec87d2014-12-08 14:24:46 +00002376 case Instruction::REM_FLOAT: {
2377 Binop_23x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2378 break;
2379 }
2380
2381 case Instruction::REM_DOUBLE: {
2382 Binop_23x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2383 break;
2384 }
2385
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002386 case Instruction::AND_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002387 Binop_23x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002388 break;
2389 }
2390
2391 case Instruction::AND_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002392 Binop_23x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002393 break;
2394 }
2395
Calin Juravle9aec02f2014-11-18 23:06:35 +00002396 case Instruction::SHL_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002397 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002398 break;
2399 }
2400
2401 case Instruction::SHL_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002402 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002403 break;
2404 }
2405
2406 case Instruction::SHR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002407 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002408 break;
2409 }
2410
2411 case Instruction::SHR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002412 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002413 break;
2414 }
2415
2416 case Instruction::USHR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002417 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002418 break;
2419 }
2420
2421 case Instruction::USHR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002422 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002423 break;
2424 }
2425
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002426 case Instruction::OR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002427 Binop_23x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002428 break;
2429 }
2430
2431 case Instruction::OR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002432 Binop_23x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002433 break;
2434 }
2435
2436 case Instruction::XOR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002437 Binop_23x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002438 break;
2439 }
2440
2441 case Instruction::XOR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002442 Binop_23x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002443 break;
2444 }
2445
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002446 case Instruction::ADD_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002447 Binop_12x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002448 break;
2449 }
2450
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002451 case Instruction::ADD_DOUBLE_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002452 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002453 break;
2454 }
2455
2456 case Instruction::ADD_FLOAT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002457 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002458 break;
2459 }
2460
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002461 case Instruction::SUB_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002462 Binop_12x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002463 break;
2464 }
2465
2466 case Instruction::SUB_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002467 Binop_12x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002468 break;
2469 }
2470
Calin Juravle096cc022014-10-23 17:01:13 +01002471 case Instruction::SUB_FLOAT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002472 Binop_12x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01002473 break;
2474 }
2475
2476 case Instruction::SUB_DOUBLE_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002477 Binop_12x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01002478 break;
2479 }
2480
Calin Juravle34bacdf2014-10-07 20:23:36 +01002481 case Instruction::MUL_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002482 Binop_12x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002483 break;
2484 }
2485
2486 case Instruction::MUL_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002487 Binop_12x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002488 break;
2489 }
2490
Calin Juravleb5bfa962014-10-21 18:02:24 +01002491 case Instruction::MUL_FLOAT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002492 Binop_12x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01002493 break;
2494 }
2495
2496 case Instruction::MUL_DOUBLE_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002497 Binop_12x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01002498 break;
2499 }
2500
Calin Juravle865fc882014-11-06 17:09:03 +00002501 case Instruction::DIV_INT_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002502 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2503 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravle865fc882014-11-06 17:09:03 +00002504 break;
2505 }
2506
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002507 case Instruction::DIV_LONG_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002508 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2509 dex_pc, Primitive::kPrimLong, false, true);
2510 break;
2511 }
2512
2513 case Instruction::REM_INT_2ADDR: {
2514 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2515 dex_pc, Primitive::kPrimInt, false, false);
2516 break;
2517 }
2518
2519 case Instruction::REM_LONG_2ADDR: {
2520 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2521 dex_pc, Primitive::kPrimLong, false, false);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002522 break;
2523 }
2524
Calin Juravled2ec87d2014-12-08 14:24:46 +00002525 case Instruction::REM_FLOAT_2ADDR: {
2526 Binop_12x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2527 break;
2528 }
2529
2530 case Instruction::REM_DOUBLE_2ADDR: {
2531 Binop_12x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2532 break;
2533 }
2534
Calin Juravle9aec02f2014-11-18 23:06:35 +00002535 case Instruction::SHL_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002536 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002537 break;
2538 }
2539
2540 case Instruction::SHL_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002541 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002542 break;
2543 }
2544
2545 case Instruction::SHR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002546 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002547 break;
2548 }
2549
2550 case Instruction::SHR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002551 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002552 break;
2553 }
2554
2555 case Instruction::USHR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002556 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002557 break;
2558 }
2559
2560 case Instruction::USHR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002561 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002562 break;
2563 }
2564
Calin Juravle7c4954d2014-10-28 16:57:40 +00002565 case Instruction::DIV_FLOAT_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00002566 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002567 break;
2568 }
2569
2570 case Instruction::DIV_DOUBLE_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00002571 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002572 break;
2573 }
2574
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002575 case Instruction::AND_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002576 Binop_12x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002577 break;
2578 }
2579
2580 case Instruction::AND_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002581 Binop_12x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002582 break;
2583 }
2584
2585 case Instruction::OR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002586 Binop_12x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002587 break;
2588 }
2589
2590 case Instruction::OR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002591 Binop_12x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002592 break;
2593 }
2594
2595 case Instruction::XOR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002596 Binop_12x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002597 break;
2598 }
2599
2600 case Instruction::XOR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002601 Binop_12x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002602 break;
2603 }
2604
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002605 case Instruction::ADD_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002606 Binop_22s<HAdd>(instruction, false, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002607 break;
2608 }
2609
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002610 case Instruction::AND_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002611 Binop_22s<HAnd>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002612 break;
2613 }
2614
2615 case Instruction::OR_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002616 Binop_22s<HOr>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002617 break;
2618 }
2619
2620 case Instruction::XOR_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002621 Binop_22s<HXor>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002622 break;
2623 }
2624
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002625 case Instruction::RSUB_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002626 Binop_22s<HSub>(instruction, true, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002627 break;
2628 }
2629
Calin Juravle34bacdf2014-10-07 20:23:36 +01002630 case Instruction::MUL_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002631 Binop_22s<HMul>(instruction, false, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002632 break;
2633 }
2634
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002635 case Instruction::ADD_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002636 Binop_22b<HAdd>(instruction, false, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002637 break;
2638 }
2639
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002640 case Instruction::AND_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002641 Binop_22b<HAnd>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002642 break;
2643 }
2644
2645 case Instruction::OR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002646 Binop_22b<HOr>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002647 break;
2648 }
2649
2650 case Instruction::XOR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002651 Binop_22b<HXor>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002652 break;
2653 }
2654
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002655 case Instruction::RSUB_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002656 Binop_22b<HSub>(instruction, true, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002657 break;
2658 }
2659
Calin Juravle34bacdf2014-10-07 20:23:36 +01002660 case Instruction::MUL_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002661 Binop_22b<HMul>(instruction, false, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002662 break;
2663 }
2664
Calin Juravled0d48522014-11-04 16:40:20 +00002665 case Instruction::DIV_INT_LIT16:
2666 case Instruction::DIV_INT_LIT8: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002667 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2668 dex_pc, Primitive::kPrimInt, true, true);
2669 break;
2670 }
2671
2672 case Instruction::REM_INT_LIT16:
2673 case Instruction::REM_INT_LIT8: {
2674 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2675 dex_pc, Primitive::kPrimInt, true, false);
Calin Juravled0d48522014-11-04 16:40:20 +00002676 break;
2677 }
2678
Calin Juravle9aec02f2014-11-18 23:06:35 +00002679 case Instruction::SHL_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002680 Binop_22b<HShl>(instruction, false, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002681 break;
2682 }
2683
2684 case Instruction::SHR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002685 Binop_22b<HShr>(instruction, false, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002686 break;
2687 }
2688
2689 case Instruction::USHR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002690 Binop_22b<HUShr>(instruction, false, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002691 break;
2692 }
2693
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002694 case Instruction::NEW_INSTANCE: {
David Brazdil6de19382016-01-08 17:37:10 +00002695 if (!BuildNewInstance(instruction.VRegB_21c(), dex_pc)) {
2696 return false;
Jeff Hao848f70a2014-01-15 13:49:50 -08002697 }
David Brazdil6de19382016-01-08 17:37:10 +00002698 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002699 break;
2700 }
2701
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002702 case Instruction::NEW_ARRAY: {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002703 uint16_t type_index = instruction.VRegC_22c();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002704 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt, dex_pc);
Mingyao Yangfb8464a2015-11-02 10:56:59 -08002705 bool finalizable;
2706 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index, &finalizable)
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002707 ? kQuickAllocArrayWithAccessCheck
2708 : kQuickAllocArray;
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002709 current_block_->AddInstruction(new (arena_) HNewArray(length,
2710 graph_->GetCurrentMethod(),
2711 dex_pc,
2712 type_index,
2713 *dex_compilation_unit_->GetDexFile(),
2714 entrypoint));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002715 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002716 break;
2717 }
2718
2719 case Instruction::FILLED_NEW_ARRAY: {
2720 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
2721 uint32_t type_index = instruction.VRegB_35c();
2722 uint32_t args[5];
2723 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00002724 BuildFilledNewArray(dex_pc, type_index, number_of_vreg_arguments, false, args, 0);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002725 break;
2726 }
2727
2728 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2729 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
2730 uint32_t type_index = instruction.VRegB_3rc();
2731 uint32_t register_index = instruction.VRegC_3rc();
2732 BuildFilledNewArray(
Calin Juravle225ff812014-11-13 16:46:39 +00002733 dex_pc, type_index, number_of_vreg_arguments, true, nullptr, register_index);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002734 break;
2735 }
2736
2737 case Instruction::FILL_ARRAY_DATA: {
Calin Juravle225ff812014-11-13 16:46:39 +00002738 BuildFillArrayData(instruction, dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002739 break;
2740 }
2741
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002742 case Instruction::MOVE_RESULT:
Dave Allison20dfc792014-06-16 20:44:29 -07002743 case Instruction::MOVE_RESULT_WIDE:
David Brazdilfc6a86a2015-06-26 10:33:45 +00002744 case Instruction::MOVE_RESULT_OBJECT: {
Nicolas Geoffray1efcc222015-06-24 12:41:20 +01002745 if (latest_result_ == nullptr) {
2746 // Only dead code can lead to this situation, where the verifier
2747 // does not reject the method.
2748 } else {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002749 // An Invoke/FilledNewArray and its MoveResult could have landed in
2750 // different blocks if there was a try/catch block boundary between
2751 // them. For Invoke, we insert a StoreLocal after the instruction. For
2752 // FilledNewArray, the local needs to be updated after the array was
2753 // filled, otherwise we might overwrite an input vreg.
2754 HStoreLocal* update_local =
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002755 new (arena_) HStoreLocal(GetLocalAt(instruction.VRegA()), latest_result_, dex_pc);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002756 HBasicBlock* block = latest_result_->GetBlock();
2757 if (block == current_block_) {
2758 // MoveResult and the previous instruction are in the same block.
2759 current_block_->AddInstruction(update_local);
2760 } else {
2761 // The two instructions are in different blocks. Insert the MoveResult
2762 // before the final control-flow instruction of the previous block.
2763 DCHECK(block->EndsWithControlFlowInstruction());
2764 DCHECK(current_block_->GetInstructions().IsEmpty());
2765 block->InsertInstructionBefore(update_local, block->GetLastInstruction());
2766 }
Nicolas Geoffray1efcc222015-06-24 12:41:20 +01002767 latest_result_ = nullptr;
2768 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002769 break;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002770 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002771
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002772 case Instruction::CMP_LONG: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002773 Binop_23x_cmp(instruction, Primitive::kPrimLong, ComparisonBias::kNoBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002774 break;
2775 }
2776
2777 case Instruction::CMPG_FLOAT: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002778 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kGtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002779 break;
2780 }
2781
2782 case Instruction::CMPG_DOUBLE: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002783 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kGtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002784 break;
2785 }
2786
2787 case Instruction::CMPL_FLOAT: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002788 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kLtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002789 break;
2790 }
2791
2792 case Instruction::CMPL_DOUBLE: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002793 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kLtBias, dex_pc);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002794 break;
2795 }
2796
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002797 case Instruction::NOP:
2798 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002799
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002800 case Instruction::IGET:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002801 case Instruction::IGET_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002802 case Instruction::IGET_WIDE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002803 case Instruction::IGET_WIDE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002804 case Instruction::IGET_OBJECT:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002805 case Instruction::IGET_OBJECT_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002806 case Instruction::IGET_BOOLEAN:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002807 case Instruction::IGET_BOOLEAN_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002808 case Instruction::IGET_BYTE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002809 case Instruction::IGET_BYTE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002810 case Instruction::IGET_CHAR:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002811 case Instruction::IGET_CHAR_QUICK:
2812 case Instruction::IGET_SHORT:
2813 case Instruction::IGET_SHORT_QUICK: {
Calin Juravle225ff812014-11-13 16:46:39 +00002814 if (!BuildInstanceFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002815 return false;
2816 }
2817 break;
2818 }
2819
2820 case Instruction::IPUT:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002821 case Instruction::IPUT_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002822 case Instruction::IPUT_WIDE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002823 case Instruction::IPUT_WIDE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002824 case Instruction::IPUT_OBJECT:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002825 case Instruction::IPUT_OBJECT_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002826 case Instruction::IPUT_BOOLEAN:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002827 case Instruction::IPUT_BOOLEAN_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002828 case Instruction::IPUT_BYTE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002829 case Instruction::IPUT_BYTE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002830 case Instruction::IPUT_CHAR:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002831 case Instruction::IPUT_CHAR_QUICK:
2832 case Instruction::IPUT_SHORT:
2833 case Instruction::IPUT_SHORT_QUICK: {
Calin Juravle225ff812014-11-13 16:46:39 +00002834 if (!BuildInstanceFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002835 return false;
2836 }
2837 break;
2838 }
2839
2840 case Instruction::SGET:
2841 case Instruction::SGET_WIDE:
2842 case Instruction::SGET_OBJECT:
2843 case Instruction::SGET_BOOLEAN:
2844 case Instruction::SGET_BYTE:
2845 case Instruction::SGET_CHAR:
2846 case Instruction::SGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002847 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002848 return false;
2849 }
2850 break;
2851 }
2852
2853 case Instruction::SPUT:
2854 case Instruction::SPUT_WIDE:
2855 case Instruction::SPUT_OBJECT:
2856 case Instruction::SPUT_BOOLEAN:
2857 case Instruction::SPUT_BYTE:
2858 case Instruction::SPUT_CHAR:
2859 case Instruction::SPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002860 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002861 return false;
2862 }
2863 break;
2864 }
2865
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002866#define ARRAY_XX(kind, anticipated_type) \
2867 case Instruction::AGET##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00002868 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002869 break; \
2870 } \
2871 case Instruction::APUT##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00002872 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002873 break; \
2874 }
2875
2876 ARRAY_XX(, Primitive::kPrimInt);
2877 ARRAY_XX(_WIDE, Primitive::kPrimLong);
2878 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
2879 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
2880 ARRAY_XX(_BYTE, Primitive::kPrimByte);
2881 ARRAY_XX(_CHAR, Primitive::kPrimChar);
2882 ARRAY_XX(_SHORT, Primitive::kPrimShort);
2883
Nicolas Geoffray39468442014-09-02 15:17:15 +01002884 case Instruction::ARRAY_LENGTH: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002885 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot, dex_pc);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002886 // No need for a temporary for the null check, it is the only input of the following
2887 // instruction.
Calin Juravle225ff812014-11-13 16:46:39 +00002888 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002889 current_block_->AddInstruction(object);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002890 current_block_->AddInstruction(new (arena_) HArrayLength(object, dex_pc));
2891 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002892 break;
2893 }
2894
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002895 case Instruction::CONST_STRING: {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00002896 uint32_t string_index = instruction.VRegB_21c();
2897 bool in_dex_cache = compiler_driver_->CanAssumeStringIsPresentInDexCache(
2898 *dex_file_, string_index);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002899 current_block_->AddInstruction(
Nicolas Geoffray917d0162015-11-24 18:25:35 +00002900 new (arena_) HLoadString(graph_->GetCurrentMethod(), string_index, dex_pc, in_dex_cache));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002901 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002902 break;
2903 }
2904
2905 case Instruction::CONST_STRING_JUMBO: {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00002906 uint32_t string_index = instruction.VRegB_31c();
2907 bool in_dex_cache = compiler_driver_->CanAssumeStringIsPresentInDexCache(
2908 *dex_file_, string_index);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002909 current_block_->AddInstruction(
Nicolas Geoffray917d0162015-11-24 18:25:35 +00002910 new (arena_) HLoadString(graph_->GetCurrentMethod(), string_index, dex_pc, in_dex_cache));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002911 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002912 break;
2913 }
2914
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002915 case Instruction::CONST_CLASS: {
2916 uint16_t type_index = instruction.VRegB_21c();
2917 bool type_known_final;
2918 bool type_known_abstract;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002919 bool dont_use_is_referrers_class;
2920 // `CanAccessTypeWithoutChecks` will tell whether the method being
2921 // built is trying to access its own class, so that the generated
2922 // code can optimize for this case. However, the optimization does not
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002923 // work for inlining, so we use `IsOutermostCompilingClass` instead.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002924 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
2925 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002926 &type_known_final, &type_known_abstract, &dont_use_is_referrers_class);
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002927 current_block_->AddInstruction(new (arena_) HLoadClass(
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002928 graph_->GetCurrentMethod(),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002929 type_index,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00002930 *dex_file_,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002931 IsOutermostCompilingClass(type_index),
Calin Juravle98893e12015-10-02 21:05:03 +01002932 dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00002933 !can_access,
2934 compiler_driver_->CanAssumeTypeIsPresentInDexCache(*dex_file_, type_index)));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002935 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002936 break;
2937 }
2938
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002939 case Instruction::MOVE_EXCEPTION: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002940 current_block_->AddInstruction(new (arena_) HLoadException(dex_pc));
2941 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction(), dex_pc);
2942 current_block_->AddInstruction(new (arena_) HClearException(dex_pc));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002943 break;
2944 }
2945
2946 case Instruction::THROW: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002947 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00002948 current_block_->AddInstruction(new (arena_) HThrow(exception, dex_pc));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002949 // A throw instruction must branch to the exit block.
2950 current_block_->AddSuccessor(exit_block_);
2951 // We finished building this block. Set the current block to null to avoid
2952 // adding dead instructions to it.
2953 current_block_ = nullptr;
2954 break;
2955 }
2956
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002957 case Instruction::INSTANCE_OF: {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002958 uint8_t destination = instruction.VRegA_22c();
2959 uint8_t reference = instruction.VRegB_22c();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002960 uint16_t type_index = instruction.VRegC_22c();
Calin Juravle98893e12015-10-02 21:05:03 +01002961 BuildTypeCheck(instruction, destination, reference, type_index, dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002962 break;
2963 }
2964
2965 case Instruction::CHECK_CAST: {
2966 uint8_t reference = instruction.VRegA_21c();
2967 uint16_t type_index = instruction.VRegB_21c();
Calin Juravle98893e12015-10-02 21:05:03 +01002968 BuildTypeCheck(instruction, -1, reference, type_index, dex_pc);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002969 break;
2970 }
2971
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002972 case Instruction::MONITOR_ENTER: {
2973 current_block_->AddInstruction(new (arena_) HMonitorOperation(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002974 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot, dex_pc),
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002975 HMonitorOperation::kEnter,
Calin Juravle225ff812014-11-13 16:46:39 +00002976 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002977 break;
2978 }
2979
2980 case Instruction::MONITOR_EXIT: {
2981 current_block_->AddInstruction(new (arena_) HMonitorOperation(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002982 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot, dex_pc),
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002983 HMonitorOperation::kExit,
Calin Juravle225ff812014-11-13 16:46:39 +00002984 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002985 break;
2986 }
2987
Andreas Gamped881df52014-11-24 23:28:39 -08002988 case Instruction::PACKED_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002989 BuildPackedSwitch(instruction, dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08002990 break;
2991 }
2992
Andreas Gampee4d4d322014-12-04 09:09:57 -08002993 case Instruction::SPARSE_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002994 BuildSparseSwitch(instruction, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08002995 break;
2996 }
2997
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002998 default:
Calin Juravle48c2b032014-12-09 18:11:36 +00002999 VLOG(compiler) << "Did not compile "
3000 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
3001 << " because of unhandled instruction "
3002 << instruction.Name();
3003 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnhandledInstruction);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003004 return false;
3005 }
3006 return true;
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00003007} // NOLINT(readability/fn_size)
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003008
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01003009HLocal* HGraphBuilder::GetLocalAt(uint32_t register_index) const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01003010 return locals_[register_index];
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003011}
3012
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01003013void HGraphBuilder::UpdateLocal(uint32_t register_index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003014 HInstruction* instruction,
3015 uint32_t dex_pc) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003016 HLocal* local = GetLocalAt(register_index);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003017 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction, dex_pc));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003018}
3019
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01003020HInstruction* HGraphBuilder::LoadLocal(uint32_t register_index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003021 Primitive::Type type,
3022 uint32_t dex_pc) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003023 HLocal* local = GetLocalAt(register_index);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003024 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type, dex_pc));
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003025 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003026}
3027
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003028} // namespace art