blob: 05e1356ed8202a79d8d277b0d000fa4773a9c28a [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"
David Brazdilbadd8262016-02-02 16:28:56 +000035#include "ssa_builder.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010036#include "thread.h"
Vladimir Marko58155012015-08-19 12:49:41 +000037#include "utils/dex_cache_arrays_layout-inl.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000038
39namespace art {
40
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010041void HGraphBuilder::InitializeLocals(uint16_t count) {
42 graph_->SetNumberOfVRegs(count);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010043 locals_.resize(count);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000044 for (int i = 0; i < count; i++) {
45 HLocal* local = new (arena_) HLocal(i);
46 entry_block_->AddInstruction(local);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010047 locals_[i] = local;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000048 }
49}
50
Nicolas Geoffray52e832b2014-11-06 15:15:31 +000051void HGraphBuilder::InitializeParameters(uint16_t number_of_parameters) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010052 // dex_compilation_unit_ is null only when unit testing.
53 if (dex_compilation_unit_ == nullptr) {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +000054 return;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010055 }
56
57 graph_->SetNumberOfInVRegs(number_of_parameters);
58 const char* shorty = dex_compilation_unit_->GetShorty();
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010059 int locals_index = locals_.size() - number_of_parameters;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010060 int parameter_index = 0;
61
Calin Juravlee6e3bea2015-10-14 13:53:10 +000062 const DexFile::MethodId& referrer_method_id =
63 dex_file_->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010064 if (!dex_compilation_unit_->IsStatic()) {
65 // Add the implicit 'this' argument, not expressed in the signature.
Calin Juravlee6e3bea2015-10-14 13:53:10 +000066 HParameterValue* parameter = new (arena_) HParameterValue(*dex_file_,
67 referrer_method_id.class_idx_,
68 parameter_index++,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060069 Primitive::kPrimNot,
70 true);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +010071 entry_block_->AddInstruction(parameter);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010072 HLocal* local = GetLocalAt(locals_index++);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060073 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter, local->GetDexPc()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010074 number_of_parameters--;
75 }
76
Calin Juravlee6e3bea2015-10-14 13:53:10 +000077 const DexFile::ProtoId& proto = dex_file_->GetMethodPrototype(referrer_method_id);
78 const DexFile::TypeList* arg_types = dex_file_->GetProtoParameters(proto);
79 for (int i = 0, shorty_pos = 1; i < number_of_parameters; i++) {
80 HParameterValue* parameter = new (arena_) HParameterValue(
81 *dex_file_,
82 arg_types->GetTypeItem(shorty_pos - 1).type_idx_,
83 parameter_index++,
84 Primitive::GetType(shorty[shorty_pos]),
85 false);
86 ++shorty_pos;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010087 entry_block_->AddInstruction(parameter);
88 HLocal* local = GetLocalAt(locals_index++);
89 // Store the parameter value in the local that the dex code will use
90 // to reference that parameter.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060091 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter, local->GetDexPc()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010092 bool is_wide = (parameter->GetType() == Primitive::kPrimLong)
93 || (parameter->GetType() == Primitive::kPrimDouble);
94 if (is_wide) {
95 i++;
96 locals_index++;
97 parameter_index++;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010098 }
99 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100100}
101
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100102template<typename T>
Calin Juravle225ff812014-11-13 16:46:39 +0000103void HGraphBuilder::If_22t(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000104 int32_t target_offset = instruction.GetTargetOffset();
David Brazdil852eaff2015-02-02 15:23:05 +0000105 HBasicBlock* branch_target = FindBlockStartingAt(dex_pc + target_offset);
106 HBasicBlock* fallthrough_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
107 DCHECK(branch_target != nullptr);
108 DCHECK(fallthrough_target != nullptr);
109 PotentiallyAddSuspendCheck(branch_target, dex_pc);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600110 HInstruction* first = LoadLocal(instruction.VRegA(), Primitive::kPrimInt, dex_pc);
111 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
112 T* comparison = new (arena_) T(first, second, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700113 current_block_->AddInstruction(comparison);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600114 HInstruction* ifinst = new (arena_) HIf(comparison, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700115 current_block_->AddInstruction(ifinst);
David Brazdil852eaff2015-02-02 15:23:05 +0000116 current_block_->AddSuccessor(branch_target);
117 current_block_->AddSuccessor(fallthrough_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700118 current_block_ = nullptr;
119}
120
121template<typename T>
Calin Juravle225ff812014-11-13 16:46:39 +0000122void HGraphBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000123 int32_t target_offset = instruction.GetTargetOffset();
David Brazdil852eaff2015-02-02 15:23:05 +0000124 HBasicBlock* branch_target = FindBlockStartingAt(dex_pc + target_offset);
125 HBasicBlock* fallthrough_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
126 DCHECK(branch_target != nullptr);
127 DCHECK(fallthrough_target != nullptr);
128 PotentiallyAddSuspendCheck(branch_target, dex_pc);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600129 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt, dex_pc);
130 T* comparison = new (arena_) T(value, graph_->GetIntConstant(0, dex_pc), dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700131 current_block_->AddInstruction(comparison);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600132 HInstruction* ifinst = new (arena_) HIf(comparison, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700133 current_block_->AddInstruction(ifinst);
David Brazdil852eaff2015-02-02 15:23:05 +0000134 current_block_->AddSuccessor(branch_target);
135 current_block_->AddSuccessor(fallthrough_target);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100136 current_block_ = nullptr;
137}
138
Calin Juravle48c2b032014-12-09 18:11:36 +0000139void HGraphBuilder::MaybeRecordStat(MethodCompilationStat compilation_stat) {
140 if (compilation_stats_ != nullptr) {
141 compilation_stats_->RecordStat(compilation_stat);
142 }
143}
144
David Brazdil1b498722015-03-31 11:37:18 +0100145bool HGraphBuilder::SkipCompilation(const DexFile::CodeItem& code_item,
Calin Juravle48c2b032014-12-09 18:11:36 +0000146 size_t number_of_branches) {
147 const CompilerOptions& compiler_options = compiler_driver_->GetCompilerOptions();
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000148 CompilerOptions::CompilerFilter compiler_filter = compiler_options.GetCompilerFilter();
149 if (compiler_filter == CompilerOptions::kEverything) {
150 return false;
151 }
152
David Brazdil1b498722015-03-31 11:37:18 +0100153 if (compiler_options.IsHugeMethod(code_item.insns_size_in_code_units_)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000154 VLOG(compiler) << "Skip compilation of huge method "
155 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
David Brazdil1b498722015-03-31 11:37:18 +0100156 << ": " << code_item.insns_size_in_code_units_ << " code units";
Calin Juravle48c2b032014-12-09 18:11:36 +0000157 MaybeRecordStat(MethodCompilationStat::kNotCompiledHugeMethod);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000158 return true;
159 }
160
161 // If it's large and contains no branches, it's likely to be machine generated initialization.
David Brazdil1b498722015-03-31 11:37:18 +0100162 if (compiler_options.IsLargeMethod(code_item.insns_size_in_code_units_)
163 && (number_of_branches == 0)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000164 VLOG(compiler) << "Skip compilation of large method with no branch "
165 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
David Brazdil1b498722015-03-31 11:37:18 +0100166 << ": " << code_item.insns_size_in_code_units_ << " code units";
Calin Juravle48c2b032014-12-09 18:11:36 +0000167 MaybeRecordStat(MethodCompilationStat::kNotCompiledLargeMethodNoBranches);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000168 return true;
169 }
170
171 return false;
172}
173
David Brazdilfc6a86a2015-06-26 10:33:45 +0000174void HGraphBuilder::CreateBlocksForTryCatch(const DexFile::CodeItem& code_item) {
175 if (code_item.tries_size_ == 0) {
176 return;
177 }
178
179 // Create branch targets at the start/end of the TryItem range. These are
180 // places where the program might fall through into/out of the a block and
181 // where TryBoundary instructions will be inserted later. Other edges which
182 // enter/exit the try blocks are a result of branches/switches.
183 for (size_t idx = 0; idx < code_item.tries_size_; ++idx) {
184 const DexFile::TryItem* try_item = DexFile::GetTryItems(code_item, idx);
185 uint32_t dex_pc_start = try_item->start_addr_;
186 uint32_t dex_pc_end = dex_pc_start + try_item->insn_count_;
187 FindOrCreateBlockStartingAt(dex_pc_start);
188 if (dex_pc_end < code_item.insns_size_in_code_units_) {
189 // TODO: Do not create block if the last instruction cannot fall through.
190 FindOrCreateBlockStartingAt(dex_pc_end);
191 } else {
192 // The TryItem spans until the very end of the CodeItem (or beyond if
193 // invalid) and therefore cannot have any code afterwards.
194 }
195 }
196
197 // Create branch targets for exception handlers.
198 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(code_item, 0);
199 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
200 for (uint32_t idx = 0; idx < handlers_size; ++idx) {
201 CatchHandlerIterator iterator(handlers_ptr);
202 for (; iterator.HasNext(); iterator.Next()) {
203 uint32_t address = iterator.GetHandlerAddress();
204 HBasicBlock* block = FindOrCreateBlockStartingAt(address);
David Brazdilec16f792015-08-19 15:04:01 +0100205 block->SetTryCatchInformation(
206 new (arena_) TryCatchInformation(iterator.GetHandlerTypeIndex(), *dex_file_));
David Brazdilfc6a86a2015-06-26 10:33:45 +0000207 }
208 handlers_ptr = iterator.EndDataPointer();
209 }
210}
211
David Brazdild7558da2015-09-22 13:04:14 +0100212// Returns the TryItem stored for `block` or nullptr if there is no info for it.
213static const DexFile::TryItem* GetTryItem(
214 HBasicBlock* block,
215 const ArenaSafeMap<uint32_t, const DexFile::TryItem*>& try_block_info) {
216 auto iterator = try_block_info.find(block->GetBlockId());
217 return (iterator == try_block_info.end()) ? nullptr : iterator->second;
218}
David Brazdil56e1acc2015-06-30 15:41:36 +0100219
David Brazdild7558da2015-09-22 13:04:14 +0100220void HGraphBuilder::LinkToCatchBlocks(HTryBoundary* try_boundary,
221 const DexFile::CodeItem& code_item,
222 const DexFile::TryItem* try_item) {
223 for (CatchHandlerIterator it(code_item, *try_item); it.HasNext(); it.Next()) {
David Brazdil56e1acc2015-06-30 15:41:36 +0100224 try_boundary->AddExceptionHandler(FindBlockStartingAt(it.GetHandlerAddress()));
225 }
226}
227
David Brazdilfc6a86a2015-06-26 10:33:45 +0000228void HGraphBuilder::InsertTryBoundaryBlocks(const DexFile::CodeItem& code_item) {
229 if (code_item.tries_size_ == 0) {
230 return;
231 }
232
David Brazdild7558da2015-09-22 13:04:14 +0100233 // Keep a map of all try blocks and their respective TryItems. We do not use
234 // the block's pointer but rather its id to ensure deterministic iteration.
235 ArenaSafeMap<uint32_t, const DexFile::TryItem*> try_block_info(
Vladimir Marko5233f932015-09-29 19:01:15 +0100236 std::less<uint32_t>(), arena_->Adapter(kArenaAllocGraphBuilder));
David Brazdilbff75032015-07-08 17:26:51 +0000237
David Brazdild7558da2015-09-22 13:04:14 +0100238 // Obtain TryItem information for blocks with throwing instructions, and split
239 // blocks which are both try & catch to simplify the graph.
240 // NOTE: We are appending new blocks inside the loop, so we need to use index
241 // because iterators can be invalidated. We remember the initial size to avoid
242 // iterating over the new blocks which cannot throw.
243 for (size_t i = 0, e = graph_->GetBlocks().size(); i < e; ++i) {
244 HBasicBlock* block = graph_->GetBlocks()[i];
David Brazdil72783ff2015-07-09 14:36:05 +0100245
David Brazdild7558da2015-09-22 13:04:14 +0100246 // Do not bother creating exceptional edges for try blocks which have no
247 // throwing instructions. In that case we simply assume that the block is
248 // not covered by a TryItem. This prevents us from creating a throw-catch
249 // loop for synchronized blocks.
250 if (block->HasThrowingInstructions()) {
251 // Try to find a TryItem covering the block.
David Brazdilbadd8262016-02-02 16:28:56 +0000252 DCHECK_NE(block->GetDexPc(), kNoDexPc) << "Block must have a dex_pc to find its TryItem.";
David Brazdild7558da2015-09-22 13:04:14 +0100253 const int32_t try_item_idx = DexFile::FindTryItem(code_item, block->GetDexPc());
254 if (try_item_idx != -1) {
255 // Block throwing and in a TryItem. Store the try block information.
256 HBasicBlock* throwing_block = block;
257 if (block->IsCatchBlock()) {
258 // Simplify blocks which are both try and catch, otherwise we would
259 // need a strategy for splitting exceptional edges. We split the block
260 // after the move-exception (if present) and mark the first part not
261 // throwing. The normal-flow edge between them will be split later.
David Brazdil9bc43612015-11-05 21:25:24 +0000262 throwing_block = block->SplitCatchBlockAfterMoveException();
263 // Move-exception does not throw and the block has throwing insructions
264 // so it must have been possible to split it.
265 DCHECK(throwing_block != nullptr);
David Brazdil72783ff2015-07-09 14:36:05 +0100266 }
David Brazdild7558da2015-09-22 13:04:14 +0100267
268 try_block_info.Put(throwing_block->GetBlockId(),
269 DexFile::GetTryItems(code_item, try_item_idx));
David Brazdil72783ff2015-07-09 14:36:05 +0100270 }
David Brazdil72783ff2015-07-09 14:36:05 +0100271 }
David Brazdilbff75032015-07-08 17:26:51 +0000272 }
273
David Brazdild7558da2015-09-22 13:04:14 +0100274 // Do a pass over the try blocks and insert entering TryBoundaries where at
275 // least one predecessor is not covered by the same TryItem as the try block.
276 // We do not split each edge separately, but rather create one boundary block
277 // that all predecessors are relinked to. This preserves loop headers (b/23895756).
278 for (auto entry : try_block_info) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100279 HBasicBlock* try_block = graph_->GetBlocks()[entry.first];
David Brazdild7558da2015-09-22 13:04:14 +0100280 for (HBasicBlock* predecessor : try_block->GetPredecessors()) {
281 if (GetTryItem(predecessor, try_block_info) != entry.second) {
282 // Found a predecessor not covered by the same TryItem. Insert entering
283 // boundary block.
284 HTryBoundary* try_entry =
285 new (arena_) HTryBoundary(HTryBoundary::kEntry, try_block->GetDexPc());
286 try_block->CreateImmediateDominator()->AddInstruction(try_entry);
287 LinkToCatchBlocks(try_entry, code_item, entry.second);
288 break;
289 }
David Brazdil281a6322015-07-03 10:34:57 +0100290 }
David Brazdild7558da2015-09-22 13:04:14 +0100291 }
David Brazdil281a6322015-07-03 10:34:57 +0100292
David Brazdild7558da2015-09-22 13:04:14 +0100293 // Do a second pass over the try blocks and insert exit TryBoundaries where
294 // the successor is not in the same TryItem.
295 for (auto entry : try_block_info) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100296 HBasicBlock* try_block = graph_->GetBlocks()[entry.first];
David Brazdild7558da2015-09-22 13:04:14 +0100297 // NOTE: Do not use iterators because SplitEdge would invalidate them.
298 for (size_t i = 0, e = try_block->GetSuccessors().size(); i < e; ++i) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100299 HBasicBlock* successor = try_block->GetSuccessors()[i];
David Brazdil72783ff2015-07-09 14:36:05 +0100300
David Brazdild7558da2015-09-22 13:04:14 +0100301 // If the successor is a try block, all of its predecessors must be
302 // covered by the same TryItem. Otherwise the previous pass would have
303 // created a non-throwing boundary block.
304 if (GetTryItem(successor, try_block_info) != nullptr) {
305 DCHECK_EQ(entry.second, GetTryItem(successor, try_block_info));
David Brazdil72783ff2015-07-09 14:36:05 +0100306 continue;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000307 }
David Brazdil281a6322015-07-03 10:34:57 +0100308
David Brazdild7558da2015-09-22 13:04:14 +0100309 // Preserve the invariant that Return(Void) always jumps to Exit by moving
310 // it outside the try block if necessary.
311 HInstruction* last_instruction = try_block->GetLastInstruction();
312 if (last_instruction->IsReturn() || last_instruction->IsReturnVoid()) {
313 DCHECK_EQ(successor, exit_block_);
314 successor = try_block->SplitBefore(last_instruction);
David Brazdil281a6322015-07-03 10:34:57 +0100315 }
David Brazdild7558da2015-09-22 13:04:14 +0100316
317 // Insert TryBoundary and link to catch blocks.
318 HTryBoundary* try_exit =
319 new (arena_) HTryBoundary(HTryBoundary::kExit, successor->GetDexPc());
320 graph_->SplitEdge(try_block, successor)->AddInstruction(try_exit);
321 LinkToCatchBlocks(try_exit, code_item, entry.second);
David Brazdil281a6322015-07-03 10:34:57 +0100322 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000323 }
324}
325
David Brazdilbadd8262016-02-02 16:28:56 +0000326GraphAnalysisResult HGraphBuilder::BuildGraph(const DexFile::CodeItem& code_item,
327 StackHandleScopeCollection* handles) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100328 DCHECK(graph_->GetBlocks().empty());
David Brazdil5e8b1372015-01-23 14:39:08 +0000329
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000330 const uint16_t* code_ptr = code_item.insns_;
331 const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100332 code_start_ = code_ptr;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000333
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000334 // Setup the graph with the entry block and exit block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100335 entry_block_ = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000336 graph_->AddBlock(entry_block_);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100337 exit_block_ = new (arena_) HBasicBlock(graph_, kNoDexPc);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000338 graph_->SetEntryBlock(entry_block_);
339 graph_->SetExitBlock(exit_block_);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000340
David Brazdil77a48ae2015-09-15 12:34:04 +0000341 graph_->SetHasTryCatch(code_item.tries_size_ != 0);
342
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000343 InitializeLocals(code_item.registers_size_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000344 graph_->SetMaximumNumberOfOutVRegs(code_item.outs_size_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000345
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000346 // Compute the number of dex instructions, blocks, and branches. We will
347 // check these values against limits given to the compiler.
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000348 size_t number_of_branches = 0;
349
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000350 // To avoid splitting blocks, we compute ahead of time the instructions that
351 // start a new block, and create these blocks.
Calin Juravle702d2602015-04-30 19:28:21 +0100352 if (!ComputeBranchTargets(code_ptr, code_end, &number_of_branches)) {
353 MaybeRecordStat(MethodCompilationStat::kNotCompiledBranchOutsideMethodCode);
David Brazdilbadd8262016-02-02 16:28:56 +0000354 return kAnalysisInvalidBytecode;
Calin Juravle702d2602015-04-30 19:28:21 +0100355 }
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000356
357 // Note that the compiler driver is null when unit testing.
David Brazdil1b498722015-03-31 11:37:18 +0100358 if ((compiler_driver_ != nullptr) && SkipCompilation(code_item, number_of_branches)) {
David Brazdilbadd8262016-02-02 16:28:56 +0000359 return kAnalysisInvalidBytecode;
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000360 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000361
David Srbecky0cf44932015-12-09 14:09:59 +0000362 // Find locations where we want to generate extra stackmaps for native debugging.
363 // This allows us to generate the info only at interesting points (for example,
364 // at start of java statement) rather than before every dex instruction.
365 const bool native_debuggable = compiler_driver_ != nullptr &&
366 compiler_driver_->GetCompilerOptions().GetNativeDebuggable();
367 ArenaBitVector* native_debug_info_locations;
368 if (native_debuggable) {
369 const uint32_t num_instructions = code_item.insns_size_in_code_units_;
370 native_debug_info_locations = new (arena_) ArenaBitVector (arena_, num_instructions, false);
371 native_debug_info_locations->ClearAllBits();
372 FindNativeDebugInfoLocations(code_item, native_debug_info_locations);
373 }
374
David Brazdilfc6a86a2015-06-26 10:33:45 +0000375 CreateBlocksForTryCatch(code_item);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000376
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000377 InitializeParameters(code_item.ins_size_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100378
Calin Juravle225ff812014-11-13 16:46:39 +0000379 size_t dex_pc = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000380 while (code_ptr < code_end) {
Calin Juravle225ff812014-11-13 16:46:39 +0000381 // Update the current block if dex_pc starts a new block.
382 MaybeUpdateCurrentBlock(dex_pc);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000383 const Instruction& instruction = *Instruction::At(code_ptr);
David Srbecky0cf44932015-12-09 14:09:59 +0000384 if (native_debuggable && native_debug_info_locations->IsBitSet(dex_pc)) {
385 if (current_block_ != nullptr) {
386 current_block_->AddInstruction(new (arena_) HNativeDebugInfo(dex_pc));
387 }
388 }
Calin Juravle48c2b032014-12-09 18:11:36 +0000389 if (!AnalyzeDexInstruction(instruction, dex_pc)) {
David Brazdilbadd8262016-02-02 16:28:56 +0000390 return kAnalysisInvalidBytecode;
Calin Juravle48c2b032014-12-09 18:11:36 +0000391 }
Calin Juravle225ff812014-11-13 16:46:39 +0000392 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000393 code_ptr += instruction.SizeInCodeUnits();
394 }
395
David Brazdilfc6a86a2015-06-26 10:33:45 +0000396 // Add Exit to the exit block.
David Brazdil3e187382015-06-26 09:59:52 +0000397 exit_block_->AddInstruction(new (arena_) HExit());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000398 // Add the suspend check to the entry block.
399 entry_block_->AddInstruction(new (arena_) HSuspendCheck(0));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000400 entry_block_->AddInstruction(new (arena_) HGoto());
David Brazdilbff75032015-07-08 17:26:51 +0000401 // Add the exit block at the end.
402 graph_->AddBlock(exit_block_);
David Brazdil5e8b1372015-01-23 14:39:08 +0000403
David Brazdilfc6a86a2015-06-26 10:33:45 +0000404 // Iterate over blocks covered by TryItems and insert TryBoundaries at entry
405 // and exit points. This requires all control-flow instructions and
406 // non-exceptional edges to have been created.
407 InsertTryBoundaryBlocks(code_item);
408
David Brazdilbadd8262016-02-02 16:28:56 +0000409 GraphAnalysisResult result = graph_->BuildDominatorTree();
410 if (result != kAnalysisSuccess) {
411 return result;
412 }
413
414 graph_->InitializeInexactObjectRTI(handles);
415 return SsaBuilder(graph_, handles).BuildSsa();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000416}
417
David Brazdilfc6a86a2015-06-26 10:33:45 +0000418void HGraphBuilder::MaybeUpdateCurrentBlock(size_t dex_pc) {
419 HBasicBlock* block = FindBlockStartingAt(dex_pc);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000420 if (block == nullptr) {
421 return;
422 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000423
424 if (current_block_ != nullptr) {
425 // Branching instructions clear current_block, so we know
426 // the last instruction of the current block is not a branching
427 // instruction. We add an unconditional goto to the found block.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600428 current_block_->AddInstruction(new (arena_) HGoto(dex_pc));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000429 current_block_->AddSuccessor(block);
430 }
431 graph_->AddBlock(block);
432 current_block_ = block;
433}
434
David Srbecky0cf44932015-12-09 14:09:59 +0000435void HGraphBuilder::FindNativeDebugInfoLocations(const DexFile::CodeItem& code_item,
436 ArenaBitVector* locations) {
437 // The callback gets called when the line number changes.
438 // In other words, it marks the start of new java statement.
439 struct Callback {
440 static bool Position(void* ctx, const DexFile::PositionInfo& entry) {
441 static_cast<ArenaBitVector*>(ctx)->SetBit(entry.address_);
442 return false;
443 }
444 };
445 dex_file_->DecodeDebugPositionInfo(&code_item, Callback::Position, locations);
446 // Add native debug info at the start of every basic block.
447 for (uint32_t pc = 0; pc < code_item.insns_size_in_code_units_; pc++) {
448 if (FindBlockStartingAt(pc) != nullptr) {
449 locations->SetBit(pc);
450 }
451 }
452 // Instruction-specific tweaks.
453 const Instruction* const begin = Instruction::At(code_item.insns_);
454 const Instruction* const end = begin->RelativeAt(code_item.insns_size_in_code_units_);
455 for (const Instruction* inst = begin; inst < end; inst = inst->Next()) {
456 switch (inst->Opcode()) {
457 case Instruction::MOVE_EXCEPTION:
458 case Instruction::MOVE_RESULT:
459 case Instruction::MOVE_RESULT_WIDE:
460 case Instruction::MOVE_RESULT_OBJECT: {
461 // The compiler checks that there are no instructions before those.
462 // So generate HNativeDebugInfo after them instead.
463 locations->ClearBit(inst->GetDexPc(code_item.insns_));
464 const Instruction* next = inst->Next();
465 if (next < end) {
466 locations->SetBit(next->GetDexPc(code_item.insns_));
467 }
468 break;
469 }
470 default:
471 break;
472 }
473 }
474}
475
Calin Juravle702d2602015-04-30 19:28:21 +0100476bool HGraphBuilder::ComputeBranchTargets(const uint16_t* code_ptr,
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000477 const uint16_t* code_end,
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000478 size_t* number_of_branches) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100479 branch_targets_.resize(code_end - code_ptr, nullptr);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000480
481 // Create the first block for the dex instructions, single successor of the entry block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100482 HBasicBlock* block = new (arena_) HBasicBlock(graph_, 0);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100483 branch_targets_[0] = block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000484 entry_block_->AddSuccessor(block);
485
486 // Iterate over all instructions and find branching instructions. Create blocks for
487 // the locations these instructions branch to.
Andreas Gamped881df52014-11-24 23:28:39 -0800488 uint32_t dex_pc = 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000489 while (code_ptr < code_end) {
490 const Instruction& instruction = *Instruction::At(code_ptr);
491 if (instruction.IsBranch()) {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000492 (*number_of_branches)++;
Calin Juravle225ff812014-11-13 16:46:39 +0000493 int32_t target = instruction.GetTargetOffset() + dex_pc;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000494 // Create a block for the target instruction.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000495 FindOrCreateBlockStartingAt(target);
496
Calin Juravle225ff812014-11-13 16:46:39 +0000497 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000498 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle702d2602015-04-30 19:28:21 +0100499
David Brazdilfe659462015-06-24 14:23:56 +0100500 if (instruction.CanFlowThrough()) {
501 if (code_ptr >= code_end) {
Calin Juravle702d2602015-04-30 19:28:21 +0100502 // In the normal case we should never hit this but someone can artificially forge a dex
503 // file to fall-through out the method code. In this case we bail out compilation.
504 return false;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000505 } else {
506 FindOrCreateBlockStartingAt(dex_pc);
Calin Juravle702d2602015-04-30 19:28:21 +0100507 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000508 }
Andreas Gampee4d4d322014-12-04 09:09:57 -0800509 } else if (instruction.IsSwitch()) {
510 SwitchTable table(instruction, dex_pc, instruction.Opcode() == Instruction::SPARSE_SWITCH);
Andreas Gamped881df52014-11-24 23:28:39 -0800511
512 uint16_t num_entries = table.GetNumEntries();
513
Andreas Gampee4d4d322014-12-04 09:09:57 -0800514 // In a packed-switch, the entry at index 0 is the starting key. In a sparse-switch, the
515 // entry at index 0 is the first key, and values are after *all* keys.
516 size_t offset = table.GetFirstValueIndex();
517
518 // Use a larger loop counter type to avoid overflow issues.
519 for (size_t i = 0; i < num_entries; ++i) {
Andreas Gamped881df52014-11-24 23:28:39 -0800520 // The target of the case.
Andreas Gampee4d4d322014-12-04 09:09:57 -0800521 uint32_t target = dex_pc + table.GetEntryAt(i + offset);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000522 FindOrCreateBlockStartingAt(target);
Andreas Gamped881df52014-11-24 23:28:39 -0800523
David Brazdil281a6322015-07-03 10:34:57 +0100524 // Create a block for the switch-case logic. The block gets the dex_pc
525 // of the SWITCH instruction because it is part of its semantics.
526 block = new (arena_) HBasicBlock(graph_, dex_pc);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100527 branch_targets_[table.GetDexPcForIndex(i)] = block;
Andreas Gamped881df52014-11-24 23:28:39 -0800528 }
529
530 // Fall-through. Add a block if there is more code afterwards.
531 dex_pc += instruction.SizeInCodeUnits();
532 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle702d2602015-04-30 19:28:21 +0100533 if (code_ptr >= code_end) {
534 // In the normal case we should never hit this but someone can artificially forge a dex
535 // file to fall-through out the method code. In this case we bail out compilation.
536 // (A switch can fall-through so we don't need to check CanFlowThrough().)
537 return false;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000538 } else {
539 FindOrCreateBlockStartingAt(dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -0800540 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000541 } else {
542 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle225ff812014-11-13 16:46:39 +0000543 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000544 }
545 }
Calin Juravle702d2602015-04-30 19:28:21 +0100546 return true;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000547}
548
David Brazdilfc6a86a2015-06-26 10:33:45 +0000549HBasicBlock* HGraphBuilder::FindBlockStartingAt(int32_t dex_pc) const {
550 DCHECK_GE(dex_pc, 0);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100551 return branch_targets_[dex_pc];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000552}
553
554HBasicBlock* HGraphBuilder::FindOrCreateBlockStartingAt(int32_t dex_pc) {
555 HBasicBlock* block = FindBlockStartingAt(dex_pc);
556 if (block == nullptr) {
557 block = new (arena_) HBasicBlock(graph_, dex_pc);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100558 branch_targets_[dex_pc] = block;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000559 }
560 return block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000561}
562
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100563template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600564void HGraphBuilder::Unop_12x(const Instruction& instruction,
565 Primitive::Type type,
566 uint32_t dex_pc) {
567 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
568 current_block_->AddInstruction(new (arena_) T(type, first, dex_pc));
569 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Roland Levillain88cb1752014-10-20 16:36:47 +0100570}
571
Roland Levillaindff1f282014-11-05 14:15:05 +0000572void HGraphBuilder::Conversion_12x(const Instruction& instruction,
573 Primitive::Type input_type,
Roland Levillain624279f2014-12-04 11:54:28 +0000574 Primitive::Type result_type,
575 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600576 HInstruction* first = LoadLocal(instruction.VRegB(), input_type, dex_pc);
Roland Levillain624279f2014-12-04 11:54:28 +0000577 current_block_->AddInstruction(new (arena_) HTypeConversion(result_type, first, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600578 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100579}
580
581template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000582void HGraphBuilder::Binop_23x(const Instruction& instruction,
583 Primitive::Type type,
584 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600585 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
586 HInstruction* second = LoadLocal(instruction.VRegC(), type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000587 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600588 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000589}
590
591template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000592void HGraphBuilder::Binop_23x_shift(const Instruction& instruction,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600593 Primitive::Type type,
594 uint32_t dex_pc) {
595 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
596 HInstruction* second = LoadLocal(instruction.VRegC(), Primitive::kPrimInt, dex_pc);
597 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
598 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000599}
600
Calin Juravleddb7df22014-11-25 20:56:51 +0000601void HGraphBuilder::Binop_23x_cmp(const Instruction& instruction,
602 Primitive::Type type,
Mark Mendellc4701932015-04-10 13:18:51 -0400603 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700604 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600605 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
606 HInstruction* second = LoadLocal(instruction.VRegC(), type, dex_pc);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700607 current_block_->AddInstruction(new (arena_) HCompare(type, first, second, bias, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600608 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +0000609}
610
Calin Juravle9aec02f2014-11-18 23:06:35 +0000611template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600612void HGraphBuilder::Binop_12x_shift(const Instruction& instruction, Primitive::Type type,
613 uint32_t dex_pc) {
614 HInstruction* first = LoadLocal(instruction.VRegA(), type, dex_pc);
615 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
616 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
617 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000618}
619
620template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000621void HGraphBuilder::Binop_12x(const Instruction& instruction,
622 Primitive::Type type,
623 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600624 HInstruction* first = LoadLocal(instruction.VRegA(), type, dex_pc);
625 HInstruction* second = LoadLocal(instruction.VRegB(), type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000626 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600627 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000628}
629
630template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600631void HGraphBuilder::Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
632 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
633 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100634 if (reverse) {
635 std::swap(first, second);
636 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600637 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second, dex_pc));
638 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100639}
640
641template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600642void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
643 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
644 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100645 if (reverse) {
646 std::swap(first, second);
647 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600648 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second, dex_pc));
649 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100650}
651
Calin Juravle0c25d102015-04-20 14:49:09 +0100652static bool RequiresConstructorBarrier(const DexCompilationUnit* cu, const CompilerDriver& driver) {
Calin Juravle27df7582015-04-17 19:12:31 +0100653 Thread* self = Thread::Current();
Calin Juravle0c25d102015-04-20 14:49:09 +0100654 return cu->IsConstructor()
655 && driver.RequiresConstructorBarrier(self, cu->GetDexFile(), cu->GetClassDefIndex());
Calin Juravle27df7582015-04-17 19:12:31 +0100656}
657
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600658void HGraphBuilder::BuildReturn(const Instruction& instruction,
659 Primitive::Type type,
660 uint32_t dex_pc) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100661 if (type == Primitive::kPrimVoid) {
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100662 if (graph_->ShouldGenerateConstructorBarrier()) {
663 // The compilation unit is null during testing.
664 if (dex_compilation_unit_ != nullptr) {
665 DCHECK(RequiresConstructorBarrier(dex_compilation_unit_, *compiler_driver_))
666 << "Inconsistent use of ShouldGenerateConstructorBarrier. Should not generate a barrier.";
667 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600668 current_block_->AddInstruction(new (arena_) HMemoryBarrier(kStoreStore, dex_pc));
Calin Juravle27df7582015-04-17 19:12:31 +0100669 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600670 current_block_->AddInstruction(new (arena_) HReturnVoid(dex_pc));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100671 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600672 HInstruction* value = LoadLocal(instruction.VRegA(), type, dex_pc);
673 current_block_->AddInstruction(new (arena_) HReturn(value, dex_pc));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100674 }
675 current_block_->AddSuccessor(exit_block_);
676 current_block_ = nullptr;
677}
678
Calin Juravle68ad6492015-08-18 17:08:12 +0100679static InvokeType GetInvokeTypeFromOpCode(Instruction::Code opcode) {
680 switch (opcode) {
681 case Instruction::INVOKE_STATIC:
682 case Instruction::INVOKE_STATIC_RANGE:
683 return kStatic;
684 case Instruction::INVOKE_DIRECT:
685 case Instruction::INVOKE_DIRECT_RANGE:
686 return kDirect;
687 case Instruction::INVOKE_VIRTUAL:
688 case Instruction::INVOKE_VIRTUAL_QUICK:
689 case Instruction::INVOKE_VIRTUAL_RANGE:
690 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
691 return kVirtual;
692 case Instruction::INVOKE_INTERFACE:
693 case Instruction::INVOKE_INTERFACE_RANGE:
694 return kInterface;
695 case Instruction::INVOKE_SUPER_RANGE:
696 case Instruction::INVOKE_SUPER:
697 return kSuper;
698 default:
699 LOG(FATAL) << "Unexpected invoke opcode: " << opcode;
700 UNREACHABLE();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100701 }
Calin Juravle68ad6492015-08-18 17:08:12 +0100702}
703
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000704ArtMethod* HGraphBuilder::ResolveMethod(uint16_t method_idx, InvokeType invoke_type) {
705 ScopedObjectAccess soa(Thread::Current());
Alex Lightfedd91d2016-01-07 14:49:16 -0800706 StackHandleScope<3> hs(soa.Self());
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000707
708 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
709 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
710 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
711 Handle<mirror::Class> compiling_class(hs.NewHandle(GetCompilingClass()));
712
Andreas Gampedae24142015-12-03 17:27:32 -0800713 ArtMethod* resolved_method = class_linker->ResolveMethod<ClassLinker::kForceICCECheck>(
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000714 *dex_compilation_unit_->GetDexFile(),
715 method_idx,
716 dex_compilation_unit_->GetDexCache(),
717 class_loader,
718 /* referrer */ nullptr,
719 invoke_type);
720
721 if (UNLIKELY(resolved_method == nullptr)) {
722 // Clean up any exception left by type resolution.
723 soa.Self()->ClearException();
724 return nullptr;
725 }
726
727 // Check access. The class linker has a fast path for looking into the dex cache
728 // and does not check the access if it hits it.
729 if (compiling_class.Get() == nullptr) {
730 if (!resolved_method->IsPublic()) {
731 return nullptr;
732 }
733 } else if (!compiling_class->CanAccessResolvedMethod(resolved_method->GetDeclaringClass(),
734 resolved_method,
735 dex_compilation_unit_->GetDexCache().Get(),
736 method_idx)) {
737 return nullptr;
738 }
739
740 // We have to special case the invoke-super case, as ClassLinker::ResolveMethod does not.
Alex Lightfedd91d2016-01-07 14:49:16 -0800741 // We need to look at the referrer's super class vtable. We need to do this to know if we need to
742 // make this an invoke-unresolved to handle cross-dex invokes or abstract super methods, both of
743 // which require runtime handling.
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000744 if (invoke_type == kSuper) {
745 if (compiling_class.Get() == nullptr) {
Alex Lightfedd91d2016-01-07 14:49:16 -0800746 // We could not determine the method's class we need to wait until runtime.
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000747 DCHECK(Runtime::Current()->IsAotCompiler());
748 return nullptr;
749 }
Alex Lightfedd91d2016-01-07 14:49:16 -0800750 ArtMethod* current_method = graph_->GetArtMethod();
751 DCHECK(current_method != nullptr);
752 Handle<mirror::Class> methods_class(hs.NewHandle(
753 dex_compilation_unit_->GetClassLinker()->ResolveReferencedClassOfMethod(Thread::Current(),
754 method_idx,
755 current_method)));
756 if (methods_class.Get() == nullptr) {
757 // Invoking a super method requires knowing the actual super class. If we did not resolve
758 // the compiling method's declaring class (which only happens for ahead of time
759 // compilation), bail out.
760 DCHECK(Runtime::Current()->IsAotCompiler());
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000761 return nullptr;
Alex Lightfedd91d2016-01-07 14:49:16 -0800762 } else {
763 ArtMethod* actual_method;
764 if (methods_class->IsInterface()) {
765 actual_method = methods_class->FindVirtualMethodForInterfaceSuper(
766 resolved_method, class_linker->GetImagePointerSize());
767 } else {
768 uint16_t vtable_index = resolved_method->GetMethodIndex();
769 actual_method = compiling_class->GetSuperClass()->GetVTableEntry(
770 vtable_index, class_linker->GetImagePointerSize());
771 }
772 if (actual_method != resolved_method &&
773 !IsSameDexFile(*actual_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
774 // The back-end code generator relies on this check in order to ensure that it will not
775 // attempt to read the dex_cache with a dex_method_index that is not from the correct
776 // dex_file. If we didn't do this check then the dex_method_index will not be updated in the
777 // builder, which means that the code-generator (and compiler driver during sharpening and
778 // inliner, maybe) might invoke an incorrect method.
779 // TODO: The actual method could still be referenced in the current dex file, so we
780 // could try locating it.
781 // TODO: Remove the dex_file restriction.
782 return nullptr;
783 }
784 if (!actual_method->IsInvokable()) {
785 // Fail if the actual method cannot be invoked. Otherwise, the runtime resolution stub
786 // could resolve the callee to the wrong method.
787 return nullptr;
788 }
789 resolved_method = actual_method;
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000790 }
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000791 }
792
793 // Check for incompatible class changes. The class linker has a fast path for
794 // looking into the dex cache and does not check incompatible class changes if it hits it.
795 if (resolved_method->CheckIncompatibleClassChange(invoke_type)) {
796 return nullptr;
797 }
798
799 return resolved_method;
800}
801
Calin Juravle68ad6492015-08-18 17:08:12 +0100802bool HGraphBuilder::BuildInvoke(const Instruction& instruction,
803 uint32_t dex_pc,
804 uint32_t method_idx,
805 uint32_t number_of_vreg_arguments,
806 bool is_range,
807 uint32_t* args,
808 uint32_t register_index) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000809 InvokeType invoke_type = GetInvokeTypeFromOpCode(instruction.Opcode());
Calin Juravle68ad6492015-08-18 17:08:12 +0100810 const char* descriptor = dex_file_->GetMethodShorty(method_idx);
811 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
812
813 // Remove the return type from the 'proto'.
814 size_t number_of_arguments = strlen(descriptor) - 1;
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000815 if (invoke_type != kStatic) { // instance call
Calin Juravle68ad6492015-08-18 17:08:12 +0100816 // One extra argument for 'this'.
817 number_of_arguments++;
818 }
819
820 MethodReference target_method(dex_file_, method_idx);
Calin Juravle68ad6492015-08-18 17:08:12 +0100821
Calin Juravle5d01db12015-08-25 15:02:42 +0100822 // Special handling for string init.
823 int32_t string_init_offset = 0;
824 bool is_string_init = compiler_driver_->IsStringInit(method_idx,
825 dex_file_,
826 &string_init_offset);
827 // Replace calls to String.<init> with StringFactory.
828 if (is_string_init) {
Vladimir Markodc151b22015-10-15 18:02:30 +0100829 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
830 HInvokeStaticOrDirect::MethodLoadKind::kStringInit,
831 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
832 dchecked_integral_cast<uint64_t>(string_init_offset),
833 0U
834 };
Calin Juravle5d01db12015-08-25 15:02:42 +0100835 HInvoke* invoke = new (arena_) HInvokeStaticOrDirect(
836 arena_,
837 number_of_arguments - 1,
838 Primitive::kPrimNot /*return_type */,
839 dex_pc,
840 method_idx,
841 target_method,
842 dispatch_info,
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000843 invoke_type,
Calin Juravle5d01db12015-08-25 15:02:42 +0100844 kStatic /* optimized_invoke_type */,
845 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit);
846 return HandleStringInit(invoke,
847 number_of_vreg_arguments,
848 args,
849 register_index,
850 is_range,
851 descriptor);
852 }
853
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000854 ArtMethod* resolved_method = ResolveMethod(method_idx, invoke_type);
855
Alex Lightfedd91d2016-01-07 14:49:16 -0800856 if (UNLIKELY(resolved_method == nullptr)) {
Calin Juravle175dc732015-08-25 15:42:32 +0100857 MaybeRecordStat(MethodCompilationStat::kUnresolvedMethod);
858 HInvoke* invoke = new (arena_) HInvokeUnresolved(arena_,
859 number_of_arguments,
860 return_type,
861 dex_pc,
862 method_idx,
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000863 invoke_type);
Calin Juravle175dc732015-08-25 15:42:32 +0100864 return HandleInvoke(invoke,
865 number_of_vreg_arguments,
866 args,
867 register_index,
868 is_range,
869 descriptor,
870 nullptr /* clinit_check */);
Calin Juravle68ad6492015-08-18 17:08:12 +0100871 }
872
Calin Juravle68ad6492015-08-18 17:08:12 +0100873 // Potential class initialization check, in the case of a static method call.
874 HClinitCheck* clinit_check = nullptr;
875 HInvoke* invoke = nullptr;
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000876 if (invoke_type == kDirect || invoke_type == kStatic || invoke_type == kSuper) {
Calin Juravle68ad6492015-08-18 17:08:12 +0100877 // By default, consider that the called method implicitly requires
878 // an initialization check of its declaring method.
879 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement
880 = HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000881 ScopedObjectAccess soa(Thread::Current());
882 if (invoke_type == kStatic) {
883 clinit_check = ProcessClinitCheckForInvoke(
884 dex_pc, resolved_method, method_idx, &clinit_check_requirement);
885 } else if (invoke_type == kSuper) {
886 if (IsSameDexFile(*resolved_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
887 // Update the target method to the one resolved. Note that this may be a no-op if
888 // we resolved to the method referenced by the instruction.
889 method_idx = resolved_method->GetDexMethodIndex();
890 target_method = MethodReference(dex_file_, method_idx);
891 }
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100892 }
Calin Juravle68ad6492015-08-18 17:08:12 +0100893
Vladimir Markodc151b22015-10-15 18:02:30 +0100894 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
895 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
896 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
897 0u,
898 0U
899 };
Calin Juravle68ad6492015-08-18 17:08:12 +0100900 invoke = new (arena_) HInvokeStaticOrDirect(arena_,
901 number_of_arguments,
902 return_type,
903 dex_pc,
904 method_idx,
905 target_method,
906 dispatch_info,
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000907 invoke_type,
908 invoke_type,
Calin Juravle68ad6492015-08-18 17:08:12 +0100909 clinit_check_requirement);
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000910 } else if (invoke_type == kVirtual) {
911 ScopedObjectAccess soa(Thread::Current()); // Needed for the method index
Calin Juravle68ad6492015-08-18 17:08:12 +0100912 invoke = new (arena_) HInvokeVirtual(arena_,
913 number_of_arguments,
914 return_type,
915 dex_pc,
916 method_idx,
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000917 resolved_method->GetMethodIndex());
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100918 } else {
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000919 DCHECK_EQ(invoke_type, kInterface);
920 ScopedObjectAccess soa(Thread::Current()); // Needed for the method index
Calin Juravle68ad6492015-08-18 17:08:12 +0100921 invoke = new (arena_) HInvokeInterface(arena_,
922 number_of_arguments,
923 return_type,
924 dex_pc,
925 method_idx,
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000926 resolved_method->GetDexMethodIndex());
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100927 }
Calin Juravle68ad6492015-08-18 17:08:12 +0100928
Calin Juravle5d01db12015-08-25 15:02:42 +0100929 return HandleInvoke(invoke,
930 number_of_vreg_arguments,
931 args,
932 register_index,
933 is_range,
934 descriptor,
935 clinit_check);
Calin Juravle68ad6492015-08-18 17:08:12 +0100936}
937
Nicolas Geoffray729645a2015-11-19 13:29:02 +0000938bool HGraphBuilder::BuildNewInstance(uint16_t type_index, uint32_t dex_pc) {
939 bool finalizable;
940 bool can_throw = NeedsAccessCheck(type_index, &finalizable);
941
942 // Only the non-resolved entrypoint handles the finalizable class case. If we
943 // need access checks, then we haven't resolved the method and the class may
944 // again be finalizable.
945 QuickEntrypointEnum entrypoint = (finalizable || can_throw)
946 ? kQuickAllocObject
947 : kQuickAllocObjectInitialized;
948
949 ScopedObjectAccess soa(Thread::Current());
950 StackHandleScope<3> hs(soa.Self());
951 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
952 dex_compilation_unit_->GetClassLinker()->FindDexCache(
953 soa.Self(), *dex_compilation_unit_->GetDexFile())));
954 Handle<mirror::Class> resolved_class(hs.NewHandle(dex_cache->GetResolvedType(type_index)));
955 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
956 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
957 outer_compilation_unit_->GetClassLinker()->FindDexCache(soa.Self(), outer_dex_file)));
958
959 if (outer_dex_cache.Get() != dex_cache.Get()) {
960 // We currently do not support inlining allocations across dex files.
961 return false;
962 }
963
964 HLoadClass* load_class = new (arena_) HLoadClass(
965 graph_->GetCurrentMethod(),
966 type_index,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +0000967 outer_dex_file,
Nicolas Geoffray729645a2015-11-19 13:29:02 +0000968 IsOutermostCompilingClass(type_index),
969 dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +0000970 /*needs_access_check*/ can_throw,
971 compiler_driver_->CanAssumeTypeIsPresentInDexCache(outer_dex_file, type_index));
Nicolas Geoffray729645a2015-11-19 13:29:02 +0000972
973 current_block_->AddInstruction(load_class);
974 HInstruction* cls = load_class;
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +0000975 if (!IsInitialized(resolved_class)) {
Nicolas Geoffray729645a2015-11-19 13:29:02 +0000976 cls = new (arena_) HClinitCheck(load_class, dex_pc);
977 current_block_->AddInstruction(cls);
978 }
979
980 current_block_->AddInstruction(new (arena_) HNewInstance(
981 cls,
982 graph_->GetCurrentMethod(),
983 dex_pc,
984 type_index,
985 *dex_compilation_unit_->GetDexFile(),
986 can_throw,
987 finalizable,
988 entrypoint));
989 return true;
990}
991
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +0000992static bool IsSubClass(mirror::Class* to_test, mirror::Class* super_class)
993 SHARED_REQUIRES(Locks::mutator_lock_) {
994 return to_test != nullptr && !to_test->IsInterface() && to_test->IsSubClass(super_class);
995}
996
997bool HGraphBuilder::IsInitialized(Handle<mirror::Class> cls) const {
Nicolas Geoffray729645a2015-11-19 13:29:02 +0000998 if (cls.Get() == nullptr) {
999 return false;
1000 }
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +00001001
1002 // `CanAssumeClassIsLoaded` will return true if we're JITting, or will
1003 // check whether the class is in an image for the AOT compilation.
1004 if (cls->IsInitialized() &&
1005 compiler_driver_->CanAssumeClassIsLoaded(cls.Get())) {
Nicolas Geoffray729645a2015-11-19 13:29:02 +00001006 return true;
1007 }
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +00001008
1009 if (IsSubClass(GetOutermostCompilingClass(), cls.Get())) {
1010 return true;
1011 }
1012
1013 // TODO: We should walk over the inlined methods, but we don't pass
1014 // that information to the builder.
1015 if (IsSubClass(GetCompilingClass(), cls.Get())) {
1016 return true;
1017 }
1018
1019 return false;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00001020}
1021
Calin Juravle68ad6492015-08-18 17:08:12 +01001022HClinitCheck* HGraphBuilder::ProcessClinitCheckForInvoke(
1023 uint32_t dex_pc,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001024 ArtMethod* resolved_method,
Calin Juravle68ad6492015-08-18 17:08:12 +01001025 uint32_t method_idx,
1026 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001027 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
1028 Thread* self = Thread::Current();
1029 StackHandleScope<4> hs(self);
Calin Juravle68ad6492015-08-18 17:08:12 +01001030 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
1031 dex_compilation_unit_->GetClassLinker()->FindDexCache(
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001032 self, *dex_compilation_unit_->GetDexFile())));
Calin Juravle68ad6492015-08-18 17:08:12 +01001033 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001034 outer_compilation_unit_->GetClassLinker()->FindDexCache(
1035 self, outer_dex_file)));
Calin Juravle68ad6492015-08-18 17:08:12 +01001036 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +00001037 Handle<mirror::Class> resolved_method_class(hs.NewHandle(resolved_method->GetDeclaringClass()));
Calin Juravle68ad6492015-08-18 17:08:12 +01001038
1039 // The index at which the method's class is stored in the DexCache's type array.
1040 uint32_t storage_index = DexFile::kDexNoIndex;
1041 bool is_outer_class = (resolved_method->GetDeclaringClass() == outer_class.Get());
1042 if (is_outer_class) {
1043 storage_index = outer_class->GetDexTypeIndex();
1044 } else if (outer_dex_cache.Get() == dex_cache.Get()) {
1045 // Get `storage_index` from IsClassOfStaticMethodAvailableToReferrer.
1046 compiler_driver_->IsClassOfStaticMethodAvailableToReferrer(outer_dex_cache.Get(),
1047 GetCompilingClass(),
1048 resolved_method,
1049 method_idx,
1050 &storage_index);
1051 }
1052
1053 HClinitCheck* clinit_check = nullptr;
1054
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +00001055 if (IsInitialized(resolved_method_class)) {
Calin Juravle68ad6492015-08-18 17:08:12 +01001056 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
1057 } else if (storage_index != DexFile::kDexNoIndex) {
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +00001058 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
1059 HLoadClass* load_class = new (arena_) HLoadClass(
1060 graph_->GetCurrentMethod(),
1061 storage_index,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001062 outer_dex_file,
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +00001063 is_outer_class,
1064 dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001065 /*needs_access_check*/ false,
1066 compiler_driver_->CanAssumeTypeIsPresentInDexCache(outer_dex_file, storage_index));
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +00001067 current_block_->AddInstruction(load_class);
1068 clinit_check = new (arena_) HClinitCheck(load_class, dex_pc);
1069 current_block_->AddInstruction(clinit_check);
Calin Juravle68ad6492015-08-18 17:08:12 +01001070 }
1071 return clinit_check;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001072}
1073
Calin Juravle5d01db12015-08-25 15:02:42 +01001074bool HGraphBuilder::SetupInvokeArguments(HInvoke* invoke,
1075 uint32_t number_of_vreg_arguments,
1076 uint32_t* args,
1077 uint32_t register_index,
1078 bool is_range,
1079 const char* descriptor,
1080 size_t start_index,
1081 size_t* argument_index) {
Calin Juravle68ad6492015-08-18 17:08:12 +01001082 uint32_t descriptor_index = 1; // Skip the return type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001083 uint32_t dex_pc = invoke->GetDexPc();
Calin Juravle68ad6492015-08-18 17:08:12 +01001084
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001085 for (size_t i = start_index;
1086 // Make sure we don't go over the expected arguments or over the number of
1087 // dex registers given. If the instruction was seen as dead by the verifier,
1088 // it hasn't been properly checked.
Calin Juravle5d01db12015-08-25 15:02:42 +01001089 (i < number_of_vreg_arguments) && (*argument_index < invoke->GetNumberOfArguments());
1090 i++, (*argument_index)++) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001091 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001092 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001093 if (!is_range
1094 && is_wide
1095 && ((i + 1 == number_of_vreg_arguments) || (args[i] + 1 != args[i + 1]))) {
1096 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
1097 // reject any class where this is violated. However, the verifier only does these checks
1098 // on non trivially dead instructions, so we just bailout the compilation.
1099 VLOG(compiler) << "Did not compile "
1100 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
1101 << " because of non-sequential dex register pair in wide argument";
1102 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1103 return false;
1104 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001105 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type, dex_pc);
Calin Juravle5d01db12015-08-25 15:02:42 +01001106 invoke->SetArgumentAt(*argument_index, arg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001107 if (is_wide) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001108 i++;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001109 }
1110 }
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001111
Calin Juravle5d01db12015-08-25 15:02:42 +01001112 if (*argument_index != invoke->GetNumberOfArguments()) {
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001113 VLOG(compiler) << "Did not compile "
1114 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
1115 << " because of wrong number of arguments in invoke instruction";
1116 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1117 return false;
1118 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001119
Vladimir Markob554b5a2015-11-06 12:57:55 +00001120 if (invoke->IsInvokeStaticOrDirect() &&
1121 HInvokeStaticOrDirect::NeedsCurrentMethodInput(
1122 invoke->AsInvokeStaticOrDirect()->GetMethodLoadKind())) {
Calin Juravle5d01db12015-08-25 15:02:42 +01001123 invoke->SetArgumentAt(*argument_index, graph_->GetCurrentMethod());
1124 (*argument_index)++;
1125 }
1126
1127 return true;
1128}
1129
1130bool HGraphBuilder::HandleInvoke(HInvoke* invoke,
1131 uint32_t number_of_vreg_arguments,
1132 uint32_t* args,
1133 uint32_t register_index,
1134 bool is_range,
1135 const char* descriptor,
1136 HClinitCheck* clinit_check) {
1137 DCHECK(!invoke->IsInvokeStaticOrDirect() || !invoke->AsInvokeStaticOrDirect()->IsStringInit());
1138
1139 size_t start_index = 0;
1140 size_t argument_index = 0;
1141 if (invoke->GetOriginalInvokeType() != InvokeType::kStatic) { // Instance call.
Calin Juravle5d01db12015-08-25 15:02:42 +01001142 HInstruction* arg = LoadLocal(
1143 is_range ? register_index : args[0], Primitive::kPrimNot, invoke->GetDexPc());
1144 HNullCheck* null_check = new (arena_) HNullCheck(arg, invoke->GetDexPc());
1145 current_block_->AddInstruction(null_check);
Calin Juravle5d01db12015-08-25 15:02:42 +01001146 invoke->SetArgumentAt(0, null_check);
1147 start_index = 1;
1148 argument_index = 1;
1149 }
1150
1151 if (!SetupInvokeArguments(invoke,
1152 number_of_vreg_arguments,
1153 args,
1154 register_index,
1155 is_range,
1156 descriptor,
1157 start_index,
1158 &argument_index)) {
1159 return false;
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001160 }
1161
Calin Juravle68ad6492015-08-18 17:08:12 +01001162 if (clinit_check != nullptr) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001163 // Add the class initialization check as last input of `invoke`.
Calin Juravle68ad6492015-08-18 17:08:12 +01001164 DCHECK(invoke->IsInvokeStaticOrDirect());
1165 DCHECK(invoke->AsInvokeStaticOrDirect()->GetClinitCheckRequirement()
1166 == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit);
Roland Levillain3e3d7332015-04-28 11:00:54 +01001167 invoke->SetArgumentAt(argument_index, clinit_check);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001168 argument_index++;
Roland Levillain4c0eb422015-04-24 16:43:49 +01001169 }
1170
Calin Juravle5d01db12015-08-25 15:02:42 +01001171 current_block_->AddInstruction(invoke);
1172 latest_result_ = invoke;
1173
1174 return true;
1175}
1176
1177bool HGraphBuilder::HandleStringInit(HInvoke* invoke,
1178 uint32_t number_of_vreg_arguments,
1179 uint32_t* args,
1180 uint32_t register_index,
1181 bool is_range,
1182 const char* descriptor) {
1183 DCHECK(invoke->IsInvokeStaticOrDirect());
1184 DCHECK(invoke->AsInvokeStaticOrDirect()->IsStringInit());
1185
1186 size_t start_index = 1;
1187 size_t argument_index = 0;
1188 if (!SetupInvokeArguments(invoke,
1189 number_of_vreg_arguments,
1190 args,
1191 register_index,
1192 is_range,
1193 descriptor,
1194 start_index,
1195 &argument_index)) {
1196 return false;
Jeff Hao848f70a2014-01-15 13:49:50 -08001197 }
Calin Juravle0eedd7e2015-08-20 14:48:00 +01001198
Calin Juravle5d01db12015-08-25 15:02:42 +01001199 // Add move-result for StringFactory method.
1200 uint32_t orig_this_reg = is_range ? register_index : args[0];
David Brazdil6de19382016-01-08 17:37:10 +00001201 HInstruction* new_instance = LoadLocal(orig_this_reg, Primitive::kPrimNot, invoke->GetDexPc());
1202 invoke->SetArgumentAt(argument_index, new_instance);
Calin Juravle5d01db12015-08-25 15:02:42 +01001203 current_block_->AddInstruction(invoke);
Calin Juravle5d01db12015-08-25 15:02:42 +01001204
Calin Juravle0eedd7e2015-08-20 14:48:00 +01001205 latest_result_ = invoke;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001206 return true;
1207}
1208
Calin Juravlee460d1d2015-09-29 04:52:17 +01001209static Primitive::Type GetFieldAccessType(const DexFile& dex_file, uint16_t field_index) {
1210 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
1211 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
1212 return Primitive::GetType(type[0]);
1213}
1214
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001215bool HGraphBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001216 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001217 bool is_put) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001218 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1219 uint32_t obj_reg = instruction.VRegB_22c();
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001220 uint16_t field_index;
1221 if (instruction.IsQuickened()) {
1222 if (!CanDecodeQuickenedInfo()) {
1223 return false;
1224 }
1225 field_index = LookupQuickenedInfo(dex_pc);
1226 } else {
1227 field_index = instruction.VRegC_22c();
1228 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001229
1230 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierc7853442015-03-27 14:35:38 -07001231 ArtField* resolved_field =
1232 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001233
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001234
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001235 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot, dex_pc);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001236 HInstruction* null_check = new (arena_) HNullCheck(object, dex_pc);
1237 current_block_->AddInstruction(null_check);
1238
1239 Primitive::Type field_type = (resolved_field == nullptr)
1240 ? GetFieldAccessType(*dex_file_, field_index)
1241 : resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001242 if (is_put) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001243 HInstruction* value = LoadLocal(source_or_dest_reg, field_type, dex_pc);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001244 HInstruction* field_set = nullptr;
1245 if (resolved_field == nullptr) {
1246 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
1247 field_set = new (arena_) HUnresolvedInstanceFieldSet(null_check,
1248 value,
1249 field_type,
1250 field_index,
1251 dex_pc);
1252 } else {
Mingyao Yang8df69d42015-10-22 15:40:58 -07001253 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Calin Juravlee460d1d2015-09-29 04:52:17 +01001254 field_set = new (arena_) HInstanceFieldSet(null_check,
1255 value,
1256 field_type,
1257 resolved_field->GetOffset(),
1258 resolved_field->IsVolatile(),
1259 field_index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07001260 class_def_index,
Calin Juravlee460d1d2015-09-29 04:52:17 +01001261 *dex_file_,
1262 dex_compilation_unit_->GetDexCache(),
1263 dex_pc);
1264 }
1265 current_block_->AddInstruction(field_set);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001266 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001267 HInstruction* field_get = nullptr;
1268 if (resolved_field == nullptr) {
1269 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
1270 field_get = new (arena_) HUnresolvedInstanceFieldGet(null_check,
1271 field_type,
1272 field_index,
1273 dex_pc);
1274 } else {
Mingyao Yang8df69d42015-10-22 15:40:58 -07001275 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Calin Juravlee460d1d2015-09-29 04:52:17 +01001276 field_get = new (arena_) HInstanceFieldGet(null_check,
1277 field_type,
1278 resolved_field->GetOffset(),
1279 resolved_field->IsVolatile(),
1280 field_index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07001281 class_def_index,
Calin Juravlee460d1d2015-09-29 04:52:17 +01001282 *dex_file_,
1283 dex_compilation_unit_->GetDexCache(),
1284 dex_pc);
1285 }
1286 current_block_->AddInstruction(field_get);
1287 UpdateLocal(source_or_dest_reg, field_get, dex_pc);
Calin Juravlee6f49b42015-09-17 14:04:33 +00001288 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001289
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001290 return true;
1291}
1292
Nicolas Geoffray30451742015-06-19 13:32:41 +01001293static mirror::Class* GetClassFrom(CompilerDriver* driver,
1294 const DexCompilationUnit& compilation_unit) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001295 ScopedObjectAccess soa(Thread::Current());
1296 StackHandleScope<2> hs(soa.Self());
Nicolas Geoffray30451742015-06-19 13:32:41 +01001297 const DexFile& dex_file = *compilation_unit.GetDexFile();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001298 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Nicolas Geoffray30451742015-06-19 13:32:41 +01001299 soa.Decode<mirror::ClassLoader*>(compilation_unit.GetClassLoader())));
1300 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001301 compilation_unit.GetClassLinker()->FindDexCache(soa.Self(), dex_file)));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001302
Nicolas Geoffray30451742015-06-19 13:32:41 +01001303 return driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, &compilation_unit);
1304}
1305
1306mirror::Class* HGraphBuilder::GetOutermostCompilingClass() const {
1307 return GetClassFrom(compiler_driver_, *outer_compilation_unit_);
1308}
1309
1310mirror::Class* HGraphBuilder::GetCompilingClass() const {
1311 return GetClassFrom(compiler_driver_, *dex_compilation_unit_);
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001312}
1313
1314bool HGraphBuilder::IsOutermostCompilingClass(uint16_t type_index) const {
1315 ScopedObjectAccess soa(Thread::Current());
1316 StackHandleScope<4> hs(soa.Self());
1317 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001318 dex_compilation_unit_->GetClassLinker()->FindDexCache(
1319 soa.Self(), *dex_compilation_unit_->GetDexFile())));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001320 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1321 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
1322 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
1323 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
Nicolas Geoffrayafd06412015-06-20 22:44:47 +01001324 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001325
Calin Juravle4e2a5572015-10-07 18:55:43 +01001326 // GetOutermostCompilingClass returns null when the class is unresolved
1327 // (e.g. if it derives from an unresolved class). This is bogus knowing that
1328 // we are compiling it.
1329 // When this happens we cannot establish a direct relation between the current
1330 // class and the outer class, so we return false.
1331 // (Note that this is only used for optimizing invokes and field accesses)
1332 return (cls.Get() != nullptr) && (outer_class.Get() == cls.Get());
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001333}
1334
Calin Juravle07380a22015-09-17 14:15:12 +01001335void HGraphBuilder::BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
1336 uint32_t dex_pc,
1337 bool is_put,
1338 Primitive::Type field_type) {
1339 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1340 uint16_t field_index = instruction.VRegB_21c();
1341
1342 if (is_put) {
1343 HInstruction* value = LoadLocal(source_or_dest_reg, field_type, dex_pc);
1344 current_block_->AddInstruction(
1345 new (arena_) HUnresolvedStaticFieldSet(value, field_type, field_index, dex_pc));
1346 } else {
1347 current_block_->AddInstruction(
1348 new (arena_) HUnresolvedStaticFieldGet(field_type, field_index, dex_pc));
1349 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction(), dex_pc);
1350 }
1351}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001352bool HGraphBuilder::BuildStaticFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001353 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001354 bool is_put) {
1355 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1356 uint16_t field_index = instruction.VRegB_21c();
1357
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001358 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray729645a2015-11-19 13:29:02 +00001359 StackHandleScope<5> hs(soa.Self());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001360 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001361 dex_compilation_unit_->GetClassLinker()->FindDexCache(
1362 soa.Self(), *dex_compilation_unit_->GetDexFile())));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001363 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1364 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
Mathieu Chartierc7853442015-03-27 14:35:38 -07001365 ArtField* resolved_field = compiler_driver_->ResolveField(
1366 soa, dex_cache, class_loader, dex_compilation_unit_, field_index, true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001367
Mathieu Chartierc7853442015-03-27 14:35:38 -07001368 if (resolved_field == nullptr) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001369 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
1370 Primitive::Type field_type = GetFieldAccessType(*dex_file_, field_index);
Calin Juravle07380a22015-09-17 14:15:12 +01001371 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001372 return true;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001373 }
1374
Calin Juravle07380a22015-09-17 14:15:12 +01001375 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001376 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
1377 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001378 outer_compilation_unit_->GetClassLinker()->FindDexCache(soa.Self(), outer_dex_file)));
Nicolas Geoffray30451742015-06-19 13:32:41 +01001379 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001380
1381 // The index at which the field's class is stored in the DexCache's type array.
1382 uint32_t storage_index;
Nicolas Geoffray30451742015-06-19 13:32:41 +01001383 bool is_outer_class = (outer_class.Get() == resolved_field->GetDeclaringClass());
1384 if (is_outer_class) {
1385 storage_index = outer_class->GetDexTypeIndex();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001386 } else if (outer_dex_cache.Get() != dex_cache.Get()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001387 // The compiler driver cannot currently understand multiple dex caches involved. Just bailout.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001388 return false;
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001389 } else {
Calin Juravle07380a22015-09-17 14:15:12 +01001390 // TODO: This is rather expensive. Perf it and cache the results if needed.
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001391 std::pair<bool, bool> pair = compiler_driver_->IsFastStaticField(
1392 outer_dex_cache.Get(),
Nicolas Geoffray30451742015-06-19 13:32:41 +01001393 GetCompilingClass(),
Mathieu Chartierc7853442015-03-27 14:35:38 -07001394 resolved_field,
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001395 field_index,
1396 &storage_index);
1397 bool can_easily_access = is_put ? pair.second : pair.first;
1398 if (!can_easily_access) {
Calin Juravle07380a22015-09-17 14:15:12 +01001399 MaybeRecordStat(MethodCompilationStat::kUnresolvedFieldNotAFastAccess);
1400 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
1401 return true;
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001402 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001403 }
1404
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001405 bool is_in_cache =
1406 compiler_driver_->CanAssumeTypeIsPresentInDexCache(outer_dex_file, storage_index);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001407 HLoadClass* constant = new (arena_) HLoadClass(graph_->GetCurrentMethod(),
1408 storage_index,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001409 outer_dex_file,
Nicolas Geoffray30451742015-06-19 13:32:41 +01001410 is_outer_class,
Calin Juravle98893e12015-10-02 21:05:03 +01001411 dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001412 /*needs_access_check*/ false,
1413 is_in_cache);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001414 current_block_->AddInstruction(constant);
1415
1416 HInstruction* cls = constant;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00001417
1418 Handle<mirror::Class> klass(hs.NewHandle(resolved_field->GetDeclaringClass()));
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +00001419 if (!IsInitialized(klass)) {
Calin Juravle225ff812014-11-13 16:46:39 +00001420 cls = new (arena_) HClinitCheck(constant, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001421 current_block_->AddInstruction(cls);
1422 }
Mingyao Yang8df69d42015-10-22 15:40:58 -07001423
Nicolas Geoffray729645a2015-11-19 13:29:02 +00001424 uint16_t class_def_index = klass->GetDexClassDefIndex();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001425 if (is_put) {
1426 // We need to keep the class alive before loading the value.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001427 HInstruction* value = LoadLocal(source_or_dest_reg, field_type, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001428 DCHECK_EQ(value->GetType(), field_type);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001429 current_block_->AddInstruction(new (arena_) HStaticFieldSet(cls,
1430 value,
1431 field_type,
1432 resolved_field->GetOffset(),
1433 resolved_field->IsVolatile(),
1434 field_index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07001435 class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07001436 *dex_file_,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001437 dex_cache_,
1438 dex_pc));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001439 } else {
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001440 current_block_->AddInstruction(new (arena_) HStaticFieldGet(cls,
1441 field_type,
1442 resolved_field->GetOffset(),
1443 resolved_field->IsVolatile(),
1444 field_index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07001445 class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07001446 *dex_file_,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001447 dex_cache_,
1448 dex_pc));
1449 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001450 }
1451 return true;
1452}
1453
Calin Juravlebacfec32014-11-14 15:54:36 +00001454void HGraphBuilder::BuildCheckedDivRem(uint16_t out_vreg,
1455 uint16_t first_vreg,
1456 int64_t second_vreg_or_constant,
1457 uint32_t dex_pc,
1458 Primitive::Type type,
1459 bool second_is_constant,
1460 bool isDiv) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001461 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Calin Juravled0d48522014-11-04 16:40:20 +00001462
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001463 HInstruction* first = LoadLocal(first_vreg, type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001464 HInstruction* second = nullptr;
1465 if (second_is_constant) {
1466 if (type == Primitive::kPrimInt) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001467 second = graph_->GetIntConstant(second_vreg_or_constant, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001468 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001469 second = graph_->GetLongConstant(second_vreg_or_constant, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001470 }
1471 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001472 second = LoadLocal(second_vreg_or_constant, type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001473 }
1474
1475 if (!second_is_constant
1476 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0)
1477 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) {
1478 second = new (arena_) HDivZeroCheck(second, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001479 current_block_->AddInstruction(second);
Calin Juravled0d48522014-11-04 16:40:20 +00001480 }
1481
Calin Juravlebacfec32014-11-14 15:54:36 +00001482 if (isDiv) {
1483 current_block_->AddInstruction(new (arena_) HDiv(type, first, second, dex_pc));
1484 } else {
1485 current_block_->AddInstruction(new (arena_) HRem(type, first, second, dex_pc));
1486 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001487 UpdateLocal(out_vreg, current_block_->GetLastInstruction(), dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001488}
1489
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001490void HGraphBuilder::BuildArrayAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001491 uint32_t dex_pc,
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001492 bool is_put,
1493 Primitive::Type anticipated_type) {
1494 uint8_t source_or_dest_reg = instruction.VRegA_23x();
1495 uint8_t array_reg = instruction.VRegB_23x();
1496 uint8_t index_reg = instruction.VRegC_23x();
1497
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001498 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001499 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001500 current_block_->AddInstruction(object);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001501
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001502 HInstruction* length = new (arena_) HArrayLength(object, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001503 current_block_->AddInstruction(length);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001504 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001505 index = new (arena_) HBoundsCheck(index, length, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001506 current_block_->AddInstruction(index);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001507 if (is_put) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001508 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001509 // TODO: Insert a type check node if the type is Object.
Nicolas Geoffray39468442014-09-02 15:17:15 +01001510 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001511 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001512 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001513 current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type, dex_pc));
1514 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001515 }
Mark Mendell1152c922015-04-24 17:06:35 -04001516 graph_->SetHasBoundsChecks(true);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001517}
1518
Calin Juravle225ff812014-11-13 16:46:39 +00001519void HGraphBuilder::BuildFilledNewArray(uint32_t dex_pc,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001520 uint32_t type_index,
1521 uint32_t number_of_vreg_arguments,
1522 bool is_range,
1523 uint32_t* args,
1524 uint32_t register_index) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001525 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments, dex_pc);
Mingyao Yangfb8464a2015-11-02 10:56:59 -08001526 bool finalizable;
1527 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index, &finalizable)
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001528 ? kQuickAllocArrayWithAccessCheck
1529 : kQuickAllocArray;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01001530 HInstruction* object = new (arena_) HNewArray(length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01001531 graph_->GetCurrentMethod(),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01001532 dex_pc,
1533 type_index,
1534 *dex_compilation_unit_->GetDexFile(),
1535 entrypoint);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001536 current_block_->AddInstruction(object);
1537
1538 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1539 DCHECK_EQ(descriptor[0], '[') << descriptor;
1540 char primitive = descriptor[1];
1541 DCHECK(primitive == 'I'
1542 || primitive == 'L'
1543 || primitive == '[') << descriptor;
1544 bool is_reference_array = (primitive == 'L') || (primitive == '[');
1545 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
1546
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001547 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001548 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type, dex_pc);
1549 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001550 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001551 new (arena_) HArraySet(object, index, value, type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001552 }
1553 latest_result_ = object;
1554}
1555
1556template <typename T>
1557void HGraphBuilder::BuildFillArrayData(HInstruction* object,
1558 const T* data,
1559 uint32_t element_count,
1560 Primitive::Type anticipated_type,
Calin Juravle225ff812014-11-13 16:46:39 +00001561 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001562 for (uint32_t i = 0; i < element_count; ++i) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001563 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1564 HInstruction* value = graph_->GetIntConstant(data[i], dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001565 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001566 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001567 }
1568}
1569
Calin Juravle225ff812014-11-13 16:46:39 +00001570void HGraphBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001571 HInstruction* array = LoadLocal(instruction.VRegA_31t(), Primitive::kPrimNot, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001572 HNullCheck* null_check = new (arena_) HNullCheck(array, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001573 current_block_->AddInstruction(null_check);
Calin Juravled0d48522014-11-04 16:40:20 +00001574
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001575 HInstruction* length = new (arena_) HArrayLength(null_check, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001576 current_block_->AddInstruction(length);
1577
Calin Juravle225ff812014-11-13 16:46:39 +00001578 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
Calin Juravled0d48522014-11-04 16:40:20 +00001579 const Instruction::ArrayDataPayload* payload =
1580 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_start_ + payload_offset);
1581 const uint8_t* data = payload->data;
1582 uint32_t element_count = payload->element_count;
1583
1584 // Implementation of this DEX instruction seems to be that the bounds check is
1585 // done before doing any stores.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001586 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001587 current_block_->AddInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
Calin Juravled0d48522014-11-04 16:40:20 +00001588
1589 switch (payload->element_width) {
1590 case 1:
1591 BuildFillArrayData(null_check,
1592 reinterpret_cast<const int8_t*>(data),
1593 element_count,
1594 Primitive::kPrimByte,
Calin Juravle225ff812014-11-13 16:46:39 +00001595 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001596 break;
1597 case 2:
1598 BuildFillArrayData(null_check,
1599 reinterpret_cast<const int16_t*>(data),
1600 element_count,
1601 Primitive::kPrimShort,
Calin Juravle225ff812014-11-13 16:46:39 +00001602 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001603 break;
1604 case 4:
1605 BuildFillArrayData(null_check,
1606 reinterpret_cast<const int32_t*>(data),
1607 element_count,
1608 Primitive::kPrimInt,
Calin Juravle225ff812014-11-13 16:46:39 +00001609 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001610 break;
1611 case 8:
1612 BuildFillWideArrayData(null_check,
1613 reinterpret_cast<const int64_t*>(data),
1614 element_count,
Calin Juravle225ff812014-11-13 16:46:39 +00001615 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001616 break;
1617 default:
1618 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1619 }
Mark Mendell1152c922015-04-24 17:06:35 -04001620 graph_->SetHasBoundsChecks(true);
Calin Juravled0d48522014-11-04 16:40:20 +00001621}
1622
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001623void HGraphBuilder::BuildFillWideArrayData(HInstruction* object,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +01001624 const int64_t* data,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001625 uint32_t element_count,
Calin Juravle225ff812014-11-13 16:46:39 +00001626 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001627 for (uint32_t i = 0; i < element_count; ++i) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001628 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1629 HInstruction* value = graph_->GetLongConstant(data[i], dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001630 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001631 object, index, value, Primitive::kPrimLong, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001632 }
1633}
1634
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001635static TypeCheckKind ComputeTypeCheckKind(Handle<mirror::Class> cls)
1636 SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle98893e12015-10-02 21:05:03 +01001637 if (cls.Get() == nullptr) {
1638 return TypeCheckKind::kUnresolvedCheck;
1639 } else if (cls->IsInterface()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001640 return TypeCheckKind::kInterfaceCheck;
1641 } else if (cls->IsArrayClass()) {
1642 if (cls->GetComponentType()->IsObjectClass()) {
1643 return TypeCheckKind::kArrayObjectCheck;
1644 } else if (cls->CannotBeAssignedFromOtherTypes()) {
1645 return TypeCheckKind::kExactCheck;
1646 } else {
1647 return TypeCheckKind::kArrayCheck;
1648 }
1649 } else if (cls->IsFinal()) {
1650 return TypeCheckKind::kExactCheck;
1651 } else if (cls->IsAbstract()) {
1652 return TypeCheckKind::kAbstractClassCheck;
1653 } else {
1654 return TypeCheckKind::kClassHierarchyCheck;
1655 }
1656}
1657
Calin Juravle98893e12015-10-02 21:05:03 +01001658void HGraphBuilder::BuildTypeCheck(const Instruction& instruction,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001659 uint8_t destination,
1660 uint8_t reference,
1661 uint16_t type_index,
Calin Juravle225ff812014-11-13 16:46:39 +00001662 uint32_t dex_pc) {
Calin Juravle98893e12015-10-02 21:05:03 +01001663 bool type_known_final, type_known_abstract, use_declaring_class;
1664 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
1665 dex_compilation_unit_->GetDexMethodIndex(),
1666 *dex_compilation_unit_->GetDexFile(),
1667 type_index,
1668 &type_known_final,
1669 &type_known_abstract,
1670 &use_declaring_class);
1671
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001672 ScopedObjectAccess soa(Thread::Current());
1673 StackHandleScope<2> hs(soa.Self());
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001674 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001675 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001676 dex_compilation_unit_->GetClassLinker()->FindDexCache(soa.Self(), dex_file)));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001677 Handle<mirror::Class> resolved_class(hs.NewHandle(dex_cache->GetResolvedType(type_index)));
1678
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001679 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot, dex_pc);
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001680 HLoadClass* cls = new (arena_) HLoadClass(
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001681 graph_->GetCurrentMethod(),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01001682 type_index,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001683 dex_file,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01001684 IsOutermostCompilingClass(type_index),
Calin Juravle98893e12015-10-02 21:05:03 +01001685 dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001686 !can_access,
1687 compiler_driver_->CanAssumeTypeIsPresentInDexCache(dex_file, type_index));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001688 current_block_->AddInstruction(cls);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001689
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001690 TypeCheckKind check_kind = ComputeTypeCheckKind(resolved_class);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001691 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001692 current_block_->AddInstruction(new (arena_) HInstanceOf(object, cls, check_kind, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001693 UpdateLocal(destination, current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001694 } else {
1695 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
David Brazdilf5552582015-12-27 13:36:12 +00001696 // We emit a CheckCast followed by a BoundType. CheckCast is a statement
1697 // which may throw. If it succeeds BoundType sets the new type of `object`
1698 // for all subsequent uses.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001699 current_block_->AddInstruction(new (arena_) HCheckCast(object, cls, check_kind, dex_pc));
David Brazdilf5552582015-12-27 13:36:12 +00001700 current_block_->AddInstruction(new (arena_) HBoundType(object, dex_pc));
1701 UpdateLocal(reference, current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001702 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001703}
1704
Mingyao Yangfb8464a2015-11-02 10:56:59 -08001705bool HGraphBuilder::NeedsAccessCheck(uint32_t type_index, bool* finalizable) const {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001706 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
Mingyao Yangfb8464a2015-11-02 10:56:59 -08001707 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index, finalizable);
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001708}
1709
Mark Mendellfe57faa2015-09-18 09:26:15 -04001710void HGraphBuilder::BuildSwitchJumpTable(const SwitchTable& table,
1711 const Instruction& instruction,
1712 HInstruction* value,
1713 uint32_t dex_pc) {
1714 // Add the successor blocks to the current block.
1715 uint16_t num_entries = table.GetNumEntries();
1716 for (size_t i = 1; i <= num_entries; i++) {
1717 int32_t target_offset = table.GetEntryAt(i);
1718 HBasicBlock* case_target = FindBlockStartingAt(dex_pc + target_offset);
1719 DCHECK(case_target != nullptr);
1720
1721 // Add the target block as a successor.
1722 current_block_->AddSuccessor(case_target);
1723 }
1724
1725 // Add the default target block as the last successor.
1726 HBasicBlock* default_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
1727 DCHECK(default_target != nullptr);
1728 current_block_->AddSuccessor(default_target);
1729
1730 // Now add the Switch instruction.
1731 int32_t starting_key = table.GetEntryAt(0);
1732 current_block_->AddInstruction(
1733 new (arena_) HPackedSwitch(starting_key, num_entries, value, dex_pc));
1734 // This block ends with control flow.
1735 current_block_ = nullptr;
1736}
1737
Calin Juravle48c2b032014-12-09 18:11:36 +00001738void HGraphBuilder::BuildPackedSwitch(const Instruction& instruction, uint32_t dex_pc) {
David Brazdil2ef645b2015-06-17 18:20:52 +01001739 // Verifier guarantees that the payload for PackedSwitch contains:
1740 // (a) number of entries (may be zero)
1741 // (b) first and lowest switch case value (entry 0, always present)
1742 // (c) list of target pcs (entries 1 <= i <= N)
Andreas Gamped881df52014-11-24 23:28:39 -08001743 SwitchTable table(instruction, dex_pc, false);
1744
1745 // Value to test against.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001746 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt, dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08001747
Mark Mendellfe57faa2015-09-18 09:26:15 -04001748 // Starting key value.
1749 int32_t starting_key = table.GetEntryAt(0);
1750
David Brazdil2ef645b2015-06-17 18:20:52 +01001751 // Retrieve number of entries.
Andreas Gampee4d4d322014-12-04 09:09:57 -08001752 uint16_t num_entries = table.GetNumEntries();
David Brazdil2ef645b2015-06-17 18:20:52 +01001753 if (num_entries == 0) {
1754 return;
1755 }
Andreas Gampee4d4d322014-12-04 09:09:57 -08001756
Mark Mendellfe57faa2015-09-18 09:26:15 -04001757 // Don't use a packed switch if there are very few entries.
1758 if (num_entries > kSmallSwitchThreshold) {
1759 BuildSwitchJumpTable(table, instruction, value, dex_pc);
1760 } else {
1761 // Chained cmp-and-branch, starting from starting_key.
1762 for (size_t i = 1; i <= num_entries; i++) {
Mark Mendell3b9f3042015-09-24 08:43:40 -04001763 BuildSwitchCaseHelper(instruction,
1764 i,
1765 i == num_entries,
1766 table,
1767 value,
1768 starting_key + i - 1,
1769 table.GetEntryAt(i),
1770 dex_pc);
Mark Mendellfe57faa2015-09-18 09:26:15 -04001771 }
Andreas Gamped881df52014-11-24 23:28:39 -08001772 }
Andreas Gamped881df52014-11-24 23:28:39 -08001773}
1774
Calin Juravle48c2b032014-12-09 18:11:36 +00001775void HGraphBuilder::BuildSparseSwitch(const Instruction& instruction, uint32_t dex_pc) {
David Brazdil2ef645b2015-06-17 18:20:52 +01001776 // Verifier guarantees that the payload for SparseSwitch contains:
1777 // (a) number of entries (may be zero)
1778 // (b) sorted key values (entries 0 <= i < N)
1779 // (c) target pcs corresponding to the switch values (entries N <= i < 2*N)
Andreas Gampee4d4d322014-12-04 09:09:57 -08001780 SwitchTable table(instruction, dex_pc, true);
1781
1782 // Value to test against.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001783 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001784
1785 uint16_t num_entries = table.GetNumEntries();
Andreas Gampee4d4d322014-12-04 09:09:57 -08001786
1787 for (size_t i = 0; i < num_entries; i++) {
1788 BuildSwitchCaseHelper(instruction, i, i == static_cast<size_t>(num_entries) - 1, table, value,
1789 table.GetEntryAt(i), table.GetEntryAt(i + num_entries), dex_pc);
1790 }
Andreas Gampee4d4d322014-12-04 09:09:57 -08001791}
1792
1793void HGraphBuilder::BuildSwitchCaseHelper(const Instruction& instruction, size_t index,
1794 bool is_last_case, const SwitchTable& table,
1795 HInstruction* value, int32_t case_value_int,
1796 int32_t target_offset, uint32_t dex_pc) {
David Brazdil852eaff2015-02-02 15:23:05 +00001797 HBasicBlock* case_target = FindBlockStartingAt(dex_pc + target_offset);
1798 DCHECK(case_target != nullptr);
1799 PotentiallyAddSuspendCheck(case_target, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001800
1801 // The current case's value.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001802 HInstruction* this_case_value = graph_->GetIntConstant(case_value_int, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001803
1804 // Compare value and this_case_value.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001805 HEqual* comparison = new (arena_) HEqual(value, this_case_value, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001806 current_block_->AddInstruction(comparison);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001807 HInstruction* ifinst = new (arena_) HIf(comparison, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001808 current_block_->AddInstruction(ifinst);
1809
1810 // Case hit: use the target offset to determine where to go.
Andreas Gampee4d4d322014-12-04 09:09:57 -08001811 current_block_->AddSuccessor(case_target);
1812
1813 // Case miss: go to the next case (or default fall-through).
1814 // When there is a next case, we use the block stored with the table offset representing this
1815 // case (that is where we registered them in ComputeBranchTargets).
1816 // When there is no next case, we use the following instruction.
1817 // TODO: Find a good way to peel the last iteration to avoid conditional, but still have re-use.
1818 if (!is_last_case) {
1819 HBasicBlock* next_case_target = FindBlockStartingAt(table.GetDexPcForIndex(index));
1820 DCHECK(next_case_target != nullptr);
1821 current_block_->AddSuccessor(next_case_target);
1822
1823 // Need to manually add the block, as there is no dex-pc transition for the cases.
1824 graph_->AddBlock(next_case_target);
1825
1826 current_block_ = next_case_target;
1827 } else {
1828 HBasicBlock* default_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
1829 DCHECK(default_target != nullptr);
1830 current_block_->AddSuccessor(default_target);
1831 current_block_ = nullptr;
1832 }
1833}
1834
David Brazdil852eaff2015-02-02 15:23:05 +00001835void HGraphBuilder::PotentiallyAddSuspendCheck(HBasicBlock* target, uint32_t dex_pc) {
1836 int32_t target_offset = target->GetDexPc() - dex_pc;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001837 if (target_offset <= 0) {
David Brazdil852eaff2015-02-02 15:23:05 +00001838 // DX generates back edges to the first encountered return. We can save
1839 // time of later passes by not adding redundant suspend checks.
David Brazdil2fd6aa52015-02-02 18:58:27 +00001840 HInstruction* last_in_target = target->GetLastInstruction();
1841 if (last_in_target != nullptr &&
1842 (last_in_target->IsReturn() || last_in_target->IsReturnVoid())) {
1843 return;
David Brazdil852eaff2015-02-02 15:23:05 +00001844 }
1845
1846 // Add a suspend check to backward branches which may potentially loop. We
1847 // can remove them after we recognize loops in the graph.
Calin Juravle225ff812014-11-13 16:46:39 +00001848 current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_pc));
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001849 }
1850}
1851
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001852bool HGraphBuilder::CanDecodeQuickenedInfo() const {
1853 return interpreter_metadata_ != nullptr;
1854}
1855
1856uint16_t HGraphBuilder::LookupQuickenedInfo(uint32_t dex_pc) {
1857 DCHECK(interpreter_metadata_ != nullptr);
1858 uint32_t dex_pc_in_map = DecodeUnsignedLeb128(&interpreter_metadata_);
1859 DCHECK_EQ(dex_pc, dex_pc_in_map);
1860 return DecodeUnsignedLeb128(&interpreter_metadata_);
1861}
1862
Calin Juravle225ff812014-11-13 16:46:39 +00001863bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001864 if (current_block_ == nullptr) {
1865 return true; // Dead code
1866 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001867
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001868 switch (instruction.Opcode()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001869 case Instruction::CONST_4: {
1870 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001871 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n(), dex_pc);
1872 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001873 break;
1874 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001875
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001876 case Instruction::CONST_16: {
1877 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001878 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s(), dex_pc);
1879 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001880 break;
1881 }
1882
Dave Allison20dfc792014-06-16 20:44:29 -07001883 case Instruction::CONST: {
1884 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001885 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i(), dex_pc);
1886 UpdateLocal(register_index, constant, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001887 break;
1888 }
1889
1890 case Instruction::CONST_HIGH16: {
1891 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001892 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16, dex_pc);
1893 UpdateLocal(register_index, constant, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001894 break;
1895 }
1896
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001897 case Instruction::CONST_WIDE_16: {
1898 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001899 // Get 16 bits of constant value, sign extended to 64 bits.
1900 int64_t value = instruction.VRegB_21s();
1901 value <<= 48;
1902 value >>= 48;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001903 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1904 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001905 break;
1906 }
1907
1908 case Instruction::CONST_WIDE_32: {
1909 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001910 // Get 32 bits of constant value, sign extended to 64 bits.
1911 int64_t value = instruction.VRegB_31i();
1912 value <<= 32;
1913 value >>= 32;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001914 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1915 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001916 break;
1917 }
1918
1919 case Instruction::CONST_WIDE: {
1920 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001921 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l(), dex_pc);
1922 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001923 break;
1924 }
1925
Dave Allison20dfc792014-06-16 20:44:29 -07001926 case Instruction::CONST_WIDE_HIGH16: {
1927 int32_t register_index = instruction.VRegA();
1928 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001929 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1930 UpdateLocal(register_index, constant, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001931 break;
1932 }
1933
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001934 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001935 case Instruction::MOVE:
1936 case Instruction::MOVE_FROM16:
1937 case Instruction::MOVE_16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001938 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
1939 UpdateLocal(instruction.VRegA(), value, dex_pc);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001940 break;
1941 }
1942
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001943 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001944 case Instruction::MOVE_WIDE:
1945 case Instruction::MOVE_WIDE_FROM16:
1946 case Instruction::MOVE_WIDE_16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001947 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong, dex_pc);
1948 UpdateLocal(instruction.VRegA(), value, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001949 break;
1950 }
1951
1952 case Instruction::MOVE_OBJECT:
1953 case Instruction::MOVE_OBJECT_16:
1954 case Instruction::MOVE_OBJECT_FROM16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001955 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot, dex_pc);
1956 UpdateLocal(instruction.VRegA(), value, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001957 break;
1958 }
1959
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001960 case Instruction::RETURN_VOID_NO_BARRIER:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001961 case Instruction::RETURN_VOID: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001962 BuildReturn(instruction, Primitive::kPrimVoid, dex_pc);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001963 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001964 }
1965
Dave Allison20dfc792014-06-16 20:44:29 -07001966#define IF_XX(comparison, cond) \
Calin Juravle225ff812014-11-13 16:46:39 +00001967 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
1968 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001969
Dave Allison20dfc792014-06-16 20:44:29 -07001970 IF_XX(HEqual, EQ);
1971 IF_XX(HNotEqual, NE);
1972 IF_XX(HLessThan, LT);
1973 IF_XX(HLessThanOrEqual, LE);
1974 IF_XX(HGreaterThan, GT);
1975 IF_XX(HGreaterThanOrEqual, GE);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001976
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001977 case Instruction::GOTO:
1978 case Instruction::GOTO_16:
1979 case Instruction::GOTO_32: {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001980 int32_t offset = instruction.GetTargetOffset();
Calin Juravle225ff812014-11-13 16:46:39 +00001981 HBasicBlock* target = FindBlockStartingAt(offset + dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001982 DCHECK(target != nullptr);
David Brazdil852eaff2015-02-02 15:23:05 +00001983 PotentiallyAddSuspendCheck(target, dex_pc);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001984 current_block_->AddInstruction(new (arena_) HGoto(dex_pc));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001985 current_block_->AddSuccessor(target);
1986 current_block_ = nullptr;
1987 break;
1988 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001989
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001990 case Instruction::RETURN: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001991 BuildReturn(instruction, return_type_, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001992 break;
1993 }
1994
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001995 case Instruction::RETURN_OBJECT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001996 BuildReturn(instruction, return_type_, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001997 break;
1998 }
1999
2000 case Instruction::RETURN_WIDE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002001 BuildReturn(instruction, return_type_, dex_pc);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002002 break;
2003 }
2004
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002005 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00002006 case Instruction::INVOKE_INTERFACE:
2007 case Instruction::INVOKE_STATIC:
2008 case Instruction::INVOKE_SUPER:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002009 case Instruction::INVOKE_VIRTUAL:
2010 case Instruction::INVOKE_VIRTUAL_QUICK: {
2011 uint16_t method_idx;
2012 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_QUICK) {
2013 if (!CanDecodeQuickenedInfo()) {
2014 return false;
2015 }
2016 method_idx = LookupQuickenedInfo(dex_pc);
2017 } else {
2018 method_idx = instruction.VRegB_35c();
2019 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002020 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002021 uint32_t args[5];
Ian Rogers29a26482014-05-02 15:27:29 -07002022 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00002023 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00002024 number_of_vreg_arguments, false, args, -1)) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002025 return false;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002026 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002027 break;
2028 }
2029
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002030 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00002031 case Instruction::INVOKE_INTERFACE_RANGE:
2032 case Instruction::INVOKE_STATIC_RANGE:
2033 case Instruction::INVOKE_SUPER_RANGE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002034 case Instruction::INVOKE_VIRTUAL_RANGE:
2035 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2036 uint16_t method_idx;
2037 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK) {
2038 if (!CanDecodeQuickenedInfo()) {
2039 return false;
2040 }
2041 method_idx = LookupQuickenedInfo(dex_pc);
2042 } else {
2043 method_idx = instruction.VRegB_3rc();
2044 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002045 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
2046 uint32_t register_index = instruction.VRegC();
Calin Juravle225ff812014-11-13 16:46:39 +00002047 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002048 number_of_vreg_arguments, true, nullptr, register_index)) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002049 return false;
2050 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002051 break;
2052 }
2053
Roland Levillain88cb1752014-10-20 16:36:47 +01002054 case Instruction::NEG_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002055 Unop_12x<HNeg>(instruction, Primitive::kPrimInt, dex_pc);
Roland Levillain88cb1752014-10-20 16:36:47 +01002056 break;
2057 }
2058
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002059 case Instruction::NEG_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002060 Unop_12x<HNeg>(instruction, Primitive::kPrimLong, dex_pc);
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002061 break;
2062 }
2063
Roland Levillain3dbcb382014-10-28 17:30:07 +00002064 case Instruction::NEG_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002065 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat, dex_pc);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002066 break;
2067 }
2068
2069 case Instruction::NEG_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002070 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble, dex_pc);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002071 break;
2072 }
2073
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002074 case Instruction::NOT_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002075 Unop_12x<HNot>(instruction, Primitive::kPrimInt, dex_pc);
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002076 break;
2077 }
2078
Roland Levillain70566432014-10-24 16:20:17 +01002079 case Instruction::NOT_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002080 Unop_12x<HNot>(instruction, Primitive::kPrimLong, dex_pc);
Roland Levillain70566432014-10-24 16:20:17 +01002081 break;
2082 }
2083
Roland Levillaindff1f282014-11-05 14:15:05 +00002084 case Instruction::INT_TO_LONG: {
Roland Levillain624279f2014-12-04 11:54:28 +00002085 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong, dex_pc);
Roland Levillaindff1f282014-11-05 14:15:05 +00002086 break;
2087 }
2088
Roland Levillaincff13742014-11-17 14:32:17 +00002089 case Instruction::INT_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00002090 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00002091 break;
2092 }
2093
2094 case Instruction::INT_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00002095 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00002096 break;
2097 }
2098
Roland Levillain946e1432014-11-11 17:35:19 +00002099 case Instruction::LONG_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00002100 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt, dex_pc);
Roland Levillain946e1432014-11-11 17:35:19 +00002101 break;
2102 }
2103
Roland Levillain6d0e4832014-11-27 18:31:21 +00002104 case Instruction::LONG_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00002105 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimFloat, dex_pc);
Roland Levillain6d0e4832014-11-27 18:31:21 +00002106 break;
2107 }
2108
Roland Levillain647b9ed2014-11-27 12:06:00 +00002109 case Instruction::LONG_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00002110 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble, dex_pc);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002111 break;
2112 }
2113
Roland Levillain3f8f9362014-12-02 17:45:01 +00002114 case Instruction::FLOAT_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00002115 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimInt, dex_pc);
2116 break;
2117 }
2118
2119 case Instruction::FLOAT_TO_LONG: {
2120 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimLong, dex_pc);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002121 break;
2122 }
2123
Roland Levillain8964e2b2014-12-04 12:10:50 +00002124 case Instruction::FLOAT_TO_DOUBLE: {
2125 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimDouble, dex_pc);
2126 break;
2127 }
2128
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002129 case Instruction::DOUBLE_TO_INT: {
2130 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimInt, dex_pc);
2131 break;
2132 }
2133
2134 case Instruction::DOUBLE_TO_LONG: {
2135 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimLong, dex_pc);
2136 break;
2137 }
2138
Roland Levillain8964e2b2014-12-04 12:10:50 +00002139 case Instruction::DOUBLE_TO_FLOAT: {
2140 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimFloat, dex_pc);
2141 break;
2142 }
2143
Roland Levillain51d3fc42014-11-13 14:11:42 +00002144 case Instruction::INT_TO_BYTE: {
Roland Levillain624279f2014-12-04 11:54:28 +00002145 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte, dex_pc);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002146 break;
2147 }
2148
Roland Levillain01a8d712014-11-14 16:27:39 +00002149 case Instruction::INT_TO_SHORT: {
Roland Levillain624279f2014-12-04 11:54:28 +00002150 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort, dex_pc);
Roland Levillain01a8d712014-11-14 16:27:39 +00002151 break;
2152 }
2153
Roland Levillain981e4542014-11-14 11:47:14 +00002154 case Instruction::INT_TO_CHAR: {
Roland Levillain624279f2014-12-04 11:54:28 +00002155 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar, dex_pc);
Roland Levillain981e4542014-11-14 11:47:14 +00002156 break;
2157 }
2158
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002159 case Instruction::ADD_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002160 Binop_23x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002161 break;
2162 }
2163
2164 case Instruction::ADD_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002165 Binop_23x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002166 break;
2167 }
2168
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002169 case Instruction::ADD_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002170 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002171 break;
2172 }
2173
2174 case Instruction::ADD_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002175 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002176 break;
2177 }
2178
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002179 case Instruction::SUB_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002180 Binop_23x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002181 break;
2182 }
2183
2184 case Instruction::SUB_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002185 Binop_23x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002186 break;
2187 }
2188
Calin Juravle096cc022014-10-23 17:01:13 +01002189 case Instruction::SUB_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002190 Binop_23x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01002191 break;
2192 }
2193
2194 case Instruction::SUB_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002195 Binop_23x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01002196 break;
2197 }
2198
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002199 case Instruction::ADD_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002200 Binop_12x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002201 break;
2202 }
2203
Calin Juravle34bacdf2014-10-07 20:23:36 +01002204 case Instruction::MUL_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002205 Binop_23x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002206 break;
2207 }
2208
2209 case Instruction::MUL_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002210 Binop_23x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002211 break;
2212 }
2213
Calin Juravleb5bfa962014-10-21 18:02:24 +01002214 case Instruction::MUL_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002215 Binop_23x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01002216 break;
2217 }
2218
2219 case Instruction::MUL_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002220 Binop_23x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01002221 break;
2222 }
2223
Calin Juravled0d48522014-11-04 16:40:20 +00002224 case Instruction::DIV_INT: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002225 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2226 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravled0d48522014-11-04 16:40:20 +00002227 break;
2228 }
2229
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002230 case Instruction::DIV_LONG: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002231 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2232 dex_pc, Primitive::kPrimLong, false, true);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002233 break;
2234 }
2235
Calin Juravle7c4954d2014-10-28 16:57:40 +00002236 case Instruction::DIV_FLOAT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002237 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002238 break;
2239 }
2240
2241 case Instruction::DIV_DOUBLE: {
Calin Juravle225ff812014-11-13 16:46:39 +00002242 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002243 break;
2244 }
2245
Calin Juravlebacfec32014-11-14 15:54:36 +00002246 case Instruction::REM_INT: {
2247 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2248 dex_pc, Primitive::kPrimInt, false, false);
2249 break;
2250 }
2251
2252 case Instruction::REM_LONG: {
2253 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2254 dex_pc, Primitive::kPrimLong, false, false);
2255 break;
2256 }
2257
Calin Juravled2ec87d2014-12-08 14:24:46 +00002258 case Instruction::REM_FLOAT: {
2259 Binop_23x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2260 break;
2261 }
2262
2263 case Instruction::REM_DOUBLE: {
2264 Binop_23x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2265 break;
2266 }
2267
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002268 case Instruction::AND_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002269 Binop_23x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002270 break;
2271 }
2272
2273 case Instruction::AND_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002274 Binop_23x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002275 break;
2276 }
2277
Calin Juravle9aec02f2014-11-18 23:06:35 +00002278 case Instruction::SHL_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002279 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002280 break;
2281 }
2282
2283 case Instruction::SHL_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002284 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002285 break;
2286 }
2287
2288 case Instruction::SHR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002289 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002290 break;
2291 }
2292
2293 case Instruction::SHR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002294 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002295 break;
2296 }
2297
2298 case Instruction::USHR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002299 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002300 break;
2301 }
2302
2303 case Instruction::USHR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002304 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002305 break;
2306 }
2307
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002308 case Instruction::OR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002309 Binop_23x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002310 break;
2311 }
2312
2313 case Instruction::OR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002314 Binop_23x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002315 break;
2316 }
2317
2318 case Instruction::XOR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002319 Binop_23x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002320 break;
2321 }
2322
2323 case Instruction::XOR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002324 Binop_23x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002325 break;
2326 }
2327
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002328 case Instruction::ADD_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002329 Binop_12x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002330 break;
2331 }
2332
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002333 case Instruction::ADD_DOUBLE_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002334 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002335 break;
2336 }
2337
2338 case Instruction::ADD_FLOAT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002339 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002340 break;
2341 }
2342
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002343 case Instruction::SUB_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002344 Binop_12x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002345 break;
2346 }
2347
2348 case Instruction::SUB_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002349 Binop_12x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002350 break;
2351 }
2352
Calin Juravle096cc022014-10-23 17:01:13 +01002353 case Instruction::SUB_FLOAT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002354 Binop_12x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01002355 break;
2356 }
2357
2358 case Instruction::SUB_DOUBLE_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002359 Binop_12x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01002360 break;
2361 }
2362
Calin Juravle34bacdf2014-10-07 20:23:36 +01002363 case Instruction::MUL_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002364 Binop_12x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002365 break;
2366 }
2367
2368 case Instruction::MUL_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002369 Binop_12x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002370 break;
2371 }
2372
Calin Juravleb5bfa962014-10-21 18:02:24 +01002373 case Instruction::MUL_FLOAT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002374 Binop_12x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01002375 break;
2376 }
2377
2378 case Instruction::MUL_DOUBLE_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002379 Binop_12x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01002380 break;
2381 }
2382
Calin Juravle865fc882014-11-06 17:09:03 +00002383 case Instruction::DIV_INT_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002384 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2385 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravle865fc882014-11-06 17:09:03 +00002386 break;
2387 }
2388
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002389 case Instruction::DIV_LONG_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002390 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2391 dex_pc, Primitive::kPrimLong, false, true);
2392 break;
2393 }
2394
2395 case Instruction::REM_INT_2ADDR: {
2396 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2397 dex_pc, Primitive::kPrimInt, false, false);
2398 break;
2399 }
2400
2401 case Instruction::REM_LONG_2ADDR: {
2402 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2403 dex_pc, Primitive::kPrimLong, false, false);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002404 break;
2405 }
2406
Calin Juravled2ec87d2014-12-08 14:24:46 +00002407 case Instruction::REM_FLOAT_2ADDR: {
2408 Binop_12x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2409 break;
2410 }
2411
2412 case Instruction::REM_DOUBLE_2ADDR: {
2413 Binop_12x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2414 break;
2415 }
2416
Calin Juravle9aec02f2014-11-18 23:06:35 +00002417 case Instruction::SHL_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002418 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002419 break;
2420 }
2421
2422 case Instruction::SHL_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002423 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002424 break;
2425 }
2426
2427 case Instruction::SHR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002428 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002429 break;
2430 }
2431
2432 case Instruction::SHR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002433 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002434 break;
2435 }
2436
2437 case Instruction::USHR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002438 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002439 break;
2440 }
2441
2442 case Instruction::USHR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002443 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002444 break;
2445 }
2446
Calin Juravle7c4954d2014-10-28 16:57:40 +00002447 case Instruction::DIV_FLOAT_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00002448 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002449 break;
2450 }
2451
2452 case Instruction::DIV_DOUBLE_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00002453 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002454 break;
2455 }
2456
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002457 case Instruction::AND_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002458 Binop_12x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002459 break;
2460 }
2461
2462 case Instruction::AND_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002463 Binop_12x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002464 break;
2465 }
2466
2467 case Instruction::OR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002468 Binop_12x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002469 break;
2470 }
2471
2472 case Instruction::OR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002473 Binop_12x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002474 break;
2475 }
2476
2477 case Instruction::XOR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002478 Binop_12x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002479 break;
2480 }
2481
2482 case Instruction::XOR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002483 Binop_12x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002484 break;
2485 }
2486
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002487 case Instruction::ADD_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002488 Binop_22s<HAdd>(instruction, false, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002489 break;
2490 }
2491
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002492 case Instruction::AND_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002493 Binop_22s<HAnd>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002494 break;
2495 }
2496
2497 case Instruction::OR_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002498 Binop_22s<HOr>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002499 break;
2500 }
2501
2502 case Instruction::XOR_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002503 Binop_22s<HXor>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002504 break;
2505 }
2506
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002507 case Instruction::RSUB_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002508 Binop_22s<HSub>(instruction, true, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002509 break;
2510 }
2511
Calin Juravle34bacdf2014-10-07 20:23:36 +01002512 case Instruction::MUL_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002513 Binop_22s<HMul>(instruction, false, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002514 break;
2515 }
2516
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002517 case Instruction::ADD_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002518 Binop_22b<HAdd>(instruction, false, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002519 break;
2520 }
2521
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002522 case Instruction::AND_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002523 Binop_22b<HAnd>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002524 break;
2525 }
2526
2527 case Instruction::OR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002528 Binop_22b<HOr>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002529 break;
2530 }
2531
2532 case Instruction::XOR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002533 Binop_22b<HXor>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002534 break;
2535 }
2536
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002537 case Instruction::RSUB_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002538 Binop_22b<HSub>(instruction, true, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002539 break;
2540 }
2541
Calin Juravle34bacdf2014-10-07 20:23:36 +01002542 case Instruction::MUL_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002543 Binop_22b<HMul>(instruction, false, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002544 break;
2545 }
2546
Calin Juravled0d48522014-11-04 16:40:20 +00002547 case Instruction::DIV_INT_LIT16:
2548 case Instruction::DIV_INT_LIT8: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002549 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2550 dex_pc, Primitive::kPrimInt, true, true);
2551 break;
2552 }
2553
2554 case Instruction::REM_INT_LIT16:
2555 case Instruction::REM_INT_LIT8: {
2556 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2557 dex_pc, Primitive::kPrimInt, true, false);
Calin Juravled0d48522014-11-04 16:40:20 +00002558 break;
2559 }
2560
Calin Juravle9aec02f2014-11-18 23:06:35 +00002561 case Instruction::SHL_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002562 Binop_22b<HShl>(instruction, false, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002563 break;
2564 }
2565
2566 case Instruction::SHR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002567 Binop_22b<HShr>(instruction, false, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002568 break;
2569 }
2570
2571 case Instruction::USHR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002572 Binop_22b<HUShr>(instruction, false, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002573 break;
2574 }
2575
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002576 case Instruction::NEW_INSTANCE: {
David Brazdil6de19382016-01-08 17:37:10 +00002577 if (!BuildNewInstance(instruction.VRegB_21c(), dex_pc)) {
2578 return false;
Jeff Hao848f70a2014-01-15 13:49:50 -08002579 }
David Brazdil6de19382016-01-08 17:37:10 +00002580 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002581 break;
2582 }
2583
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002584 case Instruction::NEW_ARRAY: {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002585 uint16_t type_index = instruction.VRegC_22c();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002586 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt, dex_pc);
Mingyao Yangfb8464a2015-11-02 10:56:59 -08002587 bool finalizable;
2588 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index, &finalizable)
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002589 ? kQuickAllocArrayWithAccessCheck
2590 : kQuickAllocArray;
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002591 current_block_->AddInstruction(new (arena_) HNewArray(length,
2592 graph_->GetCurrentMethod(),
2593 dex_pc,
2594 type_index,
2595 *dex_compilation_unit_->GetDexFile(),
2596 entrypoint));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002597 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002598 break;
2599 }
2600
2601 case Instruction::FILLED_NEW_ARRAY: {
2602 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
2603 uint32_t type_index = instruction.VRegB_35c();
2604 uint32_t args[5];
2605 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00002606 BuildFilledNewArray(dex_pc, type_index, number_of_vreg_arguments, false, args, 0);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002607 break;
2608 }
2609
2610 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2611 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
2612 uint32_t type_index = instruction.VRegB_3rc();
2613 uint32_t register_index = instruction.VRegC_3rc();
2614 BuildFilledNewArray(
Calin Juravle225ff812014-11-13 16:46:39 +00002615 dex_pc, type_index, number_of_vreg_arguments, true, nullptr, register_index);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002616 break;
2617 }
2618
2619 case Instruction::FILL_ARRAY_DATA: {
Calin Juravle225ff812014-11-13 16:46:39 +00002620 BuildFillArrayData(instruction, dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002621 break;
2622 }
2623
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002624 case Instruction::MOVE_RESULT:
Dave Allison20dfc792014-06-16 20:44:29 -07002625 case Instruction::MOVE_RESULT_WIDE:
David Brazdilfc6a86a2015-06-26 10:33:45 +00002626 case Instruction::MOVE_RESULT_OBJECT: {
Nicolas Geoffray1efcc222015-06-24 12:41:20 +01002627 if (latest_result_ == nullptr) {
2628 // Only dead code can lead to this situation, where the verifier
2629 // does not reject the method.
2630 } else {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002631 // An Invoke/FilledNewArray and its MoveResult could have landed in
2632 // different blocks if there was a try/catch block boundary between
2633 // them. For Invoke, we insert a StoreLocal after the instruction. For
2634 // FilledNewArray, the local needs to be updated after the array was
2635 // filled, otherwise we might overwrite an input vreg.
2636 HStoreLocal* update_local =
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002637 new (arena_) HStoreLocal(GetLocalAt(instruction.VRegA()), latest_result_, dex_pc);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002638 HBasicBlock* block = latest_result_->GetBlock();
2639 if (block == current_block_) {
2640 // MoveResult and the previous instruction are in the same block.
2641 current_block_->AddInstruction(update_local);
2642 } else {
2643 // The two instructions are in different blocks. Insert the MoveResult
2644 // before the final control-flow instruction of the previous block.
2645 DCHECK(block->EndsWithControlFlowInstruction());
2646 DCHECK(current_block_->GetInstructions().IsEmpty());
2647 block->InsertInstructionBefore(update_local, block->GetLastInstruction());
2648 }
Nicolas Geoffray1efcc222015-06-24 12:41:20 +01002649 latest_result_ = nullptr;
2650 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002651 break;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002652 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002653
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002654 case Instruction::CMP_LONG: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002655 Binop_23x_cmp(instruction, Primitive::kPrimLong, ComparisonBias::kNoBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002656 break;
2657 }
2658
2659 case Instruction::CMPG_FLOAT: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002660 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kGtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002661 break;
2662 }
2663
2664 case Instruction::CMPG_DOUBLE: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002665 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kGtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002666 break;
2667 }
2668
2669 case Instruction::CMPL_FLOAT: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002670 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kLtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002671 break;
2672 }
2673
2674 case Instruction::CMPL_DOUBLE: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002675 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kLtBias, dex_pc);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002676 break;
2677 }
2678
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002679 case Instruction::NOP:
2680 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002681
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002682 case Instruction::IGET:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002683 case Instruction::IGET_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002684 case Instruction::IGET_WIDE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002685 case Instruction::IGET_WIDE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002686 case Instruction::IGET_OBJECT:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002687 case Instruction::IGET_OBJECT_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002688 case Instruction::IGET_BOOLEAN:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002689 case Instruction::IGET_BOOLEAN_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002690 case Instruction::IGET_BYTE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002691 case Instruction::IGET_BYTE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002692 case Instruction::IGET_CHAR:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002693 case Instruction::IGET_CHAR_QUICK:
2694 case Instruction::IGET_SHORT:
2695 case Instruction::IGET_SHORT_QUICK: {
Calin Juravle225ff812014-11-13 16:46:39 +00002696 if (!BuildInstanceFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002697 return false;
2698 }
2699 break;
2700 }
2701
2702 case Instruction::IPUT:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002703 case Instruction::IPUT_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002704 case Instruction::IPUT_WIDE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002705 case Instruction::IPUT_WIDE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002706 case Instruction::IPUT_OBJECT:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002707 case Instruction::IPUT_OBJECT_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002708 case Instruction::IPUT_BOOLEAN:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002709 case Instruction::IPUT_BOOLEAN_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002710 case Instruction::IPUT_BYTE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002711 case Instruction::IPUT_BYTE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002712 case Instruction::IPUT_CHAR:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002713 case Instruction::IPUT_CHAR_QUICK:
2714 case Instruction::IPUT_SHORT:
2715 case Instruction::IPUT_SHORT_QUICK: {
Calin Juravle225ff812014-11-13 16:46:39 +00002716 if (!BuildInstanceFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002717 return false;
2718 }
2719 break;
2720 }
2721
2722 case Instruction::SGET:
2723 case Instruction::SGET_WIDE:
2724 case Instruction::SGET_OBJECT:
2725 case Instruction::SGET_BOOLEAN:
2726 case Instruction::SGET_BYTE:
2727 case Instruction::SGET_CHAR:
2728 case Instruction::SGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002729 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002730 return false;
2731 }
2732 break;
2733 }
2734
2735 case Instruction::SPUT:
2736 case Instruction::SPUT_WIDE:
2737 case Instruction::SPUT_OBJECT:
2738 case Instruction::SPUT_BOOLEAN:
2739 case Instruction::SPUT_BYTE:
2740 case Instruction::SPUT_CHAR:
2741 case Instruction::SPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002742 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002743 return false;
2744 }
2745 break;
2746 }
2747
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002748#define ARRAY_XX(kind, anticipated_type) \
2749 case Instruction::AGET##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00002750 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002751 break; \
2752 } \
2753 case Instruction::APUT##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00002754 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002755 break; \
2756 }
2757
2758 ARRAY_XX(, Primitive::kPrimInt);
2759 ARRAY_XX(_WIDE, Primitive::kPrimLong);
2760 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
2761 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
2762 ARRAY_XX(_BYTE, Primitive::kPrimByte);
2763 ARRAY_XX(_CHAR, Primitive::kPrimChar);
2764 ARRAY_XX(_SHORT, Primitive::kPrimShort);
2765
Nicolas Geoffray39468442014-09-02 15:17:15 +01002766 case Instruction::ARRAY_LENGTH: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002767 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00002768 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002769 current_block_->AddInstruction(object);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002770 current_block_->AddInstruction(new (arena_) HArrayLength(object, dex_pc));
2771 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002772 break;
2773 }
2774
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002775 case Instruction::CONST_STRING: {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00002776 uint32_t string_index = instruction.VRegB_21c();
2777 bool in_dex_cache = compiler_driver_->CanAssumeStringIsPresentInDexCache(
2778 *dex_file_, string_index);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002779 current_block_->AddInstruction(
Nicolas Geoffray917d0162015-11-24 18:25:35 +00002780 new (arena_) HLoadString(graph_->GetCurrentMethod(), string_index, dex_pc, in_dex_cache));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002781 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002782 break;
2783 }
2784
2785 case Instruction::CONST_STRING_JUMBO: {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00002786 uint32_t string_index = instruction.VRegB_31c();
2787 bool in_dex_cache = compiler_driver_->CanAssumeStringIsPresentInDexCache(
2788 *dex_file_, string_index);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002789 current_block_->AddInstruction(
Nicolas Geoffray917d0162015-11-24 18:25:35 +00002790 new (arena_) HLoadString(graph_->GetCurrentMethod(), string_index, dex_pc, in_dex_cache));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002791 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002792 break;
2793 }
2794
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002795 case Instruction::CONST_CLASS: {
2796 uint16_t type_index = instruction.VRegB_21c();
2797 bool type_known_final;
2798 bool type_known_abstract;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002799 bool dont_use_is_referrers_class;
2800 // `CanAccessTypeWithoutChecks` will tell whether the method being
2801 // built is trying to access its own class, so that the generated
2802 // code can optimize for this case. However, the optimization does not
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002803 // work for inlining, so we use `IsOutermostCompilingClass` instead.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002804 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
2805 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002806 &type_known_final, &type_known_abstract, &dont_use_is_referrers_class);
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002807 current_block_->AddInstruction(new (arena_) HLoadClass(
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002808 graph_->GetCurrentMethod(),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002809 type_index,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00002810 *dex_file_,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002811 IsOutermostCompilingClass(type_index),
Calin Juravle98893e12015-10-02 21:05:03 +01002812 dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00002813 !can_access,
2814 compiler_driver_->CanAssumeTypeIsPresentInDexCache(*dex_file_, type_index)));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002815 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002816 break;
2817 }
2818
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002819 case Instruction::MOVE_EXCEPTION: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002820 current_block_->AddInstruction(new (arena_) HLoadException(dex_pc));
2821 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction(), dex_pc);
2822 current_block_->AddInstruction(new (arena_) HClearException(dex_pc));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002823 break;
2824 }
2825
2826 case Instruction::THROW: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002827 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00002828 current_block_->AddInstruction(new (arena_) HThrow(exception, dex_pc));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002829 // A throw instruction must branch to the exit block.
2830 current_block_->AddSuccessor(exit_block_);
2831 // We finished building this block. Set the current block to null to avoid
2832 // adding dead instructions to it.
2833 current_block_ = nullptr;
2834 break;
2835 }
2836
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002837 case Instruction::INSTANCE_OF: {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002838 uint8_t destination = instruction.VRegA_22c();
2839 uint8_t reference = instruction.VRegB_22c();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002840 uint16_t type_index = instruction.VRegC_22c();
Calin Juravle98893e12015-10-02 21:05:03 +01002841 BuildTypeCheck(instruction, destination, reference, type_index, dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002842 break;
2843 }
2844
2845 case Instruction::CHECK_CAST: {
2846 uint8_t reference = instruction.VRegA_21c();
2847 uint16_t type_index = instruction.VRegB_21c();
Calin Juravle98893e12015-10-02 21:05:03 +01002848 BuildTypeCheck(instruction, -1, reference, type_index, dex_pc);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002849 break;
2850 }
2851
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002852 case Instruction::MONITOR_ENTER: {
2853 current_block_->AddInstruction(new (arena_) HMonitorOperation(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002854 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot, dex_pc),
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002855 HMonitorOperation::kEnter,
Calin Juravle225ff812014-11-13 16:46:39 +00002856 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002857 break;
2858 }
2859
2860 case Instruction::MONITOR_EXIT: {
2861 current_block_->AddInstruction(new (arena_) HMonitorOperation(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002862 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot, dex_pc),
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002863 HMonitorOperation::kExit,
Calin Juravle225ff812014-11-13 16:46:39 +00002864 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002865 break;
2866 }
2867
Andreas Gamped881df52014-11-24 23:28:39 -08002868 case Instruction::PACKED_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002869 BuildPackedSwitch(instruction, dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08002870 break;
2871 }
2872
Andreas Gampee4d4d322014-12-04 09:09:57 -08002873 case Instruction::SPARSE_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002874 BuildSparseSwitch(instruction, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08002875 break;
2876 }
2877
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002878 default:
Calin Juravle48c2b032014-12-09 18:11:36 +00002879 VLOG(compiler) << "Did not compile "
2880 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
2881 << " because of unhandled instruction "
2882 << instruction.Name();
2883 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnhandledInstruction);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002884 return false;
2885 }
2886 return true;
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00002887} // NOLINT(readability/fn_size)
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002888
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01002889HLocal* HGraphBuilder::GetLocalAt(uint32_t register_index) const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01002890 return locals_[register_index];
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002891}
2892
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01002893void HGraphBuilder::UpdateLocal(uint32_t register_index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002894 HInstruction* instruction,
2895 uint32_t dex_pc) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002896 HLocal* local = GetLocalAt(register_index);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002897 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction, dex_pc));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002898}
2899
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01002900HInstruction* HGraphBuilder::LoadLocal(uint32_t register_index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002901 Primitive::Type type,
2902 uint32_t dex_pc) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002903 HLocal* local = GetLocalAt(register_index);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002904 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type, dex_pc));
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002905 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002906}
2907
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002908} // namespace art