blob: 167c35d075464dd78eea47f43e7f6fa84ded5cc9 [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.
David Brazdil9bc43612015-11-05 21:25:24 +0000362 throwing_block = block->SplitCatchBlockAfterMoveException();
363 // Move-exception does not throw and the block has throwing insructions
364 // so it must have been possible to split it.
365 DCHECK(throwing_block != nullptr);
David Brazdil72783ff2015-07-09 14:36:05 +0100366 }
David Brazdild7558da2015-09-22 13:04:14 +0100367
368 try_block_info.Put(throwing_block->GetBlockId(),
369 DexFile::GetTryItems(code_item, try_item_idx));
David Brazdil72783ff2015-07-09 14:36:05 +0100370 }
David Brazdil72783ff2015-07-09 14:36:05 +0100371 }
David Brazdilbff75032015-07-08 17:26:51 +0000372 }
373
David Brazdild7558da2015-09-22 13:04:14 +0100374 // Do a pass over the try blocks and insert entering TryBoundaries where at
375 // least one predecessor is not covered by the same TryItem as the try block.
376 // We do not split each edge separately, but rather create one boundary block
377 // that all predecessors are relinked to. This preserves loop headers (b/23895756).
378 for (auto entry : try_block_info) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100379 HBasicBlock* try_block = graph_->GetBlocks()[entry.first];
David Brazdild7558da2015-09-22 13:04:14 +0100380 for (HBasicBlock* predecessor : try_block->GetPredecessors()) {
381 if (GetTryItem(predecessor, try_block_info) != entry.second) {
382 // Found a predecessor not covered by the same TryItem. Insert entering
383 // boundary block.
384 HTryBoundary* try_entry =
385 new (arena_) HTryBoundary(HTryBoundary::kEntry, try_block->GetDexPc());
386 try_block->CreateImmediateDominator()->AddInstruction(try_entry);
387 LinkToCatchBlocks(try_entry, code_item, entry.second);
388 break;
389 }
David Brazdil281a6322015-07-03 10:34:57 +0100390 }
David Brazdild7558da2015-09-22 13:04:14 +0100391 }
David Brazdil281a6322015-07-03 10:34:57 +0100392
David Brazdild7558da2015-09-22 13:04:14 +0100393 // Do a second pass over the try blocks and insert exit TryBoundaries where
394 // the successor is not in the same TryItem.
395 for (auto entry : try_block_info) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100396 HBasicBlock* try_block = graph_->GetBlocks()[entry.first];
David Brazdild7558da2015-09-22 13:04:14 +0100397 // NOTE: Do not use iterators because SplitEdge would invalidate them.
398 for (size_t i = 0, e = try_block->GetSuccessors().size(); i < e; ++i) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100399 HBasicBlock* successor = try_block->GetSuccessors()[i];
David Brazdil72783ff2015-07-09 14:36:05 +0100400
David Brazdild7558da2015-09-22 13:04:14 +0100401 // If the successor is a try block, all of its predecessors must be
402 // covered by the same TryItem. Otherwise the previous pass would have
403 // created a non-throwing boundary block.
404 if (GetTryItem(successor, try_block_info) != nullptr) {
405 DCHECK_EQ(entry.second, GetTryItem(successor, try_block_info));
David Brazdil72783ff2015-07-09 14:36:05 +0100406 continue;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000407 }
David Brazdil281a6322015-07-03 10:34:57 +0100408
David Brazdild7558da2015-09-22 13:04:14 +0100409 // Preserve the invariant that Return(Void) always jumps to Exit by moving
410 // it outside the try block if necessary.
411 HInstruction* last_instruction = try_block->GetLastInstruction();
412 if (last_instruction->IsReturn() || last_instruction->IsReturnVoid()) {
413 DCHECK_EQ(successor, exit_block_);
414 successor = try_block->SplitBefore(last_instruction);
David Brazdil281a6322015-07-03 10:34:57 +0100415 }
David Brazdild7558da2015-09-22 13:04:14 +0100416
417 // Insert TryBoundary and link to catch blocks.
418 HTryBoundary* try_exit =
419 new (arena_) HTryBoundary(HTryBoundary::kExit, successor->GetDexPc());
420 graph_->SplitEdge(try_block, successor)->AddInstruction(try_exit);
421 LinkToCatchBlocks(try_exit, code_item, entry.second);
David Brazdil281a6322015-07-03 10:34:57 +0100422 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000423 }
424}
425
David Brazdil5e8b1372015-01-23 14:39:08 +0000426bool HGraphBuilder::BuildGraph(const DexFile::CodeItem& code_item) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100427 DCHECK(graph_->GetBlocks().empty());
David Brazdil5e8b1372015-01-23 14:39:08 +0000428
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000429 const uint16_t* code_ptr = code_item.insns_;
430 const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100431 code_start_ = code_ptr;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000432
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000433 // Setup the graph with the entry block and exit block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100434 entry_block_ = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000435 graph_->AddBlock(entry_block_);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100436 exit_block_ = new (arena_) HBasicBlock(graph_, kNoDexPc);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000437 graph_->SetEntryBlock(entry_block_);
438 graph_->SetExitBlock(exit_block_);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000439
David Brazdil77a48ae2015-09-15 12:34:04 +0000440 graph_->SetHasTryCatch(code_item.tries_size_ != 0);
441
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000442 InitializeLocals(code_item.registers_size_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000443 graph_->SetMaximumNumberOfOutVRegs(code_item.outs_size_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000444
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000445 // Compute the number of dex instructions, blocks, and branches. We will
446 // check these values against limits given to the compiler.
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000447 size_t number_of_branches = 0;
448
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000449 // To avoid splitting blocks, we compute ahead of time the instructions that
450 // start a new block, and create these blocks.
Calin Juravle702d2602015-04-30 19:28:21 +0100451 if (!ComputeBranchTargets(code_ptr, code_end, &number_of_branches)) {
452 MaybeRecordStat(MethodCompilationStat::kNotCompiledBranchOutsideMethodCode);
453 return false;
454 }
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000455
456 // Note that the compiler driver is null when unit testing.
David Brazdil1b498722015-03-31 11:37:18 +0100457 if ((compiler_driver_ != nullptr) && SkipCompilation(code_item, number_of_branches)) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000458 return false;
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000459 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000460
David Brazdilfc6a86a2015-06-26 10:33:45 +0000461 CreateBlocksForTryCatch(code_item);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000462
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000463 InitializeParameters(code_item.ins_size_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100464
Calin Juravle225ff812014-11-13 16:46:39 +0000465 size_t dex_pc = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000466 while (code_ptr < code_end) {
Calin Juravle225ff812014-11-13 16:46:39 +0000467 // Update the current block if dex_pc starts a new block.
468 MaybeUpdateCurrentBlock(dex_pc);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000469 const Instruction& instruction = *Instruction::At(code_ptr);
Calin Juravle48c2b032014-12-09 18:11:36 +0000470 if (!AnalyzeDexInstruction(instruction, dex_pc)) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000471 return false;
Calin Juravle48c2b032014-12-09 18:11:36 +0000472 }
Calin Juravle225ff812014-11-13 16:46:39 +0000473 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000474 code_ptr += instruction.SizeInCodeUnits();
475 }
476
David Brazdilfc6a86a2015-06-26 10:33:45 +0000477 // Add Exit to the exit block.
David Brazdil3e187382015-06-26 09:59:52 +0000478 exit_block_->AddInstruction(new (arena_) HExit());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000479 // Add the suspend check to the entry block.
480 entry_block_->AddInstruction(new (arena_) HSuspendCheck(0));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000481 entry_block_->AddInstruction(new (arena_) HGoto());
David Brazdilbff75032015-07-08 17:26:51 +0000482 // Add the exit block at the end.
483 graph_->AddBlock(exit_block_);
David Brazdil5e8b1372015-01-23 14:39:08 +0000484
David Brazdilfc6a86a2015-06-26 10:33:45 +0000485 // Iterate over blocks covered by TryItems and insert TryBoundaries at entry
486 // and exit points. This requires all control-flow instructions and
487 // non-exceptional edges to have been created.
488 InsertTryBoundaryBlocks(code_item);
489
David Brazdil5e8b1372015-01-23 14:39:08 +0000490 return true;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000491}
492
David Brazdilfc6a86a2015-06-26 10:33:45 +0000493void HGraphBuilder::MaybeUpdateCurrentBlock(size_t dex_pc) {
494 HBasicBlock* block = FindBlockStartingAt(dex_pc);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000495 if (block == nullptr) {
496 return;
497 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000498
499 if (current_block_ != nullptr) {
500 // Branching instructions clear current_block, so we know
501 // the last instruction of the current block is not a branching
502 // instruction. We add an unconditional goto to the found block.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600503 current_block_->AddInstruction(new (arena_) HGoto(dex_pc));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000504 current_block_->AddSuccessor(block);
505 }
506 graph_->AddBlock(block);
507 current_block_ = block;
508}
509
Calin Juravle702d2602015-04-30 19:28:21 +0100510bool HGraphBuilder::ComputeBranchTargets(const uint16_t* code_ptr,
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000511 const uint16_t* code_end,
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000512 size_t* number_of_branches) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100513 branch_targets_.resize(code_end - code_ptr, nullptr);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000514
515 // Create the first block for the dex instructions, single successor of the entry block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100516 HBasicBlock* block = new (arena_) HBasicBlock(graph_, 0);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100517 branch_targets_[0] = block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000518 entry_block_->AddSuccessor(block);
519
520 // Iterate over all instructions and find branching instructions. Create blocks for
521 // the locations these instructions branch to.
Andreas Gamped881df52014-11-24 23:28:39 -0800522 uint32_t dex_pc = 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000523 while (code_ptr < code_end) {
524 const Instruction& instruction = *Instruction::At(code_ptr);
525 if (instruction.IsBranch()) {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000526 (*number_of_branches)++;
Calin Juravle225ff812014-11-13 16:46:39 +0000527 int32_t target = instruction.GetTargetOffset() + dex_pc;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000528 // Create a block for the target instruction.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000529 FindOrCreateBlockStartingAt(target);
530
Calin Juravle225ff812014-11-13 16:46:39 +0000531 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000532 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle702d2602015-04-30 19:28:21 +0100533
David Brazdilfe659462015-06-24 14:23:56 +0100534 if (instruction.CanFlowThrough()) {
535 if (code_ptr >= code_end) {
Calin Juravle702d2602015-04-30 19:28:21 +0100536 // In the normal case we should never hit this but someone can artificially forge a dex
537 // file to fall-through out the method code. In this case we bail out compilation.
538 return false;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000539 } else {
540 FindOrCreateBlockStartingAt(dex_pc);
Calin Juravle702d2602015-04-30 19:28:21 +0100541 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000542 }
Andreas Gampee4d4d322014-12-04 09:09:57 -0800543 } else if (instruction.IsSwitch()) {
544 SwitchTable table(instruction, dex_pc, instruction.Opcode() == Instruction::SPARSE_SWITCH);
Andreas Gamped881df52014-11-24 23:28:39 -0800545
546 uint16_t num_entries = table.GetNumEntries();
547
Andreas Gampee4d4d322014-12-04 09:09:57 -0800548 // In a packed-switch, the entry at index 0 is the starting key. In a sparse-switch, the
549 // entry at index 0 is the first key, and values are after *all* keys.
550 size_t offset = table.GetFirstValueIndex();
551
552 // Use a larger loop counter type to avoid overflow issues.
553 for (size_t i = 0; i < num_entries; ++i) {
Andreas Gamped881df52014-11-24 23:28:39 -0800554 // The target of the case.
Andreas Gampee4d4d322014-12-04 09:09:57 -0800555 uint32_t target = dex_pc + table.GetEntryAt(i + offset);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000556 FindOrCreateBlockStartingAt(target);
Andreas Gamped881df52014-11-24 23:28:39 -0800557
David Brazdil281a6322015-07-03 10:34:57 +0100558 // Create a block for the switch-case logic. The block gets the dex_pc
559 // of the SWITCH instruction because it is part of its semantics.
560 block = new (arena_) HBasicBlock(graph_, dex_pc);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100561 branch_targets_[table.GetDexPcForIndex(i)] = block;
Andreas Gamped881df52014-11-24 23:28:39 -0800562 }
563
564 // Fall-through. Add a block if there is more code afterwards.
565 dex_pc += instruction.SizeInCodeUnits();
566 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle702d2602015-04-30 19:28:21 +0100567 if (code_ptr >= code_end) {
568 // In the normal case we should never hit this but someone can artificially forge a dex
569 // file to fall-through out the method code. In this case we bail out compilation.
570 // (A switch can fall-through so we don't need to check CanFlowThrough().)
571 return false;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000572 } else {
573 FindOrCreateBlockStartingAt(dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -0800574 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000575 } else {
576 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle225ff812014-11-13 16:46:39 +0000577 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000578 }
579 }
Calin Juravle702d2602015-04-30 19:28:21 +0100580 return true;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000581}
582
David Brazdilfc6a86a2015-06-26 10:33:45 +0000583HBasicBlock* HGraphBuilder::FindBlockStartingAt(int32_t dex_pc) const {
584 DCHECK_GE(dex_pc, 0);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100585 return branch_targets_[dex_pc];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000586}
587
588HBasicBlock* HGraphBuilder::FindOrCreateBlockStartingAt(int32_t dex_pc) {
589 HBasicBlock* block = FindBlockStartingAt(dex_pc);
590 if (block == nullptr) {
591 block = new (arena_) HBasicBlock(graph_, dex_pc);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100592 branch_targets_[dex_pc] = block;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000593 }
594 return block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000595}
596
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100597template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600598void HGraphBuilder::Unop_12x(const Instruction& instruction,
599 Primitive::Type type,
600 uint32_t dex_pc) {
601 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
602 current_block_->AddInstruction(new (arena_) T(type, first, dex_pc));
603 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Roland Levillain88cb1752014-10-20 16:36:47 +0100604}
605
Roland Levillaindff1f282014-11-05 14:15:05 +0000606void HGraphBuilder::Conversion_12x(const Instruction& instruction,
607 Primitive::Type input_type,
Roland Levillain624279f2014-12-04 11:54:28 +0000608 Primitive::Type result_type,
609 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600610 HInstruction* first = LoadLocal(instruction.VRegB(), input_type, dex_pc);
Roland Levillain624279f2014-12-04 11:54:28 +0000611 current_block_->AddInstruction(new (arena_) HTypeConversion(result_type, first, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600612 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100613}
614
615template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000616void HGraphBuilder::Binop_23x(const Instruction& instruction,
617 Primitive::Type type,
618 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600619 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
620 HInstruction* second = LoadLocal(instruction.VRegC(), type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000621 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600622 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000623}
624
625template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000626void HGraphBuilder::Binop_23x_shift(const Instruction& instruction,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600627 Primitive::Type type,
628 uint32_t dex_pc) {
629 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
630 HInstruction* second = LoadLocal(instruction.VRegC(), Primitive::kPrimInt, dex_pc);
631 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
632 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000633}
634
Calin Juravleddb7df22014-11-25 20:56:51 +0000635void HGraphBuilder::Binop_23x_cmp(const Instruction& instruction,
636 Primitive::Type type,
Mark Mendellc4701932015-04-10 13:18:51 -0400637 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700638 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600639 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
640 HInstruction* second = LoadLocal(instruction.VRegC(), type, dex_pc);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700641 current_block_->AddInstruction(new (arena_) HCompare(type, first, second, bias, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600642 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +0000643}
644
Calin Juravle9aec02f2014-11-18 23:06:35 +0000645template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600646void HGraphBuilder::Binop_12x_shift(const Instruction& instruction, Primitive::Type type,
647 uint32_t dex_pc) {
648 HInstruction* first = LoadLocal(instruction.VRegA(), type, dex_pc);
649 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
650 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
651 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000652}
653
654template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000655void HGraphBuilder::Binop_12x(const Instruction& instruction,
656 Primitive::Type type,
657 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600658 HInstruction* first = LoadLocal(instruction.VRegA(), type, dex_pc);
659 HInstruction* second = LoadLocal(instruction.VRegB(), type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000660 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600661 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000662}
663
664template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600665void HGraphBuilder::Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
666 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
667 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100668 if (reverse) {
669 std::swap(first, second);
670 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600671 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second, dex_pc));
672 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100673}
674
675template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600676void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
677 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
678 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100679 if (reverse) {
680 std::swap(first, second);
681 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600682 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second, dex_pc));
683 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100684}
685
Calin Juravle0c25d102015-04-20 14:49:09 +0100686static bool RequiresConstructorBarrier(const DexCompilationUnit* cu, const CompilerDriver& driver) {
Calin Juravle27df7582015-04-17 19:12:31 +0100687 Thread* self = Thread::Current();
Calin Juravle0c25d102015-04-20 14:49:09 +0100688 return cu->IsConstructor()
689 && driver.RequiresConstructorBarrier(self, cu->GetDexFile(), cu->GetClassDefIndex());
Calin Juravle27df7582015-04-17 19:12:31 +0100690}
691
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600692void HGraphBuilder::BuildReturn(const Instruction& instruction,
693 Primitive::Type type,
694 uint32_t dex_pc) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100695 if (type == Primitive::kPrimVoid) {
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100696 if (graph_->ShouldGenerateConstructorBarrier()) {
697 // The compilation unit is null during testing.
698 if (dex_compilation_unit_ != nullptr) {
699 DCHECK(RequiresConstructorBarrier(dex_compilation_unit_, *compiler_driver_))
700 << "Inconsistent use of ShouldGenerateConstructorBarrier. Should not generate a barrier.";
701 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600702 current_block_->AddInstruction(new (arena_) HMemoryBarrier(kStoreStore, dex_pc));
Calin Juravle27df7582015-04-17 19:12:31 +0100703 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600704 current_block_->AddInstruction(new (arena_) HReturnVoid(dex_pc));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100705 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600706 HInstruction* value = LoadLocal(instruction.VRegA(), type, dex_pc);
707 current_block_->AddInstruction(new (arena_) HReturn(value, dex_pc));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100708 }
709 current_block_->AddSuccessor(exit_block_);
710 current_block_ = nullptr;
711}
712
Calin Juravle68ad6492015-08-18 17:08:12 +0100713static InvokeType GetInvokeTypeFromOpCode(Instruction::Code opcode) {
714 switch (opcode) {
715 case Instruction::INVOKE_STATIC:
716 case Instruction::INVOKE_STATIC_RANGE:
717 return kStatic;
718 case Instruction::INVOKE_DIRECT:
719 case Instruction::INVOKE_DIRECT_RANGE:
720 return kDirect;
721 case Instruction::INVOKE_VIRTUAL:
722 case Instruction::INVOKE_VIRTUAL_QUICK:
723 case Instruction::INVOKE_VIRTUAL_RANGE:
724 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
725 return kVirtual;
726 case Instruction::INVOKE_INTERFACE:
727 case Instruction::INVOKE_INTERFACE_RANGE:
728 return kInterface;
729 case Instruction::INVOKE_SUPER_RANGE:
730 case Instruction::INVOKE_SUPER:
731 return kSuper;
732 default:
733 LOG(FATAL) << "Unexpected invoke opcode: " << opcode;
734 UNREACHABLE();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100735 }
Calin Juravle68ad6492015-08-18 17:08:12 +0100736}
737
738bool HGraphBuilder::BuildInvoke(const Instruction& instruction,
739 uint32_t dex_pc,
740 uint32_t method_idx,
741 uint32_t number_of_vreg_arguments,
742 bool is_range,
743 uint32_t* args,
744 uint32_t register_index) {
745 InvokeType original_invoke_type = GetInvokeTypeFromOpCode(instruction.Opcode());
746 InvokeType optimized_invoke_type = original_invoke_type;
747 const char* descriptor = dex_file_->GetMethodShorty(method_idx);
748 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
749
750 // Remove the return type from the 'proto'.
751 size_t number_of_arguments = strlen(descriptor) - 1;
752 if (original_invoke_type != kStatic) { // instance call
753 // One extra argument for 'this'.
754 number_of_arguments++;
755 }
756
757 MethodReference target_method(dex_file_, method_idx);
Calin Juravle5d01db12015-08-25 15:02:42 +0100758 int32_t table_index = 0;
759 uintptr_t direct_code = 0;
760 uintptr_t direct_method = 0;
Calin Juravle68ad6492015-08-18 17:08:12 +0100761
Calin Juravle5d01db12015-08-25 15:02:42 +0100762 // Special handling for string init.
763 int32_t string_init_offset = 0;
764 bool is_string_init = compiler_driver_->IsStringInit(method_idx,
765 dex_file_,
766 &string_init_offset);
767 // Replace calls to String.<init> with StringFactory.
768 if (is_string_init) {
Vladimir Markodc151b22015-10-15 18:02:30 +0100769 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
770 HInvokeStaticOrDirect::MethodLoadKind::kStringInit,
771 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
772 dchecked_integral_cast<uint64_t>(string_init_offset),
773 0U
774 };
Calin Juravle5d01db12015-08-25 15:02:42 +0100775 HInvoke* invoke = new (arena_) HInvokeStaticOrDirect(
776 arena_,
777 number_of_arguments - 1,
778 Primitive::kPrimNot /*return_type */,
779 dex_pc,
780 method_idx,
781 target_method,
782 dispatch_info,
783 original_invoke_type,
784 kStatic /* optimized_invoke_type */,
785 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit);
786 return HandleStringInit(invoke,
787 number_of_vreg_arguments,
788 args,
789 register_index,
790 is_range,
791 descriptor);
792 }
793
794 // Handle unresolved methods.
Calin Juravle68ad6492015-08-18 17:08:12 +0100795 if (!compiler_driver_->ComputeInvokeInfo(dex_compilation_unit_,
796 dex_pc,
797 true /* update_stats */,
798 true /* enable_devirtualization */,
799 &optimized_invoke_type,
800 &target_method,
801 &table_index,
802 &direct_code,
803 &direct_method)) {
Calin Juravle175dc732015-08-25 15:42:32 +0100804 MaybeRecordStat(MethodCompilationStat::kUnresolvedMethod);
805 HInvoke* invoke = new (arena_) HInvokeUnresolved(arena_,
806 number_of_arguments,
807 return_type,
808 dex_pc,
809 method_idx,
810 original_invoke_type);
811 return HandleInvoke(invoke,
812 number_of_vreg_arguments,
813 args,
814 register_index,
815 is_range,
816 descriptor,
817 nullptr /* clinit_check */);
Calin Juravle68ad6492015-08-18 17:08:12 +0100818 }
819
Calin Juravle5d01db12015-08-25 15:02:42 +0100820 // Handle resolved methods (non string init).
Calin Juravle68ad6492015-08-18 17:08:12 +0100821
Calin Juravle5d01db12015-08-25 15:02:42 +0100822 DCHECK(optimized_invoke_type != kSuper);
Calin Juravle68ad6492015-08-18 17:08:12 +0100823
824 // Potential class initialization check, in the case of a static method call.
825 HClinitCheck* clinit_check = nullptr;
826 HInvoke* invoke = nullptr;
827
Calin Juravle5d01db12015-08-25 15:02:42 +0100828 if (optimized_invoke_type == kDirect || optimized_invoke_type == kStatic) {
Calin Juravle68ad6492015-08-18 17:08:12 +0100829 // By default, consider that the called method implicitly requires
830 // an initialization check of its declaring method.
831 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement
832 = HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
Calin Juravle5d01db12015-08-25 15:02:42 +0100833 if (optimized_invoke_type == kStatic) {
Calin Juravle68ad6492015-08-18 17:08:12 +0100834 clinit_check = ProcessClinitCheckForInvoke(dex_pc, method_idx, &clinit_check_requirement);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100835 }
Calin Juravle68ad6492015-08-18 17:08:12 +0100836
Vladimir Markodc151b22015-10-15 18:02:30 +0100837 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
838 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
839 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
840 0u,
841 0U
842 };
Calin Juravle68ad6492015-08-18 17:08:12 +0100843 invoke = new (arena_) HInvokeStaticOrDirect(arena_,
844 number_of_arguments,
845 return_type,
846 dex_pc,
847 method_idx,
848 target_method,
849 dispatch_info,
850 original_invoke_type,
851 optimized_invoke_type,
852 clinit_check_requirement);
853 } else if (optimized_invoke_type == kVirtual) {
854 invoke = new (arena_) HInvokeVirtual(arena_,
855 number_of_arguments,
856 return_type,
857 dex_pc,
858 method_idx,
859 table_index);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100860 } else {
Calin Juravle68ad6492015-08-18 17:08:12 +0100861 DCHECK_EQ(optimized_invoke_type, kInterface);
862 invoke = new (arena_) HInvokeInterface(arena_,
863 number_of_arguments,
864 return_type,
865 dex_pc,
866 method_idx,
867 table_index);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100868 }
Calin Juravle68ad6492015-08-18 17:08:12 +0100869
Calin Juravle5d01db12015-08-25 15:02:42 +0100870 return HandleInvoke(invoke,
871 number_of_vreg_arguments,
872 args,
873 register_index,
874 is_range,
875 descriptor,
876 clinit_check);
Calin Juravle68ad6492015-08-18 17:08:12 +0100877}
878
879HClinitCheck* HGraphBuilder::ProcessClinitCheckForInvoke(
880 uint32_t dex_pc,
881 uint32_t method_idx,
882 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
883 ScopedObjectAccess soa(Thread::Current());
884 StackHandleScope<4> hs(soa.Self());
885 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
886 dex_compilation_unit_->GetClassLinker()->FindDexCache(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700887 soa.Self(), *dex_compilation_unit_->GetDexFile())));
Calin Juravle68ad6492015-08-18 17:08:12 +0100888 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
889 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
890 ArtMethod* resolved_method = compiler_driver_->ResolveMethod(
891 soa, dex_cache, class_loader, dex_compilation_unit_, method_idx, InvokeType::kStatic);
892
893 DCHECK(resolved_method != nullptr);
894
895 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
896 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700897 outer_compilation_unit_->GetClassLinker()->FindDexCache(soa.Self(), outer_dex_file)));
Calin Juravle68ad6492015-08-18 17:08:12 +0100898 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
899
900 // The index at which the method's class is stored in the DexCache's type array.
901 uint32_t storage_index = DexFile::kDexNoIndex;
902 bool is_outer_class = (resolved_method->GetDeclaringClass() == outer_class.Get());
903 if (is_outer_class) {
904 storage_index = outer_class->GetDexTypeIndex();
905 } else if (outer_dex_cache.Get() == dex_cache.Get()) {
906 // Get `storage_index` from IsClassOfStaticMethodAvailableToReferrer.
907 compiler_driver_->IsClassOfStaticMethodAvailableToReferrer(outer_dex_cache.Get(),
908 GetCompilingClass(),
909 resolved_method,
910 method_idx,
911 &storage_index);
912 }
913
914 HClinitCheck* clinit_check = nullptr;
915
916 if (!outer_class->IsInterface()
917 && outer_class->IsSubClass(resolved_method->GetDeclaringClass())) {
918 // If the outer class is the declaring class or a subclass
919 // of the declaring class, no class initialization is needed
920 // before the static method call.
921 // Note that in case of inlining, we do not need to add clinit checks
922 // to calls that satisfy this subclass check with any inlined methods. This
923 // will be detected by the optimization passes.
924 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
925 } else if (storage_index != DexFile::kDexNoIndex) {
926 // If the method's class type index is available, check
927 // whether we should add an explicit class initialization
928 // check for its declaring class before the static method call.
929
930 // TODO: find out why this check is needed.
931 bool is_in_dex_cache = compiler_driver_->CanAssumeTypeIsPresentInDexCache(
932 *outer_compilation_unit_->GetDexFile(), storage_index);
933 bool is_initialized =
934 resolved_method->GetDeclaringClass()->IsInitialized() && is_in_dex_cache;
935
936 if (is_initialized) {
937 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
938 } else {
939 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
940 HLoadClass* load_class = new (arena_) HLoadClass(
941 graph_->GetCurrentMethod(),
942 storage_index,
943 *dex_compilation_unit_->GetDexFile(),
944 is_outer_class,
Calin Juravle98893e12015-10-02 21:05:03 +0100945 dex_pc,
946 /*needs_access_check*/ false);
Calin Juravle68ad6492015-08-18 17:08:12 +0100947 current_block_->AddInstruction(load_class);
948 clinit_check = new (arena_) HClinitCheck(load_class, dex_pc);
949 current_block_->AddInstruction(clinit_check);
950 }
951 }
952 return clinit_check;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100953}
954
Calin Juravle5d01db12015-08-25 15:02:42 +0100955bool HGraphBuilder::SetupInvokeArguments(HInvoke* invoke,
956 uint32_t number_of_vreg_arguments,
957 uint32_t* args,
958 uint32_t register_index,
959 bool is_range,
960 const char* descriptor,
961 size_t start_index,
962 size_t* argument_index) {
Calin Juravle68ad6492015-08-18 17:08:12 +0100963 uint32_t descriptor_index = 1; // Skip the return type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600964 uint32_t dex_pc = invoke->GetDexPc();
Calin Juravle68ad6492015-08-18 17:08:12 +0100965
Nicolas Geoffray2e335252015-06-18 11:11:27 +0100966 for (size_t i = start_index;
967 // Make sure we don't go over the expected arguments or over the number of
968 // dex registers given. If the instruction was seen as dead by the verifier,
969 // it hasn't been properly checked.
Calin Juravle5d01db12015-08-25 15:02:42 +0100970 (i < number_of_vreg_arguments) && (*argument_index < invoke->GetNumberOfArguments());
971 i++, (*argument_index)++) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100972 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100973 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
Nicolas Geoffray2e335252015-06-18 11:11:27 +0100974 if (!is_range
975 && is_wide
976 && ((i + 1 == number_of_vreg_arguments) || (args[i] + 1 != args[i + 1]))) {
977 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
978 // reject any class where this is violated. However, the verifier only does these checks
979 // on non trivially dead instructions, so we just bailout the compilation.
980 VLOG(compiler) << "Did not compile "
981 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
982 << " because of non-sequential dex register pair in wide argument";
983 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
984 return false;
985 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600986 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type, dex_pc);
Calin Juravle5d01db12015-08-25 15:02:42 +0100987 invoke->SetArgumentAt(*argument_index, arg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100988 if (is_wide) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100989 i++;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100990 }
991 }
Nicolas Geoffray2e335252015-06-18 11:11:27 +0100992
Calin Juravle5d01db12015-08-25 15:02:42 +0100993 if (*argument_index != invoke->GetNumberOfArguments()) {
Nicolas Geoffray2e335252015-06-18 11:11:27 +0100994 VLOG(compiler) << "Did not compile "
995 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
996 << " because of wrong number of arguments in invoke instruction";
997 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
998 return false;
999 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001000
Vladimir Markob554b5a2015-11-06 12:57:55 +00001001 if (invoke->IsInvokeStaticOrDirect() &&
1002 HInvokeStaticOrDirect::NeedsCurrentMethodInput(
1003 invoke->AsInvokeStaticOrDirect()->GetMethodLoadKind())) {
Calin Juravle5d01db12015-08-25 15:02:42 +01001004 invoke->SetArgumentAt(*argument_index, graph_->GetCurrentMethod());
1005 (*argument_index)++;
1006 }
1007
1008 return true;
1009}
1010
1011bool HGraphBuilder::HandleInvoke(HInvoke* invoke,
1012 uint32_t number_of_vreg_arguments,
1013 uint32_t* args,
1014 uint32_t register_index,
1015 bool is_range,
1016 const char* descriptor,
1017 HClinitCheck* clinit_check) {
1018 DCHECK(!invoke->IsInvokeStaticOrDirect() || !invoke->AsInvokeStaticOrDirect()->IsStringInit());
1019
1020 size_t start_index = 0;
1021 size_t argument_index = 0;
1022 if (invoke->GetOriginalInvokeType() != InvokeType::kStatic) { // Instance call.
1023 Temporaries temps(graph_);
1024 HInstruction* arg = LoadLocal(
1025 is_range ? register_index : args[0], Primitive::kPrimNot, invoke->GetDexPc());
1026 HNullCheck* null_check = new (arena_) HNullCheck(arg, invoke->GetDexPc());
1027 current_block_->AddInstruction(null_check);
1028 temps.Add(null_check);
1029 invoke->SetArgumentAt(0, null_check);
1030 start_index = 1;
1031 argument_index = 1;
1032 }
1033
1034 if (!SetupInvokeArguments(invoke,
1035 number_of_vreg_arguments,
1036 args,
1037 register_index,
1038 is_range,
1039 descriptor,
1040 start_index,
1041 &argument_index)) {
1042 return false;
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001043 }
1044
Calin Juravle68ad6492015-08-18 17:08:12 +01001045 if (clinit_check != nullptr) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001046 // Add the class initialization check as last input of `invoke`.
Calin Juravle68ad6492015-08-18 17:08:12 +01001047 DCHECK(invoke->IsInvokeStaticOrDirect());
1048 DCHECK(invoke->AsInvokeStaticOrDirect()->GetClinitCheckRequirement()
1049 == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit);
Roland Levillain3e3d7332015-04-28 11:00:54 +01001050 invoke->SetArgumentAt(argument_index, clinit_check);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001051 argument_index++;
Roland Levillain4c0eb422015-04-24 16:43:49 +01001052 }
1053
Calin Juravle5d01db12015-08-25 15:02:42 +01001054 current_block_->AddInstruction(invoke);
1055 latest_result_ = invoke;
1056
1057 return true;
1058}
1059
1060bool HGraphBuilder::HandleStringInit(HInvoke* invoke,
1061 uint32_t number_of_vreg_arguments,
1062 uint32_t* args,
1063 uint32_t register_index,
1064 bool is_range,
1065 const char* descriptor) {
1066 DCHECK(invoke->IsInvokeStaticOrDirect());
1067 DCHECK(invoke->AsInvokeStaticOrDirect()->IsStringInit());
1068
1069 size_t start_index = 1;
1070 size_t argument_index = 0;
1071 if (!SetupInvokeArguments(invoke,
1072 number_of_vreg_arguments,
1073 args,
1074 register_index,
1075 is_range,
1076 descriptor,
1077 start_index,
1078 &argument_index)) {
1079 return false;
Jeff Hao848f70a2014-01-15 13:49:50 -08001080 }
Calin Juravle0eedd7e2015-08-20 14:48:00 +01001081
Calin Juravle5d01db12015-08-25 15:02:42 +01001082 // Add move-result for StringFactory method.
1083 uint32_t orig_this_reg = is_range ? register_index : args[0];
1084 HInstruction* fake_string = LoadLocal(orig_this_reg, Primitive::kPrimNot, invoke->GetDexPc());
1085 invoke->SetArgumentAt(argument_index, fake_string);
1086 current_block_->AddInstruction(invoke);
1087 PotentiallySimplifyFakeString(orig_this_reg, invoke->GetDexPc(), invoke);
1088
Calin Juravle0eedd7e2015-08-20 14:48:00 +01001089 latest_result_ = invoke;
1090
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001091 return true;
1092}
1093
Calin Juravle68ad6492015-08-18 17:08:12 +01001094void HGraphBuilder::PotentiallySimplifyFakeString(uint16_t original_dex_register,
1095 uint32_t dex_pc,
1096 HInvoke* actual_string) {
1097 if (!graph_->IsDebuggable()) {
1098 // Notify that we cannot compile with baseline. The dex registers aliasing
1099 // with `original_dex_register` will be handled when we optimize
1100 // (see HInstructionSimplifer::VisitFakeString).
1101 can_use_baseline_for_string_init_ = false;
1102 return;
1103 }
1104 const VerifiedMethod* verified_method =
1105 compiler_driver_->GetVerifiedMethod(dex_file_, dex_compilation_unit_->GetDexMethodIndex());
1106 if (verified_method != nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001107 UpdateLocal(original_dex_register, actual_string, dex_pc);
Calin Juravle68ad6492015-08-18 17:08:12 +01001108 const SafeMap<uint32_t, std::set<uint32_t>>& string_init_map =
1109 verified_method->GetStringInitPcRegMap();
1110 auto map_it = string_init_map.find(dex_pc);
1111 if (map_it != string_init_map.end()) {
Vladimir Markodbc23372015-10-12 12:45:52 +01001112 for (uint32_t reg : map_it->second) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001113 HInstruction* load_local = LoadLocal(original_dex_register, Primitive::kPrimNot, dex_pc);
Vladimir Markodbc23372015-10-12 12:45:52 +01001114 UpdateLocal(reg, load_local, dex_pc);
Calin Juravle68ad6492015-08-18 17:08:12 +01001115 }
1116 }
1117 } else {
1118 can_use_baseline_for_string_init_ = false;
1119 }
1120}
1121
Calin Juravlee460d1d2015-09-29 04:52:17 +01001122static Primitive::Type GetFieldAccessType(const DexFile& dex_file, uint16_t field_index) {
1123 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
1124 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
1125 return Primitive::GetType(type[0]);
1126}
1127
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001128bool HGraphBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001129 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001130 bool is_put) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001131 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1132 uint32_t obj_reg = instruction.VRegB_22c();
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001133 uint16_t field_index;
1134 if (instruction.IsQuickened()) {
1135 if (!CanDecodeQuickenedInfo()) {
1136 return false;
1137 }
1138 field_index = LookupQuickenedInfo(dex_pc);
1139 } else {
1140 field_index = instruction.VRegC_22c();
1141 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001142
1143 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierc7853442015-03-27 14:35:38 -07001144 ArtField* resolved_field =
1145 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001146
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001147
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001148 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot, dex_pc);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001149 HInstruction* null_check = new (arena_) HNullCheck(object, dex_pc);
1150 current_block_->AddInstruction(null_check);
1151
1152 Primitive::Type field_type = (resolved_field == nullptr)
1153 ? GetFieldAccessType(*dex_file_, field_index)
1154 : resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001155 if (is_put) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001156 Temporaries temps(graph_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001157 // We need one temporary for the null check.
1158 temps.Add(null_check);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001159 HInstruction* value = LoadLocal(source_or_dest_reg, field_type, dex_pc);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001160 HInstruction* field_set = nullptr;
1161 if (resolved_field == nullptr) {
1162 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
1163 field_set = new (arena_) HUnresolvedInstanceFieldSet(null_check,
1164 value,
1165 field_type,
1166 field_index,
1167 dex_pc);
1168 } else {
Mingyao Yang8df69d42015-10-22 15:40:58 -07001169 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Calin Juravlee460d1d2015-09-29 04:52:17 +01001170 field_set = new (arena_) HInstanceFieldSet(null_check,
1171 value,
1172 field_type,
1173 resolved_field->GetOffset(),
1174 resolved_field->IsVolatile(),
1175 field_index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07001176 class_def_index,
Calin Juravlee460d1d2015-09-29 04:52:17 +01001177 *dex_file_,
1178 dex_compilation_unit_->GetDexCache(),
1179 dex_pc);
1180 }
1181 current_block_->AddInstruction(field_set);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001182 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001183 HInstruction* field_get = nullptr;
1184 if (resolved_field == nullptr) {
1185 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
1186 field_get = new (arena_) HUnresolvedInstanceFieldGet(null_check,
1187 field_type,
1188 field_index,
1189 dex_pc);
1190 } else {
Mingyao Yang8df69d42015-10-22 15:40:58 -07001191 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Calin Juravlee460d1d2015-09-29 04:52:17 +01001192 field_get = new (arena_) HInstanceFieldGet(null_check,
1193 field_type,
1194 resolved_field->GetOffset(),
1195 resolved_field->IsVolatile(),
1196 field_index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07001197 class_def_index,
Calin Juravlee460d1d2015-09-29 04:52:17 +01001198 *dex_file_,
1199 dex_compilation_unit_->GetDexCache(),
1200 dex_pc);
1201 }
1202 current_block_->AddInstruction(field_get);
1203 UpdateLocal(source_or_dest_reg, field_get, dex_pc);
Calin Juravlee6f49b42015-09-17 14:04:33 +00001204 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001205
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001206 return true;
1207}
1208
Nicolas Geoffray30451742015-06-19 13:32:41 +01001209static mirror::Class* GetClassFrom(CompilerDriver* driver,
1210 const DexCompilationUnit& compilation_unit) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001211 ScopedObjectAccess soa(Thread::Current());
1212 StackHandleScope<2> hs(soa.Self());
Nicolas Geoffray30451742015-06-19 13:32:41 +01001213 const DexFile& dex_file = *compilation_unit.GetDexFile();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001214 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Nicolas Geoffray30451742015-06-19 13:32:41 +01001215 soa.Decode<mirror::ClassLoader*>(compilation_unit.GetClassLoader())));
1216 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001217 compilation_unit.GetClassLinker()->FindDexCache(soa.Self(), dex_file)));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001218
Nicolas Geoffray30451742015-06-19 13:32:41 +01001219 return driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, &compilation_unit);
1220}
1221
1222mirror::Class* HGraphBuilder::GetOutermostCompilingClass() const {
1223 return GetClassFrom(compiler_driver_, *outer_compilation_unit_);
1224}
1225
1226mirror::Class* HGraphBuilder::GetCompilingClass() const {
1227 return GetClassFrom(compiler_driver_, *dex_compilation_unit_);
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001228}
1229
1230bool HGraphBuilder::IsOutermostCompilingClass(uint16_t type_index) const {
1231 ScopedObjectAccess soa(Thread::Current());
1232 StackHandleScope<4> hs(soa.Self());
1233 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001234 dex_compilation_unit_->GetClassLinker()->FindDexCache(
1235 soa.Self(), *dex_compilation_unit_->GetDexFile())));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001236 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1237 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
1238 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
1239 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
Nicolas Geoffrayafd06412015-06-20 22:44:47 +01001240 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001241
Calin Juravle4e2a5572015-10-07 18:55:43 +01001242 // GetOutermostCompilingClass returns null when the class is unresolved
1243 // (e.g. if it derives from an unresolved class). This is bogus knowing that
1244 // we are compiling it.
1245 // When this happens we cannot establish a direct relation between the current
1246 // class and the outer class, so we return false.
1247 // (Note that this is only used for optimizing invokes and field accesses)
1248 return (cls.Get() != nullptr) && (outer_class.Get() == cls.Get());
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001249}
1250
Calin Juravle07380a22015-09-17 14:15:12 +01001251void HGraphBuilder::BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
1252 uint32_t dex_pc,
1253 bool is_put,
1254 Primitive::Type field_type) {
1255 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1256 uint16_t field_index = instruction.VRegB_21c();
1257
1258 if (is_put) {
1259 HInstruction* value = LoadLocal(source_or_dest_reg, field_type, dex_pc);
1260 current_block_->AddInstruction(
1261 new (arena_) HUnresolvedStaticFieldSet(value, field_type, field_index, dex_pc));
1262 } else {
1263 current_block_->AddInstruction(
1264 new (arena_) HUnresolvedStaticFieldGet(field_type, field_index, dex_pc));
1265 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction(), dex_pc);
1266 }
1267}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001268bool HGraphBuilder::BuildStaticFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001269 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001270 bool is_put) {
1271 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1272 uint16_t field_index = instruction.VRegB_21c();
1273
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001274 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierc7853442015-03-27 14:35:38 -07001275 StackHandleScope<4> hs(soa.Self());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001276 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001277 dex_compilation_unit_->GetClassLinker()->FindDexCache(
1278 soa.Self(), *dex_compilation_unit_->GetDexFile())));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001279 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1280 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
Mathieu Chartierc7853442015-03-27 14:35:38 -07001281 ArtField* resolved_field = compiler_driver_->ResolveField(
1282 soa, dex_cache, class_loader, dex_compilation_unit_, field_index, true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001283
Mathieu Chartierc7853442015-03-27 14:35:38 -07001284 if (resolved_field == nullptr) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001285 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
1286 Primitive::Type field_type = GetFieldAccessType(*dex_file_, field_index);
Calin Juravle07380a22015-09-17 14:15:12 +01001287 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001288 return true;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001289 }
1290
Calin Juravle07380a22015-09-17 14:15:12 +01001291 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001292 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
1293 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001294 outer_compilation_unit_->GetClassLinker()->FindDexCache(soa.Self(), outer_dex_file)));
Nicolas Geoffray30451742015-06-19 13:32:41 +01001295 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001296
1297 // The index at which the field's class is stored in the DexCache's type array.
1298 uint32_t storage_index;
Nicolas Geoffray30451742015-06-19 13:32:41 +01001299 bool is_outer_class = (outer_class.Get() == resolved_field->GetDeclaringClass());
1300 if (is_outer_class) {
1301 storage_index = outer_class->GetDexTypeIndex();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001302 } else if (outer_dex_cache.Get() != dex_cache.Get()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001303 // The compiler driver cannot currently understand multiple dex caches involved. Just bailout.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001304 return false;
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001305 } else {
Calin Juravle07380a22015-09-17 14:15:12 +01001306 // TODO: This is rather expensive. Perf it and cache the results if needed.
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001307 std::pair<bool, bool> pair = compiler_driver_->IsFastStaticField(
1308 outer_dex_cache.Get(),
Nicolas Geoffray30451742015-06-19 13:32:41 +01001309 GetCompilingClass(),
Mathieu Chartierc7853442015-03-27 14:35:38 -07001310 resolved_field,
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001311 field_index,
1312 &storage_index);
1313 bool can_easily_access = is_put ? pair.second : pair.first;
1314 if (!can_easily_access) {
Calin Juravle07380a22015-09-17 14:15:12 +01001315 MaybeRecordStat(MethodCompilationStat::kUnresolvedFieldNotAFastAccess);
1316 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
1317 return true;
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001318 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001319 }
1320
1321 // TODO: find out why this check is needed.
1322 bool is_in_dex_cache = compiler_driver_->CanAssumeTypeIsPresentInDexCache(
Nicolas Geoffray6a816cf2015-03-24 16:17:56 +00001323 *outer_compilation_unit_->GetDexFile(), storage_index);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001324 bool is_initialized = resolved_field->GetDeclaringClass()->IsInitialized() && is_in_dex_cache;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001325
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001326 HLoadClass* constant = new (arena_) HLoadClass(graph_->GetCurrentMethod(),
1327 storage_index,
1328 *dex_compilation_unit_->GetDexFile(),
Nicolas Geoffray30451742015-06-19 13:32:41 +01001329 is_outer_class,
Calin Juravle98893e12015-10-02 21:05:03 +01001330 dex_pc,
1331 /*needs_access_check*/ false);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001332 current_block_->AddInstruction(constant);
1333
1334 HInstruction* cls = constant;
Nicolas Geoffray30451742015-06-19 13:32:41 +01001335 if (!is_initialized && !is_outer_class) {
Calin Juravle225ff812014-11-13 16:46:39 +00001336 cls = new (arena_) HClinitCheck(constant, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001337 current_block_->AddInstruction(cls);
1338 }
Mingyao Yang8df69d42015-10-22 15:40:58 -07001339
1340 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001341 if (is_put) {
1342 // We need to keep the class alive before loading the value.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001343 Temporaries temps(graph_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001344 temps.Add(cls);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001345 HInstruction* value = LoadLocal(source_or_dest_reg, field_type, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001346 DCHECK_EQ(value->GetType(), field_type);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001347 current_block_->AddInstruction(new (arena_) HStaticFieldSet(cls,
1348 value,
1349 field_type,
1350 resolved_field->GetOffset(),
1351 resolved_field->IsVolatile(),
1352 field_index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07001353 class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07001354 *dex_file_,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001355 dex_cache_,
1356 dex_pc));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001357 } else {
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001358 current_block_->AddInstruction(new (arena_) HStaticFieldGet(cls,
1359 field_type,
1360 resolved_field->GetOffset(),
1361 resolved_field->IsVolatile(),
1362 field_index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07001363 class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07001364 *dex_file_,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001365 dex_cache_,
1366 dex_pc));
1367 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001368 }
1369 return true;
1370}
1371
Calin Juravlebacfec32014-11-14 15:54:36 +00001372void HGraphBuilder::BuildCheckedDivRem(uint16_t out_vreg,
1373 uint16_t first_vreg,
1374 int64_t second_vreg_or_constant,
1375 uint32_t dex_pc,
1376 Primitive::Type type,
1377 bool second_is_constant,
1378 bool isDiv) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001379 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Calin Juravled0d48522014-11-04 16:40:20 +00001380
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001381 HInstruction* first = LoadLocal(first_vreg, type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001382 HInstruction* second = nullptr;
1383 if (second_is_constant) {
1384 if (type == Primitive::kPrimInt) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001385 second = graph_->GetIntConstant(second_vreg_or_constant, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001386 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001387 second = graph_->GetLongConstant(second_vreg_or_constant, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001388 }
1389 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001390 second = LoadLocal(second_vreg_or_constant, type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001391 }
1392
1393 if (!second_is_constant
1394 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0)
1395 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) {
1396 second = new (arena_) HDivZeroCheck(second, dex_pc);
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001397 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +00001398 current_block_->AddInstruction(second);
1399 temps.Add(current_block_->GetLastInstruction());
1400 }
1401
Calin Juravlebacfec32014-11-14 15:54:36 +00001402 if (isDiv) {
1403 current_block_->AddInstruction(new (arena_) HDiv(type, first, second, dex_pc));
1404 } else {
1405 current_block_->AddInstruction(new (arena_) HRem(type, first, second, dex_pc));
1406 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001407 UpdateLocal(out_vreg, current_block_->GetLastInstruction(), dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001408}
1409
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001410void HGraphBuilder::BuildArrayAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001411 uint32_t dex_pc,
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001412 bool is_put,
1413 Primitive::Type anticipated_type) {
1414 uint8_t source_or_dest_reg = instruction.VRegA_23x();
1415 uint8_t array_reg = instruction.VRegB_23x();
1416 uint8_t index_reg = instruction.VRegC_23x();
1417
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001418 // We need one temporary for the null check, one for the index, and one for the length.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001419 Temporaries temps(graph_);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001420
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001421 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001422 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001423 current_block_->AddInstruction(object);
1424 temps.Add(object);
1425
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001426 HInstruction* length = new (arena_) HArrayLength(object, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001427 current_block_->AddInstruction(length);
1428 temps.Add(length);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001429 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001430 index = new (arena_) HBoundsCheck(index, length, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001431 current_block_->AddInstruction(index);
1432 temps.Add(index);
1433 if (is_put) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001434 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001435 // TODO: Insert a type check node if the type is Object.
Nicolas Geoffray39468442014-09-02 15:17:15 +01001436 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001437 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001438 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001439 current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type, dex_pc));
1440 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001441 }
Mark Mendell1152c922015-04-24 17:06:35 -04001442 graph_->SetHasBoundsChecks(true);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001443}
1444
Calin Juravle225ff812014-11-13 16:46:39 +00001445void HGraphBuilder::BuildFilledNewArray(uint32_t dex_pc,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001446 uint32_t type_index,
1447 uint32_t number_of_vreg_arguments,
1448 bool is_range,
1449 uint32_t* args,
1450 uint32_t register_index) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001451 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments, dex_pc);
Andreas Gampe55d02cf2015-10-29 02:59:50 +00001452 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001453 ? kQuickAllocArrayWithAccessCheck
1454 : kQuickAllocArray;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01001455 HInstruction* object = new (arena_) HNewArray(length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01001456 graph_->GetCurrentMethod(),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01001457 dex_pc,
1458 type_index,
1459 *dex_compilation_unit_->GetDexFile(),
1460 entrypoint);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001461 current_block_->AddInstruction(object);
1462
1463 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1464 DCHECK_EQ(descriptor[0], '[') << descriptor;
1465 char primitive = descriptor[1];
1466 DCHECK(primitive == 'I'
1467 || primitive == 'L'
1468 || primitive == '[') << descriptor;
1469 bool is_reference_array = (primitive == 'L') || (primitive == '[');
1470 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
1471
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001472 Temporaries temps(graph_);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001473 temps.Add(object);
1474 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001475 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type, dex_pc);
1476 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001477 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001478 new (arena_) HArraySet(object, index, value, type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001479 }
1480 latest_result_ = object;
1481}
1482
1483template <typename T>
1484void HGraphBuilder::BuildFillArrayData(HInstruction* object,
1485 const T* data,
1486 uint32_t element_count,
1487 Primitive::Type anticipated_type,
Calin Juravle225ff812014-11-13 16:46:39 +00001488 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001489 for (uint32_t i = 0; i < element_count; ++i) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001490 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1491 HInstruction* value = graph_->GetIntConstant(data[i], dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001492 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001493 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001494 }
1495}
1496
Calin Juravle225ff812014-11-13 16:46:39 +00001497void HGraphBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001498 Temporaries temps(graph_);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001499 HInstruction* array = LoadLocal(instruction.VRegA_31t(), Primitive::kPrimNot, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001500 HNullCheck* null_check = new (arena_) HNullCheck(array, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001501 current_block_->AddInstruction(null_check);
1502 temps.Add(null_check);
1503
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001504 HInstruction* length = new (arena_) HArrayLength(null_check, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001505 current_block_->AddInstruction(length);
1506
Calin Juravle225ff812014-11-13 16:46:39 +00001507 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
Calin Juravled0d48522014-11-04 16:40:20 +00001508 const Instruction::ArrayDataPayload* payload =
1509 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_start_ + payload_offset);
1510 const uint8_t* data = payload->data;
1511 uint32_t element_count = payload->element_count;
1512
1513 // Implementation of this DEX instruction seems to be that the bounds check is
1514 // done before doing any stores.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001515 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001516 current_block_->AddInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
Calin Juravled0d48522014-11-04 16:40:20 +00001517
1518 switch (payload->element_width) {
1519 case 1:
1520 BuildFillArrayData(null_check,
1521 reinterpret_cast<const int8_t*>(data),
1522 element_count,
1523 Primitive::kPrimByte,
Calin Juravle225ff812014-11-13 16:46:39 +00001524 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001525 break;
1526 case 2:
1527 BuildFillArrayData(null_check,
1528 reinterpret_cast<const int16_t*>(data),
1529 element_count,
1530 Primitive::kPrimShort,
Calin Juravle225ff812014-11-13 16:46:39 +00001531 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001532 break;
1533 case 4:
1534 BuildFillArrayData(null_check,
1535 reinterpret_cast<const int32_t*>(data),
1536 element_count,
1537 Primitive::kPrimInt,
Calin Juravle225ff812014-11-13 16:46:39 +00001538 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001539 break;
1540 case 8:
1541 BuildFillWideArrayData(null_check,
1542 reinterpret_cast<const int64_t*>(data),
1543 element_count,
Calin Juravle225ff812014-11-13 16:46:39 +00001544 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001545 break;
1546 default:
1547 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1548 }
Mark Mendell1152c922015-04-24 17:06:35 -04001549 graph_->SetHasBoundsChecks(true);
Calin Juravled0d48522014-11-04 16:40:20 +00001550}
1551
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001552void HGraphBuilder::BuildFillWideArrayData(HInstruction* object,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +01001553 const int64_t* data,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001554 uint32_t element_count,
Calin Juravle225ff812014-11-13 16:46:39 +00001555 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001556 for (uint32_t i = 0; i < element_count; ++i) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001557 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1558 HInstruction* value = graph_->GetLongConstant(data[i], dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001559 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001560 object, index, value, Primitive::kPrimLong, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001561 }
1562}
1563
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001564static TypeCheckKind ComputeTypeCheckKind(Handle<mirror::Class> cls)
1565 SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle98893e12015-10-02 21:05:03 +01001566 if (cls.Get() == nullptr) {
1567 return TypeCheckKind::kUnresolvedCheck;
1568 } else if (cls->IsInterface()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001569 return TypeCheckKind::kInterfaceCheck;
1570 } else if (cls->IsArrayClass()) {
1571 if (cls->GetComponentType()->IsObjectClass()) {
1572 return TypeCheckKind::kArrayObjectCheck;
1573 } else if (cls->CannotBeAssignedFromOtherTypes()) {
1574 return TypeCheckKind::kExactCheck;
1575 } else {
1576 return TypeCheckKind::kArrayCheck;
1577 }
1578 } else if (cls->IsFinal()) {
1579 return TypeCheckKind::kExactCheck;
1580 } else if (cls->IsAbstract()) {
1581 return TypeCheckKind::kAbstractClassCheck;
1582 } else {
1583 return TypeCheckKind::kClassHierarchyCheck;
1584 }
1585}
1586
Calin Juravle98893e12015-10-02 21:05:03 +01001587void HGraphBuilder::BuildTypeCheck(const Instruction& instruction,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001588 uint8_t destination,
1589 uint8_t reference,
1590 uint16_t type_index,
Calin Juravle225ff812014-11-13 16:46:39 +00001591 uint32_t dex_pc) {
Calin Juravle98893e12015-10-02 21:05:03 +01001592 bool type_known_final, type_known_abstract, use_declaring_class;
1593 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
1594 dex_compilation_unit_->GetDexMethodIndex(),
1595 *dex_compilation_unit_->GetDexFile(),
1596 type_index,
1597 &type_known_final,
1598 &type_known_abstract,
1599 &use_declaring_class);
1600
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001601 ScopedObjectAccess soa(Thread::Current());
1602 StackHandleScope<2> hs(soa.Self());
1603 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
1604 dex_compilation_unit_->GetClassLinker()->FindDexCache(
1605 soa.Self(), *dex_compilation_unit_->GetDexFile())));
1606 Handle<mirror::Class> resolved_class(hs.NewHandle(dex_cache->GetResolvedType(type_index)));
1607
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001608 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot, dex_pc);
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001609 HLoadClass* cls = new (arena_) HLoadClass(
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001610 graph_->GetCurrentMethod(),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01001611 type_index,
1612 *dex_compilation_unit_->GetDexFile(),
1613 IsOutermostCompilingClass(type_index),
Calin Juravle98893e12015-10-02 21:05:03 +01001614 dex_pc,
1615 !can_access);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001616 current_block_->AddInstruction(cls);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001617
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001618 // The class needs a temporary before being used by the type check.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001619 Temporaries temps(graph_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001620 temps.Add(cls);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001621
1622 TypeCheckKind check_kind = ComputeTypeCheckKind(resolved_class);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001623 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001624 current_block_->AddInstruction(new (arena_) HInstanceOf(object, cls, check_kind, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001625 UpdateLocal(destination, current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001626 } else {
1627 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001628 current_block_->AddInstruction(new (arena_) HCheckCast(object, cls, check_kind, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001629 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001630}
1631
Andreas Gampe55d02cf2015-10-29 02:59:50 +00001632bool HGraphBuilder::NeedsAccessCheck(uint32_t type_index) const {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001633 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
Andreas Gampe55d02cf2015-10-29 02:59:50 +00001634 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index);
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001635}
1636
Mark Mendellfe57faa2015-09-18 09:26:15 -04001637void HGraphBuilder::BuildSwitchJumpTable(const SwitchTable& table,
1638 const Instruction& instruction,
1639 HInstruction* value,
1640 uint32_t dex_pc) {
1641 // Add the successor blocks to the current block.
1642 uint16_t num_entries = table.GetNumEntries();
1643 for (size_t i = 1; i <= num_entries; i++) {
1644 int32_t target_offset = table.GetEntryAt(i);
1645 HBasicBlock* case_target = FindBlockStartingAt(dex_pc + target_offset);
1646 DCHECK(case_target != nullptr);
1647
1648 // Add the target block as a successor.
1649 current_block_->AddSuccessor(case_target);
1650 }
1651
1652 // Add the default target block as the last successor.
1653 HBasicBlock* default_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
1654 DCHECK(default_target != nullptr);
1655 current_block_->AddSuccessor(default_target);
1656
1657 // Now add the Switch instruction.
1658 int32_t starting_key = table.GetEntryAt(0);
1659 current_block_->AddInstruction(
1660 new (arena_) HPackedSwitch(starting_key, num_entries, value, dex_pc));
1661 // This block ends with control flow.
1662 current_block_ = nullptr;
1663}
1664
Calin Juravle48c2b032014-12-09 18:11:36 +00001665void HGraphBuilder::BuildPackedSwitch(const Instruction& instruction, uint32_t dex_pc) {
David Brazdil2ef645b2015-06-17 18:20:52 +01001666 // Verifier guarantees that the payload for PackedSwitch contains:
1667 // (a) number of entries (may be zero)
1668 // (b) first and lowest switch case value (entry 0, always present)
1669 // (c) list of target pcs (entries 1 <= i <= N)
Andreas Gamped881df52014-11-24 23:28:39 -08001670 SwitchTable table(instruction, dex_pc, false);
1671
1672 // Value to test against.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001673 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt, dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08001674
Mark Mendellfe57faa2015-09-18 09:26:15 -04001675 // Starting key value.
1676 int32_t starting_key = table.GetEntryAt(0);
1677
David Brazdil2ef645b2015-06-17 18:20:52 +01001678 // Retrieve number of entries.
Andreas Gampee4d4d322014-12-04 09:09:57 -08001679 uint16_t num_entries = table.GetNumEntries();
David Brazdil2ef645b2015-06-17 18:20:52 +01001680 if (num_entries == 0) {
1681 return;
1682 }
Andreas Gampee4d4d322014-12-04 09:09:57 -08001683
Mark Mendellfe57faa2015-09-18 09:26:15 -04001684 // Don't use a packed switch if there are very few entries.
1685 if (num_entries > kSmallSwitchThreshold) {
1686 BuildSwitchJumpTable(table, instruction, value, dex_pc);
1687 } else {
1688 // Chained cmp-and-branch, starting from starting_key.
1689 for (size_t i = 1; i <= num_entries; i++) {
Mark Mendell3b9f3042015-09-24 08:43:40 -04001690 BuildSwitchCaseHelper(instruction,
1691 i,
1692 i == num_entries,
1693 table,
1694 value,
1695 starting_key + i - 1,
1696 table.GetEntryAt(i),
1697 dex_pc);
Mark Mendellfe57faa2015-09-18 09:26:15 -04001698 }
Andreas Gamped881df52014-11-24 23:28:39 -08001699 }
Andreas Gamped881df52014-11-24 23:28:39 -08001700}
1701
Calin Juravle48c2b032014-12-09 18:11:36 +00001702void HGraphBuilder::BuildSparseSwitch(const Instruction& instruction, uint32_t dex_pc) {
David Brazdil2ef645b2015-06-17 18:20:52 +01001703 // Verifier guarantees that the payload for SparseSwitch contains:
1704 // (a) number of entries (may be zero)
1705 // (b) sorted key values (entries 0 <= i < N)
1706 // (c) target pcs corresponding to the switch values (entries N <= i < 2*N)
Andreas Gampee4d4d322014-12-04 09:09:57 -08001707 SwitchTable table(instruction, dex_pc, true);
1708
1709 // Value to test against.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001710 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001711
1712 uint16_t num_entries = table.GetNumEntries();
Andreas Gampee4d4d322014-12-04 09:09:57 -08001713
1714 for (size_t i = 0; i < num_entries; i++) {
1715 BuildSwitchCaseHelper(instruction, i, i == static_cast<size_t>(num_entries) - 1, table, value,
1716 table.GetEntryAt(i), table.GetEntryAt(i + num_entries), dex_pc);
1717 }
Andreas Gampee4d4d322014-12-04 09:09:57 -08001718}
1719
1720void HGraphBuilder::BuildSwitchCaseHelper(const Instruction& instruction, size_t index,
1721 bool is_last_case, const SwitchTable& table,
1722 HInstruction* value, int32_t case_value_int,
1723 int32_t target_offset, uint32_t dex_pc) {
David Brazdil852eaff2015-02-02 15:23:05 +00001724 HBasicBlock* case_target = FindBlockStartingAt(dex_pc + target_offset);
1725 DCHECK(case_target != nullptr);
1726 PotentiallyAddSuspendCheck(case_target, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001727
1728 // The current case's value.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001729 HInstruction* this_case_value = graph_->GetIntConstant(case_value_int, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001730
1731 // Compare value and this_case_value.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001732 HEqual* comparison = new (arena_) HEqual(value, this_case_value, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001733 current_block_->AddInstruction(comparison);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001734 HInstruction* ifinst = new (arena_) HIf(comparison, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001735 current_block_->AddInstruction(ifinst);
1736
1737 // Case hit: use the target offset to determine where to go.
Andreas Gampee4d4d322014-12-04 09:09:57 -08001738 current_block_->AddSuccessor(case_target);
1739
1740 // Case miss: go to the next case (or default fall-through).
1741 // When there is a next case, we use the block stored with the table offset representing this
1742 // case (that is where we registered them in ComputeBranchTargets).
1743 // When there is no next case, we use the following instruction.
1744 // TODO: Find a good way to peel the last iteration to avoid conditional, but still have re-use.
1745 if (!is_last_case) {
1746 HBasicBlock* next_case_target = FindBlockStartingAt(table.GetDexPcForIndex(index));
1747 DCHECK(next_case_target != nullptr);
1748 current_block_->AddSuccessor(next_case_target);
1749
1750 // Need to manually add the block, as there is no dex-pc transition for the cases.
1751 graph_->AddBlock(next_case_target);
1752
1753 current_block_ = next_case_target;
1754 } else {
1755 HBasicBlock* default_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
1756 DCHECK(default_target != nullptr);
1757 current_block_->AddSuccessor(default_target);
1758 current_block_ = nullptr;
1759 }
1760}
1761
David Brazdil852eaff2015-02-02 15:23:05 +00001762void HGraphBuilder::PotentiallyAddSuspendCheck(HBasicBlock* target, uint32_t dex_pc) {
1763 int32_t target_offset = target->GetDexPc() - dex_pc;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001764 if (target_offset <= 0) {
David Brazdil852eaff2015-02-02 15:23:05 +00001765 // DX generates back edges to the first encountered return. We can save
1766 // time of later passes by not adding redundant suspend checks.
David Brazdil2fd6aa52015-02-02 18:58:27 +00001767 HInstruction* last_in_target = target->GetLastInstruction();
1768 if (last_in_target != nullptr &&
1769 (last_in_target->IsReturn() || last_in_target->IsReturnVoid())) {
1770 return;
David Brazdil852eaff2015-02-02 15:23:05 +00001771 }
1772
1773 // Add a suspend check to backward branches which may potentially loop. We
1774 // can remove them after we recognize loops in the graph.
Calin Juravle225ff812014-11-13 16:46:39 +00001775 current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_pc));
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001776 }
1777}
1778
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001779bool HGraphBuilder::CanDecodeQuickenedInfo() const {
1780 return interpreter_metadata_ != nullptr;
1781}
1782
1783uint16_t HGraphBuilder::LookupQuickenedInfo(uint32_t dex_pc) {
1784 DCHECK(interpreter_metadata_ != nullptr);
1785 uint32_t dex_pc_in_map = DecodeUnsignedLeb128(&interpreter_metadata_);
1786 DCHECK_EQ(dex_pc, dex_pc_in_map);
1787 return DecodeUnsignedLeb128(&interpreter_metadata_);
1788}
1789
Calin Juravle225ff812014-11-13 16:46:39 +00001790bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001791 if (current_block_ == nullptr) {
1792 return true; // Dead code
1793 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001794
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001795 switch (instruction.Opcode()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001796 case Instruction::CONST_4: {
1797 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001798 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n(), dex_pc);
1799 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001800 break;
1801 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001802
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001803 case Instruction::CONST_16: {
1804 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001805 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s(), dex_pc);
1806 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001807 break;
1808 }
1809
Dave Allison20dfc792014-06-16 20:44:29 -07001810 case Instruction::CONST: {
1811 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001812 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i(), dex_pc);
1813 UpdateLocal(register_index, constant, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001814 break;
1815 }
1816
1817 case Instruction::CONST_HIGH16: {
1818 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001819 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16, dex_pc);
1820 UpdateLocal(register_index, constant, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001821 break;
1822 }
1823
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001824 case Instruction::CONST_WIDE_16: {
1825 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001826 // Get 16 bits of constant value, sign extended to 64 bits.
1827 int64_t value = instruction.VRegB_21s();
1828 value <<= 48;
1829 value >>= 48;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001830 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1831 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001832 break;
1833 }
1834
1835 case Instruction::CONST_WIDE_32: {
1836 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001837 // Get 32 bits of constant value, sign extended to 64 bits.
1838 int64_t value = instruction.VRegB_31i();
1839 value <<= 32;
1840 value >>= 32;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001841 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1842 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001843 break;
1844 }
1845
1846 case Instruction::CONST_WIDE: {
1847 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001848 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l(), dex_pc);
1849 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001850 break;
1851 }
1852
Dave Allison20dfc792014-06-16 20:44:29 -07001853 case Instruction::CONST_WIDE_HIGH16: {
1854 int32_t register_index = instruction.VRegA();
1855 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001856 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1857 UpdateLocal(register_index, constant, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001858 break;
1859 }
1860
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001861 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001862 case Instruction::MOVE:
1863 case Instruction::MOVE_FROM16:
1864 case Instruction::MOVE_16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001865 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
1866 UpdateLocal(instruction.VRegA(), value, dex_pc);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001867 break;
1868 }
1869
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001870 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001871 case Instruction::MOVE_WIDE:
1872 case Instruction::MOVE_WIDE_FROM16:
1873 case Instruction::MOVE_WIDE_16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001874 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong, dex_pc);
1875 UpdateLocal(instruction.VRegA(), value, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001876 break;
1877 }
1878
1879 case Instruction::MOVE_OBJECT:
1880 case Instruction::MOVE_OBJECT_16:
1881 case Instruction::MOVE_OBJECT_FROM16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001882 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot, dex_pc);
1883 UpdateLocal(instruction.VRegA(), value, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001884 break;
1885 }
1886
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001887 case Instruction::RETURN_VOID_NO_BARRIER:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001888 case Instruction::RETURN_VOID: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001889 BuildReturn(instruction, Primitive::kPrimVoid, dex_pc);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001890 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001891 }
1892
Dave Allison20dfc792014-06-16 20:44:29 -07001893#define IF_XX(comparison, cond) \
Calin Juravle225ff812014-11-13 16:46:39 +00001894 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
1895 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001896
Dave Allison20dfc792014-06-16 20:44:29 -07001897 IF_XX(HEqual, EQ);
1898 IF_XX(HNotEqual, NE);
1899 IF_XX(HLessThan, LT);
1900 IF_XX(HLessThanOrEqual, LE);
1901 IF_XX(HGreaterThan, GT);
1902 IF_XX(HGreaterThanOrEqual, GE);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001903
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001904 case Instruction::GOTO:
1905 case Instruction::GOTO_16:
1906 case Instruction::GOTO_32: {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001907 int32_t offset = instruction.GetTargetOffset();
Calin Juravle225ff812014-11-13 16:46:39 +00001908 HBasicBlock* target = FindBlockStartingAt(offset + dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001909 DCHECK(target != nullptr);
David Brazdil852eaff2015-02-02 15:23:05 +00001910 PotentiallyAddSuspendCheck(target, dex_pc);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001911 current_block_->AddInstruction(new (arena_) HGoto(dex_pc));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001912 current_block_->AddSuccessor(target);
1913 current_block_ = nullptr;
1914 break;
1915 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001916
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001917 case Instruction::RETURN: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001918 BuildReturn(instruction, return_type_, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001919 break;
1920 }
1921
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001922 case Instruction::RETURN_OBJECT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001923 BuildReturn(instruction, return_type_, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001924 break;
1925 }
1926
1927 case Instruction::RETURN_WIDE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001928 BuildReturn(instruction, return_type_, dex_pc);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001929 break;
1930 }
1931
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001932 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001933 case Instruction::INVOKE_INTERFACE:
1934 case Instruction::INVOKE_STATIC:
1935 case Instruction::INVOKE_SUPER:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001936 case Instruction::INVOKE_VIRTUAL:
1937 case Instruction::INVOKE_VIRTUAL_QUICK: {
1938 uint16_t method_idx;
1939 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_QUICK) {
1940 if (!CanDecodeQuickenedInfo()) {
1941 return false;
1942 }
1943 method_idx = LookupQuickenedInfo(dex_pc);
1944 } else {
1945 method_idx = instruction.VRegB_35c();
1946 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001947 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001948 uint32_t args[5];
Ian Rogers29a26482014-05-02 15:27:29 -07001949 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00001950 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001951 number_of_vreg_arguments, false, args, -1)) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001952 return false;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001953 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001954 break;
1955 }
1956
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001957 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001958 case Instruction::INVOKE_INTERFACE_RANGE:
1959 case Instruction::INVOKE_STATIC_RANGE:
1960 case Instruction::INVOKE_SUPER_RANGE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001961 case Instruction::INVOKE_VIRTUAL_RANGE:
1962 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
1963 uint16_t method_idx;
1964 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK) {
1965 if (!CanDecodeQuickenedInfo()) {
1966 return false;
1967 }
1968 method_idx = LookupQuickenedInfo(dex_pc);
1969 } else {
1970 method_idx = instruction.VRegB_3rc();
1971 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001972 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1973 uint32_t register_index = instruction.VRegC();
Calin Juravle225ff812014-11-13 16:46:39 +00001974 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001975 number_of_vreg_arguments, true, nullptr, register_index)) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001976 return false;
1977 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001978 break;
1979 }
1980
Roland Levillain88cb1752014-10-20 16:36:47 +01001981 case Instruction::NEG_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001982 Unop_12x<HNeg>(instruction, Primitive::kPrimInt, dex_pc);
Roland Levillain88cb1752014-10-20 16:36:47 +01001983 break;
1984 }
1985
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001986 case Instruction::NEG_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001987 Unop_12x<HNeg>(instruction, Primitive::kPrimLong, dex_pc);
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001988 break;
1989 }
1990
Roland Levillain3dbcb382014-10-28 17:30:07 +00001991 case Instruction::NEG_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001992 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat, dex_pc);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001993 break;
1994 }
1995
1996 case Instruction::NEG_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001997 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble, dex_pc);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001998 break;
1999 }
2000
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002001 case Instruction::NOT_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002002 Unop_12x<HNot>(instruction, Primitive::kPrimInt, dex_pc);
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002003 break;
2004 }
2005
Roland Levillain70566432014-10-24 16:20:17 +01002006 case Instruction::NOT_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002007 Unop_12x<HNot>(instruction, Primitive::kPrimLong, dex_pc);
Roland Levillain70566432014-10-24 16:20:17 +01002008 break;
2009 }
2010
Roland Levillaindff1f282014-11-05 14:15:05 +00002011 case Instruction::INT_TO_LONG: {
Roland Levillain624279f2014-12-04 11:54:28 +00002012 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong, dex_pc);
Roland Levillaindff1f282014-11-05 14:15:05 +00002013 break;
2014 }
2015
Roland Levillaincff13742014-11-17 14:32:17 +00002016 case Instruction::INT_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00002017 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00002018 break;
2019 }
2020
2021 case Instruction::INT_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00002022 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00002023 break;
2024 }
2025
Roland Levillain946e1432014-11-11 17:35:19 +00002026 case Instruction::LONG_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00002027 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt, dex_pc);
Roland Levillain946e1432014-11-11 17:35:19 +00002028 break;
2029 }
2030
Roland Levillain6d0e4832014-11-27 18:31:21 +00002031 case Instruction::LONG_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00002032 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimFloat, dex_pc);
Roland Levillain6d0e4832014-11-27 18:31:21 +00002033 break;
2034 }
2035
Roland Levillain647b9ed2014-11-27 12:06:00 +00002036 case Instruction::LONG_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00002037 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble, dex_pc);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002038 break;
2039 }
2040
Roland Levillain3f8f9362014-12-02 17:45:01 +00002041 case Instruction::FLOAT_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00002042 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimInt, dex_pc);
2043 break;
2044 }
2045
2046 case Instruction::FLOAT_TO_LONG: {
2047 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimLong, dex_pc);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002048 break;
2049 }
2050
Roland Levillain8964e2b2014-12-04 12:10:50 +00002051 case Instruction::FLOAT_TO_DOUBLE: {
2052 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimDouble, dex_pc);
2053 break;
2054 }
2055
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002056 case Instruction::DOUBLE_TO_INT: {
2057 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimInt, dex_pc);
2058 break;
2059 }
2060
2061 case Instruction::DOUBLE_TO_LONG: {
2062 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimLong, dex_pc);
2063 break;
2064 }
2065
Roland Levillain8964e2b2014-12-04 12:10:50 +00002066 case Instruction::DOUBLE_TO_FLOAT: {
2067 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimFloat, dex_pc);
2068 break;
2069 }
2070
Roland Levillain51d3fc42014-11-13 14:11:42 +00002071 case Instruction::INT_TO_BYTE: {
Roland Levillain624279f2014-12-04 11:54:28 +00002072 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte, dex_pc);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002073 break;
2074 }
2075
Roland Levillain01a8d712014-11-14 16:27:39 +00002076 case Instruction::INT_TO_SHORT: {
Roland Levillain624279f2014-12-04 11:54:28 +00002077 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort, dex_pc);
Roland Levillain01a8d712014-11-14 16:27:39 +00002078 break;
2079 }
2080
Roland Levillain981e4542014-11-14 11:47:14 +00002081 case Instruction::INT_TO_CHAR: {
Roland Levillain624279f2014-12-04 11:54:28 +00002082 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar, dex_pc);
Roland Levillain981e4542014-11-14 11:47:14 +00002083 break;
2084 }
2085
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002086 case Instruction::ADD_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002087 Binop_23x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002088 break;
2089 }
2090
2091 case Instruction::ADD_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002092 Binop_23x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002093 break;
2094 }
2095
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002096 case Instruction::ADD_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002097 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002098 break;
2099 }
2100
2101 case Instruction::ADD_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002102 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002103 break;
2104 }
2105
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002106 case Instruction::SUB_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002107 Binop_23x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002108 break;
2109 }
2110
2111 case Instruction::SUB_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002112 Binop_23x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002113 break;
2114 }
2115
Calin Juravle096cc022014-10-23 17:01:13 +01002116 case Instruction::SUB_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002117 Binop_23x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01002118 break;
2119 }
2120
2121 case Instruction::SUB_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002122 Binop_23x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01002123 break;
2124 }
2125
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002126 case Instruction::ADD_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002127 Binop_12x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002128 break;
2129 }
2130
Calin Juravle34bacdf2014-10-07 20:23:36 +01002131 case Instruction::MUL_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002132 Binop_23x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002133 break;
2134 }
2135
2136 case Instruction::MUL_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002137 Binop_23x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002138 break;
2139 }
2140
Calin Juravleb5bfa962014-10-21 18:02:24 +01002141 case Instruction::MUL_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002142 Binop_23x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01002143 break;
2144 }
2145
2146 case Instruction::MUL_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002147 Binop_23x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01002148 break;
2149 }
2150
Calin Juravled0d48522014-11-04 16:40:20 +00002151 case Instruction::DIV_INT: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002152 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2153 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravled0d48522014-11-04 16:40:20 +00002154 break;
2155 }
2156
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002157 case Instruction::DIV_LONG: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002158 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2159 dex_pc, Primitive::kPrimLong, false, true);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002160 break;
2161 }
2162
Calin Juravle7c4954d2014-10-28 16:57:40 +00002163 case Instruction::DIV_FLOAT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002164 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002165 break;
2166 }
2167
2168 case Instruction::DIV_DOUBLE: {
Calin Juravle225ff812014-11-13 16:46:39 +00002169 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002170 break;
2171 }
2172
Calin Juravlebacfec32014-11-14 15:54:36 +00002173 case Instruction::REM_INT: {
2174 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2175 dex_pc, Primitive::kPrimInt, false, false);
2176 break;
2177 }
2178
2179 case Instruction::REM_LONG: {
2180 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2181 dex_pc, Primitive::kPrimLong, false, false);
2182 break;
2183 }
2184
Calin Juravled2ec87d2014-12-08 14:24:46 +00002185 case Instruction::REM_FLOAT: {
2186 Binop_23x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2187 break;
2188 }
2189
2190 case Instruction::REM_DOUBLE: {
2191 Binop_23x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2192 break;
2193 }
2194
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002195 case Instruction::AND_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002196 Binop_23x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002197 break;
2198 }
2199
2200 case Instruction::AND_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002201 Binop_23x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002202 break;
2203 }
2204
Calin Juravle9aec02f2014-11-18 23:06:35 +00002205 case Instruction::SHL_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002206 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002207 break;
2208 }
2209
2210 case Instruction::SHL_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002211 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002212 break;
2213 }
2214
2215 case Instruction::SHR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002216 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002217 break;
2218 }
2219
2220 case Instruction::SHR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002221 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002222 break;
2223 }
2224
2225 case Instruction::USHR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002226 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002227 break;
2228 }
2229
2230 case Instruction::USHR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002231 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002232 break;
2233 }
2234
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002235 case Instruction::OR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002236 Binop_23x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002237 break;
2238 }
2239
2240 case Instruction::OR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002241 Binop_23x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002242 break;
2243 }
2244
2245 case Instruction::XOR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002246 Binop_23x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002247 break;
2248 }
2249
2250 case Instruction::XOR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002251 Binop_23x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002252 break;
2253 }
2254
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002255 case Instruction::ADD_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002256 Binop_12x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002257 break;
2258 }
2259
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002260 case Instruction::ADD_DOUBLE_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002261 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002262 break;
2263 }
2264
2265 case Instruction::ADD_FLOAT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002266 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002267 break;
2268 }
2269
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002270 case Instruction::SUB_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002271 Binop_12x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002272 break;
2273 }
2274
2275 case Instruction::SUB_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002276 Binop_12x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002277 break;
2278 }
2279
Calin Juravle096cc022014-10-23 17:01:13 +01002280 case Instruction::SUB_FLOAT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002281 Binop_12x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01002282 break;
2283 }
2284
2285 case Instruction::SUB_DOUBLE_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002286 Binop_12x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01002287 break;
2288 }
2289
Calin Juravle34bacdf2014-10-07 20:23:36 +01002290 case Instruction::MUL_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002291 Binop_12x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002292 break;
2293 }
2294
2295 case Instruction::MUL_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002296 Binop_12x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002297 break;
2298 }
2299
Calin Juravleb5bfa962014-10-21 18:02:24 +01002300 case Instruction::MUL_FLOAT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002301 Binop_12x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01002302 break;
2303 }
2304
2305 case Instruction::MUL_DOUBLE_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002306 Binop_12x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01002307 break;
2308 }
2309
Calin Juravle865fc882014-11-06 17:09:03 +00002310 case Instruction::DIV_INT_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002311 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2312 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravle865fc882014-11-06 17:09:03 +00002313 break;
2314 }
2315
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002316 case Instruction::DIV_LONG_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002317 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2318 dex_pc, Primitive::kPrimLong, false, true);
2319 break;
2320 }
2321
2322 case Instruction::REM_INT_2ADDR: {
2323 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2324 dex_pc, Primitive::kPrimInt, false, false);
2325 break;
2326 }
2327
2328 case Instruction::REM_LONG_2ADDR: {
2329 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2330 dex_pc, Primitive::kPrimLong, false, false);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002331 break;
2332 }
2333
Calin Juravled2ec87d2014-12-08 14:24:46 +00002334 case Instruction::REM_FLOAT_2ADDR: {
2335 Binop_12x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2336 break;
2337 }
2338
2339 case Instruction::REM_DOUBLE_2ADDR: {
2340 Binop_12x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2341 break;
2342 }
2343
Calin Juravle9aec02f2014-11-18 23:06:35 +00002344 case Instruction::SHL_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002345 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002346 break;
2347 }
2348
2349 case Instruction::SHL_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002350 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002351 break;
2352 }
2353
2354 case Instruction::SHR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002355 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002356 break;
2357 }
2358
2359 case Instruction::SHR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002360 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002361 break;
2362 }
2363
2364 case Instruction::USHR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002365 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002366 break;
2367 }
2368
2369 case Instruction::USHR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002370 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002371 break;
2372 }
2373
Calin Juravle7c4954d2014-10-28 16:57:40 +00002374 case Instruction::DIV_FLOAT_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00002375 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002376 break;
2377 }
2378
2379 case Instruction::DIV_DOUBLE_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00002380 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002381 break;
2382 }
2383
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002384 case Instruction::AND_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002385 Binop_12x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002386 break;
2387 }
2388
2389 case Instruction::AND_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002390 Binop_12x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002391 break;
2392 }
2393
2394 case Instruction::OR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002395 Binop_12x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002396 break;
2397 }
2398
2399 case Instruction::OR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002400 Binop_12x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002401 break;
2402 }
2403
2404 case Instruction::XOR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002405 Binop_12x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002406 break;
2407 }
2408
2409 case Instruction::XOR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002410 Binop_12x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002411 break;
2412 }
2413
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002414 case Instruction::ADD_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002415 Binop_22s<HAdd>(instruction, false, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002416 break;
2417 }
2418
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002419 case Instruction::AND_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002420 Binop_22s<HAnd>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002421 break;
2422 }
2423
2424 case Instruction::OR_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002425 Binop_22s<HOr>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002426 break;
2427 }
2428
2429 case Instruction::XOR_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002430 Binop_22s<HXor>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002431 break;
2432 }
2433
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002434 case Instruction::RSUB_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002435 Binop_22s<HSub>(instruction, true, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002436 break;
2437 }
2438
Calin Juravle34bacdf2014-10-07 20:23:36 +01002439 case Instruction::MUL_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002440 Binop_22s<HMul>(instruction, false, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002441 break;
2442 }
2443
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002444 case Instruction::ADD_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002445 Binop_22b<HAdd>(instruction, false, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002446 break;
2447 }
2448
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002449 case Instruction::AND_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002450 Binop_22b<HAnd>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002451 break;
2452 }
2453
2454 case Instruction::OR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002455 Binop_22b<HOr>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002456 break;
2457 }
2458
2459 case Instruction::XOR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002460 Binop_22b<HXor>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002461 break;
2462 }
2463
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002464 case Instruction::RSUB_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002465 Binop_22b<HSub>(instruction, true, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002466 break;
2467 }
2468
Calin Juravle34bacdf2014-10-07 20:23:36 +01002469 case Instruction::MUL_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002470 Binop_22b<HMul>(instruction, false, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002471 break;
2472 }
2473
Calin Juravled0d48522014-11-04 16:40:20 +00002474 case Instruction::DIV_INT_LIT16:
2475 case Instruction::DIV_INT_LIT8: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002476 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2477 dex_pc, Primitive::kPrimInt, true, true);
2478 break;
2479 }
2480
2481 case Instruction::REM_INT_LIT16:
2482 case Instruction::REM_INT_LIT8: {
2483 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2484 dex_pc, Primitive::kPrimInt, true, false);
Calin Juravled0d48522014-11-04 16:40:20 +00002485 break;
2486 }
2487
Calin Juravle9aec02f2014-11-18 23:06:35 +00002488 case Instruction::SHL_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002489 Binop_22b<HShl>(instruction, false, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002490 break;
2491 }
2492
2493 case Instruction::SHR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002494 Binop_22b<HShr>(instruction, false, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002495 break;
2496 }
2497
2498 case Instruction::USHR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002499 Binop_22b<HUShr>(instruction, false, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002500 break;
2501 }
2502
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002503 case Instruction::NEW_INSTANCE: {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002504 uint16_t type_index = instruction.VRegB_21c();
Jeff Hao848f70a2014-01-15 13:49:50 -08002505 if (compiler_driver_->IsStringTypeIndex(type_index, dex_file_)) {
Jeff Hao848f70a2014-01-15 13:49:50 -08002506 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002507 HFakeString* fake_string = new (arena_) HFakeString(dex_pc);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002508 current_block_->AddInstruction(fake_string);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002509 UpdateLocal(register_index, fake_string, dex_pc);
Jeff Hao848f70a2014-01-15 13:49:50 -08002510 } else {
Andreas Gampe55d02cf2015-10-29 02:59:50 +00002511 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
Jeff Hao848f70a2014-01-15 13:49:50 -08002512 ? kQuickAllocObjectWithAccessCheck
2513 : kQuickAllocObject;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002514
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002515 current_block_->AddInstruction(new (arena_) HNewInstance(
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002516 graph_->GetCurrentMethod(),
2517 dex_pc,
2518 type_index,
2519 *dex_compilation_unit_->GetDexFile(),
2520 entrypoint));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002521 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Jeff Hao848f70a2014-01-15 13:49:50 -08002522 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002523 break;
2524 }
2525
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002526 case Instruction::NEW_ARRAY: {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002527 uint16_t type_index = instruction.VRegC_22c();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002528 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt, dex_pc);
Andreas Gampe55d02cf2015-10-29 02:59:50 +00002529 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002530 ? kQuickAllocArrayWithAccessCheck
2531 : kQuickAllocArray;
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002532 current_block_->AddInstruction(new (arena_) HNewArray(length,
2533 graph_->GetCurrentMethod(),
2534 dex_pc,
2535 type_index,
2536 *dex_compilation_unit_->GetDexFile(),
2537 entrypoint));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002538 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002539 break;
2540 }
2541
2542 case Instruction::FILLED_NEW_ARRAY: {
2543 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
2544 uint32_t type_index = instruction.VRegB_35c();
2545 uint32_t args[5];
2546 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00002547 BuildFilledNewArray(dex_pc, type_index, number_of_vreg_arguments, false, args, 0);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002548 break;
2549 }
2550
2551 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2552 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
2553 uint32_t type_index = instruction.VRegB_3rc();
2554 uint32_t register_index = instruction.VRegC_3rc();
2555 BuildFilledNewArray(
Calin Juravle225ff812014-11-13 16:46:39 +00002556 dex_pc, type_index, number_of_vreg_arguments, true, nullptr, register_index);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002557 break;
2558 }
2559
2560 case Instruction::FILL_ARRAY_DATA: {
Calin Juravle225ff812014-11-13 16:46:39 +00002561 BuildFillArrayData(instruction, dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002562 break;
2563 }
2564
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002565 case Instruction::MOVE_RESULT:
Dave Allison20dfc792014-06-16 20:44:29 -07002566 case Instruction::MOVE_RESULT_WIDE:
David Brazdilfc6a86a2015-06-26 10:33:45 +00002567 case Instruction::MOVE_RESULT_OBJECT: {
Nicolas Geoffray1efcc222015-06-24 12:41:20 +01002568 if (latest_result_ == nullptr) {
2569 // Only dead code can lead to this situation, where the verifier
2570 // does not reject the method.
2571 } else {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002572 // An Invoke/FilledNewArray and its MoveResult could have landed in
2573 // different blocks if there was a try/catch block boundary between
2574 // them. For Invoke, we insert a StoreLocal after the instruction. For
2575 // FilledNewArray, the local needs to be updated after the array was
2576 // filled, otherwise we might overwrite an input vreg.
2577 HStoreLocal* update_local =
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002578 new (arena_) HStoreLocal(GetLocalAt(instruction.VRegA()), latest_result_, dex_pc);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002579 HBasicBlock* block = latest_result_->GetBlock();
2580 if (block == current_block_) {
2581 // MoveResult and the previous instruction are in the same block.
2582 current_block_->AddInstruction(update_local);
2583 } else {
2584 // The two instructions are in different blocks. Insert the MoveResult
2585 // before the final control-flow instruction of the previous block.
2586 DCHECK(block->EndsWithControlFlowInstruction());
2587 DCHECK(current_block_->GetInstructions().IsEmpty());
2588 block->InsertInstructionBefore(update_local, block->GetLastInstruction());
2589 }
Nicolas Geoffray1efcc222015-06-24 12:41:20 +01002590 latest_result_ = nullptr;
2591 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002592 break;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002593 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002594
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002595 case Instruction::CMP_LONG: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002596 Binop_23x_cmp(instruction, Primitive::kPrimLong, ComparisonBias::kNoBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002597 break;
2598 }
2599
2600 case Instruction::CMPG_FLOAT: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002601 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kGtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002602 break;
2603 }
2604
2605 case Instruction::CMPG_DOUBLE: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002606 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kGtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002607 break;
2608 }
2609
2610 case Instruction::CMPL_FLOAT: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002611 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kLtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002612 break;
2613 }
2614
2615 case Instruction::CMPL_DOUBLE: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002616 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kLtBias, dex_pc);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002617 break;
2618 }
2619
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002620 case Instruction::NOP:
2621 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002622
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002623 case Instruction::IGET:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002624 case Instruction::IGET_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002625 case Instruction::IGET_WIDE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002626 case Instruction::IGET_WIDE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002627 case Instruction::IGET_OBJECT:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002628 case Instruction::IGET_OBJECT_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002629 case Instruction::IGET_BOOLEAN:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002630 case Instruction::IGET_BOOLEAN_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002631 case Instruction::IGET_BYTE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002632 case Instruction::IGET_BYTE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002633 case Instruction::IGET_CHAR:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002634 case Instruction::IGET_CHAR_QUICK:
2635 case Instruction::IGET_SHORT:
2636 case Instruction::IGET_SHORT_QUICK: {
Calin Juravle225ff812014-11-13 16:46:39 +00002637 if (!BuildInstanceFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002638 return false;
2639 }
2640 break;
2641 }
2642
2643 case Instruction::IPUT:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002644 case Instruction::IPUT_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002645 case Instruction::IPUT_WIDE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002646 case Instruction::IPUT_WIDE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002647 case Instruction::IPUT_OBJECT:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002648 case Instruction::IPUT_OBJECT_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002649 case Instruction::IPUT_BOOLEAN:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002650 case Instruction::IPUT_BOOLEAN_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002651 case Instruction::IPUT_BYTE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002652 case Instruction::IPUT_BYTE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002653 case Instruction::IPUT_CHAR:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002654 case Instruction::IPUT_CHAR_QUICK:
2655 case Instruction::IPUT_SHORT:
2656 case Instruction::IPUT_SHORT_QUICK: {
Calin Juravle225ff812014-11-13 16:46:39 +00002657 if (!BuildInstanceFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002658 return false;
2659 }
2660 break;
2661 }
2662
2663 case Instruction::SGET:
2664 case Instruction::SGET_WIDE:
2665 case Instruction::SGET_OBJECT:
2666 case Instruction::SGET_BOOLEAN:
2667 case Instruction::SGET_BYTE:
2668 case Instruction::SGET_CHAR:
2669 case Instruction::SGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002670 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002671 return false;
2672 }
2673 break;
2674 }
2675
2676 case Instruction::SPUT:
2677 case Instruction::SPUT_WIDE:
2678 case Instruction::SPUT_OBJECT:
2679 case Instruction::SPUT_BOOLEAN:
2680 case Instruction::SPUT_BYTE:
2681 case Instruction::SPUT_CHAR:
2682 case Instruction::SPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002683 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002684 return false;
2685 }
2686 break;
2687 }
2688
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002689#define ARRAY_XX(kind, anticipated_type) \
2690 case Instruction::AGET##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00002691 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002692 break; \
2693 } \
2694 case Instruction::APUT##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00002695 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002696 break; \
2697 }
2698
2699 ARRAY_XX(, Primitive::kPrimInt);
2700 ARRAY_XX(_WIDE, Primitive::kPrimLong);
2701 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
2702 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
2703 ARRAY_XX(_BYTE, Primitive::kPrimByte);
2704 ARRAY_XX(_CHAR, Primitive::kPrimChar);
2705 ARRAY_XX(_SHORT, Primitive::kPrimShort);
2706
Nicolas Geoffray39468442014-09-02 15:17:15 +01002707 case Instruction::ARRAY_LENGTH: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002708 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot, dex_pc);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002709 // No need for a temporary for the null check, it is the only input of the following
2710 // instruction.
Calin Juravle225ff812014-11-13 16:46:39 +00002711 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002712 current_block_->AddInstruction(object);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002713 current_block_->AddInstruction(new (arena_) HArrayLength(object, dex_pc));
2714 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002715 break;
2716 }
2717
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002718 case Instruction::CONST_STRING: {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002719 current_block_->AddInstruction(
2720 new (arena_) HLoadString(graph_->GetCurrentMethod(), instruction.VRegB_21c(), dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002721 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002722 break;
2723 }
2724
2725 case Instruction::CONST_STRING_JUMBO: {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002726 current_block_->AddInstruction(
2727 new (arena_) HLoadString(graph_->GetCurrentMethod(), instruction.VRegB_31c(), dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002728 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002729 break;
2730 }
2731
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002732 case Instruction::CONST_CLASS: {
2733 uint16_t type_index = instruction.VRegB_21c();
2734 bool type_known_final;
2735 bool type_known_abstract;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002736 bool dont_use_is_referrers_class;
2737 // `CanAccessTypeWithoutChecks` will tell whether the method being
2738 // built is trying to access its own class, so that the generated
2739 // code can optimize for this case. However, the optimization does not
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002740 // work for inlining, so we use `IsOutermostCompilingClass` instead.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002741 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
2742 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002743 &type_known_final, &type_known_abstract, &dont_use_is_referrers_class);
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002744 current_block_->AddInstruction(new (arena_) HLoadClass(
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002745 graph_->GetCurrentMethod(),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002746 type_index,
2747 *dex_compilation_unit_->GetDexFile(),
2748 IsOutermostCompilingClass(type_index),
Calin Juravle98893e12015-10-02 21:05:03 +01002749 dex_pc,
2750 !can_access));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002751 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002752 break;
2753 }
2754
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002755 case Instruction::MOVE_EXCEPTION: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002756 current_block_->AddInstruction(new (arena_) HLoadException(dex_pc));
2757 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction(), dex_pc);
2758 current_block_->AddInstruction(new (arena_) HClearException(dex_pc));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002759 break;
2760 }
2761
2762 case Instruction::THROW: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002763 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00002764 current_block_->AddInstruction(new (arena_) HThrow(exception, dex_pc));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002765 // A throw instruction must branch to the exit block.
2766 current_block_->AddSuccessor(exit_block_);
2767 // We finished building this block. Set the current block to null to avoid
2768 // adding dead instructions to it.
2769 current_block_ = nullptr;
2770 break;
2771 }
2772
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002773 case Instruction::INSTANCE_OF: {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002774 uint8_t destination = instruction.VRegA_22c();
2775 uint8_t reference = instruction.VRegB_22c();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002776 uint16_t type_index = instruction.VRegC_22c();
Calin Juravle98893e12015-10-02 21:05:03 +01002777 BuildTypeCheck(instruction, destination, reference, type_index, dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002778 break;
2779 }
2780
2781 case Instruction::CHECK_CAST: {
2782 uint8_t reference = instruction.VRegA_21c();
2783 uint16_t type_index = instruction.VRegB_21c();
Calin Juravle98893e12015-10-02 21:05:03 +01002784 BuildTypeCheck(instruction, -1, reference, type_index, dex_pc);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002785 break;
2786 }
2787
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002788 case Instruction::MONITOR_ENTER: {
2789 current_block_->AddInstruction(new (arena_) HMonitorOperation(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002790 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot, dex_pc),
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002791 HMonitorOperation::kEnter,
Calin Juravle225ff812014-11-13 16:46:39 +00002792 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002793 break;
2794 }
2795
2796 case Instruction::MONITOR_EXIT: {
2797 current_block_->AddInstruction(new (arena_) HMonitorOperation(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002798 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot, dex_pc),
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002799 HMonitorOperation::kExit,
Calin Juravle225ff812014-11-13 16:46:39 +00002800 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002801 break;
2802 }
2803
Andreas Gamped881df52014-11-24 23:28:39 -08002804 case Instruction::PACKED_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002805 BuildPackedSwitch(instruction, dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08002806 break;
2807 }
2808
Andreas Gampee4d4d322014-12-04 09:09:57 -08002809 case Instruction::SPARSE_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002810 BuildSparseSwitch(instruction, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08002811 break;
2812 }
2813
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002814 default:
Calin Juravle48c2b032014-12-09 18:11:36 +00002815 VLOG(compiler) << "Did not compile "
2816 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
2817 << " because of unhandled instruction "
2818 << instruction.Name();
2819 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnhandledInstruction);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002820 return false;
2821 }
2822 return true;
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00002823} // NOLINT(readability/fn_size)
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002824
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01002825HLocal* HGraphBuilder::GetLocalAt(uint32_t register_index) const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01002826 return locals_[register_index];
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002827}
2828
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01002829void HGraphBuilder::UpdateLocal(uint32_t register_index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002830 HInstruction* instruction,
2831 uint32_t dex_pc) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002832 HLocal* local = GetLocalAt(register_index);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002833 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction, dex_pc));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002834}
2835
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01002836HInstruction* HGraphBuilder::LoadLocal(uint32_t register_index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002837 Primitive::Type type,
2838 uint32_t dex_pc) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002839 HLocal* local = GetLocalAt(register_index);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002840 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type, dex_pc));
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002841 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002842}
2843
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002844} // namespace art