blob: 8d77daf1833f446a6b6cecf09829527fea3c573a [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Nicolas Geoffraye5038322014-07-04 09:41:32 +010017#include "builder.h"
18
Mathieu Chartierc7853442015-03-27 14:35:38 -070019#include "art_field-inl.h"
David Srbecky0cf44932015-12-09 14:09:59 +000020#include "base/arena_bit_vector.h"
21#include "base/bit_vector-inl.h"
Andreas Gamped881df52014-11-24 23:28:39 -080022#include "base/logging.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010023#include "class_linker.h"
Jeff Hao848f70a2014-01-15 13:49:50 -080024#include "dex/verified_method.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000025#include "dex_file-inl.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000026#include "dex_instruction-inl.h"
Roland Levillain4c0eb422015-04-24 16:43:49 +010027#include "dex/verified_method.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010028#include "driver/compiler_driver-inl.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000029#include "driver/compiler_options.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010030#include "mirror/class_loader.h"
31#include "mirror/dex_cache.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000032#include "nodes.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000033#include "primitive.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010034#include "scoped_thread_state_change.h"
35#include "thread.h"
Vladimir Marko58155012015-08-19 12:49:41 +000036#include "utils/dex_cache_arrays_layout-inl.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000037
38namespace art {
39
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010040/**
41 * Helper class to add HTemporary instructions. This class is used when
42 * converting a DEX instruction to multiple HInstruction, and where those
43 * instructions do not die at the following instruction, but instead spans
44 * multiple instructions.
45 */
46class Temporaries : public ValueObject {
47 public:
Calin Juravlef97f9fb2014-11-11 15:38:19 +000048 explicit Temporaries(HGraph* graph) : graph_(graph), index_(0) {}
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010049
50 void Add(HInstruction* instruction) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060051 HInstruction* temp = new (graph_->GetArena()) HTemporary(index_, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010052 instruction->GetBlock()->AddInstruction(temp);
Calin Juravlef97f9fb2014-11-11 15:38:19 +000053
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010054 DCHECK(temp->GetPrevious() == instruction);
Calin Juravlef97f9fb2014-11-11 15:38:19 +000055
56 size_t offset;
57 if (instruction->GetType() == Primitive::kPrimLong
58 || instruction->GetType() == Primitive::kPrimDouble) {
59 offset = 2;
60 } else {
61 offset = 1;
62 }
63 index_ += offset;
64
65 graph_->UpdateTemporariesVRegSlots(index_);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010066 }
67
68 private:
69 HGraph* const graph_;
70
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010071 // Current index in the temporary stack, updated by `Add`.
72 size_t index_;
73};
74
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010075void HGraphBuilder::InitializeLocals(uint16_t count) {
76 graph_->SetNumberOfVRegs(count);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010077 locals_.resize(count);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000078 for (int i = 0; i < count; i++) {
79 HLocal* local = new (arena_) HLocal(i);
80 entry_block_->AddInstruction(local);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010081 locals_[i] = local;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000082 }
83}
84
Nicolas Geoffray52e832b2014-11-06 15:15:31 +000085void HGraphBuilder::InitializeParameters(uint16_t number_of_parameters) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010086 // dex_compilation_unit_ is null only when unit testing.
87 if (dex_compilation_unit_ == nullptr) {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +000088 return;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010089 }
90
91 graph_->SetNumberOfInVRegs(number_of_parameters);
92 const char* shorty = dex_compilation_unit_->GetShorty();
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010093 int locals_index = locals_.size() - number_of_parameters;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010094 int parameter_index = 0;
95
Calin Juravlee6e3bea2015-10-14 13:53:10 +000096 const DexFile::MethodId& referrer_method_id =
97 dex_file_->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010098 if (!dex_compilation_unit_->IsStatic()) {
99 // Add the implicit 'this' argument, not expressed in the signature.
Calin Juravlee6e3bea2015-10-14 13:53:10 +0000100 HParameterValue* parameter = new (arena_) HParameterValue(*dex_file_,
101 referrer_method_id.class_idx_,
102 parameter_index++,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600103 Primitive::kPrimNot,
104 true);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100105 entry_block_->AddInstruction(parameter);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100106 HLocal* local = GetLocalAt(locals_index++);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600107 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter, local->GetDexPc()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100108 number_of_parameters--;
109 }
110
Calin Juravlee6e3bea2015-10-14 13:53:10 +0000111 const DexFile::ProtoId& proto = dex_file_->GetMethodPrototype(referrer_method_id);
112 const DexFile::TypeList* arg_types = dex_file_->GetProtoParameters(proto);
113 for (int i = 0, shorty_pos = 1; i < number_of_parameters; i++) {
114 HParameterValue* parameter = new (arena_) HParameterValue(
115 *dex_file_,
116 arg_types->GetTypeItem(shorty_pos - 1).type_idx_,
117 parameter_index++,
118 Primitive::GetType(shorty[shorty_pos]),
119 false);
120 ++shorty_pos;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100121 entry_block_->AddInstruction(parameter);
122 HLocal* local = GetLocalAt(locals_index++);
123 // Store the parameter value in the local that the dex code will use
124 // to reference that parameter.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600125 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter, local->GetDexPc()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100126 bool is_wide = (parameter->GetType() == Primitive::kPrimLong)
127 || (parameter->GetType() == Primitive::kPrimDouble);
128 if (is_wide) {
129 i++;
130 locals_index++;
131 parameter_index++;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100132 }
133 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100134}
135
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100136template<typename T>
Calin Juravle225ff812014-11-13 16:46:39 +0000137void HGraphBuilder::If_22t(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000138 int32_t target_offset = instruction.GetTargetOffset();
David Brazdil852eaff2015-02-02 15:23:05 +0000139 HBasicBlock* branch_target = FindBlockStartingAt(dex_pc + target_offset);
140 HBasicBlock* fallthrough_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
141 DCHECK(branch_target != nullptr);
142 DCHECK(fallthrough_target != nullptr);
143 PotentiallyAddSuspendCheck(branch_target, dex_pc);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600144 HInstruction* first = LoadLocal(instruction.VRegA(), Primitive::kPrimInt, dex_pc);
145 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
146 T* comparison = new (arena_) T(first, second, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700147 current_block_->AddInstruction(comparison);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600148 HInstruction* ifinst = new (arena_) HIf(comparison, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700149 current_block_->AddInstruction(ifinst);
David Brazdil852eaff2015-02-02 15:23:05 +0000150 current_block_->AddSuccessor(branch_target);
151 current_block_->AddSuccessor(fallthrough_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700152 current_block_ = nullptr;
153}
154
155template<typename T>
Calin Juravle225ff812014-11-13 16:46:39 +0000156void HGraphBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000157 int32_t target_offset = instruction.GetTargetOffset();
David Brazdil852eaff2015-02-02 15:23:05 +0000158 HBasicBlock* branch_target = FindBlockStartingAt(dex_pc + target_offset);
159 HBasicBlock* fallthrough_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
160 DCHECK(branch_target != nullptr);
161 DCHECK(fallthrough_target != nullptr);
162 PotentiallyAddSuspendCheck(branch_target, dex_pc);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600163 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt, dex_pc);
164 T* comparison = new (arena_) T(value, graph_->GetIntConstant(0, dex_pc), dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700165 current_block_->AddInstruction(comparison);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600166 HInstruction* ifinst = new (arena_) HIf(comparison, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700167 current_block_->AddInstruction(ifinst);
David Brazdil852eaff2015-02-02 15:23:05 +0000168 current_block_->AddSuccessor(branch_target);
169 current_block_->AddSuccessor(fallthrough_target);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100170 current_block_ = nullptr;
171}
172
Calin Juravle48c2b032014-12-09 18:11:36 +0000173void HGraphBuilder::MaybeRecordStat(MethodCompilationStat compilation_stat) {
174 if (compilation_stats_ != nullptr) {
175 compilation_stats_->RecordStat(compilation_stat);
176 }
177}
178
David Brazdil1b498722015-03-31 11:37:18 +0100179bool HGraphBuilder::SkipCompilation(const DexFile::CodeItem& code_item,
Calin Juravle48c2b032014-12-09 18:11:36 +0000180 size_t number_of_branches) {
181 const CompilerOptions& compiler_options = compiler_driver_->GetCompilerOptions();
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000182 CompilerOptions::CompilerFilter compiler_filter = compiler_options.GetCompilerFilter();
183 if (compiler_filter == CompilerOptions::kEverything) {
184 return false;
185 }
186
David Brazdil1b498722015-03-31 11:37:18 +0100187 if (compiler_options.IsHugeMethod(code_item.insns_size_in_code_units_)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000188 VLOG(compiler) << "Skip compilation of huge method "
189 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
David Brazdil1b498722015-03-31 11:37:18 +0100190 << ": " << code_item.insns_size_in_code_units_ << " code units";
Calin Juravle48c2b032014-12-09 18:11:36 +0000191 MaybeRecordStat(MethodCompilationStat::kNotCompiledHugeMethod);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000192 return true;
193 }
194
195 // If it's large and contains no branches, it's likely to be machine generated initialization.
David Brazdil1b498722015-03-31 11:37:18 +0100196 if (compiler_options.IsLargeMethod(code_item.insns_size_in_code_units_)
197 && (number_of_branches == 0)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000198 VLOG(compiler) << "Skip compilation of large method with no branch "
199 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
David Brazdil1b498722015-03-31 11:37:18 +0100200 << ": " << code_item.insns_size_in_code_units_ << " code units";
Calin Juravle48c2b032014-12-09 18:11:36 +0000201 MaybeRecordStat(MethodCompilationStat::kNotCompiledLargeMethodNoBranches);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000202 return true;
203 }
204
205 return false;
206}
207
David Brazdilfc6a86a2015-06-26 10:33:45 +0000208void HGraphBuilder::CreateBlocksForTryCatch(const DexFile::CodeItem& code_item) {
209 if (code_item.tries_size_ == 0) {
210 return;
211 }
212
213 // Create branch targets at the start/end of the TryItem range. These are
214 // places where the program might fall through into/out of the a block and
215 // where TryBoundary instructions will be inserted later. Other edges which
216 // enter/exit the try blocks are a result of branches/switches.
217 for (size_t idx = 0; idx < code_item.tries_size_; ++idx) {
218 const DexFile::TryItem* try_item = DexFile::GetTryItems(code_item, idx);
219 uint32_t dex_pc_start = try_item->start_addr_;
220 uint32_t dex_pc_end = dex_pc_start + try_item->insn_count_;
221 FindOrCreateBlockStartingAt(dex_pc_start);
222 if (dex_pc_end < code_item.insns_size_in_code_units_) {
223 // TODO: Do not create block if the last instruction cannot fall through.
224 FindOrCreateBlockStartingAt(dex_pc_end);
225 } else {
226 // The TryItem spans until the very end of the CodeItem (or beyond if
227 // invalid) and therefore cannot have any code afterwards.
228 }
229 }
230
231 // Create branch targets for exception handlers.
232 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(code_item, 0);
233 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
234 for (uint32_t idx = 0; idx < handlers_size; ++idx) {
235 CatchHandlerIterator iterator(handlers_ptr);
236 for (; iterator.HasNext(); iterator.Next()) {
237 uint32_t address = iterator.GetHandlerAddress();
238 HBasicBlock* block = FindOrCreateBlockStartingAt(address);
David Brazdilec16f792015-08-19 15:04:01 +0100239 block->SetTryCatchInformation(
240 new (arena_) TryCatchInformation(iterator.GetHandlerTypeIndex(), *dex_file_));
David Brazdilfc6a86a2015-06-26 10:33:45 +0000241 }
242 handlers_ptr = iterator.EndDataPointer();
243 }
244}
245
David Brazdild7558da2015-09-22 13:04:14 +0100246// Returns the TryItem stored for `block` or nullptr if there is no info for it.
247static const DexFile::TryItem* GetTryItem(
248 HBasicBlock* block,
249 const ArenaSafeMap<uint32_t, const DexFile::TryItem*>& try_block_info) {
250 auto iterator = try_block_info.find(block->GetBlockId());
251 return (iterator == try_block_info.end()) ? nullptr : iterator->second;
252}
David Brazdil56e1acc2015-06-30 15:41:36 +0100253
David Brazdild7558da2015-09-22 13:04:14 +0100254void HGraphBuilder::LinkToCatchBlocks(HTryBoundary* try_boundary,
255 const DexFile::CodeItem& code_item,
256 const DexFile::TryItem* try_item) {
257 for (CatchHandlerIterator it(code_item, *try_item); it.HasNext(); it.Next()) {
David Brazdil56e1acc2015-06-30 15:41:36 +0100258 try_boundary->AddExceptionHandler(FindBlockStartingAt(it.GetHandlerAddress()));
259 }
260}
261
David Brazdilfc6a86a2015-06-26 10:33:45 +0000262void HGraphBuilder::InsertTryBoundaryBlocks(const DexFile::CodeItem& code_item) {
263 if (code_item.tries_size_ == 0) {
264 return;
265 }
266
David Brazdild7558da2015-09-22 13:04:14 +0100267 // Keep a map of all try blocks and their respective TryItems. We do not use
268 // the block's pointer but rather its id to ensure deterministic iteration.
269 ArenaSafeMap<uint32_t, const DexFile::TryItem*> try_block_info(
Vladimir Marko5233f932015-09-29 19:01:15 +0100270 std::less<uint32_t>(), arena_->Adapter(kArenaAllocGraphBuilder));
David Brazdilbff75032015-07-08 17:26:51 +0000271
David Brazdild7558da2015-09-22 13:04:14 +0100272 // Obtain TryItem information for blocks with throwing instructions, and split
273 // blocks which are both try & catch to simplify the graph.
274 // NOTE: We are appending new blocks inside the loop, so we need to use index
275 // because iterators can be invalidated. We remember the initial size to avoid
276 // iterating over the new blocks which cannot throw.
277 for (size_t i = 0, e = graph_->GetBlocks().size(); i < e; ++i) {
278 HBasicBlock* block = graph_->GetBlocks()[i];
David Brazdil72783ff2015-07-09 14:36:05 +0100279
David Brazdild7558da2015-09-22 13:04:14 +0100280 // Do not bother creating exceptional edges for try blocks which have no
281 // throwing instructions. In that case we simply assume that the block is
282 // not covered by a TryItem. This prevents us from creating a throw-catch
283 // loop for synchronized blocks.
284 if (block->HasThrowingInstructions()) {
285 // Try to find a TryItem covering the block.
286 DCHECK_NE(block->GetDexPc(), kNoDexPc) << "Block must have a dec_pc to find its TryItem.";
287 const int32_t try_item_idx = DexFile::FindTryItem(code_item, block->GetDexPc());
288 if (try_item_idx != -1) {
289 // Block throwing and in a TryItem. Store the try block information.
290 HBasicBlock* throwing_block = block;
291 if (block->IsCatchBlock()) {
292 // Simplify blocks which are both try and catch, otherwise we would
293 // need a strategy for splitting exceptional edges. We split the block
294 // after the move-exception (if present) and mark the first part not
295 // throwing. The normal-flow edge between them will be split later.
David Brazdil9bc43612015-11-05 21:25:24 +0000296 throwing_block = block->SplitCatchBlockAfterMoveException();
297 // Move-exception does not throw and the block has throwing insructions
298 // so it must have been possible to split it.
299 DCHECK(throwing_block != nullptr);
David Brazdil72783ff2015-07-09 14:36:05 +0100300 }
David Brazdild7558da2015-09-22 13:04:14 +0100301
302 try_block_info.Put(throwing_block->GetBlockId(),
303 DexFile::GetTryItems(code_item, try_item_idx));
David Brazdil72783ff2015-07-09 14:36:05 +0100304 }
David Brazdil72783ff2015-07-09 14:36:05 +0100305 }
David Brazdilbff75032015-07-08 17:26:51 +0000306 }
307
David Brazdild7558da2015-09-22 13:04:14 +0100308 // Do a pass over the try blocks and insert entering TryBoundaries where at
309 // least one predecessor is not covered by the same TryItem as the try block.
310 // We do not split each edge separately, but rather create one boundary block
311 // that all predecessors are relinked to. This preserves loop headers (b/23895756).
312 for (auto entry : try_block_info) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100313 HBasicBlock* try_block = graph_->GetBlocks()[entry.first];
David Brazdild7558da2015-09-22 13:04:14 +0100314 for (HBasicBlock* predecessor : try_block->GetPredecessors()) {
315 if (GetTryItem(predecessor, try_block_info) != entry.second) {
316 // Found a predecessor not covered by the same TryItem. Insert entering
317 // boundary block.
318 HTryBoundary* try_entry =
319 new (arena_) HTryBoundary(HTryBoundary::kEntry, try_block->GetDexPc());
320 try_block->CreateImmediateDominator()->AddInstruction(try_entry);
321 LinkToCatchBlocks(try_entry, code_item, entry.second);
322 break;
323 }
David Brazdil281a6322015-07-03 10:34:57 +0100324 }
David Brazdild7558da2015-09-22 13:04:14 +0100325 }
David Brazdil281a6322015-07-03 10:34:57 +0100326
David Brazdild7558da2015-09-22 13:04:14 +0100327 // Do a second pass over the try blocks and insert exit TryBoundaries where
328 // the successor is not in the same TryItem.
329 for (auto entry : try_block_info) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100330 HBasicBlock* try_block = graph_->GetBlocks()[entry.first];
David Brazdild7558da2015-09-22 13:04:14 +0100331 // NOTE: Do not use iterators because SplitEdge would invalidate them.
332 for (size_t i = 0, e = try_block->GetSuccessors().size(); i < e; ++i) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100333 HBasicBlock* successor = try_block->GetSuccessors()[i];
David Brazdil72783ff2015-07-09 14:36:05 +0100334
David Brazdild7558da2015-09-22 13:04:14 +0100335 // If the successor is a try block, all of its predecessors must be
336 // covered by the same TryItem. Otherwise the previous pass would have
337 // created a non-throwing boundary block.
338 if (GetTryItem(successor, try_block_info) != nullptr) {
339 DCHECK_EQ(entry.second, GetTryItem(successor, try_block_info));
David Brazdil72783ff2015-07-09 14:36:05 +0100340 continue;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000341 }
David Brazdil281a6322015-07-03 10:34:57 +0100342
David Brazdild7558da2015-09-22 13:04:14 +0100343 // Preserve the invariant that Return(Void) always jumps to Exit by moving
344 // it outside the try block if necessary.
345 HInstruction* last_instruction = try_block->GetLastInstruction();
346 if (last_instruction->IsReturn() || last_instruction->IsReturnVoid()) {
347 DCHECK_EQ(successor, exit_block_);
348 successor = try_block->SplitBefore(last_instruction);
David Brazdil281a6322015-07-03 10:34:57 +0100349 }
David Brazdild7558da2015-09-22 13:04:14 +0100350
351 // Insert TryBoundary and link to catch blocks.
352 HTryBoundary* try_exit =
353 new (arena_) HTryBoundary(HTryBoundary::kExit, successor->GetDexPc());
354 graph_->SplitEdge(try_block, successor)->AddInstruction(try_exit);
355 LinkToCatchBlocks(try_exit, code_item, entry.second);
David Brazdil281a6322015-07-03 10:34:57 +0100356 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000357 }
358}
359
David Brazdil5e8b1372015-01-23 14:39:08 +0000360bool HGraphBuilder::BuildGraph(const DexFile::CodeItem& code_item) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100361 DCHECK(graph_->GetBlocks().empty());
David Brazdil5e8b1372015-01-23 14:39:08 +0000362
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000363 const uint16_t* code_ptr = code_item.insns_;
364 const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100365 code_start_ = code_ptr;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000366
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000367 // Setup the graph with the entry block and exit block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100368 entry_block_ = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000369 graph_->AddBlock(entry_block_);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100370 exit_block_ = new (arena_) HBasicBlock(graph_, kNoDexPc);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000371 graph_->SetEntryBlock(entry_block_);
372 graph_->SetExitBlock(exit_block_);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000373
David Brazdil77a48ae2015-09-15 12:34:04 +0000374 graph_->SetHasTryCatch(code_item.tries_size_ != 0);
375
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000376 InitializeLocals(code_item.registers_size_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000377 graph_->SetMaximumNumberOfOutVRegs(code_item.outs_size_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000378
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000379 // Compute the number of dex instructions, blocks, and branches. We will
380 // check these values against limits given to the compiler.
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000381 size_t number_of_branches = 0;
382
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000383 // To avoid splitting blocks, we compute ahead of time the instructions that
384 // start a new block, and create these blocks.
Calin Juravle702d2602015-04-30 19:28:21 +0100385 if (!ComputeBranchTargets(code_ptr, code_end, &number_of_branches)) {
386 MaybeRecordStat(MethodCompilationStat::kNotCompiledBranchOutsideMethodCode);
387 return false;
388 }
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000389
390 // Note that the compiler driver is null when unit testing.
David Brazdil1b498722015-03-31 11:37:18 +0100391 if ((compiler_driver_ != nullptr) && SkipCompilation(code_item, number_of_branches)) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000392 return false;
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000393 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000394
David Srbecky0cf44932015-12-09 14:09:59 +0000395 // Find locations where we want to generate extra stackmaps for native debugging.
396 // This allows us to generate the info only at interesting points (for example,
397 // at start of java statement) rather than before every dex instruction.
398 const bool native_debuggable = compiler_driver_ != nullptr &&
399 compiler_driver_->GetCompilerOptions().GetNativeDebuggable();
400 ArenaBitVector* native_debug_info_locations;
401 if (native_debuggable) {
402 const uint32_t num_instructions = code_item.insns_size_in_code_units_;
403 native_debug_info_locations = new (arena_) ArenaBitVector (arena_, num_instructions, false);
404 native_debug_info_locations->ClearAllBits();
405 FindNativeDebugInfoLocations(code_item, native_debug_info_locations);
406 }
407
David Brazdilfc6a86a2015-06-26 10:33:45 +0000408 CreateBlocksForTryCatch(code_item);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000409
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000410 InitializeParameters(code_item.ins_size_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100411
Calin Juravle225ff812014-11-13 16:46:39 +0000412 size_t dex_pc = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000413 while (code_ptr < code_end) {
Calin Juravle225ff812014-11-13 16:46:39 +0000414 // Update the current block if dex_pc starts a new block.
415 MaybeUpdateCurrentBlock(dex_pc);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000416 const Instruction& instruction = *Instruction::At(code_ptr);
David Srbecky0cf44932015-12-09 14:09:59 +0000417 if (native_debuggable && native_debug_info_locations->IsBitSet(dex_pc)) {
418 if (current_block_ != nullptr) {
419 current_block_->AddInstruction(new (arena_) HNativeDebugInfo(dex_pc));
420 }
421 }
Calin Juravle48c2b032014-12-09 18:11:36 +0000422 if (!AnalyzeDexInstruction(instruction, dex_pc)) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000423 return false;
Calin Juravle48c2b032014-12-09 18:11:36 +0000424 }
Calin Juravle225ff812014-11-13 16:46:39 +0000425 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000426 code_ptr += instruction.SizeInCodeUnits();
427 }
428
David Brazdilfc6a86a2015-06-26 10:33:45 +0000429 // Add Exit to the exit block.
David Brazdil3e187382015-06-26 09:59:52 +0000430 exit_block_->AddInstruction(new (arena_) HExit());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000431 // Add the suspend check to the entry block.
432 entry_block_->AddInstruction(new (arena_) HSuspendCheck(0));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000433 entry_block_->AddInstruction(new (arena_) HGoto());
David Brazdilbff75032015-07-08 17:26:51 +0000434 // Add the exit block at the end.
435 graph_->AddBlock(exit_block_);
David Brazdil5e8b1372015-01-23 14:39:08 +0000436
David Brazdilfc6a86a2015-06-26 10:33:45 +0000437 // Iterate over blocks covered by TryItems and insert TryBoundaries at entry
438 // and exit points. This requires all control-flow instructions and
439 // non-exceptional edges to have been created.
440 InsertTryBoundaryBlocks(code_item);
441
David Brazdil5e8b1372015-01-23 14:39:08 +0000442 return true;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000443}
444
David Brazdilfc6a86a2015-06-26 10:33:45 +0000445void HGraphBuilder::MaybeUpdateCurrentBlock(size_t dex_pc) {
446 HBasicBlock* block = FindBlockStartingAt(dex_pc);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000447 if (block == nullptr) {
448 return;
449 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000450
451 if (current_block_ != nullptr) {
452 // Branching instructions clear current_block, so we know
453 // the last instruction of the current block is not a branching
454 // instruction. We add an unconditional goto to the found block.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600455 current_block_->AddInstruction(new (arena_) HGoto(dex_pc));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000456 current_block_->AddSuccessor(block);
457 }
458 graph_->AddBlock(block);
459 current_block_ = block;
460}
461
David Srbecky0cf44932015-12-09 14:09:59 +0000462void HGraphBuilder::FindNativeDebugInfoLocations(const DexFile::CodeItem& code_item,
463 ArenaBitVector* locations) {
464 // The callback gets called when the line number changes.
465 // In other words, it marks the start of new java statement.
466 struct Callback {
467 static bool Position(void* ctx, const DexFile::PositionInfo& entry) {
468 static_cast<ArenaBitVector*>(ctx)->SetBit(entry.address_);
469 return false;
470 }
471 };
472 dex_file_->DecodeDebugPositionInfo(&code_item, Callback::Position, locations);
473 // Add native debug info at the start of every basic block.
474 for (uint32_t pc = 0; pc < code_item.insns_size_in_code_units_; pc++) {
475 if (FindBlockStartingAt(pc) != nullptr) {
476 locations->SetBit(pc);
477 }
478 }
479 // Instruction-specific tweaks.
480 const Instruction* const begin = Instruction::At(code_item.insns_);
481 const Instruction* const end = begin->RelativeAt(code_item.insns_size_in_code_units_);
482 for (const Instruction* inst = begin; inst < end; inst = inst->Next()) {
483 switch (inst->Opcode()) {
484 case Instruction::MOVE_EXCEPTION:
485 case Instruction::MOVE_RESULT:
486 case Instruction::MOVE_RESULT_WIDE:
487 case Instruction::MOVE_RESULT_OBJECT: {
488 // The compiler checks that there are no instructions before those.
489 // So generate HNativeDebugInfo after them instead.
490 locations->ClearBit(inst->GetDexPc(code_item.insns_));
491 const Instruction* next = inst->Next();
492 if (next < end) {
493 locations->SetBit(next->GetDexPc(code_item.insns_));
494 }
495 break;
496 }
497 default:
498 break;
499 }
500 }
501}
502
Calin Juravle702d2602015-04-30 19:28:21 +0100503bool HGraphBuilder::ComputeBranchTargets(const uint16_t* code_ptr,
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000504 const uint16_t* code_end,
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000505 size_t* number_of_branches) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100506 branch_targets_.resize(code_end - code_ptr, nullptr);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000507
508 // Create the first block for the dex instructions, single successor of the entry block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100509 HBasicBlock* block = new (arena_) HBasicBlock(graph_, 0);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100510 branch_targets_[0] = block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000511 entry_block_->AddSuccessor(block);
512
513 // Iterate over all instructions and find branching instructions. Create blocks for
514 // the locations these instructions branch to.
Andreas Gamped881df52014-11-24 23:28:39 -0800515 uint32_t dex_pc = 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000516 while (code_ptr < code_end) {
517 const Instruction& instruction = *Instruction::At(code_ptr);
518 if (instruction.IsBranch()) {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000519 (*number_of_branches)++;
Calin Juravle225ff812014-11-13 16:46:39 +0000520 int32_t target = instruction.GetTargetOffset() + dex_pc;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000521 // Create a block for the target instruction.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000522 FindOrCreateBlockStartingAt(target);
523
Calin Juravle225ff812014-11-13 16:46:39 +0000524 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000525 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle702d2602015-04-30 19:28:21 +0100526
David Brazdilfe659462015-06-24 14:23:56 +0100527 if (instruction.CanFlowThrough()) {
528 if (code_ptr >= code_end) {
Calin Juravle702d2602015-04-30 19:28:21 +0100529 // In the normal case we should never hit this but someone can artificially forge a dex
530 // file to fall-through out the method code. In this case we bail out compilation.
531 return false;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000532 } else {
533 FindOrCreateBlockStartingAt(dex_pc);
Calin Juravle702d2602015-04-30 19:28:21 +0100534 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000535 }
Andreas Gampee4d4d322014-12-04 09:09:57 -0800536 } else if (instruction.IsSwitch()) {
537 SwitchTable table(instruction, dex_pc, instruction.Opcode() == Instruction::SPARSE_SWITCH);
Andreas Gamped881df52014-11-24 23:28:39 -0800538
539 uint16_t num_entries = table.GetNumEntries();
540
Andreas Gampee4d4d322014-12-04 09:09:57 -0800541 // In a packed-switch, the entry at index 0 is the starting key. In a sparse-switch, the
542 // entry at index 0 is the first key, and values are after *all* keys.
543 size_t offset = table.GetFirstValueIndex();
544
545 // Use a larger loop counter type to avoid overflow issues.
546 for (size_t i = 0; i < num_entries; ++i) {
Andreas Gamped881df52014-11-24 23:28:39 -0800547 // The target of the case.
Andreas Gampee4d4d322014-12-04 09:09:57 -0800548 uint32_t target = dex_pc + table.GetEntryAt(i + offset);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000549 FindOrCreateBlockStartingAt(target);
Andreas Gamped881df52014-11-24 23:28:39 -0800550
David Brazdil281a6322015-07-03 10:34:57 +0100551 // Create a block for the switch-case logic. The block gets the dex_pc
552 // of the SWITCH instruction because it is part of its semantics.
553 block = new (arena_) HBasicBlock(graph_, dex_pc);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100554 branch_targets_[table.GetDexPcForIndex(i)] = block;
Andreas Gamped881df52014-11-24 23:28:39 -0800555 }
556
557 // Fall-through. Add a block if there is more code afterwards.
558 dex_pc += instruction.SizeInCodeUnits();
559 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle702d2602015-04-30 19:28:21 +0100560 if (code_ptr >= code_end) {
561 // In the normal case we should never hit this but someone can artificially forge a dex
562 // file to fall-through out the method code. In this case we bail out compilation.
563 // (A switch can fall-through so we don't need to check CanFlowThrough().)
564 return false;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000565 } else {
566 FindOrCreateBlockStartingAt(dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -0800567 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000568 } else {
569 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle225ff812014-11-13 16:46:39 +0000570 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000571 }
572 }
Calin Juravle702d2602015-04-30 19:28:21 +0100573 return true;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000574}
575
David Brazdilfc6a86a2015-06-26 10:33:45 +0000576HBasicBlock* HGraphBuilder::FindBlockStartingAt(int32_t dex_pc) const {
577 DCHECK_GE(dex_pc, 0);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100578 return branch_targets_[dex_pc];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000579}
580
581HBasicBlock* HGraphBuilder::FindOrCreateBlockStartingAt(int32_t dex_pc) {
582 HBasicBlock* block = FindBlockStartingAt(dex_pc);
583 if (block == nullptr) {
584 block = new (arena_) HBasicBlock(graph_, dex_pc);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100585 branch_targets_[dex_pc] = block;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000586 }
587 return block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000588}
589
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100590template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600591void HGraphBuilder::Unop_12x(const Instruction& instruction,
592 Primitive::Type type,
593 uint32_t dex_pc) {
594 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
595 current_block_->AddInstruction(new (arena_) T(type, first, dex_pc));
596 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Roland Levillain88cb1752014-10-20 16:36:47 +0100597}
598
Roland Levillaindff1f282014-11-05 14:15:05 +0000599void HGraphBuilder::Conversion_12x(const Instruction& instruction,
600 Primitive::Type input_type,
Roland Levillain624279f2014-12-04 11:54:28 +0000601 Primitive::Type result_type,
602 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600603 HInstruction* first = LoadLocal(instruction.VRegB(), input_type, dex_pc);
Roland Levillain624279f2014-12-04 11:54:28 +0000604 current_block_->AddInstruction(new (arena_) HTypeConversion(result_type, first, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600605 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100606}
607
608template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000609void HGraphBuilder::Binop_23x(const Instruction& instruction,
610 Primitive::Type type,
611 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600612 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
613 HInstruction* second = LoadLocal(instruction.VRegC(), type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000614 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600615 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000616}
617
618template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000619void HGraphBuilder::Binop_23x_shift(const Instruction& instruction,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600620 Primitive::Type type,
621 uint32_t dex_pc) {
622 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
623 HInstruction* second = LoadLocal(instruction.VRegC(), Primitive::kPrimInt, dex_pc);
624 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
625 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000626}
627
Calin Juravleddb7df22014-11-25 20:56:51 +0000628void HGraphBuilder::Binop_23x_cmp(const Instruction& instruction,
629 Primitive::Type type,
Mark Mendellc4701932015-04-10 13:18:51 -0400630 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700631 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600632 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
633 HInstruction* second = LoadLocal(instruction.VRegC(), type, dex_pc);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700634 current_block_->AddInstruction(new (arena_) HCompare(type, first, second, bias, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600635 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +0000636}
637
Calin Juravle9aec02f2014-11-18 23:06:35 +0000638template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600639void HGraphBuilder::Binop_12x_shift(const Instruction& instruction, Primitive::Type type,
640 uint32_t dex_pc) {
641 HInstruction* first = LoadLocal(instruction.VRegA(), type, dex_pc);
642 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
643 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
644 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000645}
646
647template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000648void HGraphBuilder::Binop_12x(const Instruction& instruction,
649 Primitive::Type type,
650 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600651 HInstruction* first = LoadLocal(instruction.VRegA(), type, dex_pc);
652 HInstruction* second = LoadLocal(instruction.VRegB(), type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000653 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600654 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000655}
656
657template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600658void HGraphBuilder::Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
659 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
660 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100661 if (reverse) {
662 std::swap(first, second);
663 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600664 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second, dex_pc));
665 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100666}
667
668template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600669void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
670 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
671 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100672 if (reverse) {
673 std::swap(first, second);
674 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600675 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second, dex_pc));
676 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100677}
678
Calin Juravle0c25d102015-04-20 14:49:09 +0100679static bool RequiresConstructorBarrier(const DexCompilationUnit* cu, const CompilerDriver& driver) {
Calin Juravle27df7582015-04-17 19:12:31 +0100680 Thread* self = Thread::Current();
Calin Juravle0c25d102015-04-20 14:49:09 +0100681 return cu->IsConstructor()
682 && driver.RequiresConstructorBarrier(self, cu->GetDexFile(), cu->GetClassDefIndex());
Calin Juravle27df7582015-04-17 19:12:31 +0100683}
684
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600685void HGraphBuilder::BuildReturn(const Instruction& instruction,
686 Primitive::Type type,
687 uint32_t dex_pc) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100688 if (type == Primitive::kPrimVoid) {
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100689 if (graph_->ShouldGenerateConstructorBarrier()) {
690 // The compilation unit is null during testing.
691 if (dex_compilation_unit_ != nullptr) {
692 DCHECK(RequiresConstructorBarrier(dex_compilation_unit_, *compiler_driver_))
693 << "Inconsistent use of ShouldGenerateConstructorBarrier. Should not generate a barrier.";
694 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600695 current_block_->AddInstruction(new (arena_) HMemoryBarrier(kStoreStore, dex_pc));
Calin Juravle27df7582015-04-17 19:12:31 +0100696 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600697 current_block_->AddInstruction(new (arena_) HReturnVoid(dex_pc));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100698 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600699 HInstruction* value = LoadLocal(instruction.VRegA(), type, dex_pc);
700 current_block_->AddInstruction(new (arena_) HReturn(value, dex_pc));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100701 }
702 current_block_->AddSuccessor(exit_block_);
703 current_block_ = nullptr;
704}
705
Calin Juravle68ad6492015-08-18 17:08:12 +0100706static InvokeType GetInvokeTypeFromOpCode(Instruction::Code opcode) {
707 switch (opcode) {
708 case Instruction::INVOKE_STATIC:
709 case Instruction::INVOKE_STATIC_RANGE:
710 return kStatic;
711 case Instruction::INVOKE_DIRECT:
712 case Instruction::INVOKE_DIRECT_RANGE:
713 return kDirect;
714 case Instruction::INVOKE_VIRTUAL:
715 case Instruction::INVOKE_VIRTUAL_QUICK:
716 case Instruction::INVOKE_VIRTUAL_RANGE:
717 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
718 return kVirtual;
719 case Instruction::INVOKE_INTERFACE:
720 case Instruction::INVOKE_INTERFACE_RANGE:
721 return kInterface;
722 case Instruction::INVOKE_SUPER_RANGE:
723 case Instruction::INVOKE_SUPER:
724 return kSuper;
725 default:
726 LOG(FATAL) << "Unexpected invoke opcode: " << opcode;
727 UNREACHABLE();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100728 }
Calin Juravle68ad6492015-08-18 17:08:12 +0100729}
730
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000731ArtMethod* HGraphBuilder::ResolveMethod(uint16_t method_idx, InvokeType invoke_type) {
732 ScopedObjectAccess soa(Thread::Current());
Alex Lightfedd91d2016-01-07 14:49:16 -0800733 StackHandleScope<3> hs(soa.Self());
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000734
735 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
736 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
737 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
738 Handle<mirror::Class> compiling_class(hs.NewHandle(GetCompilingClass()));
739
Andreas Gampedae24142015-12-03 17:27:32 -0800740 ArtMethod* resolved_method = class_linker->ResolveMethod<ClassLinker::kForceICCECheck>(
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000741 *dex_compilation_unit_->GetDexFile(),
742 method_idx,
743 dex_compilation_unit_->GetDexCache(),
744 class_loader,
745 /* referrer */ nullptr,
746 invoke_type);
747
748 if (UNLIKELY(resolved_method == nullptr)) {
749 // Clean up any exception left by type resolution.
750 soa.Self()->ClearException();
751 return nullptr;
752 }
753
754 // Check access. The class linker has a fast path for looking into the dex cache
755 // and does not check the access if it hits it.
756 if (compiling_class.Get() == nullptr) {
757 if (!resolved_method->IsPublic()) {
758 return nullptr;
759 }
760 } else if (!compiling_class->CanAccessResolvedMethod(resolved_method->GetDeclaringClass(),
761 resolved_method,
762 dex_compilation_unit_->GetDexCache().Get(),
763 method_idx)) {
764 return nullptr;
765 }
766
767 // We have to special case the invoke-super case, as ClassLinker::ResolveMethod does not.
Alex Lightfedd91d2016-01-07 14:49:16 -0800768 // We need to look at the referrer's super class vtable. We need to do this to know if we need to
769 // make this an invoke-unresolved to handle cross-dex invokes or abstract super methods, both of
770 // which require runtime handling.
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000771 if (invoke_type == kSuper) {
772 if (compiling_class.Get() == nullptr) {
Alex Lightfedd91d2016-01-07 14:49:16 -0800773 // We could not determine the method's class we need to wait until runtime.
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000774 DCHECK(Runtime::Current()->IsAotCompiler());
775 return nullptr;
776 }
Alex Lightfedd91d2016-01-07 14:49:16 -0800777 ArtMethod* current_method = graph_->GetArtMethod();
778 DCHECK(current_method != nullptr);
779 Handle<mirror::Class> methods_class(hs.NewHandle(
780 dex_compilation_unit_->GetClassLinker()->ResolveReferencedClassOfMethod(Thread::Current(),
781 method_idx,
782 current_method)));
783 if (methods_class.Get() == nullptr) {
784 // Invoking a super method requires knowing the actual super class. If we did not resolve
785 // the compiling method's declaring class (which only happens for ahead of time
786 // compilation), bail out.
787 DCHECK(Runtime::Current()->IsAotCompiler());
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000788 return nullptr;
Alex Lightfedd91d2016-01-07 14:49:16 -0800789 } else {
790 ArtMethod* actual_method;
791 if (methods_class->IsInterface()) {
792 actual_method = methods_class->FindVirtualMethodForInterfaceSuper(
793 resolved_method, class_linker->GetImagePointerSize());
794 } else {
795 uint16_t vtable_index = resolved_method->GetMethodIndex();
796 actual_method = compiling_class->GetSuperClass()->GetVTableEntry(
797 vtable_index, class_linker->GetImagePointerSize());
798 }
799 if (actual_method != resolved_method &&
800 !IsSameDexFile(*actual_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
801 // The back-end code generator relies on this check in order to ensure that it will not
802 // attempt to read the dex_cache with a dex_method_index that is not from the correct
803 // dex_file. If we didn't do this check then the dex_method_index will not be updated in the
804 // builder, which means that the code-generator (and compiler driver during sharpening and
805 // inliner, maybe) might invoke an incorrect method.
806 // TODO: The actual method could still be referenced in the current dex file, so we
807 // could try locating it.
808 // TODO: Remove the dex_file restriction.
809 return nullptr;
810 }
811 if (!actual_method->IsInvokable()) {
812 // Fail if the actual method cannot be invoked. Otherwise, the runtime resolution stub
813 // could resolve the callee to the wrong method.
814 return nullptr;
815 }
816 resolved_method = actual_method;
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000817 }
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000818 }
819
820 // Check for incompatible class changes. The class linker has a fast path for
821 // looking into the dex cache and does not check incompatible class changes if it hits it.
822 if (resolved_method->CheckIncompatibleClassChange(invoke_type)) {
823 return nullptr;
824 }
825
826 return resolved_method;
827}
828
Calin Juravle68ad6492015-08-18 17:08:12 +0100829bool HGraphBuilder::BuildInvoke(const Instruction& instruction,
830 uint32_t dex_pc,
831 uint32_t method_idx,
832 uint32_t number_of_vreg_arguments,
833 bool is_range,
834 uint32_t* args,
835 uint32_t register_index) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000836 InvokeType invoke_type = GetInvokeTypeFromOpCode(instruction.Opcode());
Calin Juravle68ad6492015-08-18 17:08:12 +0100837 const char* descriptor = dex_file_->GetMethodShorty(method_idx);
838 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
839
840 // Remove the return type from the 'proto'.
841 size_t number_of_arguments = strlen(descriptor) - 1;
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000842 if (invoke_type != kStatic) { // instance call
Calin Juravle68ad6492015-08-18 17:08:12 +0100843 // One extra argument for 'this'.
844 number_of_arguments++;
845 }
846
847 MethodReference target_method(dex_file_, method_idx);
Calin Juravle68ad6492015-08-18 17:08:12 +0100848
Calin Juravle5d01db12015-08-25 15:02:42 +0100849 // Special handling for string init.
850 int32_t string_init_offset = 0;
851 bool is_string_init = compiler_driver_->IsStringInit(method_idx,
852 dex_file_,
853 &string_init_offset);
854 // Replace calls to String.<init> with StringFactory.
855 if (is_string_init) {
Vladimir Markodc151b22015-10-15 18:02:30 +0100856 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
857 HInvokeStaticOrDirect::MethodLoadKind::kStringInit,
858 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
859 dchecked_integral_cast<uint64_t>(string_init_offset),
860 0U
861 };
Calin Juravle5d01db12015-08-25 15:02:42 +0100862 HInvoke* invoke = new (arena_) HInvokeStaticOrDirect(
863 arena_,
864 number_of_arguments - 1,
865 Primitive::kPrimNot /*return_type */,
866 dex_pc,
867 method_idx,
868 target_method,
869 dispatch_info,
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000870 invoke_type,
Calin Juravle5d01db12015-08-25 15:02:42 +0100871 kStatic /* optimized_invoke_type */,
872 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit);
873 return HandleStringInit(invoke,
874 number_of_vreg_arguments,
875 args,
876 register_index,
877 is_range,
878 descriptor);
879 }
880
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000881 ArtMethod* resolved_method = ResolveMethod(method_idx, invoke_type);
882
Alex Lightfedd91d2016-01-07 14:49:16 -0800883 if (UNLIKELY(resolved_method == nullptr)) {
Calin Juravle175dc732015-08-25 15:42:32 +0100884 MaybeRecordStat(MethodCompilationStat::kUnresolvedMethod);
885 HInvoke* invoke = new (arena_) HInvokeUnresolved(arena_,
886 number_of_arguments,
887 return_type,
888 dex_pc,
889 method_idx,
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000890 invoke_type);
Calin Juravle175dc732015-08-25 15:42:32 +0100891 return HandleInvoke(invoke,
892 number_of_vreg_arguments,
893 args,
894 register_index,
895 is_range,
896 descriptor,
897 nullptr /* clinit_check */);
Calin Juravle68ad6492015-08-18 17:08:12 +0100898 }
899
Calin Juravle68ad6492015-08-18 17:08:12 +0100900 // Potential class initialization check, in the case of a static method call.
901 HClinitCheck* clinit_check = nullptr;
902 HInvoke* invoke = nullptr;
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000903 if (invoke_type == kDirect || invoke_type == kStatic || invoke_type == kSuper) {
Calin Juravle68ad6492015-08-18 17:08:12 +0100904 // By default, consider that the called method implicitly requires
905 // an initialization check of its declaring method.
906 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement
907 = HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000908 ScopedObjectAccess soa(Thread::Current());
909 if (invoke_type == kStatic) {
910 clinit_check = ProcessClinitCheckForInvoke(
911 dex_pc, resolved_method, method_idx, &clinit_check_requirement);
912 } else if (invoke_type == kSuper) {
913 if (IsSameDexFile(*resolved_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
914 // Update the target method to the one resolved. Note that this may be a no-op if
915 // we resolved to the method referenced by the instruction.
916 method_idx = resolved_method->GetDexMethodIndex();
917 target_method = MethodReference(dex_file_, method_idx);
918 }
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100919 }
Calin Juravle68ad6492015-08-18 17:08:12 +0100920
Vladimir Markodc151b22015-10-15 18:02:30 +0100921 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
922 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
923 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
924 0u,
925 0U
926 };
Calin Juravle68ad6492015-08-18 17:08:12 +0100927 invoke = new (arena_) HInvokeStaticOrDirect(arena_,
928 number_of_arguments,
929 return_type,
930 dex_pc,
931 method_idx,
932 target_method,
933 dispatch_info,
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000934 invoke_type,
935 invoke_type,
Calin Juravle68ad6492015-08-18 17:08:12 +0100936 clinit_check_requirement);
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000937 } else if (invoke_type == kVirtual) {
938 ScopedObjectAccess soa(Thread::Current()); // Needed for the method index
Calin Juravle68ad6492015-08-18 17:08:12 +0100939 invoke = new (arena_) HInvokeVirtual(arena_,
940 number_of_arguments,
941 return_type,
942 dex_pc,
943 method_idx,
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000944 resolved_method->GetMethodIndex());
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100945 } else {
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000946 DCHECK_EQ(invoke_type, kInterface);
947 ScopedObjectAccess soa(Thread::Current()); // Needed for the method index
Calin Juravle68ad6492015-08-18 17:08:12 +0100948 invoke = new (arena_) HInvokeInterface(arena_,
949 number_of_arguments,
950 return_type,
951 dex_pc,
952 method_idx,
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000953 resolved_method->GetDexMethodIndex());
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100954 }
Calin Juravle68ad6492015-08-18 17:08:12 +0100955
Calin Juravle5d01db12015-08-25 15:02:42 +0100956 return HandleInvoke(invoke,
957 number_of_vreg_arguments,
958 args,
959 register_index,
960 is_range,
961 descriptor,
962 clinit_check);
Calin Juravle68ad6492015-08-18 17:08:12 +0100963}
964
Nicolas Geoffray729645a2015-11-19 13:29:02 +0000965bool HGraphBuilder::BuildNewInstance(uint16_t type_index, uint32_t dex_pc) {
966 bool finalizable;
967 bool can_throw = NeedsAccessCheck(type_index, &finalizable);
968
969 // Only the non-resolved entrypoint handles the finalizable class case. If we
970 // need access checks, then we haven't resolved the method and the class may
971 // again be finalizable.
972 QuickEntrypointEnum entrypoint = (finalizable || can_throw)
973 ? kQuickAllocObject
974 : kQuickAllocObjectInitialized;
975
976 ScopedObjectAccess soa(Thread::Current());
977 StackHandleScope<3> hs(soa.Self());
978 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
979 dex_compilation_unit_->GetClassLinker()->FindDexCache(
980 soa.Self(), *dex_compilation_unit_->GetDexFile())));
981 Handle<mirror::Class> resolved_class(hs.NewHandle(dex_cache->GetResolvedType(type_index)));
982 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
983 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
984 outer_compilation_unit_->GetClassLinker()->FindDexCache(soa.Self(), outer_dex_file)));
985
986 if (outer_dex_cache.Get() != dex_cache.Get()) {
987 // We currently do not support inlining allocations across dex files.
988 return false;
989 }
990
991 HLoadClass* load_class = new (arena_) HLoadClass(
992 graph_->GetCurrentMethod(),
993 type_index,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +0000994 outer_dex_file,
Nicolas Geoffray729645a2015-11-19 13:29:02 +0000995 IsOutermostCompilingClass(type_index),
996 dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +0000997 /*needs_access_check*/ can_throw,
998 compiler_driver_->CanAssumeTypeIsPresentInDexCache(outer_dex_file, type_index));
Nicolas Geoffray729645a2015-11-19 13:29:02 +0000999
1000 current_block_->AddInstruction(load_class);
1001 HInstruction* cls = load_class;
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +00001002 if (!IsInitialized(resolved_class)) {
Nicolas Geoffray729645a2015-11-19 13:29:02 +00001003 cls = new (arena_) HClinitCheck(load_class, dex_pc);
1004 current_block_->AddInstruction(cls);
1005 }
1006
1007 current_block_->AddInstruction(new (arena_) HNewInstance(
1008 cls,
1009 graph_->GetCurrentMethod(),
1010 dex_pc,
1011 type_index,
1012 *dex_compilation_unit_->GetDexFile(),
1013 can_throw,
1014 finalizable,
1015 entrypoint));
1016 return true;
1017}
1018
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +00001019static bool IsSubClass(mirror::Class* to_test, mirror::Class* super_class)
1020 SHARED_REQUIRES(Locks::mutator_lock_) {
1021 return to_test != nullptr && !to_test->IsInterface() && to_test->IsSubClass(super_class);
1022}
1023
1024bool HGraphBuilder::IsInitialized(Handle<mirror::Class> cls) const {
Nicolas Geoffray729645a2015-11-19 13:29:02 +00001025 if (cls.Get() == nullptr) {
1026 return false;
1027 }
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +00001028
1029 // `CanAssumeClassIsLoaded` will return true if we're JITting, or will
1030 // check whether the class is in an image for the AOT compilation.
1031 if (cls->IsInitialized() &&
1032 compiler_driver_->CanAssumeClassIsLoaded(cls.Get())) {
Nicolas Geoffray729645a2015-11-19 13:29:02 +00001033 return true;
1034 }
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +00001035
1036 if (IsSubClass(GetOutermostCompilingClass(), cls.Get())) {
1037 return true;
1038 }
1039
1040 // TODO: We should walk over the inlined methods, but we don't pass
1041 // that information to the builder.
1042 if (IsSubClass(GetCompilingClass(), cls.Get())) {
1043 return true;
1044 }
1045
1046 return false;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00001047}
1048
Calin Juravle68ad6492015-08-18 17:08:12 +01001049HClinitCheck* HGraphBuilder::ProcessClinitCheckForInvoke(
1050 uint32_t dex_pc,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001051 ArtMethod* resolved_method,
Calin Juravle68ad6492015-08-18 17:08:12 +01001052 uint32_t method_idx,
1053 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001054 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
1055 Thread* self = Thread::Current();
1056 StackHandleScope<4> hs(self);
Calin Juravle68ad6492015-08-18 17:08:12 +01001057 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
1058 dex_compilation_unit_->GetClassLinker()->FindDexCache(
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001059 self, *dex_compilation_unit_->GetDexFile())));
Calin Juravle68ad6492015-08-18 17:08:12 +01001060 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001061 outer_compilation_unit_->GetClassLinker()->FindDexCache(
1062 self, outer_dex_file)));
Calin Juravle68ad6492015-08-18 17:08:12 +01001063 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +00001064 Handle<mirror::Class> resolved_method_class(hs.NewHandle(resolved_method->GetDeclaringClass()));
Calin Juravle68ad6492015-08-18 17:08:12 +01001065
1066 // The index at which the method's class is stored in the DexCache's type array.
1067 uint32_t storage_index = DexFile::kDexNoIndex;
1068 bool is_outer_class = (resolved_method->GetDeclaringClass() == outer_class.Get());
1069 if (is_outer_class) {
1070 storage_index = outer_class->GetDexTypeIndex();
1071 } else if (outer_dex_cache.Get() == dex_cache.Get()) {
1072 // Get `storage_index` from IsClassOfStaticMethodAvailableToReferrer.
1073 compiler_driver_->IsClassOfStaticMethodAvailableToReferrer(outer_dex_cache.Get(),
1074 GetCompilingClass(),
1075 resolved_method,
1076 method_idx,
1077 &storage_index);
1078 }
1079
1080 HClinitCheck* clinit_check = nullptr;
1081
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +00001082 if (IsInitialized(resolved_method_class)) {
Calin Juravle68ad6492015-08-18 17:08:12 +01001083 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
1084 } else if (storage_index != DexFile::kDexNoIndex) {
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +00001085 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
1086 HLoadClass* load_class = new (arena_) HLoadClass(
1087 graph_->GetCurrentMethod(),
1088 storage_index,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001089 outer_dex_file,
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +00001090 is_outer_class,
1091 dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001092 /*needs_access_check*/ false,
1093 compiler_driver_->CanAssumeTypeIsPresentInDexCache(outer_dex_file, storage_index));
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +00001094 current_block_->AddInstruction(load_class);
1095 clinit_check = new (arena_) HClinitCheck(load_class, dex_pc);
1096 current_block_->AddInstruction(clinit_check);
Calin Juravle68ad6492015-08-18 17:08:12 +01001097 }
1098 return clinit_check;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001099}
1100
Calin Juravle5d01db12015-08-25 15:02:42 +01001101bool HGraphBuilder::SetupInvokeArguments(HInvoke* invoke,
1102 uint32_t number_of_vreg_arguments,
1103 uint32_t* args,
1104 uint32_t register_index,
1105 bool is_range,
1106 const char* descriptor,
1107 size_t start_index,
1108 size_t* argument_index) {
Calin Juravle68ad6492015-08-18 17:08:12 +01001109 uint32_t descriptor_index = 1; // Skip the return type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001110 uint32_t dex_pc = invoke->GetDexPc();
Calin Juravle68ad6492015-08-18 17:08:12 +01001111
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001112 for (size_t i = start_index;
1113 // Make sure we don't go over the expected arguments or over the number of
1114 // dex registers given. If the instruction was seen as dead by the verifier,
1115 // it hasn't been properly checked.
Calin Juravle5d01db12015-08-25 15:02:42 +01001116 (i < number_of_vreg_arguments) && (*argument_index < invoke->GetNumberOfArguments());
1117 i++, (*argument_index)++) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001118 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001119 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001120 if (!is_range
1121 && is_wide
1122 && ((i + 1 == number_of_vreg_arguments) || (args[i] + 1 != args[i + 1]))) {
1123 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
1124 // reject any class where this is violated. However, the verifier only does these checks
1125 // on non trivially dead instructions, so we just bailout the compilation.
1126 VLOG(compiler) << "Did not compile "
1127 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
1128 << " because of non-sequential dex register pair in wide argument";
1129 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1130 return false;
1131 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001132 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type, dex_pc);
Calin Juravle5d01db12015-08-25 15:02:42 +01001133 invoke->SetArgumentAt(*argument_index, arg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001134 if (is_wide) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001135 i++;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001136 }
1137 }
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001138
Calin Juravle5d01db12015-08-25 15:02:42 +01001139 if (*argument_index != invoke->GetNumberOfArguments()) {
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001140 VLOG(compiler) << "Did not compile "
1141 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
1142 << " because of wrong number of arguments in invoke instruction";
1143 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1144 return false;
1145 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001146
Vladimir Markob554b5a2015-11-06 12:57:55 +00001147 if (invoke->IsInvokeStaticOrDirect() &&
1148 HInvokeStaticOrDirect::NeedsCurrentMethodInput(
1149 invoke->AsInvokeStaticOrDirect()->GetMethodLoadKind())) {
Calin Juravle5d01db12015-08-25 15:02:42 +01001150 invoke->SetArgumentAt(*argument_index, graph_->GetCurrentMethod());
1151 (*argument_index)++;
1152 }
1153
1154 return true;
1155}
1156
1157bool HGraphBuilder::HandleInvoke(HInvoke* invoke,
1158 uint32_t number_of_vreg_arguments,
1159 uint32_t* args,
1160 uint32_t register_index,
1161 bool is_range,
1162 const char* descriptor,
1163 HClinitCheck* clinit_check) {
1164 DCHECK(!invoke->IsInvokeStaticOrDirect() || !invoke->AsInvokeStaticOrDirect()->IsStringInit());
1165
1166 size_t start_index = 0;
1167 size_t argument_index = 0;
1168 if (invoke->GetOriginalInvokeType() != InvokeType::kStatic) { // Instance call.
1169 Temporaries temps(graph_);
1170 HInstruction* arg = LoadLocal(
1171 is_range ? register_index : args[0], Primitive::kPrimNot, invoke->GetDexPc());
1172 HNullCheck* null_check = new (arena_) HNullCheck(arg, invoke->GetDexPc());
1173 current_block_->AddInstruction(null_check);
1174 temps.Add(null_check);
1175 invoke->SetArgumentAt(0, null_check);
1176 start_index = 1;
1177 argument_index = 1;
1178 }
1179
1180 if (!SetupInvokeArguments(invoke,
1181 number_of_vreg_arguments,
1182 args,
1183 register_index,
1184 is_range,
1185 descriptor,
1186 start_index,
1187 &argument_index)) {
1188 return false;
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001189 }
1190
Calin Juravle68ad6492015-08-18 17:08:12 +01001191 if (clinit_check != nullptr) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001192 // Add the class initialization check as last input of `invoke`.
Calin Juravle68ad6492015-08-18 17:08:12 +01001193 DCHECK(invoke->IsInvokeStaticOrDirect());
1194 DCHECK(invoke->AsInvokeStaticOrDirect()->GetClinitCheckRequirement()
1195 == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit);
Roland Levillain3e3d7332015-04-28 11:00:54 +01001196 invoke->SetArgumentAt(argument_index, clinit_check);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001197 argument_index++;
Roland Levillain4c0eb422015-04-24 16:43:49 +01001198 }
1199
Calin Juravle5d01db12015-08-25 15:02:42 +01001200 current_block_->AddInstruction(invoke);
1201 latest_result_ = invoke;
1202
1203 return true;
1204}
1205
1206bool HGraphBuilder::HandleStringInit(HInvoke* invoke,
1207 uint32_t number_of_vreg_arguments,
1208 uint32_t* args,
1209 uint32_t register_index,
1210 bool is_range,
1211 const char* descriptor) {
1212 DCHECK(invoke->IsInvokeStaticOrDirect());
1213 DCHECK(invoke->AsInvokeStaticOrDirect()->IsStringInit());
1214
1215 size_t start_index = 1;
1216 size_t argument_index = 0;
1217 if (!SetupInvokeArguments(invoke,
1218 number_of_vreg_arguments,
1219 args,
1220 register_index,
1221 is_range,
1222 descriptor,
1223 start_index,
1224 &argument_index)) {
1225 return false;
Jeff Hao848f70a2014-01-15 13:49:50 -08001226 }
Calin Juravle0eedd7e2015-08-20 14:48:00 +01001227
Calin Juravle5d01db12015-08-25 15:02:42 +01001228 // Add move-result for StringFactory method.
1229 uint32_t orig_this_reg = is_range ? register_index : args[0];
David Brazdil6de19382016-01-08 17:37:10 +00001230 HInstruction* new_instance = LoadLocal(orig_this_reg, Primitive::kPrimNot, invoke->GetDexPc());
1231 invoke->SetArgumentAt(argument_index, new_instance);
Calin Juravle5d01db12015-08-25 15:02:42 +01001232 current_block_->AddInstruction(invoke);
Calin Juravle5d01db12015-08-25 15:02:42 +01001233
Calin Juravle0eedd7e2015-08-20 14:48:00 +01001234 latest_result_ = invoke;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001235 return true;
1236}
1237
Calin Juravlee460d1d2015-09-29 04:52:17 +01001238static Primitive::Type GetFieldAccessType(const DexFile& dex_file, uint16_t field_index) {
1239 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
1240 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
1241 return Primitive::GetType(type[0]);
1242}
1243
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001244bool HGraphBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001245 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001246 bool is_put) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001247 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1248 uint32_t obj_reg = instruction.VRegB_22c();
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001249 uint16_t field_index;
1250 if (instruction.IsQuickened()) {
1251 if (!CanDecodeQuickenedInfo()) {
1252 return false;
1253 }
1254 field_index = LookupQuickenedInfo(dex_pc);
1255 } else {
1256 field_index = instruction.VRegC_22c();
1257 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001258
1259 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierc7853442015-03-27 14:35:38 -07001260 ArtField* resolved_field =
1261 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001262
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001263
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001264 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot, dex_pc);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001265 HInstruction* null_check = new (arena_) HNullCheck(object, dex_pc);
1266 current_block_->AddInstruction(null_check);
1267
1268 Primitive::Type field_type = (resolved_field == nullptr)
1269 ? GetFieldAccessType(*dex_file_, field_index)
1270 : resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001271 if (is_put) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001272 Temporaries temps(graph_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001273 // We need one temporary for the null check.
1274 temps.Add(null_check);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001275 HInstruction* value = LoadLocal(source_or_dest_reg, field_type, dex_pc);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001276 HInstruction* field_set = nullptr;
1277 if (resolved_field == nullptr) {
1278 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
1279 field_set = new (arena_) HUnresolvedInstanceFieldSet(null_check,
1280 value,
1281 field_type,
1282 field_index,
1283 dex_pc);
1284 } else {
Mingyao Yang8df69d42015-10-22 15:40:58 -07001285 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Calin Juravlee460d1d2015-09-29 04:52:17 +01001286 field_set = new (arena_) HInstanceFieldSet(null_check,
1287 value,
1288 field_type,
1289 resolved_field->GetOffset(),
1290 resolved_field->IsVolatile(),
1291 field_index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07001292 class_def_index,
Calin Juravlee460d1d2015-09-29 04:52:17 +01001293 *dex_file_,
1294 dex_compilation_unit_->GetDexCache(),
1295 dex_pc);
1296 }
1297 current_block_->AddInstruction(field_set);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001298 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001299 HInstruction* field_get = nullptr;
1300 if (resolved_field == nullptr) {
1301 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
1302 field_get = new (arena_) HUnresolvedInstanceFieldGet(null_check,
1303 field_type,
1304 field_index,
1305 dex_pc);
1306 } else {
Mingyao Yang8df69d42015-10-22 15:40:58 -07001307 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Calin Juravlee460d1d2015-09-29 04:52:17 +01001308 field_get = new (arena_) HInstanceFieldGet(null_check,
1309 field_type,
1310 resolved_field->GetOffset(),
1311 resolved_field->IsVolatile(),
1312 field_index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07001313 class_def_index,
Calin Juravlee460d1d2015-09-29 04:52:17 +01001314 *dex_file_,
1315 dex_compilation_unit_->GetDexCache(),
1316 dex_pc);
1317 }
1318 current_block_->AddInstruction(field_get);
1319 UpdateLocal(source_or_dest_reg, field_get, dex_pc);
Calin Juravlee6f49b42015-09-17 14:04:33 +00001320 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001321
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001322 return true;
1323}
1324
Nicolas Geoffray30451742015-06-19 13:32:41 +01001325static mirror::Class* GetClassFrom(CompilerDriver* driver,
1326 const DexCompilationUnit& compilation_unit) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001327 ScopedObjectAccess soa(Thread::Current());
1328 StackHandleScope<2> hs(soa.Self());
Nicolas Geoffray30451742015-06-19 13:32:41 +01001329 const DexFile& dex_file = *compilation_unit.GetDexFile();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001330 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Nicolas Geoffray30451742015-06-19 13:32:41 +01001331 soa.Decode<mirror::ClassLoader*>(compilation_unit.GetClassLoader())));
1332 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001333 compilation_unit.GetClassLinker()->FindDexCache(soa.Self(), dex_file)));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001334
Nicolas Geoffray30451742015-06-19 13:32:41 +01001335 return driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, &compilation_unit);
1336}
1337
1338mirror::Class* HGraphBuilder::GetOutermostCompilingClass() const {
1339 return GetClassFrom(compiler_driver_, *outer_compilation_unit_);
1340}
1341
1342mirror::Class* HGraphBuilder::GetCompilingClass() const {
1343 return GetClassFrom(compiler_driver_, *dex_compilation_unit_);
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001344}
1345
1346bool HGraphBuilder::IsOutermostCompilingClass(uint16_t type_index) const {
1347 ScopedObjectAccess soa(Thread::Current());
1348 StackHandleScope<4> hs(soa.Self());
1349 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001350 dex_compilation_unit_->GetClassLinker()->FindDexCache(
1351 soa.Self(), *dex_compilation_unit_->GetDexFile())));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001352 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1353 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
1354 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
1355 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
Nicolas Geoffrayafd06412015-06-20 22:44:47 +01001356 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001357
Calin Juravle4e2a5572015-10-07 18:55:43 +01001358 // GetOutermostCompilingClass returns null when the class is unresolved
1359 // (e.g. if it derives from an unresolved class). This is bogus knowing that
1360 // we are compiling it.
1361 // When this happens we cannot establish a direct relation between the current
1362 // class and the outer class, so we return false.
1363 // (Note that this is only used for optimizing invokes and field accesses)
1364 return (cls.Get() != nullptr) && (outer_class.Get() == cls.Get());
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001365}
1366
Calin Juravle07380a22015-09-17 14:15:12 +01001367void HGraphBuilder::BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
1368 uint32_t dex_pc,
1369 bool is_put,
1370 Primitive::Type field_type) {
1371 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1372 uint16_t field_index = instruction.VRegB_21c();
1373
1374 if (is_put) {
1375 HInstruction* value = LoadLocal(source_or_dest_reg, field_type, dex_pc);
1376 current_block_->AddInstruction(
1377 new (arena_) HUnresolvedStaticFieldSet(value, field_type, field_index, dex_pc));
1378 } else {
1379 current_block_->AddInstruction(
1380 new (arena_) HUnresolvedStaticFieldGet(field_type, field_index, dex_pc));
1381 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction(), dex_pc);
1382 }
1383}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001384bool HGraphBuilder::BuildStaticFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001385 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001386 bool is_put) {
1387 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1388 uint16_t field_index = instruction.VRegB_21c();
1389
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001390 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray729645a2015-11-19 13:29:02 +00001391 StackHandleScope<5> hs(soa.Self());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001392 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001393 dex_compilation_unit_->GetClassLinker()->FindDexCache(
1394 soa.Self(), *dex_compilation_unit_->GetDexFile())));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001395 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1396 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
Mathieu Chartierc7853442015-03-27 14:35:38 -07001397 ArtField* resolved_field = compiler_driver_->ResolveField(
1398 soa, dex_cache, class_loader, dex_compilation_unit_, field_index, true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001399
Mathieu Chartierc7853442015-03-27 14:35:38 -07001400 if (resolved_field == nullptr) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001401 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
1402 Primitive::Type field_type = GetFieldAccessType(*dex_file_, field_index);
Calin Juravle07380a22015-09-17 14:15:12 +01001403 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001404 return true;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001405 }
1406
Calin Juravle07380a22015-09-17 14:15:12 +01001407 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001408 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
1409 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001410 outer_compilation_unit_->GetClassLinker()->FindDexCache(soa.Self(), outer_dex_file)));
Nicolas Geoffray30451742015-06-19 13:32:41 +01001411 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001412
1413 // The index at which the field's class is stored in the DexCache's type array.
1414 uint32_t storage_index;
Nicolas Geoffray30451742015-06-19 13:32:41 +01001415 bool is_outer_class = (outer_class.Get() == resolved_field->GetDeclaringClass());
1416 if (is_outer_class) {
1417 storage_index = outer_class->GetDexTypeIndex();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001418 } else if (outer_dex_cache.Get() != dex_cache.Get()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001419 // The compiler driver cannot currently understand multiple dex caches involved. Just bailout.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001420 return false;
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001421 } else {
Calin Juravle07380a22015-09-17 14:15:12 +01001422 // TODO: This is rather expensive. Perf it and cache the results if needed.
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001423 std::pair<bool, bool> pair = compiler_driver_->IsFastStaticField(
1424 outer_dex_cache.Get(),
Nicolas Geoffray30451742015-06-19 13:32:41 +01001425 GetCompilingClass(),
Mathieu Chartierc7853442015-03-27 14:35:38 -07001426 resolved_field,
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001427 field_index,
1428 &storage_index);
1429 bool can_easily_access = is_put ? pair.second : pair.first;
1430 if (!can_easily_access) {
Calin Juravle07380a22015-09-17 14:15:12 +01001431 MaybeRecordStat(MethodCompilationStat::kUnresolvedFieldNotAFastAccess);
1432 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
1433 return true;
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001434 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001435 }
1436
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001437 bool is_in_cache =
1438 compiler_driver_->CanAssumeTypeIsPresentInDexCache(outer_dex_file, storage_index);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001439 HLoadClass* constant = new (arena_) HLoadClass(graph_->GetCurrentMethod(),
1440 storage_index,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001441 outer_dex_file,
Nicolas Geoffray30451742015-06-19 13:32:41 +01001442 is_outer_class,
Calin Juravle98893e12015-10-02 21:05:03 +01001443 dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001444 /*needs_access_check*/ false,
1445 is_in_cache);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001446 current_block_->AddInstruction(constant);
1447
1448 HInstruction* cls = constant;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00001449
1450 Handle<mirror::Class> klass(hs.NewHandle(resolved_field->GetDeclaringClass()));
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +00001451 if (!IsInitialized(klass)) {
Calin Juravle225ff812014-11-13 16:46:39 +00001452 cls = new (arena_) HClinitCheck(constant, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001453 current_block_->AddInstruction(cls);
1454 }
Mingyao Yang8df69d42015-10-22 15:40:58 -07001455
Nicolas Geoffray729645a2015-11-19 13:29:02 +00001456 uint16_t class_def_index = klass->GetDexClassDefIndex();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001457 if (is_put) {
1458 // We need to keep the class alive before loading the value.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001459 Temporaries temps(graph_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001460 temps.Add(cls);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001461 HInstruction* value = LoadLocal(source_or_dest_reg, field_type, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001462 DCHECK_EQ(value->GetType(), field_type);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001463 current_block_->AddInstruction(new (arena_) HStaticFieldSet(cls,
1464 value,
1465 field_type,
1466 resolved_field->GetOffset(),
1467 resolved_field->IsVolatile(),
1468 field_index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07001469 class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07001470 *dex_file_,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001471 dex_cache_,
1472 dex_pc));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001473 } else {
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001474 current_block_->AddInstruction(new (arena_) HStaticFieldGet(cls,
1475 field_type,
1476 resolved_field->GetOffset(),
1477 resolved_field->IsVolatile(),
1478 field_index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07001479 class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07001480 *dex_file_,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001481 dex_cache_,
1482 dex_pc));
1483 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001484 }
1485 return true;
1486}
1487
Calin Juravlebacfec32014-11-14 15:54:36 +00001488void HGraphBuilder::BuildCheckedDivRem(uint16_t out_vreg,
1489 uint16_t first_vreg,
1490 int64_t second_vreg_or_constant,
1491 uint32_t dex_pc,
1492 Primitive::Type type,
1493 bool second_is_constant,
1494 bool isDiv) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001495 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Calin Juravled0d48522014-11-04 16:40:20 +00001496
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001497 HInstruction* first = LoadLocal(first_vreg, type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001498 HInstruction* second = nullptr;
1499 if (second_is_constant) {
1500 if (type == Primitive::kPrimInt) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001501 second = graph_->GetIntConstant(second_vreg_or_constant, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001502 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001503 second = graph_->GetLongConstant(second_vreg_or_constant, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001504 }
1505 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001506 second = LoadLocal(second_vreg_or_constant, type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001507 }
1508
1509 if (!second_is_constant
1510 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0)
1511 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) {
1512 second = new (arena_) HDivZeroCheck(second, dex_pc);
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001513 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +00001514 current_block_->AddInstruction(second);
1515 temps.Add(current_block_->GetLastInstruction());
1516 }
1517
Calin Juravlebacfec32014-11-14 15:54:36 +00001518 if (isDiv) {
1519 current_block_->AddInstruction(new (arena_) HDiv(type, first, second, dex_pc));
1520 } else {
1521 current_block_->AddInstruction(new (arena_) HRem(type, first, second, dex_pc));
1522 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001523 UpdateLocal(out_vreg, current_block_->GetLastInstruction(), dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001524}
1525
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001526void HGraphBuilder::BuildArrayAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001527 uint32_t dex_pc,
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001528 bool is_put,
1529 Primitive::Type anticipated_type) {
1530 uint8_t source_or_dest_reg = instruction.VRegA_23x();
1531 uint8_t array_reg = instruction.VRegB_23x();
1532 uint8_t index_reg = instruction.VRegC_23x();
1533
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001534 // We need one temporary for the null check, one for the index, and one for the length.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001535 Temporaries temps(graph_);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001536
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001537 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001538 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001539 current_block_->AddInstruction(object);
1540 temps.Add(object);
1541
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001542 HInstruction* length = new (arena_) HArrayLength(object, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001543 current_block_->AddInstruction(length);
1544 temps.Add(length);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001545 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001546 index = new (arena_) HBoundsCheck(index, length, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001547 current_block_->AddInstruction(index);
1548 temps.Add(index);
1549 if (is_put) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001550 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001551 // TODO: Insert a type check node if the type is Object.
Nicolas Geoffray39468442014-09-02 15:17:15 +01001552 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001553 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001554 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001555 current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type, dex_pc));
1556 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001557 }
Mark Mendell1152c922015-04-24 17:06:35 -04001558 graph_->SetHasBoundsChecks(true);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001559}
1560
Calin Juravle225ff812014-11-13 16:46:39 +00001561void HGraphBuilder::BuildFilledNewArray(uint32_t dex_pc,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001562 uint32_t type_index,
1563 uint32_t number_of_vreg_arguments,
1564 bool is_range,
1565 uint32_t* args,
1566 uint32_t register_index) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001567 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments, dex_pc);
Mingyao Yangfb8464a2015-11-02 10:56:59 -08001568 bool finalizable;
1569 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index, &finalizable)
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001570 ? kQuickAllocArrayWithAccessCheck
1571 : kQuickAllocArray;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01001572 HInstruction* object = new (arena_) HNewArray(length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01001573 graph_->GetCurrentMethod(),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01001574 dex_pc,
1575 type_index,
1576 *dex_compilation_unit_->GetDexFile(),
1577 entrypoint);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001578 current_block_->AddInstruction(object);
1579
1580 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1581 DCHECK_EQ(descriptor[0], '[') << descriptor;
1582 char primitive = descriptor[1];
1583 DCHECK(primitive == 'I'
1584 || primitive == 'L'
1585 || primitive == '[') << descriptor;
1586 bool is_reference_array = (primitive == 'L') || (primitive == '[');
1587 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
1588
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001589 Temporaries temps(graph_);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001590 temps.Add(object);
1591 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001592 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type, dex_pc);
1593 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001594 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001595 new (arena_) HArraySet(object, index, value, type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001596 }
1597 latest_result_ = object;
1598}
1599
1600template <typename T>
1601void HGraphBuilder::BuildFillArrayData(HInstruction* object,
1602 const T* data,
1603 uint32_t element_count,
1604 Primitive::Type anticipated_type,
Calin Juravle225ff812014-11-13 16:46:39 +00001605 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001606 for (uint32_t i = 0; i < element_count; ++i) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001607 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1608 HInstruction* value = graph_->GetIntConstant(data[i], dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001609 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001610 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001611 }
1612}
1613
Calin Juravle225ff812014-11-13 16:46:39 +00001614void HGraphBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001615 Temporaries temps(graph_);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001616 HInstruction* array = LoadLocal(instruction.VRegA_31t(), Primitive::kPrimNot, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001617 HNullCheck* null_check = new (arena_) HNullCheck(array, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001618 current_block_->AddInstruction(null_check);
1619 temps.Add(null_check);
1620
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001621 HInstruction* length = new (arena_) HArrayLength(null_check, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001622 current_block_->AddInstruction(length);
1623
Calin Juravle225ff812014-11-13 16:46:39 +00001624 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
Calin Juravled0d48522014-11-04 16:40:20 +00001625 const Instruction::ArrayDataPayload* payload =
1626 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_start_ + payload_offset);
1627 const uint8_t* data = payload->data;
1628 uint32_t element_count = payload->element_count;
1629
1630 // Implementation of this DEX instruction seems to be that the bounds check is
1631 // done before doing any stores.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001632 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001633 current_block_->AddInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
Calin Juravled0d48522014-11-04 16:40:20 +00001634
1635 switch (payload->element_width) {
1636 case 1:
1637 BuildFillArrayData(null_check,
1638 reinterpret_cast<const int8_t*>(data),
1639 element_count,
1640 Primitive::kPrimByte,
Calin Juravle225ff812014-11-13 16:46:39 +00001641 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001642 break;
1643 case 2:
1644 BuildFillArrayData(null_check,
1645 reinterpret_cast<const int16_t*>(data),
1646 element_count,
1647 Primitive::kPrimShort,
Calin Juravle225ff812014-11-13 16:46:39 +00001648 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001649 break;
1650 case 4:
1651 BuildFillArrayData(null_check,
1652 reinterpret_cast<const int32_t*>(data),
1653 element_count,
1654 Primitive::kPrimInt,
Calin Juravle225ff812014-11-13 16:46:39 +00001655 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001656 break;
1657 case 8:
1658 BuildFillWideArrayData(null_check,
1659 reinterpret_cast<const int64_t*>(data),
1660 element_count,
Calin Juravle225ff812014-11-13 16:46:39 +00001661 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001662 break;
1663 default:
1664 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1665 }
Mark Mendell1152c922015-04-24 17:06:35 -04001666 graph_->SetHasBoundsChecks(true);
Calin Juravled0d48522014-11-04 16:40:20 +00001667}
1668
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001669void HGraphBuilder::BuildFillWideArrayData(HInstruction* object,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +01001670 const int64_t* data,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001671 uint32_t element_count,
Calin Juravle225ff812014-11-13 16:46:39 +00001672 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001673 for (uint32_t i = 0; i < element_count; ++i) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001674 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1675 HInstruction* value = graph_->GetLongConstant(data[i], dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001676 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001677 object, index, value, Primitive::kPrimLong, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001678 }
1679}
1680
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001681static TypeCheckKind ComputeTypeCheckKind(Handle<mirror::Class> cls)
1682 SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle98893e12015-10-02 21:05:03 +01001683 if (cls.Get() == nullptr) {
1684 return TypeCheckKind::kUnresolvedCheck;
1685 } else if (cls->IsInterface()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001686 return TypeCheckKind::kInterfaceCheck;
1687 } else if (cls->IsArrayClass()) {
1688 if (cls->GetComponentType()->IsObjectClass()) {
1689 return TypeCheckKind::kArrayObjectCheck;
1690 } else if (cls->CannotBeAssignedFromOtherTypes()) {
1691 return TypeCheckKind::kExactCheck;
1692 } else {
1693 return TypeCheckKind::kArrayCheck;
1694 }
1695 } else if (cls->IsFinal()) {
1696 return TypeCheckKind::kExactCheck;
1697 } else if (cls->IsAbstract()) {
1698 return TypeCheckKind::kAbstractClassCheck;
1699 } else {
1700 return TypeCheckKind::kClassHierarchyCheck;
1701 }
1702}
1703
Calin Juravle98893e12015-10-02 21:05:03 +01001704void HGraphBuilder::BuildTypeCheck(const Instruction& instruction,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001705 uint8_t destination,
1706 uint8_t reference,
1707 uint16_t type_index,
Calin Juravle225ff812014-11-13 16:46:39 +00001708 uint32_t dex_pc) {
Calin Juravle98893e12015-10-02 21:05:03 +01001709 bool type_known_final, type_known_abstract, use_declaring_class;
1710 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
1711 dex_compilation_unit_->GetDexMethodIndex(),
1712 *dex_compilation_unit_->GetDexFile(),
1713 type_index,
1714 &type_known_final,
1715 &type_known_abstract,
1716 &use_declaring_class);
1717
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001718 ScopedObjectAccess soa(Thread::Current());
1719 StackHandleScope<2> hs(soa.Self());
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001720 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001721 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001722 dex_compilation_unit_->GetClassLinker()->FindDexCache(soa.Self(), dex_file)));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001723 Handle<mirror::Class> resolved_class(hs.NewHandle(dex_cache->GetResolvedType(type_index)));
1724
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001725 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot, dex_pc);
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001726 HLoadClass* cls = new (arena_) HLoadClass(
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001727 graph_->GetCurrentMethod(),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01001728 type_index,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001729 dex_file,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01001730 IsOutermostCompilingClass(type_index),
Calin Juravle98893e12015-10-02 21:05:03 +01001731 dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001732 !can_access,
1733 compiler_driver_->CanAssumeTypeIsPresentInDexCache(dex_file, type_index));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001734 current_block_->AddInstruction(cls);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001735
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001736 // The class needs a temporary before being used by the type check.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001737 Temporaries temps(graph_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001738 temps.Add(cls);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001739
1740 TypeCheckKind check_kind = ComputeTypeCheckKind(resolved_class);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001741 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001742 current_block_->AddInstruction(new (arena_) HInstanceOf(object, cls, check_kind, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001743 UpdateLocal(destination, current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001744 } else {
1745 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
David Brazdilf5552582015-12-27 13:36:12 +00001746 // We emit a CheckCast followed by a BoundType. CheckCast is a statement
1747 // which may throw. If it succeeds BoundType sets the new type of `object`
1748 // for all subsequent uses.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001749 current_block_->AddInstruction(new (arena_) HCheckCast(object, cls, check_kind, dex_pc));
David Brazdilf5552582015-12-27 13:36:12 +00001750 current_block_->AddInstruction(new (arena_) HBoundType(object, dex_pc));
1751 UpdateLocal(reference, current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001752 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001753}
1754
Mingyao Yangfb8464a2015-11-02 10:56:59 -08001755bool HGraphBuilder::NeedsAccessCheck(uint32_t type_index, bool* finalizable) const {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001756 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
Mingyao Yangfb8464a2015-11-02 10:56:59 -08001757 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index, finalizable);
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001758}
1759
Mark Mendellfe57faa2015-09-18 09:26:15 -04001760void HGraphBuilder::BuildSwitchJumpTable(const SwitchTable& table,
1761 const Instruction& instruction,
1762 HInstruction* value,
1763 uint32_t dex_pc) {
1764 // Add the successor blocks to the current block.
1765 uint16_t num_entries = table.GetNumEntries();
1766 for (size_t i = 1; i <= num_entries; i++) {
1767 int32_t target_offset = table.GetEntryAt(i);
1768 HBasicBlock* case_target = FindBlockStartingAt(dex_pc + target_offset);
1769 DCHECK(case_target != nullptr);
1770
1771 // Add the target block as a successor.
1772 current_block_->AddSuccessor(case_target);
1773 }
1774
1775 // Add the default target block as the last successor.
1776 HBasicBlock* default_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
1777 DCHECK(default_target != nullptr);
1778 current_block_->AddSuccessor(default_target);
1779
1780 // Now add the Switch instruction.
1781 int32_t starting_key = table.GetEntryAt(0);
1782 current_block_->AddInstruction(
1783 new (arena_) HPackedSwitch(starting_key, num_entries, value, dex_pc));
1784 // This block ends with control flow.
1785 current_block_ = nullptr;
1786}
1787
Calin Juravle48c2b032014-12-09 18:11:36 +00001788void HGraphBuilder::BuildPackedSwitch(const Instruction& instruction, uint32_t dex_pc) {
David Brazdil2ef645b2015-06-17 18:20:52 +01001789 // Verifier guarantees that the payload for PackedSwitch contains:
1790 // (a) number of entries (may be zero)
1791 // (b) first and lowest switch case value (entry 0, always present)
1792 // (c) list of target pcs (entries 1 <= i <= N)
Andreas Gamped881df52014-11-24 23:28:39 -08001793 SwitchTable table(instruction, dex_pc, false);
1794
1795 // Value to test against.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001796 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt, dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08001797
Mark Mendellfe57faa2015-09-18 09:26:15 -04001798 // Starting key value.
1799 int32_t starting_key = table.GetEntryAt(0);
1800
David Brazdil2ef645b2015-06-17 18:20:52 +01001801 // Retrieve number of entries.
Andreas Gampee4d4d322014-12-04 09:09:57 -08001802 uint16_t num_entries = table.GetNumEntries();
David Brazdil2ef645b2015-06-17 18:20:52 +01001803 if (num_entries == 0) {
1804 return;
1805 }
Andreas Gampee4d4d322014-12-04 09:09:57 -08001806
Mark Mendellfe57faa2015-09-18 09:26:15 -04001807 // Don't use a packed switch if there are very few entries.
1808 if (num_entries > kSmallSwitchThreshold) {
1809 BuildSwitchJumpTable(table, instruction, value, dex_pc);
1810 } else {
1811 // Chained cmp-and-branch, starting from starting_key.
1812 for (size_t i = 1; i <= num_entries; i++) {
Mark Mendell3b9f3042015-09-24 08:43:40 -04001813 BuildSwitchCaseHelper(instruction,
1814 i,
1815 i == num_entries,
1816 table,
1817 value,
1818 starting_key + i - 1,
1819 table.GetEntryAt(i),
1820 dex_pc);
Mark Mendellfe57faa2015-09-18 09:26:15 -04001821 }
Andreas Gamped881df52014-11-24 23:28:39 -08001822 }
Andreas Gamped881df52014-11-24 23:28:39 -08001823}
1824
Calin Juravle48c2b032014-12-09 18:11:36 +00001825void HGraphBuilder::BuildSparseSwitch(const Instruction& instruction, uint32_t dex_pc) {
David Brazdil2ef645b2015-06-17 18:20:52 +01001826 // Verifier guarantees that the payload for SparseSwitch contains:
1827 // (a) number of entries (may be zero)
1828 // (b) sorted key values (entries 0 <= i < N)
1829 // (c) target pcs corresponding to the switch values (entries N <= i < 2*N)
Andreas Gampee4d4d322014-12-04 09:09:57 -08001830 SwitchTable table(instruction, dex_pc, true);
1831
1832 // Value to test against.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001833 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001834
1835 uint16_t num_entries = table.GetNumEntries();
Andreas Gampee4d4d322014-12-04 09:09:57 -08001836
1837 for (size_t i = 0; i < num_entries; i++) {
1838 BuildSwitchCaseHelper(instruction, i, i == static_cast<size_t>(num_entries) - 1, table, value,
1839 table.GetEntryAt(i), table.GetEntryAt(i + num_entries), dex_pc);
1840 }
Andreas Gampee4d4d322014-12-04 09:09:57 -08001841}
1842
1843void HGraphBuilder::BuildSwitchCaseHelper(const Instruction& instruction, size_t index,
1844 bool is_last_case, const SwitchTable& table,
1845 HInstruction* value, int32_t case_value_int,
1846 int32_t target_offset, uint32_t dex_pc) {
David Brazdil852eaff2015-02-02 15:23:05 +00001847 HBasicBlock* case_target = FindBlockStartingAt(dex_pc + target_offset);
1848 DCHECK(case_target != nullptr);
1849 PotentiallyAddSuspendCheck(case_target, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001850
1851 // The current case's value.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001852 HInstruction* this_case_value = graph_->GetIntConstant(case_value_int, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001853
1854 // Compare value and this_case_value.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001855 HEqual* comparison = new (arena_) HEqual(value, this_case_value, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001856 current_block_->AddInstruction(comparison);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001857 HInstruction* ifinst = new (arena_) HIf(comparison, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001858 current_block_->AddInstruction(ifinst);
1859
1860 // Case hit: use the target offset to determine where to go.
Andreas Gampee4d4d322014-12-04 09:09:57 -08001861 current_block_->AddSuccessor(case_target);
1862
1863 // Case miss: go to the next case (or default fall-through).
1864 // When there is a next case, we use the block stored with the table offset representing this
1865 // case (that is where we registered them in ComputeBranchTargets).
1866 // When there is no next case, we use the following instruction.
1867 // TODO: Find a good way to peel the last iteration to avoid conditional, but still have re-use.
1868 if (!is_last_case) {
1869 HBasicBlock* next_case_target = FindBlockStartingAt(table.GetDexPcForIndex(index));
1870 DCHECK(next_case_target != nullptr);
1871 current_block_->AddSuccessor(next_case_target);
1872
1873 // Need to manually add the block, as there is no dex-pc transition for the cases.
1874 graph_->AddBlock(next_case_target);
1875
1876 current_block_ = next_case_target;
1877 } else {
1878 HBasicBlock* default_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
1879 DCHECK(default_target != nullptr);
1880 current_block_->AddSuccessor(default_target);
1881 current_block_ = nullptr;
1882 }
1883}
1884
David Brazdil852eaff2015-02-02 15:23:05 +00001885void HGraphBuilder::PotentiallyAddSuspendCheck(HBasicBlock* target, uint32_t dex_pc) {
1886 int32_t target_offset = target->GetDexPc() - dex_pc;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001887 if (target_offset <= 0) {
David Brazdil852eaff2015-02-02 15:23:05 +00001888 // DX generates back edges to the first encountered return. We can save
1889 // time of later passes by not adding redundant suspend checks.
David Brazdil2fd6aa52015-02-02 18:58:27 +00001890 HInstruction* last_in_target = target->GetLastInstruction();
1891 if (last_in_target != nullptr &&
1892 (last_in_target->IsReturn() || last_in_target->IsReturnVoid())) {
1893 return;
David Brazdil852eaff2015-02-02 15:23:05 +00001894 }
1895
1896 // Add a suspend check to backward branches which may potentially loop. We
1897 // can remove them after we recognize loops in the graph.
Calin Juravle225ff812014-11-13 16:46:39 +00001898 current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_pc));
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001899 }
1900}
1901
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001902bool HGraphBuilder::CanDecodeQuickenedInfo() const {
1903 return interpreter_metadata_ != nullptr;
1904}
1905
1906uint16_t HGraphBuilder::LookupQuickenedInfo(uint32_t dex_pc) {
1907 DCHECK(interpreter_metadata_ != nullptr);
1908 uint32_t dex_pc_in_map = DecodeUnsignedLeb128(&interpreter_metadata_);
1909 DCHECK_EQ(dex_pc, dex_pc_in_map);
1910 return DecodeUnsignedLeb128(&interpreter_metadata_);
1911}
1912
Calin Juravle225ff812014-11-13 16:46:39 +00001913bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001914 if (current_block_ == nullptr) {
1915 return true; // Dead code
1916 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001917
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001918 switch (instruction.Opcode()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001919 case Instruction::CONST_4: {
1920 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001921 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n(), dex_pc);
1922 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001923 break;
1924 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001925
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001926 case Instruction::CONST_16: {
1927 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001928 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s(), dex_pc);
1929 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001930 break;
1931 }
1932
Dave Allison20dfc792014-06-16 20:44:29 -07001933 case Instruction::CONST: {
1934 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001935 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i(), dex_pc);
1936 UpdateLocal(register_index, constant, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001937 break;
1938 }
1939
1940 case Instruction::CONST_HIGH16: {
1941 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001942 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16, dex_pc);
1943 UpdateLocal(register_index, constant, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001944 break;
1945 }
1946
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001947 case Instruction::CONST_WIDE_16: {
1948 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001949 // Get 16 bits of constant value, sign extended to 64 bits.
1950 int64_t value = instruction.VRegB_21s();
1951 value <<= 48;
1952 value >>= 48;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001953 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1954 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001955 break;
1956 }
1957
1958 case Instruction::CONST_WIDE_32: {
1959 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001960 // Get 32 bits of constant value, sign extended to 64 bits.
1961 int64_t value = instruction.VRegB_31i();
1962 value <<= 32;
1963 value >>= 32;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001964 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1965 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001966 break;
1967 }
1968
1969 case Instruction::CONST_WIDE: {
1970 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001971 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l(), dex_pc);
1972 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001973 break;
1974 }
1975
Dave Allison20dfc792014-06-16 20:44:29 -07001976 case Instruction::CONST_WIDE_HIGH16: {
1977 int32_t register_index = instruction.VRegA();
1978 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001979 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1980 UpdateLocal(register_index, constant, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001981 break;
1982 }
1983
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001984 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001985 case Instruction::MOVE:
1986 case Instruction::MOVE_FROM16:
1987 case Instruction::MOVE_16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001988 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
1989 UpdateLocal(instruction.VRegA(), value, dex_pc);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001990 break;
1991 }
1992
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001993 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001994 case Instruction::MOVE_WIDE:
1995 case Instruction::MOVE_WIDE_FROM16:
1996 case Instruction::MOVE_WIDE_16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001997 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong, dex_pc);
1998 UpdateLocal(instruction.VRegA(), value, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001999 break;
2000 }
2001
2002 case Instruction::MOVE_OBJECT:
2003 case Instruction::MOVE_OBJECT_16:
2004 case Instruction::MOVE_OBJECT_FROM16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002005 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot, dex_pc);
2006 UpdateLocal(instruction.VRegA(), value, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07002007 break;
2008 }
2009
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002010 case Instruction::RETURN_VOID_NO_BARRIER:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002011 case Instruction::RETURN_VOID: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002012 BuildReturn(instruction, Primitive::kPrimVoid, dex_pc);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002013 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002014 }
2015
Dave Allison20dfc792014-06-16 20:44:29 -07002016#define IF_XX(comparison, cond) \
Calin Juravle225ff812014-11-13 16:46:39 +00002017 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
2018 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002019
Dave Allison20dfc792014-06-16 20:44:29 -07002020 IF_XX(HEqual, EQ);
2021 IF_XX(HNotEqual, NE);
2022 IF_XX(HLessThan, LT);
2023 IF_XX(HLessThanOrEqual, LE);
2024 IF_XX(HGreaterThan, GT);
2025 IF_XX(HGreaterThanOrEqual, GE);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002026
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002027 case Instruction::GOTO:
2028 case Instruction::GOTO_16:
2029 case Instruction::GOTO_32: {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002030 int32_t offset = instruction.GetTargetOffset();
Calin Juravle225ff812014-11-13 16:46:39 +00002031 HBasicBlock* target = FindBlockStartingAt(offset + dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002032 DCHECK(target != nullptr);
David Brazdil852eaff2015-02-02 15:23:05 +00002033 PotentiallyAddSuspendCheck(target, dex_pc);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002034 current_block_->AddInstruction(new (arena_) HGoto(dex_pc));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002035 current_block_->AddSuccessor(target);
2036 current_block_ = nullptr;
2037 break;
2038 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002039
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002040 case Instruction::RETURN: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002041 BuildReturn(instruction, return_type_, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002042 break;
2043 }
2044
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002045 case Instruction::RETURN_OBJECT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002046 BuildReturn(instruction, return_type_, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002047 break;
2048 }
2049
2050 case Instruction::RETURN_WIDE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002051 BuildReturn(instruction, return_type_, dex_pc);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002052 break;
2053 }
2054
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002055 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00002056 case Instruction::INVOKE_INTERFACE:
2057 case Instruction::INVOKE_STATIC:
2058 case Instruction::INVOKE_SUPER:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002059 case Instruction::INVOKE_VIRTUAL:
2060 case Instruction::INVOKE_VIRTUAL_QUICK: {
2061 uint16_t method_idx;
2062 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_QUICK) {
2063 if (!CanDecodeQuickenedInfo()) {
2064 return false;
2065 }
2066 method_idx = LookupQuickenedInfo(dex_pc);
2067 } else {
2068 method_idx = instruction.VRegB_35c();
2069 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002070 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002071 uint32_t args[5];
Ian Rogers29a26482014-05-02 15:27:29 -07002072 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00002073 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00002074 number_of_vreg_arguments, false, args, -1)) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002075 return false;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002076 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002077 break;
2078 }
2079
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002080 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00002081 case Instruction::INVOKE_INTERFACE_RANGE:
2082 case Instruction::INVOKE_STATIC_RANGE:
2083 case Instruction::INVOKE_SUPER_RANGE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002084 case Instruction::INVOKE_VIRTUAL_RANGE:
2085 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2086 uint16_t method_idx;
2087 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK) {
2088 if (!CanDecodeQuickenedInfo()) {
2089 return false;
2090 }
2091 method_idx = LookupQuickenedInfo(dex_pc);
2092 } else {
2093 method_idx = instruction.VRegB_3rc();
2094 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002095 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
2096 uint32_t register_index = instruction.VRegC();
Calin Juravle225ff812014-11-13 16:46:39 +00002097 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002098 number_of_vreg_arguments, true, nullptr, register_index)) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002099 return false;
2100 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002101 break;
2102 }
2103
Roland Levillain88cb1752014-10-20 16:36:47 +01002104 case Instruction::NEG_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002105 Unop_12x<HNeg>(instruction, Primitive::kPrimInt, dex_pc);
Roland Levillain88cb1752014-10-20 16:36:47 +01002106 break;
2107 }
2108
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002109 case Instruction::NEG_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002110 Unop_12x<HNeg>(instruction, Primitive::kPrimLong, dex_pc);
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002111 break;
2112 }
2113
Roland Levillain3dbcb382014-10-28 17:30:07 +00002114 case Instruction::NEG_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002115 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat, dex_pc);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002116 break;
2117 }
2118
2119 case Instruction::NEG_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002120 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble, dex_pc);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002121 break;
2122 }
2123
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002124 case Instruction::NOT_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002125 Unop_12x<HNot>(instruction, Primitive::kPrimInt, dex_pc);
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002126 break;
2127 }
2128
Roland Levillain70566432014-10-24 16:20:17 +01002129 case Instruction::NOT_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002130 Unop_12x<HNot>(instruction, Primitive::kPrimLong, dex_pc);
Roland Levillain70566432014-10-24 16:20:17 +01002131 break;
2132 }
2133
Roland Levillaindff1f282014-11-05 14:15:05 +00002134 case Instruction::INT_TO_LONG: {
Roland Levillain624279f2014-12-04 11:54:28 +00002135 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong, dex_pc);
Roland Levillaindff1f282014-11-05 14:15:05 +00002136 break;
2137 }
2138
Roland Levillaincff13742014-11-17 14:32:17 +00002139 case Instruction::INT_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00002140 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00002141 break;
2142 }
2143
2144 case Instruction::INT_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00002145 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00002146 break;
2147 }
2148
Roland Levillain946e1432014-11-11 17:35:19 +00002149 case Instruction::LONG_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00002150 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt, dex_pc);
Roland Levillain946e1432014-11-11 17:35:19 +00002151 break;
2152 }
2153
Roland Levillain6d0e4832014-11-27 18:31:21 +00002154 case Instruction::LONG_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00002155 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimFloat, dex_pc);
Roland Levillain6d0e4832014-11-27 18:31:21 +00002156 break;
2157 }
2158
Roland Levillain647b9ed2014-11-27 12:06:00 +00002159 case Instruction::LONG_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00002160 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble, dex_pc);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002161 break;
2162 }
2163
Roland Levillain3f8f9362014-12-02 17:45:01 +00002164 case Instruction::FLOAT_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00002165 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimInt, dex_pc);
2166 break;
2167 }
2168
2169 case Instruction::FLOAT_TO_LONG: {
2170 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimLong, dex_pc);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002171 break;
2172 }
2173
Roland Levillain8964e2b2014-12-04 12:10:50 +00002174 case Instruction::FLOAT_TO_DOUBLE: {
2175 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimDouble, dex_pc);
2176 break;
2177 }
2178
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002179 case Instruction::DOUBLE_TO_INT: {
2180 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimInt, dex_pc);
2181 break;
2182 }
2183
2184 case Instruction::DOUBLE_TO_LONG: {
2185 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimLong, dex_pc);
2186 break;
2187 }
2188
Roland Levillain8964e2b2014-12-04 12:10:50 +00002189 case Instruction::DOUBLE_TO_FLOAT: {
2190 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimFloat, dex_pc);
2191 break;
2192 }
2193
Roland Levillain51d3fc42014-11-13 14:11:42 +00002194 case Instruction::INT_TO_BYTE: {
Roland Levillain624279f2014-12-04 11:54:28 +00002195 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte, dex_pc);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002196 break;
2197 }
2198
Roland Levillain01a8d712014-11-14 16:27:39 +00002199 case Instruction::INT_TO_SHORT: {
Roland Levillain624279f2014-12-04 11:54:28 +00002200 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort, dex_pc);
Roland Levillain01a8d712014-11-14 16:27:39 +00002201 break;
2202 }
2203
Roland Levillain981e4542014-11-14 11:47:14 +00002204 case Instruction::INT_TO_CHAR: {
Roland Levillain624279f2014-12-04 11:54:28 +00002205 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar, dex_pc);
Roland Levillain981e4542014-11-14 11:47:14 +00002206 break;
2207 }
2208
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002209 case Instruction::ADD_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002210 Binop_23x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002211 break;
2212 }
2213
2214 case Instruction::ADD_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002215 Binop_23x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002216 break;
2217 }
2218
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002219 case Instruction::ADD_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002220 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002221 break;
2222 }
2223
2224 case Instruction::ADD_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002225 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002226 break;
2227 }
2228
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002229 case Instruction::SUB_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002230 Binop_23x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002231 break;
2232 }
2233
2234 case Instruction::SUB_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002235 Binop_23x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002236 break;
2237 }
2238
Calin Juravle096cc022014-10-23 17:01:13 +01002239 case Instruction::SUB_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002240 Binop_23x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01002241 break;
2242 }
2243
2244 case Instruction::SUB_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002245 Binop_23x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01002246 break;
2247 }
2248
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002249 case Instruction::ADD_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002250 Binop_12x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002251 break;
2252 }
2253
Calin Juravle34bacdf2014-10-07 20:23:36 +01002254 case Instruction::MUL_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002255 Binop_23x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002256 break;
2257 }
2258
2259 case Instruction::MUL_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002260 Binop_23x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002261 break;
2262 }
2263
Calin Juravleb5bfa962014-10-21 18:02:24 +01002264 case Instruction::MUL_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002265 Binop_23x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01002266 break;
2267 }
2268
2269 case Instruction::MUL_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002270 Binop_23x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01002271 break;
2272 }
2273
Calin Juravled0d48522014-11-04 16:40:20 +00002274 case Instruction::DIV_INT: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002275 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2276 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravled0d48522014-11-04 16:40:20 +00002277 break;
2278 }
2279
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002280 case Instruction::DIV_LONG: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002281 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2282 dex_pc, Primitive::kPrimLong, false, true);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002283 break;
2284 }
2285
Calin Juravle7c4954d2014-10-28 16:57:40 +00002286 case Instruction::DIV_FLOAT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002287 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002288 break;
2289 }
2290
2291 case Instruction::DIV_DOUBLE: {
Calin Juravle225ff812014-11-13 16:46:39 +00002292 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002293 break;
2294 }
2295
Calin Juravlebacfec32014-11-14 15:54:36 +00002296 case Instruction::REM_INT: {
2297 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2298 dex_pc, Primitive::kPrimInt, false, false);
2299 break;
2300 }
2301
2302 case Instruction::REM_LONG: {
2303 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2304 dex_pc, Primitive::kPrimLong, false, false);
2305 break;
2306 }
2307
Calin Juravled2ec87d2014-12-08 14:24:46 +00002308 case Instruction::REM_FLOAT: {
2309 Binop_23x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2310 break;
2311 }
2312
2313 case Instruction::REM_DOUBLE: {
2314 Binop_23x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2315 break;
2316 }
2317
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002318 case Instruction::AND_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002319 Binop_23x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002320 break;
2321 }
2322
2323 case Instruction::AND_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002324 Binop_23x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002325 break;
2326 }
2327
Calin Juravle9aec02f2014-11-18 23:06:35 +00002328 case Instruction::SHL_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002329 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002330 break;
2331 }
2332
2333 case Instruction::SHL_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002334 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002335 break;
2336 }
2337
2338 case Instruction::SHR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002339 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002340 break;
2341 }
2342
2343 case Instruction::SHR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002344 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002345 break;
2346 }
2347
2348 case Instruction::USHR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002349 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002350 break;
2351 }
2352
2353 case Instruction::USHR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002354 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002355 break;
2356 }
2357
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002358 case Instruction::OR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002359 Binop_23x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002360 break;
2361 }
2362
2363 case Instruction::OR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002364 Binop_23x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002365 break;
2366 }
2367
2368 case Instruction::XOR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002369 Binop_23x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002370 break;
2371 }
2372
2373 case Instruction::XOR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002374 Binop_23x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002375 break;
2376 }
2377
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002378 case Instruction::ADD_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002379 Binop_12x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002380 break;
2381 }
2382
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002383 case Instruction::ADD_DOUBLE_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002384 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002385 break;
2386 }
2387
2388 case Instruction::ADD_FLOAT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002389 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002390 break;
2391 }
2392
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002393 case Instruction::SUB_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002394 Binop_12x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002395 break;
2396 }
2397
2398 case Instruction::SUB_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002399 Binop_12x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002400 break;
2401 }
2402
Calin Juravle096cc022014-10-23 17:01:13 +01002403 case Instruction::SUB_FLOAT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002404 Binop_12x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01002405 break;
2406 }
2407
2408 case Instruction::SUB_DOUBLE_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002409 Binop_12x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01002410 break;
2411 }
2412
Calin Juravle34bacdf2014-10-07 20:23:36 +01002413 case Instruction::MUL_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002414 Binop_12x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002415 break;
2416 }
2417
2418 case Instruction::MUL_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002419 Binop_12x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002420 break;
2421 }
2422
Calin Juravleb5bfa962014-10-21 18:02:24 +01002423 case Instruction::MUL_FLOAT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002424 Binop_12x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01002425 break;
2426 }
2427
2428 case Instruction::MUL_DOUBLE_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002429 Binop_12x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01002430 break;
2431 }
2432
Calin Juravle865fc882014-11-06 17:09:03 +00002433 case Instruction::DIV_INT_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002434 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2435 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravle865fc882014-11-06 17:09:03 +00002436 break;
2437 }
2438
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002439 case Instruction::DIV_LONG_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002440 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2441 dex_pc, Primitive::kPrimLong, false, true);
2442 break;
2443 }
2444
2445 case Instruction::REM_INT_2ADDR: {
2446 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2447 dex_pc, Primitive::kPrimInt, false, false);
2448 break;
2449 }
2450
2451 case Instruction::REM_LONG_2ADDR: {
2452 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2453 dex_pc, Primitive::kPrimLong, false, false);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002454 break;
2455 }
2456
Calin Juravled2ec87d2014-12-08 14:24:46 +00002457 case Instruction::REM_FLOAT_2ADDR: {
2458 Binop_12x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2459 break;
2460 }
2461
2462 case Instruction::REM_DOUBLE_2ADDR: {
2463 Binop_12x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2464 break;
2465 }
2466
Calin Juravle9aec02f2014-11-18 23:06:35 +00002467 case Instruction::SHL_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002468 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002469 break;
2470 }
2471
2472 case Instruction::SHL_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002473 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002474 break;
2475 }
2476
2477 case Instruction::SHR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002478 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002479 break;
2480 }
2481
2482 case Instruction::SHR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002483 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002484 break;
2485 }
2486
2487 case Instruction::USHR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002488 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002489 break;
2490 }
2491
2492 case Instruction::USHR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002493 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002494 break;
2495 }
2496
Calin Juravle7c4954d2014-10-28 16:57:40 +00002497 case Instruction::DIV_FLOAT_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00002498 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002499 break;
2500 }
2501
2502 case Instruction::DIV_DOUBLE_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00002503 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002504 break;
2505 }
2506
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002507 case Instruction::AND_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002508 Binop_12x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002509 break;
2510 }
2511
2512 case Instruction::AND_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002513 Binop_12x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002514 break;
2515 }
2516
2517 case Instruction::OR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002518 Binop_12x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002519 break;
2520 }
2521
2522 case Instruction::OR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002523 Binop_12x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002524 break;
2525 }
2526
2527 case Instruction::XOR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002528 Binop_12x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002529 break;
2530 }
2531
2532 case Instruction::XOR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002533 Binop_12x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002534 break;
2535 }
2536
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002537 case Instruction::ADD_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002538 Binop_22s<HAdd>(instruction, false, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002539 break;
2540 }
2541
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002542 case Instruction::AND_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002543 Binop_22s<HAnd>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002544 break;
2545 }
2546
2547 case Instruction::OR_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002548 Binop_22s<HOr>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002549 break;
2550 }
2551
2552 case Instruction::XOR_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002553 Binop_22s<HXor>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002554 break;
2555 }
2556
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002557 case Instruction::RSUB_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002558 Binop_22s<HSub>(instruction, true, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002559 break;
2560 }
2561
Calin Juravle34bacdf2014-10-07 20:23:36 +01002562 case Instruction::MUL_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002563 Binop_22s<HMul>(instruction, false, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002564 break;
2565 }
2566
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002567 case Instruction::ADD_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002568 Binop_22b<HAdd>(instruction, false, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002569 break;
2570 }
2571
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002572 case Instruction::AND_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002573 Binop_22b<HAnd>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002574 break;
2575 }
2576
2577 case Instruction::OR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002578 Binop_22b<HOr>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002579 break;
2580 }
2581
2582 case Instruction::XOR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002583 Binop_22b<HXor>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002584 break;
2585 }
2586
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002587 case Instruction::RSUB_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002588 Binop_22b<HSub>(instruction, true, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002589 break;
2590 }
2591
Calin Juravle34bacdf2014-10-07 20:23:36 +01002592 case Instruction::MUL_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002593 Binop_22b<HMul>(instruction, false, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002594 break;
2595 }
2596
Calin Juravled0d48522014-11-04 16:40:20 +00002597 case Instruction::DIV_INT_LIT16:
2598 case Instruction::DIV_INT_LIT8: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002599 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2600 dex_pc, Primitive::kPrimInt, true, true);
2601 break;
2602 }
2603
2604 case Instruction::REM_INT_LIT16:
2605 case Instruction::REM_INT_LIT8: {
2606 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2607 dex_pc, Primitive::kPrimInt, true, false);
Calin Juravled0d48522014-11-04 16:40:20 +00002608 break;
2609 }
2610
Calin Juravle9aec02f2014-11-18 23:06:35 +00002611 case Instruction::SHL_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002612 Binop_22b<HShl>(instruction, false, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002613 break;
2614 }
2615
2616 case Instruction::SHR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002617 Binop_22b<HShr>(instruction, false, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002618 break;
2619 }
2620
2621 case Instruction::USHR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002622 Binop_22b<HUShr>(instruction, false, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002623 break;
2624 }
2625
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002626 case Instruction::NEW_INSTANCE: {
David Brazdil6de19382016-01-08 17:37:10 +00002627 if (!BuildNewInstance(instruction.VRegB_21c(), dex_pc)) {
2628 return false;
Jeff Hao848f70a2014-01-15 13:49:50 -08002629 }
David Brazdil6de19382016-01-08 17:37:10 +00002630 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002631 break;
2632 }
2633
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002634 case Instruction::NEW_ARRAY: {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002635 uint16_t type_index = instruction.VRegC_22c();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002636 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt, dex_pc);
Mingyao Yangfb8464a2015-11-02 10:56:59 -08002637 bool finalizable;
2638 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index, &finalizable)
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002639 ? kQuickAllocArrayWithAccessCheck
2640 : kQuickAllocArray;
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002641 current_block_->AddInstruction(new (arena_) HNewArray(length,
2642 graph_->GetCurrentMethod(),
2643 dex_pc,
2644 type_index,
2645 *dex_compilation_unit_->GetDexFile(),
2646 entrypoint));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002647 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002648 break;
2649 }
2650
2651 case Instruction::FILLED_NEW_ARRAY: {
2652 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
2653 uint32_t type_index = instruction.VRegB_35c();
2654 uint32_t args[5];
2655 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00002656 BuildFilledNewArray(dex_pc, type_index, number_of_vreg_arguments, false, args, 0);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002657 break;
2658 }
2659
2660 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2661 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
2662 uint32_t type_index = instruction.VRegB_3rc();
2663 uint32_t register_index = instruction.VRegC_3rc();
2664 BuildFilledNewArray(
Calin Juravle225ff812014-11-13 16:46:39 +00002665 dex_pc, type_index, number_of_vreg_arguments, true, nullptr, register_index);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002666 break;
2667 }
2668
2669 case Instruction::FILL_ARRAY_DATA: {
Calin Juravle225ff812014-11-13 16:46:39 +00002670 BuildFillArrayData(instruction, dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002671 break;
2672 }
2673
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002674 case Instruction::MOVE_RESULT:
Dave Allison20dfc792014-06-16 20:44:29 -07002675 case Instruction::MOVE_RESULT_WIDE:
David Brazdilfc6a86a2015-06-26 10:33:45 +00002676 case Instruction::MOVE_RESULT_OBJECT: {
Nicolas Geoffray1efcc222015-06-24 12:41:20 +01002677 if (latest_result_ == nullptr) {
2678 // Only dead code can lead to this situation, where the verifier
2679 // does not reject the method.
2680 } else {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002681 // An Invoke/FilledNewArray and its MoveResult could have landed in
2682 // different blocks if there was a try/catch block boundary between
2683 // them. For Invoke, we insert a StoreLocal after the instruction. For
2684 // FilledNewArray, the local needs to be updated after the array was
2685 // filled, otherwise we might overwrite an input vreg.
2686 HStoreLocal* update_local =
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002687 new (arena_) HStoreLocal(GetLocalAt(instruction.VRegA()), latest_result_, dex_pc);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002688 HBasicBlock* block = latest_result_->GetBlock();
2689 if (block == current_block_) {
2690 // MoveResult and the previous instruction are in the same block.
2691 current_block_->AddInstruction(update_local);
2692 } else {
2693 // The two instructions are in different blocks. Insert the MoveResult
2694 // before the final control-flow instruction of the previous block.
2695 DCHECK(block->EndsWithControlFlowInstruction());
2696 DCHECK(current_block_->GetInstructions().IsEmpty());
2697 block->InsertInstructionBefore(update_local, block->GetLastInstruction());
2698 }
Nicolas Geoffray1efcc222015-06-24 12:41:20 +01002699 latest_result_ = nullptr;
2700 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002701 break;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002702 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002703
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002704 case Instruction::CMP_LONG: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002705 Binop_23x_cmp(instruction, Primitive::kPrimLong, ComparisonBias::kNoBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002706 break;
2707 }
2708
2709 case Instruction::CMPG_FLOAT: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002710 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kGtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002711 break;
2712 }
2713
2714 case Instruction::CMPG_DOUBLE: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002715 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kGtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002716 break;
2717 }
2718
2719 case Instruction::CMPL_FLOAT: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002720 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kLtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002721 break;
2722 }
2723
2724 case Instruction::CMPL_DOUBLE: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002725 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kLtBias, dex_pc);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002726 break;
2727 }
2728
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002729 case Instruction::NOP:
2730 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002731
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002732 case Instruction::IGET:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002733 case Instruction::IGET_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002734 case Instruction::IGET_WIDE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002735 case Instruction::IGET_WIDE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002736 case Instruction::IGET_OBJECT:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002737 case Instruction::IGET_OBJECT_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002738 case Instruction::IGET_BOOLEAN:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002739 case Instruction::IGET_BOOLEAN_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002740 case Instruction::IGET_BYTE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002741 case Instruction::IGET_BYTE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002742 case Instruction::IGET_CHAR:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002743 case Instruction::IGET_CHAR_QUICK:
2744 case Instruction::IGET_SHORT:
2745 case Instruction::IGET_SHORT_QUICK: {
Calin Juravle225ff812014-11-13 16:46:39 +00002746 if (!BuildInstanceFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002747 return false;
2748 }
2749 break;
2750 }
2751
2752 case Instruction::IPUT:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002753 case Instruction::IPUT_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002754 case Instruction::IPUT_WIDE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002755 case Instruction::IPUT_WIDE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002756 case Instruction::IPUT_OBJECT:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002757 case Instruction::IPUT_OBJECT_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002758 case Instruction::IPUT_BOOLEAN:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002759 case Instruction::IPUT_BOOLEAN_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002760 case Instruction::IPUT_BYTE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002761 case Instruction::IPUT_BYTE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002762 case Instruction::IPUT_CHAR:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002763 case Instruction::IPUT_CHAR_QUICK:
2764 case Instruction::IPUT_SHORT:
2765 case Instruction::IPUT_SHORT_QUICK: {
Calin Juravle225ff812014-11-13 16:46:39 +00002766 if (!BuildInstanceFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002767 return false;
2768 }
2769 break;
2770 }
2771
2772 case Instruction::SGET:
2773 case Instruction::SGET_WIDE:
2774 case Instruction::SGET_OBJECT:
2775 case Instruction::SGET_BOOLEAN:
2776 case Instruction::SGET_BYTE:
2777 case Instruction::SGET_CHAR:
2778 case Instruction::SGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002779 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002780 return false;
2781 }
2782 break;
2783 }
2784
2785 case Instruction::SPUT:
2786 case Instruction::SPUT_WIDE:
2787 case Instruction::SPUT_OBJECT:
2788 case Instruction::SPUT_BOOLEAN:
2789 case Instruction::SPUT_BYTE:
2790 case Instruction::SPUT_CHAR:
2791 case Instruction::SPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002792 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002793 return false;
2794 }
2795 break;
2796 }
2797
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002798#define ARRAY_XX(kind, anticipated_type) \
2799 case Instruction::AGET##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00002800 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002801 break; \
2802 } \
2803 case Instruction::APUT##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00002804 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002805 break; \
2806 }
2807
2808 ARRAY_XX(, Primitive::kPrimInt);
2809 ARRAY_XX(_WIDE, Primitive::kPrimLong);
2810 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
2811 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
2812 ARRAY_XX(_BYTE, Primitive::kPrimByte);
2813 ARRAY_XX(_CHAR, Primitive::kPrimChar);
2814 ARRAY_XX(_SHORT, Primitive::kPrimShort);
2815
Nicolas Geoffray39468442014-09-02 15:17:15 +01002816 case Instruction::ARRAY_LENGTH: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002817 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot, dex_pc);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002818 // No need for a temporary for the null check, it is the only input of the following
2819 // instruction.
Calin Juravle225ff812014-11-13 16:46:39 +00002820 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002821 current_block_->AddInstruction(object);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002822 current_block_->AddInstruction(new (arena_) HArrayLength(object, dex_pc));
2823 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002824 break;
2825 }
2826
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002827 case Instruction::CONST_STRING: {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00002828 uint32_t string_index = instruction.VRegB_21c();
2829 bool in_dex_cache = compiler_driver_->CanAssumeStringIsPresentInDexCache(
2830 *dex_file_, string_index);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002831 current_block_->AddInstruction(
Nicolas Geoffray917d0162015-11-24 18:25:35 +00002832 new (arena_) HLoadString(graph_->GetCurrentMethod(), string_index, dex_pc, in_dex_cache));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002833 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002834 break;
2835 }
2836
2837 case Instruction::CONST_STRING_JUMBO: {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00002838 uint32_t string_index = instruction.VRegB_31c();
2839 bool in_dex_cache = compiler_driver_->CanAssumeStringIsPresentInDexCache(
2840 *dex_file_, string_index);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002841 current_block_->AddInstruction(
Nicolas Geoffray917d0162015-11-24 18:25:35 +00002842 new (arena_) HLoadString(graph_->GetCurrentMethod(), string_index, dex_pc, in_dex_cache));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002843 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002844 break;
2845 }
2846
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002847 case Instruction::CONST_CLASS: {
2848 uint16_t type_index = instruction.VRegB_21c();
2849 bool type_known_final;
2850 bool type_known_abstract;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002851 bool dont_use_is_referrers_class;
2852 // `CanAccessTypeWithoutChecks` will tell whether the method being
2853 // built is trying to access its own class, so that the generated
2854 // code can optimize for this case. However, the optimization does not
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002855 // work for inlining, so we use `IsOutermostCompilingClass` instead.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002856 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
2857 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002858 &type_known_final, &type_known_abstract, &dont_use_is_referrers_class);
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002859 current_block_->AddInstruction(new (arena_) HLoadClass(
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002860 graph_->GetCurrentMethod(),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002861 type_index,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00002862 *dex_file_,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002863 IsOutermostCompilingClass(type_index),
Calin Juravle98893e12015-10-02 21:05:03 +01002864 dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00002865 !can_access,
2866 compiler_driver_->CanAssumeTypeIsPresentInDexCache(*dex_file_, type_index)));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002867 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002868 break;
2869 }
2870
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002871 case Instruction::MOVE_EXCEPTION: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002872 current_block_->AddInstruction(new (arena_) HLoadException(dex_pc));
2873 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction(), dex_pc);
2874 current_block_->AddInstruction(new (arena_) HClearException(dex_pc));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002875 break;
2876 }
2877
2878 case Instruction::THROW: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002879 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00002880 current_block_->AddInstruction(new (arena_) HThrow(exception, dex_pc));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002881 // A throw instruction must branch to the exit block.
2882 current_block_->AddSuccessor(exit_block_);
2883 // We finished building this block. Set the current block to null to avoid
2884 // adding dead instructions to it.
2885 current_block_ = nullptr;
2886 break;
2887 }
2888
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002889 case Instruction::INSTANCE_OF: {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002890 uint8_t destination = instruction.VRegA_22c();
2891 uint8_t reference = instruction.VRegB_22c();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002892 uint16_t type_index = instruction.VRegC_22c();
Calin Juravle98893e12015-10-02 21:05:03 +01002893 BuildTypeCheck(instruction, destination, reference, type_index, dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002894 break;
2895 }
2896
2897 case Instruction::CHECK_CAST: {
2898 uint8_t reference = instruction.VRegA_21c();
2899 uint16_t type_index = instruction.VRegB_21c();
Calin Juravle98893e12015-10-02 21:05:03 +01002900 BuildTypeCheck(instruction, -1, reference, type_index, dex_pc);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002901 break;
2902 }
2903
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002904 case Instruction::MONITOR_ENTER: {
2905 current_block_->AddInstruction(new (arena_) HMonitorOperation(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002906 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot, dex_pc),
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002907 HMonitorOperation::kEnter,
Calin Juravle225ff812014-11-13 16:46:39 +00002908 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002909 break;
2910 }
2911
2912 case Instruction::MONITOR_EXIT: {
2913 current_block_->AddInstruction(new (arena_) HMonitorOperation(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002914 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot, dex_pc),
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002915 HMonitorOperation::kExit,
Calin Juravle225ff812014-11-13 16:46:39 +00002916 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002917 break;
2918 }
2919
Andreas Gamped881df52014-11-24 23:28:39 -08002920 case Instruction::PACKED_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002921 BuildPackedSwitch(instruction, dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08002922 break;
2923 }
2924
Andreas Gampee4d4d322014-12-04 09:09:57 -08002925 case Instruction::SPARSE_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002926 BuildSparseSwitch(instruction, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08002927 break;
2928 }
2929
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002930 default:
Calin Juravle48c2b032014-12-09 18:11:36 +00002931 VLOG(compiler) << "Did not compile "
2932 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
2933 << " because of unhandled instruction "
2934 << instruction.Name();
2935 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnhandledInstruction);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002936 return false;
2937 }
2938 return true;
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00002939} // NOLINT(readability/fn_size)
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002940
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01002941HLocal* HGraphBuilder::GetLocalAt(uint32_t register_index) const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01002942 return locals_[register_index];
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002943}
2944
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01002945void HGraphBuilder::UpdateLocal(uint32_t register_index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002946 HInstruction* instruction,
2947 uint32_t dex_pc) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002948 HLocal* local = GetLocalAt(register_index);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002949 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction, dex_pc));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002950}
2951
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01002952HInstruction* HGraphBuilder::LoadLocal(uint32_t register_index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002953 Primitive::Type type,
2954 uint32_t dex_pc) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002955 HLocal* local = GetLocalAt(register_index);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002956 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type, dex_pc));
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002957 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002958}
2959
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002960} // namespace art