blob: 8ca352f573d4fede79ae88a526c3e9bdc559bff8 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Nicolas Geoffraye5038322014-07-04 09:41:32 +010017#include "builder.h"
18
Mathieu Chartierc7853442015-03-27 14:35:38 -070019#include "art_field-inl.h"
Andreas Gamped881df52014-11-24 23:28:39 -080020#include "base/logging.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010021#include "class_linker.h"
Jeff Hao848f70a2014-01-15 13:49:50 -080022#include "dex/verified_method.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000023#include "dex_file-inl.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000024#include "dex_instruction-inl.h"
Roland Levillain4c0eb422015-04-24 16:43:49 +010025#include "dex/verified_method.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010026#include "driver/compiler_driver-inl.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000027#include "driver/compiler_options.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010028#include "mirror/class_loader.h"
29#include "mirror/dex_cache.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000030#include "nodes.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000031#include "primitive.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010032#include "scoped_thread_state_change.h"
33#include "thread.h"
Vladimir Marko58155012015-08-19 12:49:41 +000034#include "utils/dex_cache_arrays_layout-inl.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000035
36namespace art {
37
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010038/**
39 * Helper class to add HTemporary instructions. This class is used when
40 * converting a DEX instruction to multiple HInstruction, and where those
41 * instructions do not die at the following instruction, but instead spans
42 * multiple instructions.
43 */
44class Temporaries : public ValueObject {
45 public:
Calin Juravlef97f9fb2014-11-11 15:38:19 +000046 explicit Temporaries(HGraph* graph) : graph_(graph), index_(0) {}
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010047
48 void Add(HInstruction* instruction) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060049 HInstruction* temp = new (graph_->GetArena()) HTemporary(index_, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010050 instruction->GetBlock()->AddInstruction(temp);
Calin Juravlef97f9fb2014-11-11 15:38:19 +000051
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010052 DCHECK(temp->GetPrevious() == instruction);
Calin Juravlef97f9fb2014-11-11 15:38:19 +000053
54 size_t offset;
55 if (instruction->GetType() == Primitive::kPrimLong
56 || instruction->GetType() == Primitive::kPrimDouble) {
57 offset = 2;
58 } else {
59 offset = 1;
60 }
61 index_ += offset;
62
63 graph_->UpdateTemporariesVRegSlots(index_);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010064 }
65
66 private:
67 HGraph* const graph_;
68
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010069 // Current index in the temporary stack, updated by `Add`.
70 size_t index_;
71};
72
Andreas Gamped881df52014-11-24 23:28:39 -080073class SwitchTable : public ValueObject {
74 public:
75 SwitchTable(const Instruction& instruction, uint32_t dex_pc, bool sparse)
76 : instruction_(instruction), dex_pc_(dex_pc), sparse_(sparse) {
77 int32_t table_offset = instruction.VRegB_31t();
78 const uint16_t* table = reinterpret_cast<const uint16_t*>(&instruction) + table_offset;
79 if (sparse) {
80 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
81 } else {
82 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
83 }
84 num_entries_ = table[1];
85 values_ = reinterpret_cast<const int32_t*>(&table[2]);
86 }
87
88 uint16_t GetNumEntries() const {
89 return num_entries_;
90 }
91
Andreas Gampee4d4d322014-12-04 09:09:57 -080092 void CheckIndex(size_t index) const {
93 if (sparse_) {
94 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
95 DCHECK_LT(index, 2 * static_cast<size_t>(num_entries_));
96 } else {
97 // In a packed table, we have the starting key and num_entries_ values.
98 DCHECK_LT(index, 1 + static_cast<size_t>(num_entries_));
99 }
100 }
101
Andreas Gamped881df52014-11-24 23:28:39 -0800102 int32_t GetEntryAt(size_t index) const {
Andreas Gampee4d4d322014-12-04 09:09:57 -0800103 CheckIndex(index);
Andreas Gamped881df52014-11-24 23:28:39 -0800104 return values_[index];
105 }
106
107 uint32_t GetDexPcForIndex(size_t index) const {
Andreas Gampee4d4d322014-12-04 09:09:57 -0800108 CheckIndex(index);
Andreas Gamped881df52014-11-24 23:28:39 -0800109 return dex_pc_ +
110 (reinterpret_cast<const int16_t*>(values_ + index) -
111 reinterpret_cast<const int16_t*>(&instruction_));
112 }
113
Andreas Gampee4d4d322014-12-04 09:09:57 -0800114 // Index of the first value in the table.
115 size_t GetFirstValueIndex() const {
116 if (sparse_) {
117 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
118 return num_entries_;
119 } else {
120 // In a packed table, we have the starting key and num_entries_ values.
121 return 1;
122 }
123 }
124
Andreas Gamped881df52014-11-24 23:28:39 -0800125 private:
126 const Instruction& instruction_;
127 const uint32_t dex_pc_;
128
129 // Whether this is a sparse-switch table (or a packed-switch one).
130 const bool sparse_;
131
132 // This can't be const as it needs to be computed off of the given instruction, and complicated
133 // expressions in the initializer list seemed very ugly.
134 uint16_t num_entries_;
135
136 const int32_t* values_;
137
138 DISALLOW_COPY_AND_ASSIGN(SwitchTable);
139};
140
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100141void HGraphBuilder::InitializeLocals(uint16_t count) {
142 graph_->SetNumberOfVRegs(count);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100143 locals_.resize(count);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000144 for (int i = 0; i < count; i++) {
145 HLocal* local = new (arena_) HLocal(i);
146 entry_block_->AddInstruction(local);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100147 locals_[i] = local;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000148 }
149}
150
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000151void HGraphBuilder::InitializeParameters(uint16_t number_of_parameters) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100152 // dex_compilation_unit_ is null only when unit testing.
153 if (dex_compilation_unit_ == nullptr) {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000154 return;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100155 }
156
157 graph_->SetNumberOfInVRegs(number_of_parameters);
158 const char* shorty = dex_compilation_unit_->GetShorty();
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100159 int locals_index = locals_.size() - number_of_parameters;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100160 int parameter_index = 0;
161
Calin Juravlee6e3bea2015-10-14 13:53:10 +0000162 const DexFile::MethodId& referrer_method_id =
163 dex_file_->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100164 if (!dex_compilation_unit_->IsStatic()) {
165 // Add the implicit 'this' argument, not expressed in the signature.
Calin Juravlee6e3bea2015-10-14 13:53:10 +0000166 HParameterValue* parameter = new (arena_) HParameterValue(*dex_file_,
167 referrer_method_id.class_idx_,
168 parameter_index++,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600169 Primitive::kPrimNot,
170 true);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100171 entry_block_->AddInstruction(parameter);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100172 HLocal* local = GetLocalAt(locals_index++);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600173 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter, local->GetDexPc()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100174 number_of_parameters--;
175 }
176
Calin Juravlee6e3bea2015-10-14 13:53:10 +0000177 const DexFile::ProtoId& proto = dex_file_->GetMethodPrototype(referrer_method_id);
178 const DexFile::TypeList* arg_types = dex_file_->GetProtoParameters(proto);
179 for (int i = 0, shorty_pos = 1; i < number_of_parameters; i++) {
180 HParameterValue* parameter = new (arena_) HParameterValue(
181 *dex_file_,
182 arg_types->GetTypeItem(shorty_pos - 1).type_idx_,
183 parameter_index++,
184 Primitive::GetType(shorty[shorty_pos]),
185 false);
186 ++shorty_pos;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100187 entry_block_->AddInstruction(parameter);
188 HLocal* local = GetLocalAt(locals_index++);
189 // Store the parameter value in the local that the dex code will use
190 // to reference that parameter.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600191 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter, local->GetDexPc()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100192 bool is_wide = (parameter->GetType() == Primitive::kPrimLong)
193 || (parameter->GetType() == Primitive::kPrimDouble);
194 if (is_wide) {
195 i++;
196 locals_index++;
197 parameter_index++;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100198 }
199 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100200}
201
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100202template<typename T>
Calin Juravle225ff812014-11-13 16:46:39 +0000203void HGraphBuilder::If_22t(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000204 int32_t target_offset = instruction.GetTargetOffset();
David Brazdil852eaff2015-02-02 15:23:05 +0000205 HBasicBlock* branch_target = FindBlockStartingAt(dex_pc + target_offset);
206 HBasicBlock* fallthrough_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
207 DCHECK(branch_target != nullptr);
208 DCHECK(fallthrough_target != nullptr);
209 PotentiallyAddSuspendCheck(branch_target, dex_pc);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600210 HInstruction* first = LoadLocal(instruction.VRegA(), Primitive::kPrimInt, dex_pc);
211 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
212 T* comparison = new (arena_) T(first, second, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700213 current_block_->AddInstruction(comparison);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600214 HInstruction* ifinst = new (arena_) HIf(comparison, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700215 current_block_->AddInstruction(ifinst);
David Brazdil852eaff2015-02-02 15:23:05 +0000216 current_block_->AddSuccessor(branch_target);
217 current_block_->AddSuccessor(fallthrough_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700218 current_block_ = nullptr;
219}
220
221template<typename T>
Calin Juravle225ff812014-11-13 16:46:39 +0000222void HGraphBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000223 int32_t target_offset = instruction.GetTargetOffset();
David Brazdil852eaff2015-02-02 15:23:05 +0000224 HBasicBlock* branch_target = FindBlockStartingAt(dex_pc + target_offset);
225 HBasicBlock* fallthrough_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
226 DCHECK(branch_target != nullptr);
227 DCHECK(fallthrough_target != nullptr);
228 PotentiallyAddSuspendCheck(branch_target, dex_pc);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600229 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt, dex_pc);
230 T* comparison = new (arena_) T(value, graph_->GetIntConstant(0, dex_pc), dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700231 current_block_->AddInstruction(comparison);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600232 HInstruction* ifinst = new (arena_) HIf(comparison, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700233 current_block_->AddInstruction(ifinst);
David Brazdil852eaff2015-02-02 15:23:05 +0000234 current_block_->AddSuccessor(branch_target);
235 current_block_->AddSuccessor(fallthrough_target);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100236 current_block_ = nullptr;
237}
238
Calin Juravle48c2b032014-12-09 18:11:36 +0000239void HGraphBuilder::MaybeRecordStat(MethodCompilationStat compilation_stat) {
240 if (compilation_stats_ != nullptr) {
241 compilation_stats_->RecordStat(compilation_stat);
242 }
243}
244
David Brazdil1b498722015-03-31 11:37:18 +0100245bool HGraphBuilder::SkipCompilation(const DexFile::CodeItem& code_item,
Calin Juravle48c2b032014-12-09 18:11:36 +0000246 size_t number_of_branches) {
247 const CompilerOptions& compiler_options = compiler_driver_->GetCompilerOptions();
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000248 CompilerOptions::CompilerFilter compiler_filter = compiler_options.GetCompilerFilter();
249 if (compiler_filter == CompilerOptions::kEverything) {
250 return false;
251 }
252
David Brazdil1b498722015-03-31 11:37:18 +0100253 if (compiler_options.IsHugeMethod(code_item.insns_size_in_code_units_)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000254 VLOG(compiler) << "Skip compilation of huge method "
255 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
David Brazdil1b498722015-03-31 11:37:18 +0100256 << ": " << code_item.insns_size_in_code_units_ << " code units";
Calin Juravle48c2b032014-12-09 18:11:36 +0000257 MaybeRecordStat(MethodCompilationStat::kNotCompiledHugeMethod);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000258 return true;
259 }
260
261 // If it's large and contains no branches, it's likely to be machine generated initialization.
David Brazdil1b498722015-03-31 11:37:18 +0100262 if (compiler_options.IsLargeMethod(code_item.insns_size_in_code_units_)
263 && (number_of_branches == 0)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000264 VLOG(compiler) << "Skip compilation of large method with no branch "
265 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
David Brazdil1b498722015-03-31 11:37:18 +0100266 << ": " << code_item.insns_size_in_code_units_ << " code units";
Calin Juravle48c2b032014-12-09 18:11:36 +0000267 MaybeRecordStat(MethodCompilationStat::kNotCompiledLargeMethodNoBranches);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000268 return true;
269 }
270
271 return false;
272}
273
David Brazdilfc6a86a2015-06-26 10:33:45 +0000274void HGraphBuilder::CreateBlocksForTryCatch(const DexFile::CodeItem& code_item) {
275 if (code_item.tries_size_ == 0) {
276 return;
277 }
278
279 // Create branch targets at the start/end of the TryItem range. These are
280 // places where the program might fall through into/out of the a block and
281 // where TryBoundary instructions will be inserted later. Other edges which
282 // enter/exit the try blocks are a result of branches/switches.
283 for (size_t idx = 0; idx < code_item.tries_size_; ++idx) {
284 const DexFile::TryItem* try_item = DexFile::GetTryItems(code_item, idx);
285 uint32_t dex_pc_start = try_item->start_addr_;
286 uint32_t dex_pc_end = dex_pc_start + try_item->insn_count_;
287 FindOrCreateBlockStartingAt(dex_pc_start);
288 if (dex_pc_end < code_item.insns_size_in_code_units_) {
289 // TODO: Do not create block if the last instruction cannot fall through.
290 FindOrCreateBlockStartingAt(dex_pc_end);
291 } else {
292 // The TryItem spans until the very end of the CodeItem (or beyond if
293 // invalid) and therefore cannot have any code afterwards.
294 }
295 }
296
297 // Create branch targets for exception handlers.
298 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(code_item, 0);
299 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
300 for (uint32_t idx = 0; idx < handlers_size; ++idx) {
301 CatchHandlerIterator iterator(handlers_ptr);
302 for (; iterator.HasNext(); iterator.Next()) {
303 uint32_t address = iterator.GetHandlerAddress();
304 HBasicBlock* block = FindOrCreateBlockStartingAt(address);
David Brazdilec16f792015-08-19 15:04:01 +0100305 block->SetTryCatchInformation(
306 new (arena_) TryCatchInformation(iterator.GetHandlerTypeIndex(), *dex_file_));
David Brazdilfc6a86a2015-06-26 10:33:45 +0000307 }
308 handlers_ptr = iterator.EndDataPointer();
309 }
310}
311
David Brazdild7558da2015-09-22 13:04:14 +0100312// Returns the TryItem stored for `block` or nullptr if there is no info for it.
313static const DexFile::TryItem* GetTryItem(
314 HBasicBlock* block,
315 const ArenaSafeMap<uint32_t, const DexFile::TryItem*>& try_block_info) {
316 auto iterator = try_block_info.find(block->GetBlockId());
317 return (iterator == try_block_info.end()) ? nullptr : iterator->second;
318}
David Brazdil56e1acc2015-06-30 15:41:36 +0100319
David Brazdild7558da2015-09-22 13:04:14 +0100320void HGraphBuilder::LinkToCatchBlocks(HTryBoundary* try_boundary,
321 const DexFile::CodeItem& code_item,
322 const DexFile::TryItem* try_item) {
323 for (CatchHandlerIterator it(code_item, *try_item); it.HasNext(); it.Next()) {
David Brazdil56e1acc2015-06-30 15:41:36 +0100324 try_boundary->AddExceptionHandler(FindBlockStartingAt(it.GetHandlerAddress()));
325 }
326}
327
David Brazdilfc6a86a2015-06-26 10:33:45 +0000328void HGraphBuilder::InsertTryBoundaryBlocks(const DexFile::CodeItem& code_item) {
329 if (code_item.tries_size_ == 0) {
330 return;
331 }
332
David Brazdild7558da2015-09-22 13:04:14 +0100333 // Keep a map of all try blocks and their respective TryItems. We do not use
334 // the block's pointer but rather its id to ensure deterministic iteration.
335 ArenaSafeMap<uint32_t, const DexFile::TryItem*> try_block_info(
Vladimir Marko5233f932015-09-29 19:01:15 +0100336 std::less<uint32_t>(), arena_->Adapter(kArenaAllocGraphBuilder));
David Brazdilbff75032015-07-08 17:26:51 +0000337
David Brazdild7558da2015-09-22 13:04:14 +0100338 // Obtain TryItem information for blocks with throwing instructions, and split
339 // blocks which are both try & catch to simplify the graph.
340 // NOTE: We are appending new blocks inside the loop, so we need to use index
341 // because iterators can be invalidated. We remember the initial size to avoid
342 // iterating over the new blocks which cannot throw.
343 for (size_t i = 0, e = graph_->GetBlocks().size(); i < e; ++i) {
344 HBasicBlock* block = graph_->GetBlocks()[i];
David Brazdil72783ff2015-07-09 14:36:05 +0100345
David Brazdild7558da2015-09-22 13:04:14 +0100346 // Do not bother creating exceptional edges for try blocks which have no
347 // throwing instructions. In that case we simply assume that the block is
348 // not covered by a TryItem. This prevents us from creating a throw-catch
349 // loop for synchronized blocks.
350 if (block->HasThrowingInstructions()) {
351 // Try to find a TryItem covering the block.
352 DCHECK_NE(block->GetDexPc(), kNoDexPc) << "Block must have a dec_pc to find its TryItem.";
353 const int32_t try_item_idx = DexFile::FindTryItem(code_item, block->GetDexPc());
354 if (try_item_idx != -1) {
355 // Block throwing and in a TryItem. Store the try block information.
356 HBasicBlock* throwing_block = block;
357 if (block->IsCatchBlock()) {
358 // Simplify blocks which are both try and catch, otherwise we would
359 // need a strategy for splitting exceptional edges. We split the block
360 // after the move-exception (if present) and mark the first part not
361 // throwing. The normal-flow edge between them will be split later.
362 HInstruction* first_insn = block->GetFirstInstruction();
363 if (first_insn->IsLoadException()) {
364 // Catch block starts with a LoadException. Split the block after
365 // the StoreLocal and ClearException which must come after the load.
366 DCHECK(first_insn->GetNext()->IsStoreLocal());
367 DCHECK(first_insn->GetNext()->GetNext()->IsClearException());
368 throwing_block = block->SplitBefore(first_insn->GetNext()->GetNext()->GetNext());
369 } else {
370 // Catch block does not load the exception. Split at the beginning
371 // to create an empty catch block.
372 throwing_block = block->SplitBefore(first_insn);
373 }
David Brazdil72783ff2015-07-09 14:36:05 +0100374 }
David Brazdild7558da2015-09-22 13:04:14 +0100375
376 try_block_info.Put(throwing_block->GetBlockId(),
377 DexFile::GetTryItems(code_item, try_item_idx));
David Brazdil72783ff2015-07-09 14:36:05 +0100378 }
David Brazdil72783ff2015-07-09 14:36:05 +0100379 }
David Brazdilbff75032015-07-08 17:26:51 +0000380 }
381
David Brazdild7558da2015-09-22 13:04:14 +0100382 // Do a pass over the try blocks and insert entering TryBoundaries where at
383 // least one predecessor is not covered by the same TryItem as the try block.
384 // We do not split each edge separately, but rather create one boundary block
385 // that all predecessors are relinked to. This preserves loop headers (b/23895756).
386 for (auto entry : try_block_info) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100387 HBasicBlock* try_block = graph_->GetBlocks()[entry.first];
David Brazdild7558da2015-09-22 13:04:14 +0100388 for (HBasicBlock* predecessor : try_block->GetPredecessors()) {
389 if (GetTryItem(predecessor, try_block_info) != entry.second) {
390 // Found a predecessor not covered by the same TryItem. Insert entering
391 // boundary block.
392 HTryBoundary* try_entry =
393 new (arena_) HTryBoundary(HTryBoundary::kEntry, try_block->GetDexPc());
394 try_block->CreateImmediateDominator()->AddInstruction(try_entry);
395 LinkToCatchBlocks(try_entry, code_item, entry.second);
396 break;
397 }
David Brazdil281a6322015-07-03 10:34:57 +0100398 }
David Brazdild7558da2015-09-22 13:04:14 +0100399 }
David Brazdil281a6322015-07-03 10:34:57 +0100400
David Brazdild7558da2015-09-22 13:04:14 +0100401 // Do a second pass over the try blocks and insert exit TryBoundaries where
402 // the successor is not in the same TryItem.
403 for (auto entry : try_block_info) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100404 HBasicBlock* try_block = graph_->GetBlocks()[entry.first];
David Brazdild7558da2015-09-22 13:04:14 +0100405 // NOTE: Do not use iterators because SplitEdge would invalidate them.
406 for (size_t i = 0, e = try_block->GetSuccessors().size(); i < e; ++i) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100407 HBasicBlock* successor = try_block->GetSuccessors()[i];
David Brazdil72783ff2015-07-09 14:36:05 +0100408
David Brazdild7558da2015-09-22 13:04:14 +0100409 // If the successor is a try block, all of its predecessors must be
410 // covered by the same TryItem. Otherwise the previous pass would have
411 // created a non-throwing boundary block.
412 if (GetTryItem(successor, try_block_info) != nullptr) {
413 DCHECK_EQ(entry.second, GetTryItem(successor, try_block_info));
David Brazdil72783ff2015-07-09 14:36:05 +0100414 continue;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000415 }
David Brazdil281a6322015-07-03 10:34:57 +0100416
David Brazdild7558da2015-09-22 13:04:14 +0100417 // Preserve the invariant that Return(Void) always jumps to Exit by moving
418 // it outside the try block if necessary.
419 HInstruction* last_instruction = try_block->GetLastInstruction();
420 if (last_instruction->IsReturn() || last_instruction->IsReturnVoid()) {
421 DCHECK_EQ(successor, exit_block_);
422 successor = try_block->SplitBefore(last_instruction);
David Brazdil281a6322015-07-03 10:34:57 +0100423 }
David Brazdild7558da2015-09-22 13:04:14 +0100424
425 // Insert TryBoundary and link to catch blocks.
426 HTryBoundary* try_exit =
427 new (arena_) HTryBoundary(HTryBoundary::kExit, successor->GetDexPc());
428 graph_->SplitEdge(try_block, successor)->AddInstruction(try_exit);
429 LinkToCatchBlocks(try_exit, code_item, entry.second);
David Brazdil281a6322015-07-03 10:34:57 +0100430 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000431 }
432}
433
David Brazdil5e8b1372015-01-23 14:39:08 +0000434bool HGraphBuilder::BuildGraph(const DexFile::CodeItem& code_item) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100435 DCHECK(graph_->GetBlocks().empty());
David Brazdil5e8b1372015-01-23 14:39:08 +0000436
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000437 const uint16_t* code_ptr = code_item.insns_;
438 const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100439 code_start_ = code_ptr;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000440
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000441 // Setup the graph with the entry block and exit block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100442 entry_block_ = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000443 graph_->AddBlock(entry_block_);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100444 exit_block_ = new (arena_) HBasicBlock(graph_, kNoDexPc);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000445 graph_->SetEntryBlock(entry_block_);
446 graph_->SetExitBlock(exit_block_);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000447
David Brazdil77a48ae2015-09-15 12:34:04 +0000448 graph_->SetHasTryCatch(code_item.tries_size_ != 0);
449
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000450 InitializeLocals(code_item.registers_size_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000451 graph_->SetMaximumNumberOfOutVRegs(code_item.outs_size_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000452
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000453 // Compute the number of dex instructions, blocks, and branches. We will
454 // check these values against limits given to the compiler.
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000455 size_t number_of_branches = 0;
456
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000457 // To avoid splitting blocks, we compute ahead of time the instructions that
458 // start a new block, and create these blocks.
Calin Juravle702d2602015-04-30 19:28:21 +0100459 if (!ComputeBranchTargets(code_ptr, code_end, &number_of_branches)) {
460 MaybeRecordStat(MethodCompilationStat::kNotCompiledBranchOutsideMethodCode);
461 return false;
462 }
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000463
464 // Note that the compiler driver is null when unit testing.
David Brazdil1b498722015-03-31 11:37:18 +0100465 if ((compiler_driver_ != nullptr) && SkipCompilation(code_item, number_of_branches)) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000466 return false;
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000467 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000468
David Brazdilfc6a86a2015-06-26 10:33:45 +0000469 CreateBlocksForTryCatch(code_item);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000470
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000471 InitializeParameters(code_item.ins_size_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100472
Calin Juravle225ff812014-11-13 16:46:39 +0000473 size_t dex_pc = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000474 while (code_ptr < code_end) {
Calin Juravle225ff812014-11-13 16:46:39 +0000475 // Update the current block if dex_pc starts a new block.
476 MaybeUpdateCurrentBlock(dex_pc);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000477 const Instruction& instruction = *Instruction::At(code_ptr);
Calin Juravle48c2b032014-12-09 18:11:36 +0000478 if (!AnalyzeDexInstruction(instruction, dex_pc)) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000479 return false;
Calin Juravle48c2b032014-12-09 18:11:36 +0000480 }
Calin Juravle225ff812014-11-13 16:46:39 +0000481 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000482 code_ptr += instruction.SizeInCodeUnits();
483 }
484
David Brazdilfc6a86a2015-06-26 10:33:45 +0000485 // Add Exit to the exit block.
David Brazdil3e187382015-06-26 09:59:52 +0000486 exit_block_->AddInstruction(new (arena_) HExit());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000487 // Add the suspend check to the entry block.
488 entry_block_->AddInstruction(new (arena_) HSuspendCheck(0));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000489 entry_block_->AddInstruction(new (arena_) HGoto());
David Brazdilbff75032015-07-08 17:26:51 +0000490 // Add the exit block at the end.
491 graph_->AddBlock(exit_block_);
David Brazdil5e8b1372015-01-23 14:39:08 +0000492
David Brazdilfc6a86a2015-06-26 10:33:45 +0000493 // Iterate over blocks covered by TryItems and insert TryBoundaries at entry
494 // and exit points. This requires all control-flow instructions and
495 // non-exceptional edges to have been created.
496 InsertTryBoundaryBlocks(code_item);
497
David Brazdil5e8b1372015-01-23 14:39:08 +0000498 return true;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000499}
500
David Brazdilfc6a86a2015-06-26 10:33:45 +0000501void HGraphBuilder::MaybeUpdateCurrentBlock(size_t dex_pc) {
502 HBasicBlock* block = FindBlockStartingAt(dex_pc);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000503 if (block == nullptr) {
504 return;
505 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000506
507 if (current_block_ != nullptr) {
508 // Branching instructions clear current_block, so we know
509 // the last instruction of the current block is not a branching
510 // instruction. We add an unconditional goto to the found block.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600511 current_block_->AddInstruction(new (arena_) HGoto(dex_pc));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000512 current_block_->AddSuccessor(block);
513 }
514 graph_->AddBlock(block);
515 current_block_ = block;
516}
517
Calin Juravle702d2602015-04-30 19:28:21 +0100518bool HGraphBuilder::ComputeBranchTargets(const uint16_t* code_ptr,
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000519 const uint16_t* code_end,
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000520 size_t* number_of_branches) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100521 branch_targets_.resize(code_end - code_ptr, nullptr);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000522
523 // Create the first block for the dex instructions, single successor of the entry block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100524 HBasicBlock* block = new (arena_) HBasicBlock(graph_, 0);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100525 branch_targets_[0] = block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000526 entry_block_->AddSuccessor(block);
527
528 // Iterate over all instructions and find branching instructions. Create blocks for
529 // the locations these instructions branch to.
Andreas Gamped881df52014-11-24 23:28:39 -0800530 uint32_t dex_pc = 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000531 while (code_ptr < code_end) {
532 const Instruction& instruction = *Instruction::At(code_ptr);
533 if (instruction.IsBranch()) {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000534 (*number_of_branches)++;
Calin Juravle225ff812014-11-13 16:46:39 +0000535 int32_t target = instruction.GetTargetOffset() + dex_pc;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000536 // Create a block for the target instruction.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000537 FindOrCreateBlockStartingAt(target);
538
Calin Juravle225ff812014-11-13 16:46:39 +0000539 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000540 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle702d2602015-04-30 19:28:21 +0100541
David Brazdilfe659462015-06-24 14:23:56 +0100542 if (instruction.CanFlowThrough()) {
543 if (code_ptr >= code_end) {
Calin Juravle702d2602015-04-30 19:28:21 +0100544 // In the normal case we should never hit this but someone can artificially forge a dex
545 // file to fall-through out the method code. In this case we bail out compilation.
546 return false;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000547 } else {
548 FindOrCreateBlockStartingAt(dex_pc);
Calin Juravle702d2602015-04-30 19:28:21 +0100549 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000550 }
Andreas Gampee4d4d322014-12-04 09:09:57 -0800551 } else if (instruction.IsSwitch()) {
552 SwitchTable table(instruction, dex_pc, instruction.Opcode() == Instruction::SPARSE_SWITCH);
Andreas Gamped881df52014-11-24 23:28:39 -0800553
554 uint16_t num_entries = table.GetNumEntries();
555
Andreas Gampee4d4d322014-12-04 09:09:57 -0800556 // In a packed-switch, the entry at index 0 is the starting key. In a sparse-switch, the
557 // entry at index 0 is the first key, and values are after *all* keys.
558 size_t offset = table.GetFirstValueIndex();
559
560 // Use a larger loop counter type to avoid overflow issues.
561 for (size_t i = 0; i < num_entries; ++i) {
Andreas Gamped881df52014-11-24 23:28:39 -0800562 // The target of the case.
Andreas Gampee4d4d322014-12-04 09:09:57 -0800563 uint32_t target = dex_pc + table.GetEntryAt(i + offset);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000564 FindOrCreateBlockStartingAt(target);
Andreas Gamped881df52014-11-24 23:28:39 -0800565
David Brazdil281a6322015-07-03 10:34:57 +0100566 // Create a block for the switch-case logic. The block gets the dex_pc
567 // of the SWITCH instruction because it is part of its semantics.
568 block = new (arena_) HBasicBlock(graph_, dex_pc);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100569 branch_targets_[table.GetDexPcForIndex(i)] = block;
Andreas Gamped881df52014-11-24 23:28:39 -0800570 }
571
572 // Fall-through. Add a block if there is more code afterwards.
573 dex_pc += instruction.SizeInCodeUnits();
574 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle702d2602015-04-30 19:28:21 +0100575 if (code_ptr >= code_end) {
576 // In the normal case we should never hit this but someone can artificially forge a dex
577 // file to fall-through out the method code. In this case we bail out compilation.
578 // (A switch can fall-through so we don't need to check CanFlowThrough().)
579 return false;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000580 } else {
581 FindOrCreateBlockStartingAt(dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -0800582 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000583 } else {
584 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle225ff812014-11-13 16:46:39 +0000585 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000586 }
587 }
Calin Juravle702d2602015-04-30 19:28:21 +0100588 return true;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000589}
590
David Brazdilfc6a86a2015-06-26 10:33:45 +0000591HBasicBlock* HGraphBuilder::FindBlockStartingAt(int32_t dex_pc) const {
592 DCHECK_GE(dex_pc, 0);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100593 return branch_targets_[dex_pc];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000594}
595
596HBasicBlock* HGraphBuilder::FindOrCreateBlockStartingAt(int32_t dex_pc) {
597 HBasicBlock* block = FindBlockStartingAt(dex_pc);
598 if (block == nullptr) {
599 block = new (arena_) HBasicBlock(graph_, dex_pc);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100600 branch_targets_[dex_pc] = block;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000601 }
602 return block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000603}
604
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100605template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600606void HGraphBuilder::Unop_12x(const Instruction& instruction,
607 Primitive::Type type,
608 uint32_t dex_pc) {
609 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
610 current_block_->AddInstruction(new (arena_) T(type, first, dex_pc));
611 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Roland Levillain88cb1752014-10-20 16:36:47 +0100612}
613
Roland Levillaindff1f282014-11-05 14:15:05 +0000614void HGraphBuilder::Conversion_12x(const Instruction& instruction,
615 Primitive::Type input_type,
Roland Levillain624279f2014-12-04 11:54:28 +0000616 Primitive::Type result_type,
617 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600618 HInstruction* first = LoadLocal(instruction.VRegB(), input_type, dex_pc);
Roland Levillain624279f2014-12-04 11:54:28 +0000619 current_block_->AddInstruction(new (arena_) HTypeConversion(result_type, first, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600620 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100621}
622
623template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000624void HGraphBuilder::Binop_23x(const Instruction& instruction,
625 Primitive::Type type,
626 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600627 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
628 HInstruction* second = LoadLocal(instruction.VRegC(), type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000629 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600630 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000631}
632
633template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000634void HGraphBuilder::Binop_23x_shift(const Instruction& instruction,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600635 Primitive::Type type,
636 uint32_t dex_pc) {
637 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
638 HInstruction* second = LoadLocal(instruction.VRegC(), Primitive::kPrimInt, dex_pc);
639 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
640 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000641}
642
Calin Juravleddb7df22014-11-25 20:56:51 +0000643void HGraphBuilder::Binop_23x_cmp(const Instruction& instruction,
644 Primitive::Type type,
Mark Mendellc4701932015-04-10 13:18:51 -0400645 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700646 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600647 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
648 HInstruction* second = LoadLocal(instruction.VRegC(), type, dex_pc);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700649 current_block_->AddInstruction(new (arena_) HCompare(type, first, second, bias, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600650 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +0000651}
652
Calin Juravle9aec02f2014-11-18 23:06:35 +0000653template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600654void HGraphBuilder::Binop_12x_shift(const Instruction& instruction, Primitive::Type type,
655 uint32_t dex_pc) {
656 HInstruction* first = LoadLocal(instruction.VRegA(), type, dex_pc);
657 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
658 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
659 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000660}
661
662template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000663void HGraphBuilder::Binop_12x(const Instruction& instruction,
664 Primitive::Type type,
665 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600666 HInstruction* first = LoadLocal(instruction.VRegA(), type, dex_pc);
667 HInstruction* second = LoadLocal(instruction.VRegB(), type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000668 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600669 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000670}
671
672template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600673void HGraphBuilder::Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
674 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
675 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100676 if (reverse) {
677 std::swap(first, second);
678 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600679 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second, dex_pc));
680 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100681}
682
683template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600684void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
685 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
686 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100687 if (reverse) {
688 std::swap(first, second);
689 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600690 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second, dex_pc));
691 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100692}
693
Calin Juravle0c25d102015-04-20 14:49:09 +0100694static bool RequiresConstructorBarrier(const DexCompilationUnit* cu, const CompilerDriver& driver) {
Calin Juravle27df7582015-04-17 19:12:31 +0100695 Thread* self = Thread::Current();
Calin Juravle0c25d102015-04-20 14:49:09 +0100696 return cu->IsConstructor()
697 && driver.RequiresConstructorBarrier(self, cu->GetDexFile(), cu->GetClassDefIndex());
Calin Juravle27df7582015-04-17 19:12:31 +0100698}
699
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600700void HGraphBuilder::BuildReturn(const Instruction& instruction,
701 Primitive::Type type,
702 uint32_t dex_pc) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100703 if (type == Primitive::kPrimVoid) {
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100704 if (graph_->ShouldGenerateConstructorBarrier()) {
705 // The compilation unit is null during testing.
706 if (dex_compilation_unit_ != nullptr) {
707 DCHECK(RequiresConstructorBarrier(dex_compilation_unit_, *compiler_driver_))
708 << "Inconsistent use of ShouldGenerateConstructorBarrier. Should not generate a barrier.";
709 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600710 current_block_->AddInstruction(new (arena_) HMemoryBarrier(kStoreStore, dex_pc));
Calin Juravle27df7582015-04-17 19:12:31 +0100711 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600712 current_block_->AddInstruction(new (arena_) HReturnVoid(dex_pc));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100713 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600714 HInstruction* value = LoadLocal(instruction.VRegA(), type, dex_pc);
715 current_block_->AddInstruction(new (arena_) HReturn(value, dex_pc));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100716 }
717 current_block_->AddSuccessor(exit_block_);
718 current_block_ = nullptr;
719}
720
Calin Juravle68ad6492015-08-18 17:08:12 +0100721static InvokeType GetInvokeTypeFromOpCode(Instruction::Code opcode) {
722 switch (opcode) {
723 case Instruction::INVOKE_STATIC:
724 case Instruction::INVOKE_STATIC_RANGE:
725 return kStatic;
726 case Instruction::INVOKE_DIRECT:
727 case Instruction::INVOKE_DIRECT_RANGE:
728 return kDirect;
729 case Instruction::INVOKE_VIRTUAL:
730 case Instruction::INVOKE_VIRTUAL_QUICK:
731 case Instruction::INVOKE_VIRTUAL_RANGE:
732 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
733 return kVirtual;
734 case Instruction::INVOKE_INTERFACE:
735 case Instruction::INVOKE_INTERFACE_RANGE:
736 return kInterface;
737 case Instruction::INVOKE_SUPER_RANGE:
738 case Instruction::INVOKE_SUPER:
739 return kSuper;
740 default:
741 LOG(FATAL) << "Unexpected invoke opcode: " << opcode;
742 UNREACHABLE();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100743 }
Calin Juravle68ad6492015-08-18 17:08:12 +0100744}
745
746bool HGraphBuilder::BuildInvoke(const Instruction& instruction,
747 uint32_t dex_pc,
748 uint32_t method_idx,
749 uint32_t number_of_vreg_arguments,
750 bool is_range,
751 uint32_t* args,
752 uint32_t register_index) {
753 InvokeType original_invoke_type = GetInvokeTypeFromOpCode(instruction.Opcode());
754 InvokeType optimized_invoke_type = original_invoke_type;
755 const char* descriptor = dex_file_->GetMethodShorty(method_idx);
756 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
757
758 // Remove the return type from the 'proto'.
759 size_t number_of_arguments = strlen(descriptor) - 1;
760 if (original_invoke_type != kStatic) { // instance call
761 // One extra argument for 'this'.
762 number_of_arguments++;
763 }
764
765 MethodReference target_method(dex_file_, method_idx);
Calin Juravle5d01db12015-08-25 15:02:42 +0100766 int32_t table_index = 0;
767 uintptr_t direct_code = 0;
768 uintptr_t direct_method = 0;
Calin Juravle68ad6492015-08-18 17:08:12 +0100769
Calin Juravle5d01db12015-08-25 15:02:42 +0100770 // Special handling for string init.
771 int32_t string_init_offset = 0;
772 bool is_string_init = compiler_driver_->IsStringInit(method_idx,
773 dex_file_,
774 &string_init_offset);
775 // Replace calls to String.<init> with StringFactory.
776 if (is_string_init) {
777 HInvokeStaticOrDirect::DispatchInfo dispatch_info = ComputeDispatchInfo(is_string_init,
778 string_init_offset,
779 target_method,
780 direct_method,
781 direct_code);
782 HInvoke* invoke = new (arena_) HInvokeStaticOrDirect(
783 arena_,
784 number_of_arguments - 1,
785 Primitive::kPrimNot /*return_type */,
786 dex_pc,
787 method_idx,
788 target_method,
789 dispatch_info,
790 original_invoke_type,
791 kStatic /* optimized_invoke_type */,
792 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit);
793 return HandleStringInit(invoke,
794 number_of_vreg_arguments,
795 args,
796 register_index,
797 is_range,
798 descriptor);
799 }
800
801 // Handle unresolved methods.
Calin Juravle68ad6492015-08-18 17:08:12 +0100802 if (!compiler_driver_->ComputeInvokeInfo(dex_compilation_unit_,
803 dex_pc,
804 true /* update_stats */,
805 true /* enable_devirtualization */,
806 &optimized_invoke_type,
807 &target_method,
808 &table_index,
809 &direct_code,
810 &direct_method)) {
Calin Juravle175dc732015-08-25 15:42:32 +0100811 MaybeRecordStat(MethodCompilationStat::kUnresolvedMethod);
812 HInvoke* invoke = new (arena_) HInvokeUnresolved(arena_,
813 number_of_arguments,
814 return_type,
815 dex_pc,
816 method_idx,
817 original_invoke_type);
818 return HandleInvoke(invoke,
819 number_of_vreg_arguments,
820 args,
821 register_index,
822 is_range,
823 descriptor,
824 nullptr /* clinit_check */);
Calin Juravle68ad6492015-08-18 17:08:12 +0100825 }
826
Calin Juravle5d01db12015-08-25 15:02:42 +0100827 // Handle resolved methods (non string init).
Calin Juravle68ad6492015-08-18 17:08:12 +0100828
Calin Juravle5d01db12015-08-25 15:02:42 +0100829 DCHECK(optimized_invoke_type != kSuper);
Calin Juravle68ad6492015-08-18 17:08:12 +0100830
831 // Potential class initialization check, in the case of a static method call.
832 HClinitCheck* clinit_check = nullptr;
833 HInvoke* invoke = nullptr;
834
Calin Juravle5d01db12015-08-25 15:02:42 +0100835 if (optimized_invoke_type == kDirect || optimized_invoke_type == kStatic) {
Calin Juravle68ad6492015-08-18 17:08:12 +0100836 // By default, consider that the called method implicitly requires
837 // an initialization check of its declaring method.
838 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement
839 = HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
Calin Juravle5d01db12015-08-25 15:02:42 +0100840 if (optimized_invoke_type == kStatic) {
Calin Juravle68ad6492015-08-18 17:08:12 +0100841 clinit_check = ProcessClinitCheckForInvoke(dex_pc, method_idx, &clinit_check_requirement);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100842 }
Calin Juravle68ad6492015-08-18 17:08:12 +0100843
Calin Juravle68ad6492015-08-18 17:08:12 +0100844 HInvokeStaticOrDirect::DispatchInfo dispatch_info = ComputeDispatchInfo(is_string_init,
845 string_init_offset,
846 target_method,
847 direct_method,
848 direct_code);
849 invoke = new (arena_) HInvokeStaticOrDirect(arena_,
850 number_of_arguments,
851 return_type,
852 dex_pc,
853 method_idx,
854 target_method,
855 dispatch_info,
856 original_invoke_type,
857 optimized_invoke_type,
858 clinit_check_requirement);
859 } else if (optimized_invoke_type == kVirtual) {
860 invoke = new (arena_) HInvokeVirtual(arena_,
861 number_of_arguments,
862 return_type,
863 dex_pc,
864 method_idx,
865 table_index);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100866 } else {
Calin Juravle68ad6492015-08-18 17:08:12 +0100867 DCHECK_EQ(optimized_invoke_type, kInterface);
868 invoke = new (arena_) HInvokeInterface(arena_,
869 number_of_arguments,
870 return_type,
871 dex_pc,
872 method_idx,
873 table_index);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100874 }
Calin Juravle68ad6492015-08-18 17:08:12 +0100875
Calin Juravle5d01db12015-08-25 15:02:42 +0100876 return HandleInvoke(invoke,
877 number_of_vreg_arguments,
878 args,
879 register_index,
880 is_range,
881 descriptor,
882 clinit_check);
Calin Juravle68ad6492015-08-18 17:08:12 +0100883}
884
885HClinitCheck* HGraphBuilder::ProcessClinitCheckForInvoke(
886 uint32_t dex_pc,
887 uint32_t method_idx,
888 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
889 ScopedObjectAccess soa(Thread::Current());
890 StackHandleScope<4> hs(soa.Self());
891 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
892 dex_compilation_unit_->GetClassLinker()->FindDexCache(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700893 soa.Self(), *dex_compilation_unit_->GetDexFile())));
Calin Juravle68ad6492015-08-18 17:08:12 +0100894 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
895 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
896 ArtMethod* resolved_method = compiler_driver_->ResolveMethod(
897 soa, dex_cache, class_loader, dex_compilation_unit_, method_idx, InvokeType::kStatic);
898
899 DCHECK(resolved_method != nullptr);
900
901 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
902 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700903 outer_compilation_unit_->GetClassLinker()->FindDexCache(soa.Self(), outer_dex_file)));
Calin Juravle68ad6492015-08-18 17:08:12 +0100904 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
905
906 // The index at which the method's class is stored in the DexCache's type array.
907 uint32_t storage_index = DexFile::kDexNoIndex;
908 bool is_outer_class = (resolved_method->GetDeclaringClass() == outer_class.Get());
909 if (is_outer_class) {
910 storage_index = outer_class->GetDexTypeIndex();
911 } else if (outer_dex_cache.Get() == dex_cache.Get()) {
912 // Get `storage_index` from IsClassOfStaticMethodAvailableToReferrer.
913 compiler_driver_->IsClassOfStaticMethodAvailableToReferrer(outer_dex_cache.Get(),
914 GetCompilingClass(),
915 resolved_method,
916 method_idx,
917 &storage_index);
918 }
919
920 HClinitCheck* clinit_check = nullptr;
921
922 if (!outer_class->IsInterface()
923 && outer_class->IsSubClass(resolved_method->GetDeclaringClass())) {
924 // If the outer class is the declaring class or a subclass
925 // of the declaring class, no class initialization is needed
926 // before the static method call.
927 // Note that in case of inlining, we do not need to add clinit checks
928 // to calls that satisfy this subclass check with any inlined methods. This
929 // will be detected by the optimization passes.
930 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
931 } else if (storage_index != DexFile::kDexNoIndex) {
932 // If the method's class type index is available, check
933 // whether we should add an explicit class initialization
934 // check for its declaring class before the static method call.
935
936 // TODO: find out why this check is needed.
937 bool is_in_dex_cache = compiler_driver_->CanAssumeTypeIsPresentInDexCache(
938 *outer_compilation_unit_->GetDexFile(), storage_index);
939 bool is_initialized =
940 resolved_method->GetDeclaringClass()->IsInitialized() && is_in_dex_cache;
941
942 if (is_initialized) {
943 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
944 } else {
945 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
946 HLoadClass* load_class = new (arena_) HLoadClass(
947 graph_->GetCurrentMethod(),
948 storage_index,
949 *dex_compilation_unit_->GetDexFile(),
950 is_outer_class,
Calin Juravle98893e12015-10-02 21:05:03 +0100951 dex_pc,
952 /*needs_access_check*/ false);
Calin Juravle68ad6492015-08-18 17:08:12 +0100953 current_block_->AddInstruction(load_class);
954 clinit_check = new (arena_) HClinitCheck(load_class, dex_pc);
955 current_block_->AddInstruction(clinit_check);
956 }
957 }
958 return clinit_check;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100959}
960
Vladimir Marko58155012015-08-19 12:49:41 +0000961HInvokeStaticOrDirect::DispatchInfo HGraphBuilder::ComputeDispatchInfo(
962 bool is_string_init,
963 int32_t string_init_offset,
964 MethodReference target_method,
965 uintptr_t direct_method,
966 uintptr_t direct_code) {
967 HInvokeStaticOrDirect::MethodLoadKind method_load_kind;
968 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location;
969 uint64_t method_load_data = 0u;
970 uint64_t direct_code_ptr = 0u;
971
972 if (is_string_init) {
973 // TODO: Use direct_method and direct_code for the appropriate StringFactory method.
974 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kStringInit;
975 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
976 method_load_data = string_init_offset;
977 } else if (target_method.dex_file == outer_compilation_unit_->GetDexFile() &&
978 target_method.dex_method_index == outer_compilation_unit_->GetDexMethodIndex()) {
979 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kRecursive;
980 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallSelf;
981 } else {
982 if (direct_method != 0u) { // Should we use a direct pointer to the method?
983 if (direct_method != static_cast<uintptr_t>(-1)) { // Is the method pointer known now?
984 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress;
985 method_load_data = direct_method;
986 } else { // The direct pointer will be known at link time.
987 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup;
988 }
989 } else { // Use dex cache.
990 DCHECK(target_method.dex_file == dex_compilation_unit_->GetDexFile());
991 DexCacheArraysLayout layout =
992 compiler_driver_->GetDexCacheArraysLayout(target_method.dex_file);
993 if (layout.Valid()) { // Can we use PC-relative access to the dex cache arrays?
994 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative;
995 method_load_data = layout.MethodOffset(target_method.dex_method_index);
996 } else { // We must go through the ArtMethod's pointer to resolved methods.
997 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
998 }
999 }
1000 if (direct_code != 0u) { // Should we use a direct pointer to the code?
1001 if (direct_code != static_cast<uintptr_t>(-1)) { // Is the code pointer known now?
1002 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallDirect;
1003 direct_code_ptr = direct_code;
1004 } else if (compiler_driver_->IsImage() ||
1005 target_method.dex_file == dex_compilation_unit_->GetDexFile()) {
1006 // Use PC-relative calls for invokes within a multi-dex oat file.
1007 // TODO: Recognize when the target dex file is within the current oat file for
1008 // app compilation. At the moment we recognize only the boot image as multi-dex.
1009 // NOTE: This will require changing the ARM backend which currently falls
1010 // through from kCallPCRelative to kDirectCodeFixup for different dex files.
1011 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative;
1012 } else { // The direct pointer will be known at link time.
1013 // NOTE: This is used for app->boot calls when compiling an app against
1014 // a relocatable but not yet relocated image.
1015 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup;
1016 }
1017 } else { // We must use the code pointer from the ArtMethod.
1018 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
1019 }
1020 }
1021
1022 if (graph_->IsDebuggable()) {
1023 // For debuggable apps always use the code pointer from ArtMethod
1024 // so that we don't circumvent instrumentation stubs if installed.
1025 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
1026 }
1027
1028 return HInvokeStaticOrDirect::DispatchInfo {
1029 method_load_kind, code_ptr_location, method_load_data, direct_code_ptr };
1030}
1031
Calin Juravle5d01db12015-08-25 15:02:42 +01001032bool HGraphBuilder::SetupInvokeArguments(HInvoke* invoke,
1033 uint32_t number_of_vreg_arguments,
1034 uint32_t* args,
1035 uint32_t register_index,
1036 bool is_range,
1037 const char* descriptor,
1038 size_t start_index,
1039 size_t* argument_index) {
Calin Juravle68ad6492015-08-18 17:08:12 +01001040 uint32_t descriptor_index = 1; // Skip the return type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001041 uint32_t dex_pc = invoke->GetDexPc();
Calin Juravle68ad6492015-08-18 17:08:12 +01001042
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001043 for (size_t i = start_index;
1044 // Make sure we don't go over the expected arguments or over the number of
1045 // dex registers given. If the instruction was seen as dead by the verifier,
1046 // it hasn't been properly checked.
Calin Juravle5d01db12015-08-25 15:02:42 +01001047 (i < number_of_vreg_arguments) && (*argument_index < invoke->GetNumberOfArguments());
1048 i++, (*argument_index)++) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001049 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001050 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001051 if (!is_range
1052 && is_wide
1053 && ((i + 1 == number_of_vreg_arguments) || (args[i] + 1 != args[i + 1]))) {
1054 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
1055 // reject any class where this is violated. However, the verifier only does these checks
1056 // on non trivially dead instructions, so we just bailout the compilation.
1057 VLOG(compiler) << "Did not compile "
1058 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
1059 << " because of non-sequential dex register pair in wide argument";
1060 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1061 return false;
1062 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001063 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type, dex_pc);
Calin Juravle5d01db12015-08-25 15:02:42 +01001064 invoke->SetArgumentAt(*argument_index, arg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001065 if (is_wide) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001066 i++;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001067 }
1068 }
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001069
Calin Juravle5d01db12015-08-25 15:02:42 +01001070 if (*argument_index != invoke->GetNumberOfArguments()) {
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001071 VLOG(compiler) << "Did not compile "
1072 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
1073 << " because of wrong number of arguments in invoke instruction";
1074 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1075 return false;
1076 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001077
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001078 if (invoke->IsInvokeStaticOrDirect()) {
Calin Juravle5d01db12015-08-25 15:02:42 +01001079 invoke->SetArgumentAt(*argument_index, graph_->GetCurrentMethod());
1080 (*argument_index)++;
1081 }
1082
1083 return true;
1084}
1085
1086bool HGraphBuilder::HandleInvoke(HInvoke* invoke,
1087 uint32_t number_of_vreg_arguments,
1088 uint32_t* args,
1089 uint32_t register_index,
1090 bool is_range,
1091 const char* descriptor,
1092 HClinitCheck* clinit_check) {
1093 DCHECK(!invoke->IsInvokeStaticOrDirect() || !invoke->AsInvokeStaticOrDirect()->IsStringInit());
1094
1095 size_t start_index = 0;
1096 size_t argument_index = 0;
1097 if (invoke->GetOriginalInvokeType() != InvokeType::kStatic) { // Instance call.
1098 Temporaries temps(graph_);
1099 HInstruction* arg = LoadLocal(
1100 is_range ? register_index : args[0], Primitive::kPrimNot, invoke->GetDexPc());
1101 HNullCheck* null_check = new (arena_) HNullCheck(arg, invoke->GetDexPc());
1102 current_block_->AddInstruction(null_check);
1103 temps.Add(null_check);
1104 invoke->SetArgumentAt(0, null_check);
1105 start_index = 1;
1106 argument_index = 1;
1107 }
1108
1109 if (!SetupInvokeArguments(invoke,
1110 number_of_vreg_arguments,
1111 args,
1112 register_index,
1113 is_range,
1114 descriptor,
1115 start_index,
1116 &argument_index)) {
1117 return false;
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001118 }
1119
Calin Juravle68ad6492015-08-18 17:08:12 +01001120 if (clinit_check != nullptr) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001121 // Add the class initialization check as last input of `invoke`.
Calin Juravle68ad6492015-08-18 17:08:12 +01001122 DCHECK(invoke->IsInvokeStaticOrDirect());
1123 DCHECK(invoke->AsInvokeStaticOrDirect()->GetClinitCheckRequirement()
1124 == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit);
Roland Levillain3e3d7332015-04-28 11:00:54 +01001125 invoke->SetArgumentAt(argument_index, clinit_check);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001126 argument_index++;
Roland Levillain4c0eb422015-04-24 16:43:49 +01001127 }
1128
Calin Juravle5d01db12015-08-25 15:02:42 +01001129 current_block_->AddInstruction(invoke);
1130 latest_result_ = invoke;
1131
1132 return true;
1133}
1134
1135bool HGraphBuilder::HandleStringInit(HInvoke* invoke,
1136 uint32_t number_of_vreg_arguments,
1137 uint32_t* args,
1138 uint32_t register_index,
1139 bool is_range,
1140 const char* descriptor) {
1141 DCHECK(invoke->IsInvokeStaticOrDirect());
1142 DCHECK(invoke->AsInvokeStaticOrDirect()->IsStringInit());
1143
1144 size_t start_index = 1;
1145 size_t argument_index = 0;
1146 if (!SetupInvokeArguments(invoke,
1147 number_of_vreg_arguments,
1148 args,
1149 register_index,
1150 is_range,
1151 descriptor,
1152 start_index,
1153 &argument_index)) {
1154 return false;
Jeff Hao848f70a2014-01-15 13:49:50 -08001155 }
Calin Juravle0eedd7e2015-08-20 14:48:00 +01001156
Calin Juravle5d01db12015-08-25 15:02:42 +01001157 // Add move-result for StringFactory method.
1158 uint32_t orig_this_reg = is_range ? register_index : args[0];
1159 HInstruction* fake_string = LoadLocal(orig_this_reg, Primitive::kPrimNot, invoke->GetDexPc());
1160 invoke->SetArgumentAt(argument_index, fake_string);
1161 current_block_->AddInstruction(invoke);
1162 PotentiallySimplifyFakeString(orig_this_reg, invoke->GetDexPc(), invoke);
1163
Calin Juravle0eedd7e2015-08-20 14:48:00 +01001164 latest_result_ = invoke;
1165
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001166 return true;
1167}
1168
Calin Juravle68ad6492015-08-18 17:08:12 +01001169void HGraphBuilder::PotentiallySimplifyFakeString(uint16_t original_dex_register,
1170 uint32_t dex_pc,
1171 HInvoke* actual_string) {
1172 if (!graph_->IsDebuggable()) {
1173 // Notify that we cannot compile with baseline. The dex registers aliasing
1174 // with `original_dex_register` will be handled when we optimize
1175 // (see HInstructionSimplifer::VisitFakeString).
1176 can_use_baseline_for_string_init_ = false;
1177 return;
1178 }
1179 const VerifiedMethod* verified_method =
1180 compiler_driver_->GetVerifiedMethod(dex_file_, dex_compilation_unit_->GetDexMethodIndex());
1181 if (verified_method != nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001182 UpdateLocal(original_dex_register, actual_string, dex_pc);
Calin Juravle68ad6492015-08-18 17:08:12 +01001183 const SafeMap<uint32_t, std::set<uint32_t>>& string_init_map =
1184 verified_method->GetStringInitPcRegMap();
1185 auto map_it = string_init_map.find(dex_pc);
1186 if (map_it != string_init_map.end()) {
Vladimir Markodbc23372015-10-12 12:45:52 +01001187 for (uint32_t reg : map_it->second) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001188 HInstruction* load_local = LoadLocal(original_dex_register, Primitive::kPrimNot, dex_pc);
Vladimir Markodbc23372015-10-12 12:45:52 +01001189 UpdateLocal(reg, load_local, dex_pc);
Calin Juravle68ad6492015-08-18 17:08:12 +01001190 }
1191 }
1192 } else {
1193 can_use_baseline_for_string_init_ = false;
1194 }
1195}
1196
Calin Juravlee460d1d2015-09-29 04:52:17 +01001197static Primitive::Type GetFieldAccessType(const DexFile& dex_file, uint16_t field_index) {
1198 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
1199 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
1200 return Primitive::GetType(type[0]);
1201}
1202
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001203bool HGraphBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001204 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001205 bool is_put) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001206 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1207 uint32_t obj_reg = instruction.VRegB_22c();
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001208 uint16_t field_index;
1209 if (instruction.IsQuickened()) {
1210 if (!CanDecodeQuickenedInfo()) {
1211 return false;
1212 }
1213 field_index = LookupQuickenedInfo(dex_pc);
1214 } else {
1215 field_index = instruction.VRegC_22c();
1216 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001217
1218 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierc7853442015-03-27 14:35:38 -07001219 ArtField* resolved_field =
1220 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001221
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001222
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001223 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot, dex_pc);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001224 HInstruction* null_check = new (arena_) HNullCheck(object, dex_pc);
1225 current_block_->AddInstruction(null_check);
1226
1227 Primitive::Type field_type = (resolved_field == nullptr)
1228 ? GetFieldAccessType(*dex_file_, field_index)
1229 : resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001230 if (is_put) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001231 Temporaries temps(graph_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001232 // We need one temporary for the null check.
1233 temps.Add(null_check);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001234 HInstruction* value = LoadLocal(source_or_dest_reg, field_type, dex_pc);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001235 HInstruction* field_set = nullptr;
1236 if (resolved_field == nullptr) {
1237 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
1238 field_set = new (arena_) HUnresolvedInstanceFieldSet(null_check,
1239 value,
1240 field_type,
1241 field_index,
1242 dex_pc);
1243 } else {
Mingyao Yang8df69d42015-10-22 15:40:58 -07001244 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Calin Juravlee460d1d2015-09-29 04:52:17 +01001245 field_set = new (arena_) HInstanceFieldSet(null_check,
1246 value,
1247 field_type,
1248 resolved_field->GetOffset(),
1249 resolved_field->IsVolatile(),
1250 field_index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07001251 class_def_index,
Calin Juravlee460d1d2015-09-29 04:52:17 +01001252 *dex_file_,
1253 dex_compilation_unit_->GetDexCache(),
1254 dex_pc);
1255 }
1256 current_block_->AddInstruction(field_set);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001257 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001258 HInstruction* field_get = nullptr;
1259 if (resolved_field == nullptr) {
1260 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
1261 field_get = new (arena_) HUnresolvedInstanceFieldGet(null_check,
1262 field_type,
1263 field_index,
1264 dex_pc);
1265 } else {
Mingyao Yang8df69d42015-10-22 15:40:58 -07001266 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Calin Juravlee460d1d2015-09-29 04:52:17 +01001267 field_get = new (arena_) HInstanceFieldGet(null_check,
1268 field_type,
1269 resolved_field->GetOffset(),
1270 resolved_field->IsVolatile(),
1271 field_index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07001272 class_def_index,
Calin Juravlee460d1d2015-09-29 04:52:17 +01001273 *dex_file_,
1274 dex_compilation_unit_->GetDexCache(),
1275 dex_pc);
1276 }
1277 current_block_->AddInstruction(field_get);
1278 UpdateLocal(source_or_dest_reg, field_get, dex_pc);
Calin Juravlee6f49b42015-09-17 14:04:33 +00001279 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001280
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001281 return true;
1282}
1283
Nicolas Geoffray30451742015-06-19 13:32:41 +01001284static mirror::Class* GetClassFrom(CompilerDriver* driver,
1285 const DexCompilationUnit& compilation_unit) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001286 ScopedObjectAccess soa(Thread::Current());
1287 StackHandleScope<2> hs(soa.Self());
Nicolas Geoffray30451742015-06-19 13:32:41 +01001288 const DexFile& dex_file = *compilation_unit.GetDexFile();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001289 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Nicolas Geoffray30451742015-06-19 13:32:41 +01001290 soa.Decode<mirror::ClassLoader*>(compilation_unit.GetClassLoader())));
1291 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001292 compilation_unit.GetClassLinker()->FindDexCache(soa.Self(), dex_file)));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001293
Nicolas Geoffray30451742015-06-19 13:32:41 +01001294 return driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, &compilation_unit);
1295}
1296
1297mirror::Class* HGraphBuilder::GetOutermostCompilingClass() const {
1298 return GetClassFrom(compiler_driver_, *outer_compilation_unit_);
1299}
1300
1301mirror::Class* HGraphBuilder::GetCompilingClass() const {
1302 return GetClassFrom(compiler_driver_, *dex_compilation_unit_);
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001303}
1304
1305bool HGraphBuilder::IsOutermostCompilingClass(uint16_t type_index) const {
1306 ScopedObjectAccess soa(Thread::Current());
1307 StackHandleScope<4> hs(soa.Self());
1308 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001309 dex_compilation_unit_->GetClassLinker()->FindDexCache(
1310 soa.Self(), *dex_compilation_unit_->GetDexFile())));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001311 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1312 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
1313 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
1314 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
Nicolas Geoffrayafd06412015-06-20 22:44:47 +01001315 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001316
Calin Juravle4e2a5572015-10-07 18:55:43 +01001317 // GetOutermostCompilingClass returns null when the class is unresolved
1318 // (e.g. if it derives from an unresolved class). This is bogus knowing that
1319 // we are compiling it.
1320 // When this happens we cannot establish a direct relation between the current
1321 // class and the outer class, so we return false.
1322 // (Note that this is only used for optimizing invokes and field accesses)
1323 return (cls.Get() != nullptr) && (outer_class.Get() == cls.Get());
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001324}
1325
Calin Juravle07380a22015-09-17 14:15:12 +01001326void HGraphBuilder::BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
1327 uint32_t dex_pc,
1328 bool is_put,
1329 Primitive::Type field_type) {
1330 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1331 uint16_t field_index = instruction.VRegB_21c();
1332
1333 if (is_put) {
1334 HInstruction* value = LoadLocal(source_or_dest_reg, field_type, dex_pc);
1335 current_block_->AddInstruction(
1336 new (arena_) HUnresolvedStaticFieldSet(value, field_type, field_index, dex_pc));
1337 } else {
1338 current_block_->AddInstruction(
1339 new (arena_) HUnresolvedStaticFieldGet(field_type, field_index, dex_pc));
1340 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction(), dex_pc);
1341 }
1342}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001343bool HGraphBuilder::BuildStaticFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001344 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001345 bool is_put) {
1346 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1347 uint16_t field_index = instruction.VRegB_21c();
1348
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001349 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierc7853442015-03-27 14:35:38 -07001350 StackHandleScope<4> hs(soa.Self());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001351 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001352 dex_compilation_unit_->GetClassLinker()->FindDexCache(
1353 soa.Self(), *dex_compilation_unit_->GetDexFile())));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001354 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1355 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
Mathieu Chartierc7853442015-03-27 14:35:38 -07001356 ArtField* resolved_field = compiler_driver_->ResolveField(
1357 soa, dex_cache, class_loader, dex_compilation_unit_, field_index, true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001358
Mathieu Chartierc7853442015-03-27 14:35:38 -07001359 if (resolved_field == nullptr) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001360 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
1361 Primitive::Type field_type = GetFieldAccessType(*dex_file_, field_index);
Calin Juravle07380a22015-09-17 14:15:12 +01001362 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001363 return true;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001364 }
1365
Calin Juravle07380a22015-09-17 14:15:12 +01001366 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001367 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
1368 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001369 outer_compilation_unit_->GetClassLinker()->FindDexCache(soa.Self(), outer_dex_file)));
Nicolas Geoffray30451742015-06-19 13:32:41 +01001370 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001371
1372 // The index at which the field's class is stored in the DexCache's type array.
1373 uint32_t storage_index;
Nicolas Geoffray30451742015-06-19 13:32:41 +01001374 bool is_outer_class = (outer_class.Get() == resolved_field->GetDeclaringClass());
1375 if (is_outer_class) {
1376 storage_index = outer_class->GetDexTypeIndex();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001377 } else if (outer_dex_cache.Get() != dex_cache.Get()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001378 // The compiler driver cannot currently understand multiple dex caches involved. Just bailout.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001379 return false;
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001380 } else {
Calin Juravle07380a22015-09-17 14:15:12 +01001381 // TODO: This is rather expensive. Perf it and cache the results if needed.
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001382 std::pair<bool, bool> pair = compiler_driver_->IsFastStaticField(
1383 outer_dex_cache.Get(),
Nicolas Geoffray30451742015-06-19 13:32:41 +01001384 GetCompilingClass(),
Mathieu Chartierc7853442015-03-27 14:35:38 -07001385 resolved_field,
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001386 field_index,
1387 &storage_index);
1388 bool can_easily_access = is_put ? pair.second : pair.first;
1389 if (!can_easily_access) {
Calin Juravle07380a22015-09-17 14:15:12 +01001390 MaybeRecordStat(MethodCompilationStat::kUnresolvedFieldNotAFastAccess);
1391 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
1392 return true;
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001393 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001394 }
1395
1396 // TODO: find out why this check is needed.
1397 bool is_in_dex_cache = compiler_driver_->CanAssumeTypeIsPresentInDexCache(
Nicolas Geoffray6a816cf2015-03-24 16:17:56 +00001398 *outer_compilation_unit_->GetDexFile(), storage_index);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001399 bool is_initialized = resolved_field->GetDeclaringClass()->IsInitialized() && is_in_dex_cache;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001400
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001401 HLoadClass* constant = new (arena_) HLoadClass(graph_->GetCurrentMethod(),
1402 storage_index,
1403 *dex_compilation_unit_->GetDexFile(),
Nicolas Geoffray30451742015-06-19 13:32:41 +01001404 is_outer_class,
Calin Juravle98893e12015-10-02 21:05:03 +01001405 dex_pc,
1406 /*needs_access_check*/ false);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001407 current_block_->AddInstruction(constant);
1408
1409 HInstruction* cls = constant;
Nicolas Geoffray30451742015-06-19 13:32:41 +01001410 if (!is_initialized && !is_outer_class) {
Calin Juravle225ff812014-11-13 16:46:39 +00001411 cls = new (arena_) HClinitCheck(constant, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001412 current_block_->AddInstruction(cls);
1413 }
Mingyao Yang8df69d42015-10-22 15:40:58 -07001414
1415 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001416 if (is_put) {
1417 // We need to keep the class alive before loading the value.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001418 Temporaries temps(graph_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001419 temps.Add(cls);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001420 HInstruction* value = LoadLocal(source_or_dest_reg, field_type, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001421 DCHECK_EQ(value->GetType(), field_type);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001422 current_block_->AddInstruction(new (arena_) HStaticFieldSet(cls,
1423 value,
1424 field_type,
1425 resolved_field->GetOffset(),
1426 resolved_field->IsVolatile(),
1427 field_index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07001428 class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07001429 *dex_file_,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001430 dex_cache_,
1431 dex_pc));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001432 } else {
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001433 current_block_->AddInstruction(new (arena_) HStaticFieldGet(cls,
1434 field_type,
1435 resolved_field->GetOffset(),
1436 resolved_field->IsVolatile(),
1437 field_index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07001438 class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07001439 *dex_file_,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001440 dex_cache_,
1441 dex_pc));
1442 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001443 }
1444 return true;
1445}
1446
Calin Juravlebacfec32014-11-14 15:54:36 +00001447void HGraphBuilder::BuildCheckedDivRem(uint16_t out_vreg,
1448 uint16_t first_vreg,
1449 int64_t second_vreg_or_constant,
1450 uint32_t dex_pc,
1451 Primitive::Type type,
1452 bool second_is_constant,
1453 bool isDiv) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001454 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Calin Juravled0d48522014-11-04 16:40:20 +00001455
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001456 HInstruction* first = LoadLocal(first_vreg, type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001457 HInstruction* second = nullptr;
1458 if (second_is_constant) {
1459 if (type == Primitive::kPrimInt) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001460 second = graph_->GetIntConstant(second_vreg_or_constant, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001461 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001462 second = graph_->GetLongConstant(second_vreg_or_constant, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001463 }
1464 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001465 second = LoadLocal(second_vreg_or_constant, type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001466 }
1467
1468 if (!second_is_constant
1469 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0)
1470 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) {
1471 second = new (arena_) HDivZeroCheck(second, dex_pc);
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001472 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +00001473 current_block_->AddInstruction(second);
1474 temps.Add(current_block_->GetLastInstruction());
1475 }
1476
Calin Juravlebacfec32014-11-14 15:54:36 +00001477 if (isDiv) {
1478 current_block_->AddInstruction(new (arena_) HDiv(type, first, second, dex_pc));
1479 } else {
1480 current_block_->AddInstruction(new (arena_) HRem(type, first, second, dex_pc));
1481 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001482 UpdateLocal(out_vreg, current_block_->GetLastInstruction(), dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001483}
1484
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001485void HGraphBuilder::BuildArrayAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001486 uint32_t dex_pc,
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001487 bool is_put,
1488 Primitive::Type anticipated_type) {
1489 uint8_t source_or_dest_reg = instruction.VRegA_23x();
1490 uint8_t array_reg = instruction.VRegB_23x();
1491 uint8_t index_reg = instruction.VRegC_23x();
1492
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001493 // We need one temporary for the null check, one for the index, and one for the length.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001494 Temporaries temps(graph_);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001495
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001496 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001497 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001498 current_block_->AddInstruction(object);
1499 temps.Add(object);
1500
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001501 HInstruction* length = new (arena_) HArrayLength(object, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001502 current_block_->AddInstruction(length);
1503 temps.Add(length);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001504 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001505 index = new (arena_) HBoundsCheck(index, length, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001506 current_block_->AddInstruction(index);
1507 temps.Add(index);
1508 if (is_put) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001509 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001510 // TODO: Insert a type check node if the type is Object.
Nicolas Geoffray39468442014-09-02 15:17:15 +01001511 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001512 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001513 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001514 current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type, dex_pc));
1515 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001516 }
Mark Mendell1152c922015-04-24 17:06:35 -04001517 graph_->SetHasBoundsChecks(true);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001518}
1519
Calin Juravle225ff812014-11-13 16:46:39 +00001520void HGraphBuilder::BuildFilledNewArray(uint32_t dex_pc,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001521 uint32_t type_index,
1522 uint32_t number_of_vreg_arguments,
1523 bool is_range,
1524 uint32_t* args,
1525 uint32_t register_index) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001526 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments, dex_pc);
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001527 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
1528 ? kQuickAllocArrayWithAccessCheck
1529 : kQuickAllocArray;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01001530 HInstruction* object = new (arena_) HNewArray(length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01001531 graph_->GetCurrentMethod(),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01001532 dex_pc,
1533 type_index,
1534 *dex_compilation_unit_->GetDexFile(),
1535 entrypoint);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001536 current_block_->AddInstruction(object);
1537
1538 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1539 DCHECK_EQ(descriptor[0], '[') << descriptor;
1540 char primitive = descriptor[1];
1541 DCHECK(primitive == 'I'
1542 || primitive == 'L'
1543 || primitive == '[') << descriptor;
1544 bool is_reference_array = (primitive == 'L') || (primitive == '[');
1545 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
1546
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001547 Temporaries temps(graph_);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001548 temps.Add(object);
1549 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001550 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type, dex_pc);
1551 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001552 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001553 new (arena_) HArraySet(object, index, value, type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001554 }
1555 latest_result_ = object;
1556}
1557
1558template <typename T>
1559void HGraphBuilder::BuildFillArrayData(HInstruction* object,
1560 const T* data,
1561 uint32_t element_count,
1562 Primitive::Type anticipated_type,
Calin Juravle225ff812014-11-13 16:46:39 +00001563 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001564 for (uint32_t i = 0; i < element_count; ++i) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001565 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1566 HInstruction* value = graph_->GetIntConstant(data[i], dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001567 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001568 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001569 }
1570}
1571
Calin Juravle225ff812014-11-13 16:46:39 +00001572void HGraphBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001573 Temporaries temps(graph_);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001574 HInstruction* array = LoadLocal(instruction.VRegA_31t(), Primitive::kPrimNot, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001575 HNullCheck* null_check = new (arena_) HNullCheck(array, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001576 current_block_->AddInstruction(null_check);
1577 temps.Add(null_check);
1578
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001579 HInstruction* length = new (arena_) HArrayLength(null_check, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001580 current_block_->AddInstruction(length);
1581
Calin Juravle225ff812014-11-13 16:46:39 +00001582 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
Calin Juravled0d48522014-11-04 16:40:20 +00001583 const Instruction::ArrayDataPayload* payload =
1584 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_start_ + payload_offset);
1585 const uint8_t* data = payload->data;
1586 uint32_t element_count = payload->element_count;
1587
1588 // Implementation of this DEX instruction seems to be that the bounds check is
1589 // done before doing any stores.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001590 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001591 current_block_->AddInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
Calin Juravled0d48522014-11-04 16:40:20 +00001592
1593 switch (payload->element_width) {
1594 case 1:
1595 BuildFillArrayData(null_check,
1596 reinterpret_cast<const int8_t*>(data),
1597 element_count,
1598 Primitive::kPrimByte,
Calin Juravle225ff812014-11-13 16:46:39 +00001599 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001600 break;
1601 case 2:
1602 BuildFillArrayData(null_check,
1603 reinterpret_cast<const int16_t*>(data),
1604 element_count,
1605 Primitive::kPrimShort,
Calin Juravle225ff812014-11-13 16:46:39 +00001606 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001607 break;
1608 case 4:
1609 BuildFillArrayData(null_check,
1610 reinterpret_cast<const int32_t*>(data),
1611 element_count,
1612 Primitive::kPrimInt,
Calin Juravle225ff812014-11-13 16:46:39 +00001613 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001614 break;
1615 case 8:
1616 BuildFillWideArrayData(null_check,
1617 reinterpret_cast<const int64_t*>(data),
1618 element_count,
Calin Juravle225ff812014-11-13 16:46:39 +00001619 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001620 break;
1621 default:
1622 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1623 }
Mark Mendell1152c922015-04-24 17:06:35 -04001624 graph_->SetHasBoundsChecks(true);
Calin Juravled0d48522014-11-04 16:40:20 +00001625}
1626
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001627void HGraphBuilder::BuildFillWideArrayData(HInstruction* object,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +01001628 const int64_t* data,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001629 uint32_t element_count,
Calin Juravle225ff812014-11-13 16:46:39 +00001630 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001631 for (uint32_t i = 0; i < element_count; ++i) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001632 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1633 HInstruction* value = graph_->GetLongConstant(data[i], dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001634 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001635 object, index, value, Primitive::kPrimLong, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001636 }
1637}
1638
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001639static TypeCheckKind ComputeTypeCheckKind(Handle<mirror::Class> cls)
1640 SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle98893e12015-10-02 21:05:03 +01001641 if (cls.Get() == nullptr) {
1642 return TypeCheckKind::kUnresolvedCheck;
1643 } else if (cls->IsInterface()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001644 return TypeCheckKind::kInterfaceCheck;
1645 } else if (cls->IsArrayClass()) {
1646 if (cls->GetComponentType()->IsObjectClass()) {
1647 return TypeCheckKind::kArrayObjectCheck;
1648 } else if (cls->CannotBeAssignedFromOtherTypes()) {
1649 return TypeCheckKind::kExactCheck;
1650 } else {
1651 return TypeCheckKind::kArrayCheck;
1652 }
1653 } else if (cls->IsFinal()) {
1654 return TypeCheckKind::kExactCheck;
1655 } else if (cls->IsAbstract()) {
1656 return TypeCheckKind::kAbstractClassCheck;
1657 } else {
1658 return TypeCheckKind::kClassHierarchyCheck;
1659 }
1660}
1661
Calin Juravle98893e12015-10-02 21:05:03 +01001662void HGraphBuilder::BuildTypeCheck(const Instruction& instruction,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001663 uint8_t destination,
1664 uint8_t reference,
1665 uint16_t type_index,
Calin Juravle225ff812014-11-13 16:46:39 +00001666 uint32_t dex_pc) {
Calin Juravle98893e12015-10-02 21:05:03 +01001667 bool type_known_final, type_known_abstract, use_declaring_class;
1668 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
1669 dex_compilation_unit_->GetDexMethodIndex(),
1670 *dex_compilation_unit_->GetDexFile(),
1671 type_index,
1672 &type_known_final,
1673 &type_known_abstract,
1674 &use_declaring_class);
1675
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001676 ScopedObjectAccess soa(Thread::Current());
1677 StackHandleScope<2> hs(soa.Self());
1678 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
1679 dex_compilation_unit_->GetClassLinker()->FindDexCache(
1680 soa.Self(), *dex_compilation_unit_->GetDexFile())));
1681 Handle<mirror::Class> resolved_class(hs.NewHandle(dex_cache->GetResolvedType(type_index)));
1682
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001683 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot, dex_pc);
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001684 HLoadClass* cls = new (arena_) HLoadClass(
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001685 graph_->GetCurrentMethod(),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01001686 type_index,
1687 *dex_compilation_unit_->GetDexFile(),
1688 IsOutermostCompilingClass(type_index),
Calin Juravle98893e12015-10-02 21:05:03 +01001689 dex_pc,
1690 !can_access);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001691 current_block_->AddInstruction(cls);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001692
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001693 // The class needs a temporary before being used by the type check.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001694 Temporaries temps(graph_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001695 temps.Add(cls);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001696
1697 TypeCheckKind check_kind = ComputeTypeCheckKind(resolved_class);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001698 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001699 current_block_->AddInstruction(new (arena_) HInstanceOf(object, cls, check_kind, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001700 UpdateLocal(destination, current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001701 } else {
1702 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001703 current_block_->AddInstruction(new (arena_) HCheckCast(object, cls, check_kind, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001704 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001705}
1706
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001707bool HGraphBuilder::NeedsAccessCheck(uint32_t type_index) const {
1708 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
1709 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index);
1710}
1711
Mark Mendellfe57faa2015-09-18 09:26:15 -04001712void HGraphBuilder::BuildSwitchJumpTable(const SwitchTable& table,
1713 const Instruction& instruction,
1714 HInstruction* value,
1715 uint32_t dex_pc) {
1716 // Add the successor blocks to the current block.
1717 uint16_t num_entries = table.GetNumEntries();
1718 for (size_t i = 1; i <= num_entries; i++) {
1719 int32_t target_offset = table.GetEntryAt(i);
1720 HBasicBlock* case_target = FindBlockStartingAt(dex_pc + target_offset);
1721 DCHECK(case_target != nullptr);
1722
1723 // Add the target block as a successor.
1724 current_block_->AddSuccessor(case_target);
1725 }
1726
1727 // Add the default target block as the last successor.
1728 HBasicBlock* default_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
1729 DCHECK(default_target != nullptr);
1730 current_block_->AddSuccessor(default_target);
1731
1732 // Now add the Switch instruction.
1733 int32_t starting_key = table.GetEntryAt(0);
1734 current_block_->AddInstruction(
1735 new (arena_) HPackedSwitch(starting_key, num_entries, value, dex_pc));
1736 // This block ends with control flow.
1737 current_block_ = nullptr;
1738}
1739
Calin Juravle48c2b032014-12-09 18:11:36 +00001740void HGraphBuilder::BuildPackedSwitch(const Instruction& instruction, uint32_t dex_pc) {
David Brazdil2ef645b2015-06-17 18:20:52 +01001741 // Verifier guarantees that the payload for PackedSwitch contains:
1742 // (a) number of entries (may be zero)
1743 // (b) first and lowest switch case value (entry 0, always present)
1744 // (c) list of target pcs (entries 1 <= i <= N)
Andreas Gamped881df52014-11-24 23:28:39 -08001745 SwitchTable table(instruction, dex_pc, false);
1746
1747 // Value to test against.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001748 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt, dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08001749
Mark Mendellfe57faa2015-09-18 09:26:15 -04001750 // Starting key value.
1751 int32_t starting_key = table.GetEntryAt(0);
1752
David Brazdil2ef645b2015-06-17 18:20:52 +01001753 // Retrieve number of entries.
Andreas Gampee4d4d322014-12-04 09:09:57 -08001754 uint16_t num_entries = table.GetNumEntries();
David Brazdil2ef645b2015-06-17 18:20:52 +01001755 if (num_entries == 0) {
1756 return;
1757 }
Andreas Gampee4d4d322014-12-04 09:09:57 -08001758
Mark Mendellfe57faa2015-09-18 09:26:15 -04001759 // Don't use a packed switch if there are very few entries.
1760 if (num_entries > kSmallSwitchThreshold) {
1761 BuildSwitchJumpTable(table, instruction, value, dex_pc);
1762 } else {
1763 // Chained cmp-and-branch, starting from starting_key.
1764 for (size_t i = 1; i <= num_entries; i++) {
Mark Mendell3b9f3042015-09-24 08:43:40 -04001765 BuildSwitchCaseHelper(instruction,
1766 i,
1767 i == num_entries,
1768 table,
1769 value,
1770 starting_key + i - 1,
1771 table.GetEntryAt(i),
1772 dex_pc);
Mark Mendellfe57faa2015-09-18 09:26:15 -04001773 }
Andreas Gamped881df52014-11-24 23:28:39 -08001774 }
Andreas Gamped881df52014-11-24 23:28:39 -08001775}
1776
Calin Juravle48c2b032014-12-09 18:11:36 +00001777void HGraphBuilder::BuildSparseSwitch(const Instruction& instruction, uint32_t dex_pc) {
David Brazdil2ef645b2015-06-17 18:20:52 +01001778 // Verifier guarantees that the payload for SparseSwitch contains:
1779 // (a) number of entries (may be zero)
1780 // (b) sorted key values (entries 0 <= i < N)
1781 // (c) target pcs corresponding to the switch values (entries N <= i < 2*N)
Andreas Gampee4d4d322014-12-04 09:09:57 -08001782 SwitchTable table(instruction, dex_pc, true);
1783
1784 // Value to test against.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001785 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001786
1787 uint16_t num_entries = table.GetNumEntries();
Andreas Gampee4d4d322014-12-04 09:09:57 -08001788
1789 for (size_t i = 0; i < num_entries; i++) {
1790 BuildSwitchCaseHelper(instruction, i, i == static_cast<size_t>(num_entries) - 1, table, value,
1791 table.GetEntryAt(i), table.GetEntryAt(i + num_entries), dex_pc);
1792 }
Andreas Gampee4d4d322014-12-04 09:09:57 -08001793}
1794
1795void HGraphBuilder::BuildSwitchCaseHelper(const Instruction& instruction, size_t index,
1796 bool is_last_case, const SwitchTable& table,
1797 HInstruction* value, int32_t case_value_int,
1798 int32_t target_offset, uint32_t dex_pc) {
David Brazdil852eaff2015-02-02 15:23:05 +00001799 HBasicBlock* case_target = FindBlockStartingAt(dex_pc + target_offset);
1800 DCHECK(case_target != nullptr);
1801 PotentiallyAddSuspendCheck(case_target, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001802
1803 // The current case's value.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001804 HInstruction* this_case_value = graph_->GetIntConstant(case_value_int, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001805
1806 // Compare value and this_case_value.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001807 HEqual* comparison = new (arena_) HEqual(value, this_case_value, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001808 current_block_->AddInstruction(comparison);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001809 HInstruction* ifinst = new (arena_) HIf(comparison, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001810 current_block_->AddInstruction(ifinst);
1811
1812 // Case hit: use the target offset to determine where to go.
Andreas Gampee4d4d322014-12-04 09:09:57 -08001813 current_block_->AddSuccessor(case_target);
1814
1815 // Case miss: go to the next case (or default fall-through).
1816 // When there is a next case, we use the block stored with the table offset representing this
1817 // case (that is where we registered them in ComputeBranchTargets).
1818 // When there is no next case, we use the following instruction.
1819 // TODO: Find a good way to peel the last iteration to avoid conditional, but still have re-use.
1820 if (!is_last_case) {
1821 HBasicBlock* next_case_target = FindBlockStartingAt(table.GetDexPcForIndex(index));
1822 DCHECK(next_case_target != nullptr);
1823 current_block_->AddSuccessor(next_case_target);
1824
1825 // Need to manually add the block, as there is no dex-pc transition for the cases.
1826 graph_->AddBlock(next_case_target);
1827
1828 current_block_ = next_case_target;
1829 } else {
1830 HBasicBlock* default_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
1831 DCHECK(default_target != nullptr);
1832 current_block_->AddSuccessor(default_target);
1833 current_block_ = nullptr;
1834 }
1835}
1836
David Brazdil852eaff2015-02-02 15:23:05 +00001837void HGraphBuilder::PotentiallyAddSuspendCheck(HBasicBlock* target, uint32_t dex_pc) {
1838 int32_t target_offset = target->GetDexPc() - dex_pc;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001839 if (target_offset <= 0) {
David Brazdil852eaff2015-02-02 15:23:05 +00001840 // DX generates back edges to the first encountered return. We can save
1841 // time of later passes by not adding redundant suspend checks.
David Brazdil2fd6aa52015-02-02 18:58:27 +00001842 HInstruction* last_in_target = target->GetLastInstruction();
1843 if (last_in_target != nullptr &&
1844 (last_in_target->IsReturn() || last_in_target->IsReturnVoid())) {
1845 return;
David Brazdil852eaff2015-02-02 15:23:05 +00001846 }
1847
1848 // Add a suspend check to backward branches which may potentially loop. We
1849 // can remove them after we recognize loops in the graph.
Calin Juravle225ff812014-11-13 16:46:39 +00001850 current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_pc));
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001851 }
1852}
1853
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001854bool HGraphBuilder::CanDecodeQuickenedInfo() const {
1855 return interpreter_metadata_ != nullptr;
1856}
1857
1858uint16_t HGraphBuilder::LookupQuickenedInfo(uint32_t dex_pc) {
1859 DCHECK(interpreter_metadata_ != nullptr);
1860 uint32_t dex_pc_in_map = DecodeUnsignedLeb128(&interpreter_metadata_);
1861 DCHECK_EQ(dex_pc, dex_pc_in_map);
1862 return DecodeUnsignedLeb128(&interpreter_metadata_);
1863}
1864
Calin Juravle225ff812014-11-13 16:46:39 +00001865bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001866 if (current_block_ == nullptr) {
1867 return true; // Dead code
1868 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001869
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001870 switch (instruction.Opcode()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001871 case Instruction::CONST_4: {
1872 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001873 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n(), dex_pc);
1874 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001875 break;
1876 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001877
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001878 case Instruction::CONST_16: {
1879 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001880 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s(), dex_pc);
1881 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001882 break;
1883 }
1884
Dave Allison20dfc792014-06-16 20:44:29 -07001885 case Instruction::CONST: {
1886 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001887 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i(), dex_pc);
1888 UpdateLocal(register_index, constant, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001889 break;
1890 }
1891
1892 case Instruction::CONST_HIGH16: {
1893 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001894 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16, dex_pc);
1895 UpdateLocal(register_index, constant, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001896 break;
1897 }
1898
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001899 case Instruction::CONST_WIDE_16: {
1900 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001901 // Get 16 bits of constant value, sign extended to 64 bits.
1902 int64_t value = instruction.VRegB_21s();
1903 value <<= 48;
1904 value >>= 48;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001905 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1906 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001907 break;
1908 }
1909
1910 case Instruction::CONST_WIDE_32: {
1911 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001912 // Get 32 bits of constant value, sign extended to 64 bits.
1913 int64_t value = instruction.VRegB_31i();
1914 value <<= 32;
1915 value >>= 32;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001916 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1917 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001918 break;
1919 }
1920
1921 case Instruction::CONST_WIDE: {
1922 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001923 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l(), dex_pc);
1924 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001925 break;
1926 }
1927
Dave Allison20dfc792014-06-16 20:44:29 -07001928 case Instruction::CONST_WIDE_HIGH16: {
1929 int32_t register_index = instruction.VRegA();
1930 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001931 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1932 UpdateLocal(register_index, constant, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001933 break;
1934 }
1935
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001936 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001937 case Instruction::MOVE:
1938 case Instruction::MOVE_FROM16:
1939 case Instruction::MOVE_16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001940 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
1941 UpdateLocal(instruction.VRegA(), value, dex_pc);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001942 break;
1943 }
1944
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001945 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001946 case Instruction::MOVE_WIDE:
1947 case Instruction::MOVE_WIDE_FROM16:
1948 case Instruction::MOVE_WIDE_16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001949 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong, dex_pc);
1950 UpdateLocal(instruction.VRegA(), value, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001951 break;
1952 }
1953
1954 case Instruction::MOVE_OBJECT:
1955 case Instruction::MOVE_OBJECT_16:
1956 case Instruction::MOVE_OBJECT_FROM16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001957 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot, dex_pc);
1958 UpdateLocal(instruction.VRegA(), value, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001959 break;
1960 }
1961
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001962 case Instruction::RETURN_VOID_NO_BARRIER:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001963 case Instruction::RETURN_VOID: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001964 BuildReturn(instruction, Primitive::kPrimVoid, dex_pc);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001965 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001966 }
1967
Dave Allison20dfc792014-06-16 20:44:29 -07001968#define IF_XX(comparison, cond) \
Calin Juravle225ff812014-11-13 16:46:39 +00001969 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
1970 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001971
Dave Allison20dfc792014-06-16 20:44:29 -07001972 IF_XX(HEqual, EQ);
1973 IF_XX(HNotEqual, NE);
1974 IF_XX(HLessThan, LT);
1975 IF_XX(HLessThanOrEqual, LE);
1976 IF_XX(HGreaterThan, GT);
1977 IF_XX(HGreaterThanOrEqual, GE);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001978
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001979 case Instruction::GOTO:
1980 case Instruction::GOTO_16:
1981 case Instruction::GOTO_32: {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001982 int32_t offset = instruction.GetTargetOffset();
Calin Juravle225ff812014-11-13 16:46:39 +00001983 HBasicBlock* target = FindBlockStartingAt(offset + dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001984 DCHECK(target != nullptr);
David Brazdil852eaff2015-02-02 15:23:05 +00001985 PotentiallyAddSuspendCheck(target, dex_pc);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001986 current_block_->AddInstruction(new (arena_) HGoto(dex_pc));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001987 current_block_->AddSuccessor(target);
1988 current_block_ = nullptr;
1989 break;
1990 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001991
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001992 case Instruction::RETURN: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001993 BuildReturn(instruction, return_type_, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001994 break;
1995 }
1996
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001997 case Instruction::RETURN_OBJECT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001998 BuildReturn(instruction, return_type_, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001999 break;
2000 }
2001
2002 case Instruction::RETURN_WIDE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002003 BuildReturn(instruction, return_type_, dex_pc);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002004 break;
2005 }
2006
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002007 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00002008 case Instruction::INVOKE_INTERFACE:
2009 case Instruction::INVOKE_STATIC:
2010 case Instruction::INVOKE_SUPER:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002011 case Instruction::INVOKE_VIRTUAL:
2012 case Instruction::INVOKE_VIRTUAL_QUICK: {
2013 uint16_t method_idx;
2014 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_QUICK) {
2015 if (!CanDecodeQuickenedInfo()) {
2016 return false;
2017 }
2018 method_idx = LookupQuickenedInfo(dex_pc);
2019 } else {
2020 method_idx = instruction.VRegB_35c();
2021 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002022 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002023 uint32_t args[5];
Ian Rogers29a26482014-05-02 15:27:29 -07002024 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00002025 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00002026 number_of_vreg_arguments, false, args, -1)) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002027 return false;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002028 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002029 break;
2030 }
2031
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002032 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00002033 case Instruction::INVOKE_INTERFACE_RANGE:
2034 case Instruction::INVOKE_STATIC_RANGE:
2035 case Instruction::INVOKE_SUPER_RANGE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002036 case Instruction::INVOKE_VIRTUAL_RANGE:
2037 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2038 uint16_t method_idx;
2039 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK) {
2040 if (!CanDecodeQuickenedInfo()) {
2041 return false;
2042 }
2043 method_idx = LookupQuickenedInfo(dex_pc);
2044 } else {
2045 method_idx = instruction.VRegB_3rc();
2046 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002047 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
2048 uint32_t register_index = instruction.VRegC();
Calin Juravle225ff812014-11-13 16:46:39 +00002049 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002050 number_of_vreg_arguments, true, nullptr, register_index)) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002051 return false;
2052 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002053 break;
2054 }
2055
Roland Levillain88cb1752014-10-20 16:36:47 +01002056 case Instruction::NEG_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002057 Unop_12x<HNeg>(instruction, Primitive::kPrimInt, dex_pc);
Roland Levillain88cb1752014-10-20 16:36:47 +01002058 break;
2059 }
2060
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002061 case Instruction::NEG_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002062 Unop_12x<HNeg>(instruction, Primitive::kPrimLong, dex_pc);
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002063 break;
2064 }
2065
Roland Levillain3dbcb382014-10-28 17:30:07 +00002066 case Instruction::NEG_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002067 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat, dex_pc);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002068 break;
2069 }
2070
2071 case Instruction::NEG_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002072 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble, dex_pc);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002073 break;
2074 }
2075
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002076 case Instruction::NOT_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002077 Unop_12x<HNot>(instruction, Primitive::kPrimInt, dex_pc);
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002078 break;
2079 }
2080
Roland Levillain70566432014-10-24 16:20:17 +01002081 case Instruction::NOT_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002082 Unop_12x<HNot>(instruction, Primitive::kPrimLong, dex_pc);
Roland Levillain70566432014-10-24 16:20:17 +01002083 break;
2084 }
2085
Roland Levillaindff1f282014-11-05 14:15:05 +00002086 case Instruction::INT_TO_LONG: {
Roland Levillain624279f2014-12-04 11:54:28 +00002087 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong, dex_pc);
Roland Levillaindff1f282014-11-05 14:15:05 +00002088 break;
2089 }
2090
Roland Levillaincff13742014-11-17 14:32:17 +00002091 case Instruction::INT_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00002092 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00002093 break;
2094 }
2095
2096 case Instruction::INT_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00002097 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00002098 break;
2099 }
2100
Roland Levillain946e1432014-11-11 17:35:19 +00002101 case Instruction::LONG_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00002102 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt, dex_pc);
Roland Levillain946e1432014-11-11 17:35:19 +00002103 break;
2104 }
2105
Roland Levillain6d0e4832014-11-27 18:31:21 +00002106 case Instruction::LONG_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00002107 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimFloat, dex_pc);
Roland Levillain6d0e4832014-11-27 18:31:21 +00002108 break;
2109 }
2110
Roland Levillain647b9ed2014-11-27 12:06:00 +00002111 case Instruction::LONG_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00002112 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble, dex_pc);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002113 break;
2114 }
2115
Roland Levillain3f8f9362014-12-02 17:45:01 +00002116 case Instruction::FLOAT_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00002117 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimInt, dex_pc);
2118 break;
2119 }
2120
2121 case Instruction::FLOAT_TO_LONG: {
2122 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimLong, dex_pc);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002123 break;
2124 }
2125
Roland Levillain8964e2b2014-12-04 12:10:50 +00002126 case Instruction::FLOAT_TO_DOUBLE: {
2127 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimDouble, dex_pc);
2128 break;
2129 }
2130
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002131 case Instruction::DOUBLE_TO_INT: {
2132 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimInt, dex_pc);
2133 break;
2134 }
2135
2136 case Instruction::DOUBLE_TO_LONG: {
2137 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimLong, dex_pc);
2138 break;
2139 }
2140
Roland Levillain8964e2b2014-12-04 12:10:50 +00002141 case Instruction::DOUBLE_TO_FLOAT: {
2142 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimFloat, dex_pc);
2143 break;
2144 }
2145
Roland Levillain51d3fc42014-11-13 14:11:42 +00002146 case Instruction::INT_TO_BYTE: {
Roland Levillain624279f2014-12-04 11:54:28 +00002147 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte, dex_pc);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002148 break;
2149 }
2150
Roland Levillain01a8d712014-11-14 16:27:39 +00002151 case Instruction::INT_TO_SHORT: {
Roland Levillain624279f2014-12-04 11:54:28 +00002152 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort, dex_pc);
Roland Levillain01a8d712014-11-14 16:27:39 +00002153 break;
2154 }
2155
Roland Levillain981e4542014-11-14 11:47:14 +00002156 case Instruction::INT_TO_CHAR: {
Roland Levillain624279f2014-12-04 11:54:28 +00002157 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar, dex_pc);
Roland Levillain981e4542014-11-14 11:47:14 +00002158 break;
2159 }
2160
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002161 case Instruction::ADD_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002162 Binop_23x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002163 break;
2164 }
2165
2166 case Instruction::ADD_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002167 Binop_23x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002168 break;
2169 }
2170
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002171 case Instruction::ADD_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002172 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002173 break;
2174 }
2175
2176 case Instruction::ADD_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002177 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002178 break;
2179 }
2180
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002181 case Instruction::SUB_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002182 Binop_23x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002183 break;
2184 }
2185
2186 case Instruction::SUB_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002187 Binop_23x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002188 break;
2189 }
2190
Calin Juravle096cc022014-10-23 17:01:13 +01002191 case Instruction::SUB_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002192 Binop_23x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01002193 break;
2194 }
2195
2196 case Instruction::SUB_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002197 Binop_23x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01002198 break;
2199 }
2200
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002201 case Instruction::ADD_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002202 Binop_12x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002203 break;
2204 }
2205
Calin Juravle34bacdf2014-10-07 20:23:36 +01002206 case Instruction::MUL_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002207 Binop_23x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002208 break;
2209 }
2210
2211 case Instruction::MUL_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002212 Binop_23x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002213 break;
2214 }
2215
Calin Juravleb5bfa962014-10-21 18:02:24 +01002216 case Instruction::MUL_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002217 Binop_23x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01002218 break;
2219 }
2220
2221 case Instruction::MUL_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002222 Binop_23x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01002223 break;
2224 }
2225
Calin Juravled0d48522014-11-04 16:40:20 +00002226 case Instruction::DIV_INT: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002227 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2228 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravled0d48522014-11-04 16:40:20 +00002229 break;
2230 }
2231
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002232 case Instruction::DIV_LONG: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002233 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2234 dex_pc, Primitive::kPrimLong, false, true);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002235 break;
2236 }
2237
Calin Juravle7c4954d2014-10-28 16:57:40 +00002238 case Instruction::DIV_FLOAT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002239 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002240 break;
2241 }
2242
2243 case Instruction::DIV_DOUBLE: {
Calin Juravle225ff812014-11-13 16:46:39 +00002244 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002245 break;
2246 }
2247
Calin Juravlebacfec32014-11-14 15:54:36 +00002248 case Instruction::REM_INT: {
2249 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2250 dex_pc, Primitive::kPrimInt, false, false);
2251 break;
2252 }
2253
2254 case Instruction::REM_LONG: {
2255 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2256 dex_pc, Primitive::kPrimLong, false, false);
2257 break;
2258 }
2259
Calin Juravled2ec87d2014-12-08 14:24:46 +00002260 case Instruction::REM_FLOAT: {
2261 Binop_23x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2262 break;
2263 }
2264
2265 case Instruction::REM_DOUBLE: {
2266 Binop_23x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2267 break;
2268 }
2269
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002270 case Instruction::AND_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002271 Binop_23x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002272 break;
2273 }
2274
2275 case Instruction::AND_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002276 Binop_23x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002277 break;
2278 }
2279
Calin Juravle9aec02f2014-11-18 23:06:35 +00002280 case Instruction::SHL_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002281 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002282 break;
2283 }
2284
2285 case Instruction::SHL_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002286 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002287 break;
2288 }
2289
2290 case Instruction::SHR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002291 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002292 break;
2293 }
2294
2295 case Instruction::SHR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002296 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002297 break;
2298 }
2299
2300 case Instruction::USHR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002301 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002302 break;
2303 }
2304
2305 case Instruction::USHR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002306 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002307 break;
2308 }
2309
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002310 case Instruction::OR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002311 Binop_23x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002312 break;
2313 }
2314
2315 case Instruction::OR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002316 Binop_23x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002317 break;
2318 }
2319
2320 case Instruction::XOR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002321 Binop_23x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002322 break;
2323 }
2324
2325 case Instruction::XOR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002326 Binop_23x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002327 break;
2328 }
2329
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002330 case Instruction::ADD_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002331 Binop_12x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002332 break;
2333 }
2334
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002335 case Instruction::ADD_DOUBLE_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002336 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002337 break;
2338 }
2339
2340 case Instruction::ADD_FLOAT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002341 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002342 break;
2343 }
2344
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002345 case Instruction::SUB_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002346 Binop_12x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002347 break;
2348 }
2349
2350 case Instruction::SUB_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002351 Binop_12x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002352 break;
2353 }
2354
Calin Juravle096cc022014-10-23 17:01:13 +01002355 case Instruction::SUB_FLOAT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002356 Binop_12x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01002357 break;
2358 }
2359
2360 case Instruction::SUB_DOUBLE_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002361 Binop_12x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01002362 break;
2363 }
2364
Calin Juravle34bacdf2014-10-07 20:23:36 +01002365 case Instruction::MUL_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002366 Binop_12x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002367 break;
2368 }
2369
2370 case Instruction::MUL_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002371 Binop_12x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002372 break;
2373 }
2374
Calin Juravleb5bfa962014-10-21 18:02:24 +01002375 case Instruction::MUL_FLOAT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002376 Binop_12x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01002377 break;
2378 }
2379
2380 case Instruction::MUL_DOUBLE_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002381 Binop_12x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01002382 break;
2383 }
2384
Calin Juravle865fc882014-11-06 17:09:03 +00002385 case Instruction::DIV_INT_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002386 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2387 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravle865fc882014-11-06 17:09:03 +00002388 break;
2389 }
2390
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002391 case Instruction::DIV_LONG_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002392 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2393 dex_pc, Primitive::kPrimLong, false, true);
2394 break;
2395 }
2396
2397 case Instruction::REM_INT_2ADDR: {
2398 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2399 dex_pc, Primitive::kPrimInt, false, false);
2400 break;
2401 }
2402
2403 case Instruction::REM_LONG_2ADDR: {
2404 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2405 dex_pc, Primitive::kPrimLong, false, false);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002406 break;
2407 }
2408
Calin Juravled2ec87d2014-12-08 14:24:46 +00002409 case Instruction::REM_FLOAT_2ADDR: {
2410 Binop_12x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2411 break;
2412 }
2413
2414 case Instruction::REM_DOUBLE_2ADDR: {
2415 Binop_12x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2416 break;
2417 }
2418
Calin Juravle9aec02f2014-11-18 23:06:35 +00002419 case Instruction::SHL_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002420 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002421 break;
2422 }
2423
2424 case Instruction::SHL_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002425 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002426 break;
2427 }
2428
2429 case Instruction::SHR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002430 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002431 break;
2432 }
2433
2434 case Instruction::SHR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002435 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002436 break;
2437 }
2438
2439 case Instruction::USHR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002440 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002441 break;
2442 }
2443
2444 case Instruction::USHR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002445 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002446 break;
2447 }
2448
Calin Juravle7c4954d2014-10-28 16:57:40 +00002449 case Instruction::DIV_FLOAT_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00002450 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002451 break;
2452 }
2453
2454 case Instruction::DIV_DOUBLE_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00002455 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002456 break;
2457 }
2458
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002459 case Instruction::AND_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002460 Binop_12x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002461 break;
2462 }
2463
2464 case Instruction::AND_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002465 Binop_12x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002466 break;
2467 }
2468
2469 case Instruction::OR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002470 Binop_12x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002471 break;
2472 }
2473
2474 case Instruction::OR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002475 Binop_12x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002476 break;
2477 }
2478
2479 case Instruction::XOR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002480 Binop_12x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002481 break;
2482 }
2483
2484 case Instruction::XOR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002485 Binop_12x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002486 break;
2487 }
2488
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002489 case Instruction::ADD_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002490 Binop_22s<HAdd>(instruction, false, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002491 break;
2492 }
2493
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002494 case Instruction::AND_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002495 Binop_22s<HAnd>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002496 break;
2497 }
2498
2499 case Instruction::OR_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002500 Binop_22s<HOr>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002501 break;
2502 }
2503
2504 case Instruction::XOR_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002505 Binop_22s<HXor>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002506 break;
2507 }
2508
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002509 case Instruction::RSUB_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002510 Binop_22s<HSub>(instruction, true, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002511 break;
2512 }
2513
Calin Juravle34bacdf2014-10-07 20:23:36 +01002514 case Instruction::MUL_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002515 Binop_22s<HMul>(instruction, false, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002516 break;
2517 }
2518
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002519 case Instruction::ADD_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002520 Binop_22b<HAdd>(instruction, false, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002521 break;
2522 }
2523
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002524 case Instruction::AND_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002525 Binop_22b<HAnd>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002526 break;
2527 }
2528
2529 case Instruction::OR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002530 Binop_22b<HOr>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002531 break;
2532 }
2533
2534 case Instruction::XOR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002535 Binop_22b<HXor>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002536 break;
2537 }
2538
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002539 case Instruction::RSUB_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002540 Binop_22b<HSub>(instruction, true, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002541 break;
2542 }
2543
Calin Juravle34bacdf2014-10-07 20:23:36 +01002544 case Instruction::MUL_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002545 Binop_22b<HMul>(instruction, false, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002546 break;
2547 }
2548
Calin Juravled0d48522014-11-04 16:40:20 +00002549 case Instruction::DIV_INT_LIT16:
2550 case Instruction::DIV_INT_LIT8: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002551 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2552 dex_pc, Primitive::kPrimInt, true, true);
2553 break;
2554 }
2555
2556 case Instruction::REM_INT_LIT16:
2557 case Instruction::REM_INT_LIT8: {
2558 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2559 dex_pc, Primitive::kPrimInt, true, false);
Calin Juravled0d48522014-11-04 16:40:20 +00002560 break;
2561 }
2562
Calin Juravle9aec02f2014-11-18 23:06:35 +00002563 case Instruction::SHL_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002564 Binop_22b<HShl>(instruction, false, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002565 break;
2566 }
2567
2568 case Instruction::SHR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002569 Binop_22b<HShr>(instruction, false, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002570 break;
2571 }
2572
2573 case Instruction::USHR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002574 Binop_22b<HUShr>(instruction, false, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002575 break;
2576 }
2577
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002578 case Instruction::NEW_INSTANCE: {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002579 uint16_t type_index = instruction.VRegB_21c();
Jeff Hao848f70a2014-01-15 13:49:50 -08002580 if (compiler_driver_->IsStringTypeIndex(type_index, dex_file_)) {
Jeff Hao848f70a2014-01-15 13:49:50 -08002581 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002582 HFakeString* fake_string = new (arena_) HFakeString(dex_pc);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002583 current_block_->AddInstruction(fake_string);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002584 UpdateLocal(register_index, fake_string, dex_pc);
Jeff Hao848f70a2014-01-15 13:49:50 -08002585 } else {
2586 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
2587 ? kQuickAllocObjectWithAccessCheck
2588 : kQuickAllocObject;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002589
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002590 current_block_->AddInstruction(new (arena_) HNewInstance(
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002591 graph_->GetCurrentMethod(),
2592 dex_pc,
2593 type_index,
2594 *dex_compilation_unit_->GetDexFile(),
2595 entrypoint));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002596 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Jeff Hao848f70a2014-01-15 13:49:50 -08002597 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002598 break;
2599 }
2600
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002601 case Instruction::NEW_ARRAY: {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002602 uint16_t type_index = instruction.VRegC_22c();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002603 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt, dex_pc);
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002604 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
2605 ? kQuickAllocArrayWithAccessCheck
2606 : kQuickAllocArray;
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002607 current_block_->AddInstruction(new (arena_) HNewArray(length,
2608 graph_->GetCurrentMethod(),
2609 dex_pc,
2610 type_index,
2611 *dex_compilation_unit_->GetDexFile(),
2612 entrypoint));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002613 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002614 break;
2615 }
2616
2617 case Instruction::FILLED_NEW_ARRAY: {
2618 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
2619 uint32_t type_index = instruction.VRegB_35c();
2620 uint32_t args[5];
2621 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00002622 BuildFilledNewArray(dex_pc, type_index, number_of_vreg_arguments, false, args, 0);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002623 break;
2624 }
2625
2626 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2627 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
2628 uint32_t type_index = instruction.VRegB_3rc();
2629 uint32_t register_index = instruction.VRegC_3rc();
2630 BuildFilledNewArray(
Calin Juravle225ff812014-11-13 16:46:39 +00002631 dex_pc, type_index, number_of_vreg_arguments, true, nullptr, register_index);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002632 break;
2633 }
2634
2635 case Instruction::FILL_ARRAY_DATA: {
Calin Juravle225ff812014-11-13 16:46:39 +00002636 BuildFillArrayData(instruction, dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002637 break;
2638 }
2639
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002640 case Instruction::MOVE_RESULT:
Dave Allison20dfc792014-06-16 20:44:29 -07002641 case Instruction::MOVE_RESULT_WIDE:
David Brazdilfc6a86a2015-06-26 10:33:45 +00002642 case Instruction::MOVE_RESULT_OBJECT: {
Nicolas Geoffray1efcc222015-06-24 12:41:20 +01002643 if (latest_result_ == nullptr) {
2644 // Only dead code can lead to this situation, where the verifier
2645 // does not reject the method.
2646 } else {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002647 // An Invoke/FilledNewArray and its MoveResult could have landed in
2648 // different blocks if there was a try/catch block boundary between
2649 // them. For Invoke, we insert a StoreLocal after the instruction. For
2650 // FilledNewArray, the local needs to be updated after the array was
2651 // filled, otherwise we might overwrite an input vreg.
2652 HStoreLocal* update_local =
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002653 new (arena_) HStoreLocal(GetLocalAt(instruction.VRegA()), latest_result_, dex_pc);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002654 HBasicBlock* block = latest_result_->GetBlock();
2655 if (block == current_block_) {
2656 // MoveResult and the previous instruction are in the same block.
2657 current_block_->AddInstruction(update_local);
2658 } else {
2659 // The two instructions are in different blocks. Insert the MoveResult
2660 // before the final control-flow instruction of the previous block.
2661 DCHECK(block->EndsWithControlFlowInstruction());
2662 DCHECK(current_block_->GetInstructions().IsEmpty());
2663 block->InsertInstructionBefore(update_local, block->GetLastInstruction());
2664 }
Nicolas Geoffray1efcc222015-06-24 12:41:20 +01002665 latest_result_ = nullptr;
2666 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002667 break;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002668 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002669
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002670 case Instruction::CMP_LONG: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002671 Binop_23x_cmp(instruction, Primitive::kPrimLong, ComparisonBias::kNoBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002672 break;
2673 }
2674
2675 case Instruction::CMPG_FLOAT: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002676 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kGtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002677 break;
2678 }
2679
2680 case Instruction::CMPG_DOUBLE: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002681 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kGtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002682 break;
2683 }
2684
2685 case Instruction::CMPL_FLOAT: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002686 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kLtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002687 break;
2688 }
2689
2690 case Instruction::CMPL_DOUBLE: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002691 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kLtBias, dex_pc);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002692 break;
2693 }
2694
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002695 case Instruction::NOP:
2696 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002697
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002698 case Instruction::IGET:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002699 case Instruction::IGET_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002700 case Instruction::IGET_WIDE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002701 case Instruction::IGET_WIDE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002702 case Instruction::IGET_OBJECT:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002703 case Instruction::IGET_OBJECT_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002704 case Instruction::IGET_BOOLEAN:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002705 case Instruction::IGET_BOOLEAN_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002706 case Instruction::IGET_BYTE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002707 case Instruction::IGET_BYTE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002708 case Instruction::IGET_CHAR:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002709 case Instruction::IGET_CHAR_QUICK:
2710 case Instruction::IGET_SHORT:
2711 case Instruction::IGET_SHORT_QUICK: {
Calin Juravle225ff812014-11-13 16:46:39 +00002712 if (!BuildInstanceFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002713 return false;
2714 }
2715 break;
2716 }
2717
2718 case Instruction::IPUT:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002719 case Instruction::IPUT_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002720 case Instruction::IPUT_WIDE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002721 case Instruction::IPUT_WIDE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002722 case Instruction::IPUT_OBJECT:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002723 case Instruction::IPUT_OBJECT_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002724 case Instruction::IPUT_BOOLEAN:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002725 case Instruction::IPUT_BOOLEAN_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002726 case Instruction::IPUT_BYTE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002727 case Instruction::IPUT_BYTE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002728 case Instruction::IPUT_CHAR:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002729 case Instruction::IPUT_CHAR_QUICK:
2730 case Instruction::IPUT_SHORT:
2731 case Instruction::IPUT_SHORT_QUICK: {
Calin Juravle225ff812014-11-13 16:46:39 +00002732 if (!BuildInstanceFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002733 return false;
2734 }
2735 break;
2736 }
2737
2738 case Instruction::SGET:
2739 case Instruction::SGET_WIDE:
2740 case Instruction::SGET_OBJECT:
2741 case Instruction::SGET_BOOLEAN:
2742 case Instruction::SGET_BYTE:
2743 case Instruction::SGET_CHAR:
2744 case Instruction::SGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002745 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002746 return false;
2747 }
2748 break;
2749 }
2750
2751 case Instruction::SPUT:
2752 case Instruction::SPUT_WIDE:
2753 case Instruction::SPUT_OBJECT:
2754 case Instruction::SPUT_BOOLEAN:
2755 case Instruction::SPUT_BYTE:
2756 case Instruction::SPUT_CHAR:
2757 case Instruction::SPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002758 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002759 return false;
2760 }
2761 break;
2762 }
2763
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002764#define ARRAY_XX(kind, anticipated_type) \
2765 case Instruction::AGET##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00002766 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002767 break; \
2768 } \
2769 case Instruction::APUT##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00002770 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002771 break; \
2772 }
2773
2774 ARRAY_XX(, Primitive::kPrimInt);
2775 ARRAY_XX(_WIDE, Primitive::kPrimLong);
2776 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
2777 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
2778 ARRAY_XX(_BYTE, Primitive::kPrimByte);
2779 ARRAY_XX(_CHAR, Primitive::kPrimChar);
2780 ARRAY_XX(_SHORT, Primitive::kPrimShort);
2781
Nicolas Geoffray39468442014-09-02 15:17:15 +01002782 case Instruction::ARRAY_LENGTH: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002783 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot, dex_pc);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002784 // No need for a temporary for the null check, it is the only input of the following
2785 // instruction.
Calin Juravle225ff812014-11-13 16:46:39 +00002786 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002787 current_block_->AddInstruction(object);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002788 current_block_->AddInstruction(new (arena_) HArrayLength(object, dex_pc));
2789 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002790 break;
2791 }
2792
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002793 case Instruction::CONST_STRING: {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002794 current_block_->AddInstruction(
2795 new (arena_) HLoadString(graph_->GetCurrentMethod(), instruction.VRegB_21c(), dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002796 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002797 break;
2798 }
2799
2800 case Instruction::CONST_STRING_JUMBO: {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002801 current_block_->AddInstruction(
2802 new (arena_) HLoadString(graph_->GetCurrentMethod(), instruction.VRegB_31c(), dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002803 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002804 break;
2805 }
2806
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002807 case Instruction::CONST_CLASS: {
2808 uint16_t type_index = instruction.VRegB_21c();
2809 bool type_known_final;
2810 bool type_known_abstract;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002811 bool dont_use_is_referrers_class;
2812 // `CanAccessTypeWithoutChecks` will tell whether the method being
2813 // built is trying to access its own class, so that the generated
2814 // code can optimize for this case. However, the optimization does not
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002815 // work for inlining, so we use `IsOutermostCompilingClass` instead.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002816 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
2817 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002818 &type_known_final, &type_known_abstract, &dont_use_is_referrers_class);
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002819 current_block_->AddInstruction(new (arena_) HLoadClass(
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002820 graph_->GetCurrentMethod(),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002821 type_index,
2822 *dex_compilation_unit_->GetDexFile(),
2823 IsOutermostCompilingClass(type_index),
Calin Juravle98893e12015-10-02 21:05:03 +01002824 dex_pc,
2825 !can_access));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002826 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002827 break;
2828 }
2829
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002830 case Instruction::MOVE_EXCEPTION: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002831 current_block_->AddInstruction(new (arena_) HLoadException(dex_pc));
2832 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction(), dex_pc);
2833 current_block_->AddInstruction(new (arena_) HClearException(dex_pc));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002834 break;
2835 }
2836
2837 case Instruction::THROW: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002838 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00002839 current_block_->AddInstruction(new (arena_) HThrow(exception, dex_pc));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002840 // A throw instruction must branch to the exit block.
2841 current_block_->AddSuccessor(exit_block_);
2842 // We finished building this block. Set the current block to null to avoid
2843 // adding dead instructions to it.
2844 current_block_ = nullptr;
2845 break;
2846 }
2847
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002848 case Instruction::INSTANCE_OF: {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002849 uint8_t destination = instruction.VRegA_22c();
2850 uint8_t reference = instruction.VRegB_22c();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002851 uint16_t type_index = instruction.VRegC_22c();
Calin Juravle98893e12015-10-02 21:05:03 +01002852 BuildTypeCheck(instruction, destination, reference, type_index, dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002853 break;
2854 }
2855
2856 case Instruction::CHECK_CAST: {
2857 uint8_t reference = instruction.VRegA_21c();
2858 uint16_t type_index = instruction.VRegB_21c();
Calin Juravle98893e12015-10-02 21:05:03 +01002859 BuildTypeCheck(instruction, -1, reference, type_index, dex_pc);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002860 break;
2861 }
2862
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002863 case Instruction::MONITOR_ENTER: {
2864 current_block_->AddInstruction(new (arena_) HMonitorOperation(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002865 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot, dex_pc),
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002866 HMonitorOperation::kEnter,
Calin Juravle225ff812014-11-13 16:46:39 +00002867 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002868 break;
2869 }
2870
2871 case Instruction::MONITOR_EXIT: {
2872 current_block_->AddInstruction(new (arena_) HMonitorOperation(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002873 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot, dex_pc),
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002874 HMonitorOperation::kExit,
Calin Juravle225ff812014-11-13 16:46:39 +00002875 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002876 break;
2877 }
2878
Andreas Gamped881df52014-11-24 23:28:39 -08002879 case Instruction::PACKED_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002880 BuildPackedSwitch(instruction, dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08002881 break;
2882 }
2883
Andreas Gampee4d4d322014-12-04 09:09:57 -08002884 case Instruction::SPARSE_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002885 BuildSparseSwitch(instruction, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08002886 break;
2887 }
2888
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002889 default:
Calin Juravle48c2b032014-12-09 18:11:36 +00002890 VLOG(compiler) << "Did not compile "
2891 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
2892 << " because of unhandled instruction "
2893 << instruction.Name();
2894 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnhandledInstruction);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002895 return false;
2896 }
2897 return true;
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00002898} // NOLINT(readability/fn_size)
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002899
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01002900HLocal* HGraphBuilder::GetLocalAt(uint32_t register_index) const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01002901 return locals_[register_index];
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002902}
2903
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01002904void HGraphBuilder::UpdateLocal(uint32_t register_index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002905 HInstruction* instruction,
2906 uint32_t dex_pc) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002907 HLocal* local = GetLocalAt(register_index);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002908 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction, dex_pc));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002909}
2910
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01002911HInstruction* HGraphBuilder::LoadLocal(uint32_t register_index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002912 Primitive::Type type,
2913 uint32_t dex_pc) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002914 HLocal* local = GetLocalAt(register_index);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002915 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type, dex_pc));
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002916 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002917}
2918
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002919} // namespace art