blob: b6b8322f03748b89cebee0aef710c1ff04130ffa [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);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600109 HInstruction* first = LoadLocal(instruction.VRegA(), Primitive::kPrimInt, dex_pc);
110 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
111 T* comparison = new (arena_) T(first, second, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700112 current_block_->AddInstruction(comparison);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600113 HInstruction* ifinst = new (arena_) HIf(comparison, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700114 current_block_->AddInstruction(ifinst);
David Brazdil852eaff2015-02-02 15:23:05 +0000115 current_block_->AddSuccessor(branch_target);
116 current_block_->AddSuccessor(fallthrough_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700117 current_block_ = nullptr;
118}
119
120template<typename T>
Calin Juravle225ff812014-11-13 16:46:39 +0000121void HGraphBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000122 int32_t target_offset = instruction.GetTargetOffset();
David Brazdil852eaff2015-02-02 15:23:05 +0000123 HBasicBlock* branch_target = FindBlockStartingAt(dex_pc + target_offset);
124 HBasicBlock* fallthrough_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
125 DCHECK(branch_target != nullptr);
126 DCHECK(fallthrough_target != nullptr);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600127 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt, dex_pc);
128 T* comparison = new (arena_) T(value, graph_->GetIntConstant(0, dex_pc), dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700129 current_block_->AddInstruction(comparison);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600130 HInstruction* ifinst = new (arena_) HIf(comparison, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700131 current_block_->AddInstruction(ifinst);
David Brazdil852eaff2015-02-02 15:23:05 +0000132 current_block_->AddSuccessor(branch_target);
133 current_block_->AddSuccessor(fallthrough_target);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100134 current_block_ = nullptr;
135}
136
Calin Juravle48c2b032014-12-09 18:11:36 +0000137void HGraphBuilder::MaybeRecordStat(MethodCompilationStat compilation_stat) {
138 if (compilation_stats_ != nullptr) {
139 compilation_stats_->RecordStat(compilation_stat);
140 }
141}
142
David Brazdil1b498722015-03-31 11:37:18 +0100143bool HGraphBuilder::SkipCompilation(const DexFile::CodeItem& code_item,
Calin Juravle48c2b032014-12-09 18:11:36 +0000144 size_t number_of_branches) {
145 const CompilerOptions& compiler_options = compiler_driver_->GetCompilerOptions();
Andreas Gampe29d38e72016-03-23 15:31:51 +0000146 CompilerFilter::Filter compiler_filter = compiler_options.GetCompilerFilter();
147 if (compiler_filter == CompilerFilter::kEverything) {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000148 return false;
149 }
150
David Brazdil1b498722015-03-31 11:37:18 +0100151 if (compiler_options.IsHugeMethod(code_item.insns_size_in_code_units_)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000152 VLOG(compiler) << "Skip compilation of huge method "
153 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
David Brazdil1b498722015-03-31 11:37:18 +0100154 << ": " << code_item.insns_size_in_code_units_ << " code units";
Calin Juravle48c2b032014-12-09 18:11:36 +0000155 MaybeRecordStat(MethodCompilationStat::kNotCompiledHugeMethod);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000156 return true;
157 }
158
159 // If it's large and contains no branches, it's likely to be machine generated initialization.
David Brazdil1b498722015-03-31 11:37:18 +0100160 if (compiler_options.IsLargeMethod(code_item.insns_size_in_code_units_)
161 && (number_of_branches == 0)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000162 VLOG(compiler) << "Skip compilation of large method with no branch "
163 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
David Brazdil1b498722015-03-31 11:37:18 +0100164 << ": " << code_item.insns_size_in_code_units_ << " code units";
Calin Juravle48c2b032014-12-09 18:11:36 +0000165 MaybeRecordStat(MethodCompilationStat::kNotCompiledLargeMethodNoBranches);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000166 return true;
167 }
168
169 return false;
170}
171
David Brazdilfc6a86a2015-06-26 10:33:45 +0000172void HGraphBuilder::CreateBlocksForTryCatch(const DexFile::CodeItem& code_item) {
173 if (code_item.tries_size_ == 0) {
174 return;
175 }
176
177 // Create branch targets at the start/end of the TryItem range. These are
178 // places where the program might fall through into/out of the a block and
179 // where TryBoundary instructions will be inserted later. Other edges which
180 // enter/exit the try blocks are a result of branches/switches.
181 for (size_t idx = 0; idx < code_item.tries_size_; ++idx) {
182 const DexFile::TryItem* try_item = DexFile::GetTryItems(code_item, idx);
183 uint32_t dex_pc_start = try_item->start_addr_;
184 uint32_t dex_pc_end = dex_pc_start + try_item->insn_count_;
185 FindOrCreateBlockStartingAt(dex_pc_start);
186 if (dex_pc_end < code_item.insns_size_in_code_units_) {
187 // TODO: Do not create block if the last instruction cannot fall through.
188 FindOrCreateBlockStartingAt(dex_pc_end);
189 } else {
190 // The TryItem spans until the very end of the CodeItem (or beyond if
191 // invalid) and therefore cannot have any code afterwards.
192 }
193 }
194
195 // Create branch targets for exception handlers.
196 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(code_item, 0);
197 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
198 for (uint32_t idx = 0; idx < handlers_size; ++idx) {
199 CatchHandlerIterator iterator(handlers_ptr);
200 for (; iterator.HasNext(); iterator.Next()) {
201 uint32_t address = iterator.GetHandlerAddress();
202 HBasicBlock* block = FindOrCreateBlockStartingAt(address);
David Brazdilec16f792015-08-19 15:04:01 +0100203 block->SetTryCatchInformation(
204 new (arena_) TryCatchInformation(iterator.GetHandlerTypeIndex(), *dex_file_));
David Brazdilfc6a86a2015-06-26 10:33:45 +0000205 }
206 handlers_ptr = iterator.EndDataPointer();
207 }
208}
209
David Brazdild7558da2015-09-22 13:04:14 +0100210// Returns the TryItem stored for `block` or nullptr if there is no info for it.
211static const DexFile::TryItem* GetTryItem(
212 HBasicBlock* block,
213 const ArenaSafeMap<uint32_t, const DexFile::TryItem*>& try_block_info) {
214 auto iterator = try_block_info.find(block->GetBlockId());
215 return (iterator == try_block_info.end()) ? nullptr : iterator->second;
216}
David Brazdil56e1acc2015-06-30 15:41:36 +0100217
David Brazdild7558da2015-09-22 13:04:14 +0100218void HGraphBuilder::LinkToCatchBlocks(HTryBoundary* try_boundary,
219 const DexFile::CodeItem& code_item,
220 const DexFile::TryItem* try_item) {
221 for (CatchHandlerIterator it(code_item, *try_item); it.HasNext(); it.Next()) {
David Brazdil56e1acc2015-06-30 15:41:36 +0100222 try_boundary->AddExceptionHandler(FindBlockStartingAt(it.GetHandlerAddress()));
223 }
224}
225
David Brazdilfc6a86a2015-06-26 10:33:45 +0000226void HGraphBuilder::InsertTryBoundaryBlocks(const DexFile::CodeItem& code_item) {
227 if (code_item.tries_size_ == 0) {
228 return;
229 }
230
David Brazdild7558da2015-09-22 13:04:14 +0100231 // Keep a map of all try blocks and their respective TryItems. We do not use
232 // the block's pointer but rather its id to ensure deterministic iteration.
233 ArenaSafeMap<uint32_t, const DexFile::TryItem*> try_block_info(
Vladimir Marko5233f932015-09-29 19:01:15 +0100234 std::less<uint32_t>(), arena_->Adapter(kArenaAllocGraphBuilder));
David Brazdilbff75032015-07-08 17:26:51 +0000235
David Brazdild7558da2015-09-22 13:04:14 +0100236 // Obtain TryItem information for blocks with throwing instructions, and split
237 // blocks which are both try & catch to simplify the graph.
238 // NOTE: We are appending new blocks inside the loop, so we need to use index
239 // because iterators can be invalidated. We remember the initial size to avoid
240 // iterating over the new blocks which cannot throw.
241 for (size_t i = 0, e = graph_->GetBlocks().size(); i < e; ++i) {
242 HBasicBlock* block = graph_->GetBlocks()[i];
David Brazdil72783ff2015-07-09 14:36:05 +0100243
David Brazdild7558da2015-09-22 13:04:14 +0100244 // Do not bother creating exceptional edges for try blocks which have no
245 // throwing instructions. In that case we simply assume that the block is
246 // not covered by a TryItem. This prevents us from creating a throw-catch
247 // loop for synchronized blocks.
248 if (block->HasThrowingInstructions()) {
249 // Try to find a TryItem covering the block.
David Brazdilbadd8262016-02-02 16:28:56 +0000250 DCHECK_NE(block->GetDexPc(), kNoDexPc) << "Block must have a dex_pc to find its TryItem.";
David Brazdild7558da2015-09-22 13:04:14 +0100251 const int32_t try_item_idx = DexFile::FindTryItem(code_item, block->GetDexPc());
252 if (try_item_idx != -1) {
253 // Block throwing and in a TryItem. Store the try block information.
254 HBasicBlock* throwing_block = block;
255 if (block->IsCatchBlock()) {
256 // Simplify blocks which are both try and catch, otherwise we would
257 // need a strategy for splitting exceptional edges. We split the block
258 // after the move-exception (if present) and mark the first part not
259 // throwing. The normal-flow edge between them will be split later.
David Brazdil9bc43612015-11-05 21:25:24 +0000260 throwing_block = block->SplitCatchBlockAfterMoveException();
261 // Move-exception does not throw and the block has throwing insructions
262 // so it must have been possible to split it.
263 DCHECK(throwing_block != nullptr);
David Brazdil72783ff2015-07-09 14:36:05 +0100264 }
David Brazdild7558da2015-09-22 13:04:14 +0100265
266 try_block_info.Put(throwing_block->GetBlockId(),
267 DexFile::GetTryItems(code_item, try_item_idx));
David Brazdil72783ff2015-07-09 14:36:05 +0100268 }
David Brazdil72783ff2015-07-09 14:36:05 +0100269 }
David Brazdilbff75032015-07-08 17:26:51 +0000270 }
271
David Brazdild7558da2015-09-22 13:04:14 +0100272 // Do a pass over the try blocks and insert entering TryBoundaries where at
273 // least one predecessor is not covered by the same TryItem as the try block.
274 // We do not split each edge separately, but rather create one boundary block
275 // that all predecessors are relinked to. This preserves loop headers (b/23895756).
276 for (auto entry : try_block_info) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100277 HBasicBlock* try_block = graph_->GetBlocks()[entry.first];
David Brazdild7558da2015-09-22 13:04:14 +0100278 for (HBasicBlock* predecessor : try_block->GetPredecessors()) {
279 if (GetTryItem(predecessor, try_block_info) != entry.second) {
280 // Found a predecessor not covered by the same TryItem. Insert entering
281 // boundary block.
282 HTryBoundary* try_entry =
Vladimir Markoa1de9182016-02-25 11:37:38 +0000283 new (arena_) HTryBoundary(HTryBoundary::BoundaryKind::kEntry, try_block->GetDexPc());
David Brazdild7558da2015-09-22 13:04:14 +0100284 try_block->CreateImmediateDominator()->AddInstruction(try_entry);
285 LinkToCatchBlocks(try_entry, code_item, entry.second);
286 break;
287 }
David Brazdil281a6322015-07-03 10:34:57 +0100288 }
David Brazdild7558da2015-09-22 13:04:14 +0100289 }
David Brazdil281a6322015-07-03 10:34:57 +0100290
David Brazdild7558da2015-09-22 13:04:14 +0100291 // Do a second pass over the try blocks and insert exit TryBoundaries where
292 // the successor is not in the same TryItem.
293 for (auto entry : try_block_info) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100294 HBasicBlock* try_block = graph_->GetBlocks()[entry.first];
David Brazdild7558da2015-09-22 13:04:14 +0100295 // NOTE: Do not use iterators because SplitEdge would invalidate them.
296 for (size_t i = 0, e = try_block->GetSuccessors().size(); i < e; ++i) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100297 HBasicBlock* successor = try_block->GetSuccessors()[i];
David Brazdil72783ff2015-07-09 14:36:05 +0100298
David Brazdild7558da2015-09-22 13:04:14 +0100299 // If the successor is a try block, all of its predecessors must be
300 // covered by the same TryItem. Otherwise the previous pass would have
301 // created a non-throwing boundary block.
302 if (GetTryItem(successor, try_block_info) != nullptr) {
303 DCHECK_EQ(entry.second, GetTryItem(successor, try_block_info));
David Brazdil72783ff2015-07-09 14:36:05 +0100304 continue;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000305 }
David Brazdil281a6322015-07-03 10:34:57 +0100306
David Brazdild7558da2015-09-22 13:04:14 +0100307 // Preserve the invariant that Return(Void) always jumps to Exit by moving
308 // it outside the try block if necessary.
309 HInstruction* last_instruction = try_block->GetLastInstruction();
310 if (last_instruction->IsReturn() || last_instruction->IsReturnVoid()) {
311 DCHECK_EQ(successor, exit_block_);
312 successor = try_block->SplitBefore(last_instruction);
David Brazdil281a6322015-07-03 10:34:57 +0100313 }
David Brazdild7558da2015-09-22 13:04:14 +0100314
315 // Insert TryBoundary and link to catch blocks.
316 HTryBoundary* try_exit =
Vladimir Markoa1de9182016-02-25 11:37:38 +0000317 new (arena_) HTryBoundary(HTryBoundary::BoundaryKind::kExit, successor->GetDexPc());
David Brazdild7558da2015-09-22 13:04:14 +0100318 graph_->SplitEdge(try_block, successor)->AddInstruction(try_exit);
319 LinkToCatchBlocks(try_exit, code_item, entry.second);
David Brazdil281a6322015-07-03 10:34:57 +0100320 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000321 }
322}
323
David Brazdilbadd8262016-02-02 16:28:56 +0000324GraphAnalysisResult HGraphBuilder::BuildGraph(const DexFile::CodeItem& code_item,
325 StackHandleScopeCollection* handles) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100326 DCHECK(graph_->GetBlocks().empty());
David Brazdil5e8b1372015-01-23 14:39:08 +0000327
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000328 const uint16_t* code_ptr = code_item.insns_;
329 const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100330 code_start_ = code_ptr;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000331
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000332 // Setup the graph with the entry block and exit block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100333 entry_block_ = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000334 graph_->AddBlock(entry_block_);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100335 exit_block_ = new (arena_) HBasicBlock(graph_, kNoDexPc);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000336 graph_->SetEntryBlock(entry_block_);
337 graph_->SetExitBlock(exit_block_);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000338
David Brazdil77a48ae2015-09-15 12:34:04 +0000339 graph_->SetHasTryCatch(code_item.tries_size_ != 0);
340
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000341 InitializeLocals(code_item.registers_size_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000342 graph_->SetMaximumNumberOfOutVRegs(code_item.outs_size_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000343
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000344 // Compute the number of dex instructions, blocks, and branches. We will
345 // check these values against limits given to the compiler.
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000346 size_t number_of_branches = 0;
347
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000348 // To avoid splitting blocks, we compute ahead of time the instructions that
349 // start a new block, and create these blocks.
Calin Juravle702d2602015-04-30 19:28:21 +0100350 if (!ComputeBranchTargets(code_ptr, code_end, &number_of_branches)) {
351 MaybeRecordStat(MethodCompilationStat::kNotCompiledBranchOutsideMethodCode);
David Brazdilbadd8262016-02-02 16:28:56 +0000352 return kAnalysisInvalidBytecode;
Calin Juravle702d2602015-04-30 19:28:21 +0100353 }
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000354
355 // Note that the compiler driver is null when unit testing.
David Brazdil1b498722015-03-31 11:37:18 +0100356 if ((compiler_driver_ != nullptr) && SkipCompilation(code_item, number_of_branches)) {
David Brazdilbadd8262016-02-02 16:28:56 +0000357 return kAnalysisInvalidBytecode;
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000358 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000359
David Srbecky0cf44932015-12-09 14:09:59 +0000360 // Find locations where we want to generate extra stackmaps for native debugging.
361 // This allows us to generate the info only at interesting points (for example,
362 // at start of java statement) rather than before every dex instruction.
363 const bool native_debuggable = compiler_driver_ != nullptr &&
364 compiler_driver_->GetCompilerOptions().GetNativeDebuggable();
365 ArenaBitVector* native_debug_info_locations;
366 if (native_debuggable) {
367 const uint32_t num_instructions = code_item.insns_size_in_code_units_;
Vladimir Markof6a35de2016-03-21 12:01:50 +0000368 native_debug_info_locations =
369 ArenaBitVector::Create(arena_, num_instructions, false, kArenaAllocGraphBuilder);
David Srbecky0cf44932015-12-09 14:09:59 +0000370 FindNativeDebugInfoLocations(code_item, native_debug_info_locations);
371 }
372
David Brazdilfc6a86a2015-06-26 10:33:45 +0000373 CreateBlocksForTryCatch(code_item);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000374
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000375 InitializeParameters(code_item.ins_size_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100376
Calin Juravle225ff812014-11-13 16:46:39 +0000377 size_t dex_pc = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000378 while (code_ptr < code_end) {
Calin Juravle225ff812014-11-13 16:46:39 +0000379 // Update the current block if dex_pc starts a new block.
380 MaybeUpdateCurrentBlock(dex_pc);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000381 const Instruction& instruction = *Instruction::At(code_ptr);
David Srbecky0cf44932015-12-09 14:09:59 +0000382 if (native_debuggable && native_debug_info_locations->IsBitSet(dex_pc)) {
383 if (current_block_ != nullptr) {
384 current_block_->AddInstruction(new (arena_) HNativeDebugInfo(dex_pc));
385 }
386 }
Calin Juravle48c2b032014-12-09 18:11:36 +0000387 if (!AnalyzeDexInstruction(instruction, dex_pc)) {
David Brazdilbadd8262016-02-02 16:28:56 +0000388 return kAnalysisInvalidBytecode;
Calin Juravle48c2b032014-12-09 18:11:36 +0000389 }
Calin Juravle225ff812014-11-13 16:46:39 +0000390 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000391 code_ptr += instruction.SizeInCodeUnits();
392 }
393
David Brazdilfc6a86a2015-06-26 10:33:45 +0000394 // Add Exit to the exit block.
David Brazdil3e187382015-06-26 09:59:52 +0000395 exit_block_->AddInstruction(new (arena_) HExit());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000396 // Add the suspend check to the entry block.
397 entry_block_->AddInstruction(new (arena_) HSuspendCheck(0));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000398 entry_block_->AddInstruction(new (arena_) HGoto());
David Brazdilbff75032015-07-08 17:26:51 +0000399 // Add the exit block at the end.
400 graph_->AddBlock(exit_block_);
David Brazdil5e8b1372015-01-23 14:39:08 +0000401
David Brazdilfc6a86a2015-06-26 10:33:45 +0000402 // Iterate over blocks covered by TryItems and insert TryBoundaries at entry
403 // and exit points. This requires all control-flow instructions and
404 // non-exceptional edges to have been created.
405 InsertTryBoundaryBlocks(code_item);
406
David Brazdilbadd8262016-02-02 16:28:56 +0000407 GraphAnalysisResult result = graph_->BuildDominatorTree();
408 if (result != kAnalysisSuccess) {
409 return result;
410 }
411
412 graph_->InitializeInexactObjectRTI(handles);
413 return SsaBuilder(graph_, handles).BuildSsa();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000414}
415
David Brazdilfc6a86a2015-06-26 10:33:45 +0000416void HGraphBuilder::MaybeUpdateCurrentBlock(size_t dex_pc) {
417 HBasicBlock* block = FindBlockStartingAt(dex_pc);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000418 if (block == nullptr) {
419 return;
420 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000421
422 if (current_block_ != nullptr) {
423 // Branching instructions clear current_block, so we know
424 // the last instruction of the current block is not a branching
425 // instruction. We add an unconditional goto to the found block.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600426 current_block_->AddInstruction(new (arena_) HGoto(dex_pc));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000427 current_block_->AddSuccessor(block);
428 }
429 graph_->AddBlock(block);
430 current_block_ = block;
431}
432
David Srbecky0cf44932015-12-09 14:09:59 +0000433void HGraphBuilder::FindNativeDebugInfoLocations(const DexFile::CodeItem& code_item,
434 ArenaBitVector* locations) {
435 // The callback gets called when the line number changes.
436 // In other words, it marks the start of new java statement.
437 struct Callback {
438 static bool Position(void* ctx, const DexFile::PositionInfo& entry) {
439 static_cast<ArenaBitVector*>(ctx)->SetBit(entry.address_);
440 return false;
441 }
442 };
443 dex_file_->DecodeDebugPositionInfo(&code_item, Callback::Position, locations);
David Srbecky0cf44932015-12-09 14:09:59 +0000444 // Instruction-specific tweaks.
445 const Instruction* const begin = Instruction::At(code_item.insns_);
446 const Instruction* const end = begin->RelativeAt(code_item.insns_size_in_code_units_);
447 for (const Instruction* inst = begin; inst < end; inst = inst->Next()) {
448 switch (inst->Opcode()) {
David Srbeckyc7098ff2016-02-09 14:30:11 +0000449 case Instruction::MOVE_EXCEPTION: {
450 // Stop in native debugger after the exception has been moved.
451 // The compiler also expects the move at the start of basic block so
452 // we do not want to interfere by inserting native-debug-info before it.
David Srbecky0cf44932015-12-09 14:09:59 +0000453 locations->ClearBit(inst->GetDexPc(code_item.insns_));
454 const Instruction* next = inst->Next();
455 if (next < end) {
456 locations->SetBit(next->GetDexPc(code_item.insns_));
457 }
458 break;
459 }
460 default:
461 break;
462 }
463 }
464}
465
Calin Juravle702d2602015-04-30 19:28:21 +0100466bool HGraphBuilder::ComputeBranchTargets(const uint16_t* code_ptr,
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000467 const uint16_t* code_end,
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000468 size_t* number_of_branches) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100469 branch_targets_.resize(code_end - code_ptr, nullptr);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000470
471 // Create the first block for the dex instructions, single successor of the entry block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100472 HBasicBlock* block = new (arena_) HBasicBlock(graph_, 0);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100473 branch_targets_[0] = block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000474 entry_block_->AddSuccessor(block);
475
476 // Iterate over all instructions and find branching instructions. Create blocks for
477 // the locations these instructions branch to.
Andreas Gamped881df52014-11-24 23:28:39 -0800478 uint32_t dex_pc = 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000479 while (code_ptr < code_end) {
480 const Instruction& instruction = *Instruction::At(code_ptr);
481 if (instruction.IsBranch()) {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000482 (*number_of_branches)++;
Calin Juravle225ff812014-11-13 16:46:39 +0000483 int32_t target = instruction.GetTargetOffset() + dex_pc;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000484 // Create a block for the target instruction.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000485 FindOrCreateBlockStartingAt(target);
486
Calin Juravle225ff812014-11-13 16:46:39 +0000487 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000488 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle702d2602015-04-30 19:28:21 +0100489
David Brazdilfe659462015-06-24 14:23:56 +0100490 if (instruction.CanFlowThrough()) {
491 if (code_ptr >= code_end) {
Calin Juravle702d2602015-04-30 19:28:21 +0100492 // In the normal case we should never hit this but someone can artificially forge a dex
493 // file to fall-through out the method code. In this case we bail out compilation.
494 return false;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000495 } else {
496 FindOrCreateBlockStartingAt(dex_pc);
Calin Juravle702d2602015-04-30 19:28:21 +0100497 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000498 }
Andreas Gampee4d4d322014-12-04 09:09:57 -0800499 } else if (instruction.IsSwitch()) {
500 SwitchTable table(instruction, dex_pc, instruction.Opcode() == Instruction::SPARSE_SWITCH);
Andreas Gamped881df52014-11-24 23:28:39 -0800501
502 uint16_t num_entries = table.GetNumEntries();
503
Andreas Gampee4d4d322014-12-04 09:09:57 -0800504 // In a packed-switch, the entry at index 0 is the starting key. In a sparse-switch, the
505 // entry at index 0 is the first key, and values are after *all* keys.
506 size_t offset = table.GetFirstValueIndex();
507
508 // Use a larger loop counter type to avoid overflow issues.
509 for (size_t i = 0; i < num_entries; ++i) {
Andreas Gamped881df52014-11-24 23:28:39 -0800510 // The target of the case.
Andreas Gampee4d4d322014-12-04 09:09:57 -0800511 uint32_t target = dex_pc + table.GetEntryAt(i + offset);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000512 FindOrCreateBlockStartingAt(target);
Andreas Gamped881df52014-11-24 23:28:39 -0800513
David Brazdil281a6322015-07-03 10:34:57 +0100514 // Create a block for the switch-case logic. The block gets the dex_pc
515 // of the SWITCH instruction because it is part of its semantics.
516 block = new (arena_) HBasicBlock(graph_, dex_pc);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100517 branch_targets_[table.GetDexPcForIndex(i)] = block;
Andreas Gamped881df52014-11-24 23:28:39 -0800518 }
519
520 // Fall-through. Add a block if there is more code afterwards.
521 dex_pc += instruction.SizeInCodeUnits();
522 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle702d2602015-04-30 19:28:21 +0100523 if (code_ptr >= code_end) {
524 // In the normal case we should never hit this but someone can artificially forge a dex
525 // file to fall-through out the method code. In this case we bail out compilation.
526 // (A switch can fall-through so we don't need to check CanFlowThrough().)
527 return false;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000528 } else {
529 FindOrCreateBlockStartingAt(dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -0800530 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000531 } else {
532 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle225ff812014-11-13 16:46:39 +0000533 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000534 }
535 }
Calin Juravle702d2602015-04-30 19:28:21 +0100536 return true;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000537}
538
David Brazdilfc6a86a2015-06-26 10:33:45 +0000539HBasicBlock* HGraphBuilder::FindBlockStartingAt(int32_t dex_pc) const {
540 DCHECK_GE(dex_pc, 0);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100541 return branch_targets_[dex_pc];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000542}
543
544HBasicBlock* HGraphBuilder::FindOrCreateBlockStartingAt(int32_t dex_pc) {
545 HBasicBlock* block = FindBlockStartingAt(dex_pc);
546 if (block == nullptr) {
547 block = new (arena_) HBasicBlock(graph_, dex_pc);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100548 branch_targets_[dex_pc] = block;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000549 }
550 return block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000551}
552
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100553template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600554void HGraphBuilder::Unop_12x(const Instruction& instruction,
555 Primitive::Type type,
556 uint32_t dex_pc) {
557 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
558 current_block_->AddInstruction(new (arena_) T(type, first, dex_pc));
559 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Roland Levillain88cb1752014-10-20 16:36:47 +0100560}
561
Roland Levillaindff1f282014-11-05 14:15:05 +0000562void HGraphBuilder::Conversion_12x(const Instruction& instruction,
563 Primitive::Type input_type,
Roland Levillain624279f2014-12-04 11:54:28 +0000564 Primitive::Type result_type,
565 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600566 HInstruction* first = LoadLocal(instruction.VRegB(), input_type, dex_pc);
Roland Levillain624279f2014-12-04 11:54:28 +0000567 current_block_->AddInstruction(new (arena_) HTypeConversion(result_type, first, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600568 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100569}
570
571template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000572void HGraphBuilder::Binop_23x(const Instruction& instruction,
573 Primitive::Type type,
574 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600575 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
576 HInstruction* second = LoadLocal(instruction.VRegC(), type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000577 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600578 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000579}
580
581template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000582void HGraphBuilder::Binop_23x_shift(const Instruction& instruction,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600583 Primitive::Type type,
584 uint32_t dex_pc) {
585 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
586 HInstruction* second = LoadLocal(instruction.VRegC(), Primitive::kPrimInt, dex_pc);
587 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
588 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000589}
590
Calin Juravleddb7df22014-11-25 20:56:51 +0000591void HGraphBuilder::Binop_23x_cmp(const Instruction& instruction,
592 Primitive::Type type,
Mark Mendellc4701932015-04-10 13:18:51 -0400593 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700594 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600595 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
596 HInstruction* second = LoadLocal(instruction.VRegC(), type, dex_pc);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700597 current_block_->AddInstruction(new (arena_) HCompare(type, first, second, bias, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600598 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +0000599}
600
Calin Juravle9aec02f2014-11-18 23:06:35 +0000601template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600602void HGraphBuilder::Binop_12x_shift(const Instruction& instruction, Primitive::Type type,
603 uint32_t dex_pc) {
604 HInstruction* first = LoadLocal(instruction.VRegA(), type, dex_pc);
605 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
606 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
607 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000608}
609
610template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000611void HGraphBuilder::Binop_12x(const Instruction& instruction,
612 Primitive::Type type,
613 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600614 HInstruction* first = LoadLocal(instruction.VRegA(), type, dex_pc);
615 HInstruction* second = LoadLocal(instruction.VRegB(), type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000616 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600617 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000618}
619
620template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600621void HGraphBuilder::Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
622 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
623 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100624 if (reverse) {
625 std::swap(first, second);
626 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600627 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second, dex_pc));
628 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100629}
630
631template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600632void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
633 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
634 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100635 if (reverse) {
636 std::swap(first, second);
637 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600638 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second, dex_pc));
639 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100640}
641
Calin Juravle0c25d102015-04-20 14:49:09 +0100642static bool RequiresConstructorBarrier(const DexCompilationUnit* cu, const CompilerDriver& driver) {
Calin Juravle27df7582015-04-17 19:12:31 +0100643 Thread* self = Thread::Current();
Calin Juravle0c25d102015-04-20 14:49:09 +0100644 return cu->IsConstructor()
645 && driver.RequiresConstructorBarrier(self, cu->GetDexFile(), cu->GetClassDefIndex());
Calin Juravle27df7582015-04-17 19:12:31 +0100646}
647
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600648void HGraphBuilder::BuildReturn(const Instruction& instruction,
649 Primitive::Type type,
650 uint32_t dex_pc) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100651 if (type == Primitive::kPrimVoid) {
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100652 if (graph_->ShouldGenerateConstructorBarrier()) {
653 // The compilation unit is null during testing.
654 if (dex_compilation_unit_ != nullptr) {
655 DCHECK(RequiresConstructorBarrier(dex_compilation_unit_, *compiler_driver_))
656 << "Inconsistent use of ShouldGenerateConstructorBarrier. Should not generate a barrier.";
657 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600658 current_block_->AddInstruction(new (arena_) HMemoryBarrier(kStoreStore, dex_pc));
Calin Juravle27df7582015-04-17 19:12:31 +0100659 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600660 current_block_->AddInstruction(new (arena_) HReturnVoid(dex_pc));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100661 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600662 HInstruction* value = LoadLocal(instruction.VRegA(), type, dex_pc);
663 current_block_->AddInstruction(new (arena_) HReturn(value, dex_pc));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100664 }
665 current_block_->AddSuccessor(exit_block_);
666 current_block_ = nullptr;
667}
668
Calin Juravle68ad6492015-08-18 17:08:12 +0100669static InvokeType GetInvokeTypeFromOpCode(Instruction::Code opcode) {
670 switch (opcode) {
671 case Instruction::INVOKE_STATIC:
672 case Instruction::INVOKE_STATIC_RANGE:
673 return kStatic;
674 case Instruction::INVOKE_DIRECT:
675 case Instruction::INVOKE_DIRECT_RANGE:
676 return kDirect;
677 case Instruction::INVOKE_VIRTUAL:
678 case Instruction::INVOKE_VIRTUAL_QUICK:
679 case Instruction::INVOKE_VIRTUAL_RANGE:
680 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
681 return kVirtual;
682 case Instruction::INVOKE_INTERFACE:
683 case Instruction::INVOKE_INTERFACE_RANGE:
684 return kInterface;
685 case Instruction::INVOKE_SUPER_RANGE:
686 case Instruction::INVOKE_SUPER:
687 return kSuper;
688 default:
689 LOG(FATAL) << "Unexpected invoke opcode: " << opcode;
690 UNREACHABLE();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100691 }
Calin Juravle68ad6492015-08-18 17:08:12 +0100692}
693
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000694ArtMethod* HGraphBuilder::ResolveMethod(uint16_t method_idx, InvokeType invoke_type) {
695 ScopedObjectAccess soa(Thread::Current());
Alex Lightfedd91d2016-01-07 14:49:16 -0800696 StackHandleScope<3> hs(soa.Self());
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000697
698 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
699 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
700 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
701 Handle<mirror::Class> compiling_class(hs.NewHandle(GetCompilingClass()));
702
Andreas Gampedae24142015-12-03 17:27:32 -0800703 ArtMethod* resolved_method = class_linker->ResolveMethod<ClassLinker::kForceICCECheck>(
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000704 *dex_compilation_unit_->GetDexFile(),
705 method_idx,
706 dex_compilation_unit_->GetDexCache(),
707 class_loader,
708 /* referrer */ nullptr,
709 invoke_type);
710
711 if (UNLIKELY(resolved_method == nullptr)) {
712 // Clean up any exception left by type resolution.
713 soa.Self()->ClearException();
714 return nullptr;
715 }
716
717 // Check access. The class linker has a fast path for looking into the dex cache
718 // and does not check the access if it hits it.
719 if (compiling_class.Get() == nullptr) {
720 if (!resolved_method->IsPublic()) {
721 return nullptr;
722 }
723 } else if (!compiling_class->CanAccessResolvedMethod(resolved_method->GetDeclaringClass(),
724 resolved_method,
725 dex_compilation_unit_->GetDexCache().Get(),
726 method_idx)) {
727 return nullptr;
728 }
729
730 // We have to special case the invoke-super case, as ClassLinker::ResolveMethod does not.
Alex Lightfedd91d2016-01-07 14:49:16 -0800731 // We need to look at the referrer's super class vtable. We need to do this to know if we need to
732 // make this an invoke-unresolved to handle cross-dex invokes or abstract super methods, both of
733 // which require runtime handling.
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000734 if (invoke_type == kSuper) {
735 if (compiling_class.Get() == nullptr) {
Alex Lightfedd91d2016-01-07 14:49:16 -0800736 // We could not determine the method's class we need to wait until runtime.
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000737 DCHECK(Runtime::Current()->IsAotCompiler());
738 return nullptr;
739 }
Alex Lightfedd91d2016-01-07 14:49:16 -0800740 ArtMethod* current_method = graph_->GetArtMethod();
741 DCHECK(current_method != nullptr);
742 Handle<mirror::Class> methods_class(hs.NewHandle(
743 dex_compilation_unit_->GetClassLinker()->ResolveReferencedClassOfMethod(Thread::Current(),
744 method_idx,
745 current_method)));
746 if (methods_class.Get() == nullptr) {
747 // Invoking a super method requires knowing the actual super class. If we did not resolve
748 // the compiling method's declaring class (which only happens for ahead of time
749 // compilation), bail out.
750 DCHECK(Runtime::Current()->IsAotCompiler());
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000751 return nullptr;
Alex Lightfedd91d2016-01-07 14:49:16 -0800752 } else {
753 ArtMethod* actual_method;
754 if (methods_class->IsInterface()) {
755 actual_method = methods_class->FindVirtualMethodForInterfaceSuper(
756 resolved_method, class_linker->GetImagePointerSize());
757 } else {
758 uint16_t vtable_index = resolved_method->GetMethodIndex();
759 actual_method = compiling_class->GetSuperClass()->GetVTableEntry(
760 vtable_index, class_linker->GetImagePointerSize());
761 }
762 if (actual_method != resolved_method &&
763 !IsSameDexFile(*actual_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
764 // The back-end code generator relies on this check in order to ensure that it will not
765 // attempt to read the dex_cache with a dex_method_index that is not from the correct
766 // dex_file. If we didn't do this check then the dex_method_index will not be updated in the
767 // builder, which means that the code-generator (and compiler driver during sharpening and
768 // inliner, maybe) might invoke an incorrect method.
769 // TODO: The actual method could still be referenced in the current dex file, so we
770 // could try locating it.
771 // TODO: Remove the dex_file restriction.
772 return nullptr;
773 }
774 if (!actual_method->IsInvokable()) {
775 // Fail if the actual method cannot be invoked. Otherwise, the runtime resolution stub
776 // could resolve the callee to the wrong method.
777 return nullptr;
778 }
779 resolved_method = actual_method;
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000780 }
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000781 }
782
783 // Check for incompatible class changes. The class linker has a fast path for
784 // looking into the dex cache and does not check incompatible class changes if it hits it.
785 if (resolved_method->CheckIncompatibleClassChange(invoke_type)) {
786 return nullptr;
787 }
788
789 return resolved_method;
790}
791
Calin Juravle68ad6492015-08-18 17:08:12 +0100792bool HGraphBuilder::BuildInvoke(const Instruction& instruction,
793 uint32_t dex_pc,
794 uint32_t method_idx,
795 uint32_t number_of_vreg_arguments,
796 bool is_range,
797 uint32_t* args,
798 uint32_t register_index) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000799 InvokeType invoke_type = GetInvokeTypeFromOpCode(instruction.Opcode());
Calin Juravle68ad6492015-08-18 17:08:12 +0100800 const char* descriptor = dex_file_->GetMethodShorty(method_idx);
801 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
802
803 // Remove the return type from the 'proto'.
804 size_t number_of_arguments = strlen(descriptor) - 1;
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000805 if (invoke_type != kStatic) { // instance call
Calin Juravle68ad6492015-08-18 17:08:12 +0100806 // One extra argument for 'this'.
807 number_of_arguments++;
808 }
809
810 MethodReference target_method(dex_file_, method_idx);
Calin Juravle68ad6492015-08-18 17:08:12 +0100811
Calin Juravle5d01db12015-08-25 15:02:42 +0100812 // Special handling for string init.
813 int32_t string_init_offset = 0;
814 bool is_string_init = compiler_driver_->IsStringInit(method_idx,
815 dex_file_,
816 &string_init_offset);
817 // Replace calls to String.<init> with StringFactory.
818 if (is_string_init) {
Vladimir Markodc151b22015-10-15 18:02:30 +0100819 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
820 HInvokeStaticOrDirect::MethodLoadKind::kStringInit,
821 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
822 dchecked_integral_cast<uint64_t>(string_init_offset),
823 0U
824 };
Calin Juravle5d01db12015-08-25 15:02:42 +0100825 HInvoke* invoke = new (arena_) HInvokeStaticOrDirect(
826 arena_,
827 number_of_arguments - 1,
828 Primitive::kPrimNot /*return_type */,
829 dex_pc,
830 method_idx,
831 target_method,
832 dispatch_info,
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000833 invoke_type,
Calin Juravle5d01db12015-08-25 15:02:42 +0100834 kStatic /* optimized_invoke_type */,
835 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit);
836 return HandleStringInit(invoke,
837 number_of_vreg_arguments,
838 args,
839 register_index,
840 is_range,
841 descriptor);
842 }
843
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000844 ArtMethod* resolved_method = ResolveMethod(method_idx, invoke_type);
845
Alex Lightfedd91d2016-01-07 14:49:16 -0800846 if (UNLIKELY(resolved_method == nullptr)) {
Calin Juravle175dc732015-08-25 15:42:32 +0100847 MaybeRecordStat(MethodCompilationStat::kUnresolvedMethod);
848 HInvoke* invoke = new (arena_) HInvokeUnresolved(arena_,
849 number_of_arguments,
850 return_type,
851 dex_pc,
852 method_idx,
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000853 invoke_type);
Calin Juravle175dc732015-08-25 15:42:32 +0100854 return HandleInvoke(invoke,
855 number_of_vreg_arguments,
856 args,
857 register_index,
858 is_range,
859 descriptor,
860 nullptr /* clinit_check */);
Calin Juravle68ad6492015-08-18 17:08:12 +0100861 }
862
Calin Juravle68ad6492015-08-18 17:08:12 +0100863 // Potential class initialization check, in the case of a static method call.
864 HClinitCheck* clinit_check = nullptr;
865 HInvoke* invoke = nullptr;
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000866 if (invoke_type == kDirect || invoke_type == kStatic || invoke_type == kSuper) {
Calin Juravle68ad6492015-08-18 17:08:12 +0100867 // By default, consider that the called method implicitly requires
868 // an initialization check of its declaring method.
869 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement
870 = HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000871 ScopedObjectAccess soa(Thread::Current());
872 if (invoke_type == kStatic) {
873 clinit_check = ProcessClinitCheckForInvoke(
874 dex_pc, resolved_method, method_idx, &clinit_check_requirement);
875 } else if (invoke_type == kSuper) {
876 if (IsSameDexFile(*resolved_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
877 // Update the target method to the one resolved. Note that this may be a no-op if
878 // we resolved to the method referenced by the instruction.
879 method_idx = resolved_method->GetDexMethodIndex();
880 target_method = MethodReference(dex_file_, method_idx);
881 }
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100882 }
Calin Juravle68ad6492015-08-18 17:08:12 +0100883
Vladimir Markodc151b22015-10-15 18:02:30 +0100884 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
885 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
886 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
887 0u,
888 0U
889 };
Calin Juravle68ad6492015-08-18 17:08:12 +0100890 invoke = new (arena_) HInvokeStaticOrDirect(arena_,
891 number_of_arguments,
892 return_type,
893 dex_pc,
894 method_idx,
895 target_method,
896 dispatch_info,
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000897 invoke_type,
898 invoke_type,
Calin Juravle68ad6492015-08-18 17:08:12 +0100899 clinit_check_requirement);
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000900 } else if (invoke_type == kVirtual) {
901 ScopedObjectAccess soa(Thread::Current()); // Needed for the method index
Calin Juravle68ad6492015-08-18 17:08:12 +0100902 invoke = new (arena_) HInvokeVirtual(arena_,
903 number_of_arguments,
904 return_type,
905 dex_pc,
906 method_idx,
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000907 resolved_method->GetMethodIndex());
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100908 } else {
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000909 DCHECK_EQ(invoke_type, kInterface);
910 ScopedObjectAccess soa(Thread::Current()); // Needed for the method index
Calin Juravle68ad6492015-08-18 17:08:12 +0100911 invoke = new (arena_) HInvokeInterface(arena_,
912 number_of_arguments,
913 return_type,
914 dex_pc,
915 method_idx,
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000916 resolved_method->GetDexMethodIndex());
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100917 }
Calin Juravle68ad6492015-08-18 17:08:12 +0100918
Calin Juravle5d01db12015-08-25 15:02:42 +0100919 return HandleInvoke(invoke,
920 number_of_vreg_arguments,
921 args,
922 register_index,
923 is_range,
924 descriptor,
925 clinit_check);
Calin Juravle68ad6492015-08-18 17:08:12 +0100926}
927
Nicolas Geoffray729645a2015-11-19 13:29:02 +0000928bool HGraphBuilder::BuildNewInstance(uint16_t type_index, uint32_t dex_pc) {
929 bool finalizable;
930 bool can_throw = NeedsAccessCheck(type_index, &finalizable);
931
932 // Only the non-resolved entrypoint handles the finalizable class case. If we
933 // need access checks, then we haven't resolved the method and the class may
934 // again be finalizable.
935 QuickEntrypointEnum entrypoint = (finalizable || can_throw)
936 ? kQuickAllocObject
937 : kQuickAllocObjectInitialized;
938
939 ScopedObjectAccess soa(Thread::Current());
940 StackHandleScope<3> hs(soa.Self());
941 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
942 dex_compilation_unit_->GetClassLinker()->FindDexCache(
943 soa.Self(), *dex_compilation_unit_->GetDexFile())));
944 Handle<mirror::Class> resolved_class(hs.NewHandle(dex_cache->GetResolvedType(type_index)));
945 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
946 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
947 outer_compilation_unit_->GetClassLinker()->FindDexCache(soa.Self(), outer_dex_file)));
948
949 if (outer_dex_cache.Get() != dex_cache.Get()) {
950 // We currently do not support inlining allocations across dex files.
951 return false;
952 }
953
954 HLoadClass* load_class = new (arena_) HLoadClass(
955 graph_->GetCurrentMethod(),
956 type_index,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +0000957 outer_dex_file,
Nicolas Geoffray729645a2015-11-19 13:29:02 +0000958 IsOutermostCompilingClass(type_index),
959 dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +0000960 /*needs_access_check*/ can_throw,
961 compiler_driver_->CanAssumeTypeIsPresentInDexCache(outer_dex_file, type_index));
Nicolas Geoffray729645a2015-11-19 13:29:02 +0000962
963 current_block_->AddInstruction(load_class);
964 HInstruction* cls = load_class;
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +0000965 if (!IsInitialized(resolved_class)) {
Nicolas Geoffray729645a2015-11-19 13:29:02 +0000966 cls = new (arena_) HClinitCheck(load_class, dex_pc);
967 current_block_->AddInstruction(cls);
968 }
969
970 current_block_->AddInstruction(new (arena_) HNewInstance(
971 cls,
972 graph_->GetCurrentMethod(),
973 dex_pc,
974 type_index,
975 *dex_compilation_unit_->GetDexFile(),
976 can_throw,
977 finalizable,
978 entrypoint));
979 return true;
980}
981
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +0000982static bool IsSubClass(mirror::Class* to_test, mirror::Class* super_class)
983 SHARED_REQUIRES(Locks::mutator_lock_) {
984 return to_test != nullptr && !to_test->IsInterface() && to_test->IsSubClass(super_class);
985}
986
987bool HGraphBuilder::IsInitialized(Handle<mirror::Class> cls) const {
Nicolas Geoffray729645a2015-11-19 13:29:02 +0000988 if (cls.Get() == nullptr) {
989 return false;
990 }
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +0000991
992 // `CanAssumeClassIsLoaded` will return true if we're JITting, or will
993 // check whether the class is in an image for the AOT compilation.
994 if (cls->IsInitialized() &&
995 compiler_driver_->CanAssumeClassIsLoaded(cls.Get())) {
Nicolas Geoffray729645a2015-11-19 13:29:02 +0000996 return true;
997 }
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +0000998
999 if (IsSubClass(GetOutermostCompilingClass(), cls.Get())) {
1000 return true;
1001 }
1002
1003 // TODO: We should walk over the inlined methods, but we don't pass
1004 // that information to the builder.
1005 if (IsSubClass(GetCompilingClass(), cls.Get())) {
1006 return true;
1007 }
1008
1009 return false;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00001010}
1011
Calin Juravle68ad6492015-08-18 17:08:12 +01001012HClinitCheck* HGraphBuilder::ProcessClinitCheckForInvoke(
1013 uint32_t dex_pc,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001014 ArtMethod* resolved_method,
Calin Juravle68ad6492015-08-18 17:08:12 +01001015 uint32_t method_idx,
1016 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001017 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
1018 Thread* self = Thread::Current();
1019 StackHandleScope<4> hs(self);
Calin Juravle68ad6492015-08-18 17:08:12 +01001020 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
1021 dex_compilation_unit_->GetClassLinker()->FindDexCache(
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001022 self, *dex_compilation_unit_->GetDexFile())));
Calin Juravle68ad6492015-08-18 17:08:12 +01001023 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001024 outer_compilation_unit_->GetClassLinker()->FindDexCache(
1025 self, outer_dex_file)));
Calin Juravle68ad6492015-08-18 17:08:12 +01001026 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +00001027 Handle<mirror::Class> resolved_method_class(hs.NewHandle(resolved_method->GetDeclaringClass()));
Calin Juravle68ad6492015-08-18 17:08:12 +01001028
1029 // The index at which the method's class is stored in the DexCache's type array.
1030 uint32_t storage_index = DexFile::kDexNoIndex;
1031 bool is_outer_class = (resolved_method->GetDeclaringClass() == outer_class.Get());
1032 if (is_outer_class) {
1033 storage_index = outer_class->GetDexTypeIndex();
1034 } else if (outer_dex_cache.Get() == dex_cache.Get()) {
1035 // Get `storage_index` from IsClassOfStaticMethodAvailableToReferrer.
1036 compiler_driver_->IsClassOfStaticMethodAvailableToReferrer(outer_dex_cache.Get(),
1037 GetCompilingClass(),
1038 resolved_method,
1039 method_idx,
1040 &storage_index);
1041 }
1042
1043 HClinitCheck* clinit_check = nullptr;
1044
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +00001045 if (IsInitialized(resolved_method_class)) {
Calin Juravle68ad6492015-08-18 17:08:12 +01001046 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
1047 } else if (storage_index != DexFile::kDexNoIndex) {
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +00001048 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
1049 HLoadClass* load_class = new (arena_) HLoadClass(
1050 graph_->GetCurrentMethod(),
1051 storage_index,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001052 outer_dex_file,
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +00001053 is_outer_class,
1054 dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001055 /*needs_access_check*/ false,
1056 compiler_driver_->CanAssumeTypeIsPresentInDexCache(outer_dex_file, storage_index));
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +00001057 current_block_->AddInstruction(load_class);
1058 clinit_check = new (arena_) HClinitCheck(load_class, dex_pc);
1059 current_block_->AddInstruction(clinit_check);
Calin Juravle68ad6492015-08-18 17:08:12 +01001060 }
1061 return clinit_check;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001062}
1063
Calin Juravle5d01db12015-08-25 15:02:42 +01001064bool HGraphBuilder::SetupInvokeArguments(HInvoke* invoke,
1065 uint32_t number_of_vreg_arguments,
1066 uint32_t* args,
1067 uint32_t register_index,
1068 bool is_range,
1069 const char* descriptor,
1070 size_t start_index,
1071 size_t* argument_index) {
Calin Juravle68ad6492015-08-18 17:08:12 +01001072 uint32_t descriptor_index = 1; // Skip the return type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001073 uint32_t dex_pc = invoke->GetDexPc();
Calin Juravle68ad6492015-08-18 17:08:12 +01001074
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001075 for (size_t i = start_index;
1076 // Make sure we don't go over the expected arguments or over the number of
1077 // dex registers given. If the instruction was seen as dead by the verifier,
1078 // it hasn't been properly checked.
Calin Juravle5d01db12015-08-25 15:02:42 +01001079 (i < number_of_vreg_arguments) && (*argument_index < invoke->GetNumberOfArguments());
1080 i++, (*argument_index)++) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001081 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001082 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001083 if (!is_range
1084 && is_wide
1085 && ((i + 1 == number_of_vreg_arguments) || (args[i] + 1 != args[i + 1]))) {
1086 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
1087 // reject any class where this is violated. However, the verifier only does these checks
1088 // on non trivially dead instructions, so we just bailout the compilation.
1089 VLOG(compiler) << "Did not compile "
1090 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
1091 << " because of non-sequential dex register pair in wide argument";
1092 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1093 return false;
1094 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001095 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type, dex_pc);
Calin Juravle5d01db12015-08-25 15:02:42 +01001096 invoke->SetArgumentAt(*argument_index, arg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001097 if (is_wide) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001098 i++;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001099 }
1100 }
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001101
Calin Juravle5d01db12015-08-25 15:02:42 +01001102 if (*argument_index != invoke->GetNumberOfArguments()) {
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001103 VLOG(compiler) << "Did not compile "
1104 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
1105 << " because of wrong number of arguments in invoke instruction";
1106 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1107 return false;
1108 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001109
Vladimir Markob554b5a2015-11-06 12:57:55 +00001110 if (invoke->IsInvokeStaticOrDirect() &&
1111 HInvokeStaticOrDirect::NeedsCurrentMethodInput(
1112 invoke->AsInvokeStaticOrDirect()->GetMethodLoadKind())) {
Calin Juravle5d01db12015-08-25 15:02:42 +01001113 invoke->SetArgumentAt(*argument_index, graph_->GetCurrentMethod());
1114 (*argument_index)++;
1115 }
1116
1117 return true;
1118}
1119
1120bool HGraphBuilder::HandleInvoke(HInvoke* invoke,
1121 uint32_t number_of_vreg_arguments,
1122 uint32_t* args,
1123 uint32_t register_index,
1124 bool is_range,
1125 const char* descriptor,
1126 HClinitCheck* clinit_check) {
1127 DCHECK(!invoke->IsInvokeStaticOrDirect() || !invoke->AsInvokeStaticOrDirect()->IsStringInit());
1128
1129 size_t start_index = 0;
1130 size_t argument_index = 0;
1131 if (invoke->GetOriginalInvokeType() != InvokeType::kStatic) { // Instance call.
Calin Juravle5d01db12015-08-25 15:02:42 +01001132 HInstruction* arg = LoadLocal(
1133 is_range ? register_index : args[0], Primitive::kPrimNot, invoke->GetDexPc());
1134 HNullCheck* null_check = new (arena_) HNullCheck(arg, invoke->GetDexPc());
1135 current_block_->AddInstruction(null_check);
Calin Juravle5d01db12015-08-25 15:02:42 +01001136 invoke->SetArgumentAt(0, null_check);
1137 start_index = 1;
1138 argument_index = 1;
1139 }
1140
1141 if (!SetupInvokeArguments(invoke,
1142 number_of_vreg_arguments,
1143 args,
1144 register_index,
1145 is_range,
1146 descriptor,
1147 start_index,
1148 &argument_index)) {
1149 return false;
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001150 }
1151
Calin Juravle68ad6492015-08-18 17:08:12 +01001152 if (clinit_check != nullptr) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001153 // Add the class initialization check as last input of `invoke`.
Calin Juravle68ad6492015-08-18 17:08:12 +01001154 DCHECK(invoke->IsInvokeStaticOrDirect());
1155 DCHECK(invoke->AsInvokeStaticOrDirect()->GetClinitCheckRequirement()
1156 == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit);
Roland Levillain3e3d7332015-04-28 11:00:54 +01001157 invoke->SetArgumentAt(argument_index, clinit_check);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001158 argument_index++;
Roland Levillain4c0eb422015-04-24 16:43:49 +01001159 }
1160
Calin Juravle5d01db12015-08-25 15:02:42 +01001161 current_block_->AddInstruction(invoke);
1162 latest_result_ = invoke;
1163
1164 return true;
1165}
1166
1167bool HGraphBuilder::HandleStringInit(HInvoke* invoke,
1168 uint32_t number_of_vreg_arguments,
1169 uint32_t* args,
1170 uint32_t register_index,
1171 bool is_range,
1172 const char* descriptor) {
1173 DCHECK(invoke->IsInvokeStaticOrDirect());
1174 DCHECK(invoke->AsInvokeStaticOrDirect()->IsStringInit());
1175
1176 size_t start_index = 1;
1177 size_t argument_index = 0;
1178 if (!SetupInvokeArguments(invoke,
1179 number_of_vreg_arguments,
1180 args,
1181 register_index,
1182 is_range,
1183 descriptor,
1184 start_index,
1185 &argument_index)) {
1186 return false;
Jeff Hao848f70a2014-01-15 13:49:50 -08001187 }
Calin Juravle0eedd7e2015-08-20 14:48:00 +01001188
Calin Juravle5d01db12015-08-25 15:02:42 +01001189 // Add move-result for StringFactory method.
1190 uint32_t orig_this_reg = is_range ? register_index : args[0];
David Brazdil6de19382016-01-08 17:37:10 +00001191 HInstruction* new_instance = LoadLocal(orig_this_reg, Primitive::kPrimNot, invoke->GetDexPc());
1192 invoke->SetArgumentAt(argument_index, new_instance);
Calin Juravle5d01db12015-08-25 15:02:42 +01001193 current_block_->AddInstruction(invoke);
Calin Juravle5d01db12015-08-25 15:02:42 +01001194
Calin Juravle0eedd7e2015-08-20 14:48:00 +01001195 latest_result_ = invoke;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001196 return true;
1197}
1198
Calin Juravlee460d1d2015-09-29 04:52:17 +01001199static Primitive::Type GetFieldAccessType(const DexFile& dex_file, uint16_t field_index) {
1200 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
1201 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
1202 return Primitive::GetType(type[0]);
1203}
1204
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001205bool HGraphBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001206 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001207 bool is_put) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001208 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1209 uint32_t obj_reg = instruction.VRegB_22c();
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001210 uint16_t field_index;
1211 if (instruction.IsQuickened()) {
1212 if (!CanDecodeQuickenedInfo()) {
1213 return false;
1214 }
1215 field_index = LookupQuickenedInfo(dex_pc);
1216 } else {
1217 field_index = instruction.VRegC_22c();
1218 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001219
1220 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierc7853442015-03-27 14:35:38 -07001221 ArtField* resolved_field =
1222 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001223
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001224
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001225 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot, dex_pc);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001226 HInstruction* null_check = new (arena_) HNullCheck(object, dex_pc);
1227 current_block_->AddInstruction(null_check);
1228
1229 Primitive::Type field_type = (resolved_field == nullptr)
1230 ? GetFieldAccessType(*dex_file_, field_index)
1231 : resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001232 if (is_put) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001233 HInstruction* value = LoadLocal(source_or_dest_reg, field_type, dex_pc);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001234 HInstruction* field_set = nullptr;
1235 if (resolved_field == nullptr) {
1236 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
1237 field_set = new (arena_) HUnresolvedInstanceFieldSet(null_check,
1238 value,
1239 field_type,
1240 field_index,
1241 dex_pc);
1242 } else {
Mingyao Yang8df69d42015-10-22 15:40:58 -07001243 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Calin Juravlee460d1d2015-09-29 04:52:17 +01001244 field_set = new (arena_) HInstanceFieldSet(null_check,
1245 value,
1246 field_type,
1247 resolved_field->GetOffset(),
1248 resolved_field->IsVolatile(),
1249 field_index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07001250 class_def_index,
Calin Juravlee460d1d2015-09-29 04:52:17 +01001251 *dex_file_,
1252 dex_compilation_unit_->GetDexCache(),
1253 dex_pc);
1254 }
1255 current_block_->AddInstruction(field_set);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001256 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001257 HInstruction* field_get = nullptr;
1258 if (resolved_field == nullptr) {
1259 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
1260 field_get = new (arena_) HUnresolvedInstanceFieldGet(null_check,
1261 field_type,
1262 field_index,
1263 dex_pc);
1264 } else {
Mingyao Yang8df69d42015-10-22 15:40:58 -07001265 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Calin Juravlee460d1d2015-09-29 04:52:17 +01001266 field_get = new (arena_) HInstanceFieldGet(null_check,
1267 field_type,
1268 resolved_field->GetOffset(),
1269 resolved_field->IsVolatile(),
1270 field_index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07001271 class_def_index,
Calin Juravlee460d1d2015-09-29 04:52:17 +01001272 *dex_file_,
1273 dex_compilation_unit_->GetDexCache(),
1274 dex_pc);
1275 }
1276 current_block_->AddInstruction(field_get);
1277 UpdateLocal(source_or_dest_reg, field_get, dex_pc);
Calin Juravlee6f49b42015-09-17 14:04:33 +00001278 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001279
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001280 return true;
1281}
1282
Nicolas Geoffray30451742015-06-19 13:32:41 +01001283static mirror::Class* GetClassFrom(CompilerDriver* driver,
1284 const DexCompilationUnit& compilation_unit) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001285 ScopedObjectAccess soa(Thread::Current());
1286 StackHandleScope<2> hs(soa.Self());
Nicolas Geoffray30451742015-06-19 13:32:41 +01001287 const DexFile& dex_file = *compilation_unit.GetDexFile();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001288 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Nicolas Geoffray30451742015-06-19 13:32:41 +01001289 soa.Decode<mirror::ClassLoader*>(compilation_unit.GetClassLoader())));
1290 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001291 compilation_unit.GetClassLinker()->FindDexCache(soa.Self(), dex_file)));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001292
Nicolas Geoffray30451742015-06-19 13:32:41 +01001293 return driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, &compilation_unit);
1294}
1295
1296mirror::Class* HGraphBuilder::GetOutermostCompilingClass() const {
1297 return GetClassFrom(compiler_driver_, *outer_compilation_unit_);
1298}
1299
1300mirror::Class* HGraphBuilder::GetCompilingClass() const {
1301 return GetClassFrom(compiler_driver_, *dex_compilation_unit_);
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001302}
1303
1304bool HGraphBuilder::IsOutermostCompilingClass(uint16_t type_index) const {
1305 ScopedObjectAccess soa(Thread::Current());
1306 StackHandleScope<4> hs(soa.Self());
1307 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001308 dex_compilation_unit_->GetClassLinker()->FindDexCache(
1309 soa.Self(), *dex_compilation_unit_->GetDexFile())));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001310 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1311 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
1312 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
1313 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
Nicolas Geoffrayafd06412015-06-20 22:44:47 +01001314 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001315
Calin Juravle4e2a5572015-10-07 18:55:43 +01001316 // GetOutermostCompilingClass returns null when the class is unresolved
1317 // (e.g. if it derives from an unresolved class). This is bogus knowing that
1318 // we are compiling it.
1319 // When this happens we cannot establish a direct relation between the current
1320 // class and the outer class, so we return false.
1321 // (Note that this is only used for optimizing invokes and field accesses)
1322 return (cls.Get() != nullptr) && (outer_class.Get() == cls.Get());
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001323}
1324
Calin Juravle07380a22015-09-17 14:15:12 +01001325void HGraphBuilder::BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
1326 uint32_t dex_pc,
1327 bool is_put,
1328 Primitive::Type field_type) {
1329 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1330 uint16_t field_index = instruction.VRegB_21c();
1331
1332 if (is_put) {
1333 HInstruction* value = LoadLocal(source_or_dest_reg, field_type, dex_pc);
1334 current_block_->AddInstruction(
1335 new (arena_) HUnresolvedStaticFieldSet(value, field_type, field_index, dex_pc));
1336 } else {
1337 current_block_->AddInstruction(
1338 new (arena_) HUnresolvedStaticFieldGet(field_type, field_index, dex_pc));
1339 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction(), dex_pc);
1340 }
1341}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001342bool HGraphBuilder::BuildStaticFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001343 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001344 bool is_put) {
1345 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1346 uint16_t field_index = instruction.VRegB_21c();
1347
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001348 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray729645a2015-11-19 13:29:02 +00001349 StackHandleScope<5> hs(soa.Self());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001350 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001351 dex_compilation_unit_->GetClassLinker()->FindDexCache(
1352 soa.Self(), *dex_compilation_unit_->GetDexFile())));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001353 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1354 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
Mathieu Chartierc7853442015-03-27 14:35:38 -07001355 ArtField* resolved_field = compiler_driver_->ResolveField(
1356 soa, dex_cache, class_loader, dex_compilation_unit_, field_index, true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001357
Mathieu Chartierc7853442015-03-27 14:35:38 -07001358 if (resolved_field == nullptr) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001359 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
1360 Primitive::Type field_type = GetFieldAccessType(*dex_file_, field_index);
Calin Juravle07380a22015-09-17 14:15:12 +01001361 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001362 return true;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001363 }
1364
Calin Juravle07380a22015-09-17 14:15:12 +01001365 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001366 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
1367 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001368 outer_compilation_unit_->GetClassLinker()->FindDexCache(soa.Self(), outer_dex_file)));
Nicolas Geoffray30451742015-06-19 13:32:41 +01001369 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001370
1371 // The index at which the field's class is stored in the DexCache's type array.
1372 uint32_t storage_index;
Nicolas Geoffray30451742015-06-19 13:32:41 +01001373 bool is_outer_class = (outer_class.Get() == resolved_field->GetDeclaringClass());
1374 if (is_outer_class) {
1375 storage_index = outer_class->GetDexTypeIndex();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001376 } else if (outer_dex_cache.Get() != dex_cache.Get()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001377 // The compiler driver cannot currently understand multiple dex caches involved. Just bailout.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001378 return false;
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001379 } else {
Calin Juravle07380a22015-09-17 14:15:12 +01001380 // TODO: This is rather expensive. Perf it and cache the results if needed.
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001381 std::pair<bool, bool> pair = compiler_driver_->IsFastStaticField(
1382 outer_dex_cache.Get(),
Nicolas Geoffray30451742015-06-19 13:32:41 +01001383 GetCompilingClass(),
Mathieu Chartierc7853442015-03-27 14:35:38 -07001384 resolved_field,
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001385 field_index,
1386 &storage_index);
1387 bool can_easily_access = is_put ? pair.second : pair.first;
1388 if (!can_easily_access) {
Calin Juravle07380a22015-09-17 14:15:12 +01001389 MaybeRecordStat(MethodCompilationStat::kUnresolvedFieldNotAFastAccess);
1390 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
1391 return true;
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001392 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001393 }
1394
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001395 bool is_in_cache =
1396 compiler_driver_->CanAssumeTypeIsPresentInDexCache(outer_dex_file, storage_index);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001397 HLoadClass* constant = new (arena_) HLoadClass(graph_->GetCurrentMethod(),
1398 storage_index,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001399 outer_dex_file,
Nicolas Geoffray30451742015-06-19 13:32:41 +01001400 is_outer_class,
Calin Juravle98893e12015-10-02 21:05:03 +01001401 dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001402 /*needs_access_check*/ false,
1403 is_in_cache);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001404 current_block_->AddInstruction(constant);
1405
1406 HInstruction* cls = constant;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00001407
1408 Handle<mirror::Class> klass(hs.NewHandle(resolved_field->GetDeclaringClass()));
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +00001409 if (!IsInitialized(klass)) {
Calin Juravle225ff812014-11-13 16:46:39 +00001410 cls = new (arena_) HClinitCheck(constant, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001411 current_block_->AddInstruction(cls);
1412 }
Mingyao Yang8df69d42015-10-22 15:40:58 -07001413
Nicolas Geoffray729645a2015-11-19 13:29:02 +00001414 uint16_t class_def_index = klass->GetDexClassDefIndex();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001415 if (is_put) {
1416 // We need to keep the class alive before loading the value.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001417 HInstruction* value = LoadLocal(source_or_dest_reg, field_type, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001418 DCHECK_EQ(value->GetType(), field_type);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001419 current_block_->AddInstruction(new (arena_) HStaticFieldSet(cls,
1420 value,
1421 field_type,
1422 resolved_field->GetOffset(),
1423 resolved_field->IsVolatile(),
1424 field_index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07001425 class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07001426 *dex_file_,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001427 dex_cache_,
1428 dex_pc));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001429 } else {
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001430 current_block_->AddInstruction(new (arena_) HStaticFieldGet(cls,
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));
1439 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001440 }
1441 return true;
1442}
1443
Calin Juravlebacfec32014-11-14 15:54:36 +00001444void HGraphBuilder::BuildCheckedDivRem(uint16_t out_vreg,
1445 uint16_t first_vreg,
1446 int64_t second_vreg_or_constant,
1447 uint32_t dex_pc,
1448 Primitive::Type type,
1449 bool second_is_constant,
1450 bool isDiv) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001451 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Calin Juravled0d48522014-11-04 16:40:20 +00001452
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001453 HInstruction* first = LoadLocal(first_vreg, type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001454 HInstruction* second = nullptr;
1455 if (second_is_constant) {
1456 if (type == Primitive::kPrimInt) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001457 second = graph_->GetIntConstant(second_vreg_or_constant, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001458 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001459 second = graph_->GetLongConstant(second_vreg_or_constant, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001460 }
1461 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001462 second = LoadLocal(second_vreg_or_constant, type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001463 }
1464
1465 if (!second_is_constant
1466 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0)
1467 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) {
1468 second = new (arena_) HDivZeroCheck(second, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001469 current_block_->AddInstruction(second);
Calin Juravled0d48522014-11-04 16:40:20 +00001470 }
1471
Calin Juravlebacfec32014-11-14 15:54:36 +00001472 if (isDiv) {
1473 current_block_->AddInstruction(new (arena_) HDiv(type, first, second, dex_pc));
1474 } else {
1475 current_block_->AddInstruction(new (arena_) HRem(type, first, second, dex_pc));
1476 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001477 UpdateLocal(out_vreg, current_block_->GetLastInstruction(), dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001478}
1479
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001480void HGraphBuilder::BuildArrayAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001481 uint32_t dex_pc,
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001482 bool is_put,
1483 Primitive::Type anticipated_type) {
1484 uint8_t source_or_dest_reg = instruction.VRegA_23x();
1485 uint8_t array_reg = instruction.VRegB_23x();
1486 uint8_t index_reg = instruction.VRegC_23x();
1487
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001488 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001489 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001490 current_block_->AddInstruction(object);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001491
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001492 HInstruction* length = new (arena_) HArrayLength(object, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001493 current_block_->AddInstruction(length);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001494 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001495 index = new (arena_) HBoundsCheck(index, length, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001496 current_block_->AddInstruction(index);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001497 if (is_put) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001498 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001499 // TODO: Insert a type check node if the type is Object.
Nicolas Geoffray39468442014-09-02 15:17:15 +01001500 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001501 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001502 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001503 current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type, dex_pc));
1504 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001505 }
Mark Mendell1152c922015-04-24 17:06:35 -04001506 graph_->SetHasBoundsChecks(true);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001507}
1508
Calin Juravle225ff812014-11-13 16:46:39 +00001509void HGraphBuilder::BuildFilledNewArray(uint32_t dex_pc,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001510 uint32_t type_index,
1511 uint32_t number_of_vreg_arguments,
1512 bool is_range,
1513 uint32_t* args,
1514 uint32_t register_index) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001515 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments, dex_pc);
Mingyao Yangfb8464a2015-11-02 10:56:59 -08001516 bool finalizable;
1517 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index, &finalizable)
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001518 ? kQuickAllocArrayWithAccessCheck
1519 : kQuickAllocArray;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01001520 HInstruction* object = new (arena_) HNewArray(length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01001521 graph_->GetCurrentMethod(),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01001522 dex_pc,
1523 type_index,
1524 *dex_compilation_unit_->GetDexFile(),
1525 entrypoint);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001526 current_block_->AddInstruction(object);
1527
1528 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1529 DCHECK_EQ(descriptor[0], '[') << descriptor;
1530 char primitive = descriptor[1];
1531 DCHECK(primitive == 'I'
1532 || primitive == 'L'
1533 || primitive == '[') << descriptor;
1534 bool is_reference_array = (primitive == 'L') || (primitive == '[');
1535 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
1536
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001537 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001538 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type, dex_pc);
1539 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001540 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001541 new (arena_) HArraySet(object, index, value, type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001542 }
1543 latest_result_ = object;
1544}
1545
1546template <typename T>
1547void HGraphBuilder::BuildFillArrayData(HInstruction* object,
1548 const T* data,
1549 uint32_t element_count,
1550 Primitive::Type anticipated_type,
Calin Juravle225ff812014-11-13 16:46:39 +00001551 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001552 for (uint32_t i = 0; i < element_count; ++i) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001553 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1554 HInstruction* value = graph_->GetIntConstant(data[i], dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001555 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001556 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001557 }
1558}
1559
Calin Juravle225ff812014-11-13 16:46:39 +00001560void HGraphBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001561 HInstruction* array = LoadLocal(instruction.VRegA_31t(), Primitive::kPrimNot, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001562 HNullCheck* null_check = new (arena_) HNullCheck(array, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001563 current_block_->AddInstruction(null_check);
Calin Juravled0d48522014-11-04 16:40:20 +00001564
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001565 HInstruction* length = new (arena_) HArrayLength(null_check, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001566 current_block_->AddInstruction(length);
1567
Calin Juravle225ff812014-11-13 16:46:39 +00001568 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
Calin Juravled0d48522014-11-04 16:40:20 +00001569 const Instruction::ArrayDataPayload* payload =
1570 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_start_ + payload_offset);
1571 const uint8_t* data = payload->data;
1572 uint32_t element_count = payload->element_count;
1573
1574 // Implementation of this DEX instruction seems to be that the bounds check is
1575 // done before doing any stores.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001576 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001577 current_block_->AddInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
Calin Juravled0d48522014-11-04 16:40:20 +00001578
1579 switch (payload->element_width) {
1580 case 1:
1581 BuildFillArrayData(null_check,
1582 reinterpret_cast<const int8_t*>(data),
1583 element_count,
1584 Primitive::kPrimByte,
Calin Juravle225ff812014-11-13 16:46:39 +00001585 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001586 break;
1587 case 2:
1588 BuildFillArrayData(null_check,
1589 reinterpret_cast<const int16_t*>(data),
1590 element_count,
1591 Primitive::kPrimShort,
Calin Juravle225ff812014-11-13 16:46:39 +00001592 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001593 break;
1594 case 4:
1595 BuildFillArrayData(null_check,
1596 reinterpret_cast<const int32_t*>(data),
1597 element_count,
1598 Primitive::kPrimInt,
Calin Juravle225ff812014-11-13 16:46:39 +00001599 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001600 break;
1601 case 8:
1602 BuildFillWideArrayData(null_check,
1603 reinterpret_cast<const int64_t*>(data),
1604 element_count,
Calin Juravle225ff812014-11-13 16:46:39 +00001605 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001606 break;
1607 default:
1608 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1609 }
Mark Mendell1152c922015-04-24 17:06:35 -04001610 graph_->SetHasBoundsChecks(true);
Calin Juravled0d48522014-11-04 16:40:20 +00001611}
1612
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001613void HGraphBuilder::BuildFillWideArrayData(HInstruction* object,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +01001614 const int64_t* data,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001615 uint32_t element_count,
Calin Juravle225ff812014-11-13 16:46:39 +00001616 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001617 for (uint32_t i = 0; i < element_count; ++i) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001618 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1619 HInstruction* value = graph_->GetLongConstant(data[i], dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001620 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001621 object, index, value, Primitive::kPrimLong, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001622 }
1623}
1624
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001625static TypeCheckKind ComputeTypeCheckKind(Handle<mirror::Class> cls)
1626 SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle98893e12015-10-02 21:05:03 +01001627 if (cls.Get() == nullptr) {
1628 return TypeCheckKind::kUnresolvedCheck;
1629 } else if (cls->IsInterface()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001630 return TypeCheckKind::kInterfaceCheck;
1631 } else if (cls->IsArrayClass()) {
1632 if (cls->GetComponentType()->IsObjectClass()) {
1633 return TypeCheckKind::kArrayObjectCheck;
1634 } else if (cls->CannotBeAssignedFromOtherTypes()) {
1635 return TypeCheckKind::kExactCheck;
1636 } else {
1637 return TypeCheckKind::kArrayCheck;
1638 }
1639 } else if (cls->IsFinal()) {
1640 return TypeCheckKind::kExactCheck;
1641 } else if (cls->IsAbstract()) {
1642 return TypeCheckKind::kAbstractClassCheck;
1643 } else {
1644 return TypeCheckKind::kClassHierarchyCheck;
1645 }
1646}
1647
Calin Juravle98893e12015-10-02 21:05:03 +01001648void HGraphBuilder::BuildTypeCheck(const Instruction& instruction,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001649 uint8_t destination,
1650 uint8_t reference,
1651 uint16_t type_index,
Calin Juravle225ff812014-11-13 16:46:39 +00001652 uint32_t dex_pc) {
Calin Juravle98893e12015-10-02 21:05:03 +01001653 bool type_known_final, type_known_abstract, use_declaring_class;
1654 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
1655 dex_compilation_unit_->GetDexMethodIndex(),
1656 *dex_compilation_unit_->GetDexFile(),
1657 type_index,
1658 &type_known_final,
1659 &type_known_abstract,
1660 &use_declaring_class);
1661
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001662 ScopedObjectAccess soa(Thread::Current());
1663 StackHandleScope<2> hs(soa.Self());
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001664 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001665 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001666 dex_compilation_unit_->GetClassLinker()->FindDexCache(soa.Self(), dex_file)));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001667 Handle<mirror::Class> resolved_class(hs.NewHandle(dex_cache->GetResolvedType(type_index)));
1668
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001669 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot, dex_pc);
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001670 HLoadClass* cls = new (arena_) HLoadClass(
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001671 graph_->GetCurrentMethod(),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01001672 type_index,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001673 dex_file,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01001674 IsOutermostCompilingClass(type_index),
Calin Juravle98893e12015-10-02 21:05:03 +01001675 dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001676 !can_access,
1677 compiler_driver_->CanAssumeTypeIsPresentInDexCache(dex_file, type_index));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001678 current_block_->AddInstruction(cls);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001679
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001680 TypeCheckKind check_kind = ComputeTypeCheckKind(resolved_class);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001681 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001682 current_block_->AddInstruction(new (arena_) HInstanceOf(object, cls, check_kind, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001683 UpdateLocal(destination, current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001684 } else {
1685 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
David Brazdilf5552582015-12-27 13:36:12 +00001686 // We emit a CheckCast followed by a BoundType. CheckCast is a statement
1687 // which may throw. If it succeeds BoundType sets the new type of `object`
1688 // for all subsequent uses.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001689 current_block_->AddInstruction(new (arena_) HCheckCast(object, cls, check_kind, dex_pc));
David Brazdilf5552582015-12-27 13:36:12 +00001690 current_block_->AddInstruction(new (arena_) HBoundType(object, dex_pc));
1691 UpdateLocal(reference, current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001692 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001693}
1694
Mingyao Yangfb8464a2015-11-02 10:56:59 -08001695bool HGraphBuilder::NeedsAccessCheck(uint32_t type_index, bool* finalizable) const {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001696 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
Mingyao Yangfb8464a2015-11-02 10:56:59 -08001697 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index, finalizable);
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001698}
1699
Mark Mendellfe57faa2015-09-18 09:26:15 -04001700void HGraphBuilder::BuildSwitchJumpTable(const SwitchTable& table,
1701 const Instruction& instruction,
1702 HInstruction* value,
1703 uint32_t dex_pc) {
1704 // Add the successor blocks to the current block.
1705 uint16_t num_entries = table.GetNumEntries();
1706 for (size_t i = 1; i <= num_entries; i++) {
1707 int32_t target_offset = table.GetEntryAt(i);
1708 HBasicBlock* case_target = FindBlockStartingAt(dex_pc + target_offset);
1709 DCHECK(case_target != nullptr);
1710
1711 // Add the target block as a successor.
1712 current_block_->AddSuccessor(case_target);
1713 }
1714
1715 // Add the default target block as the last successor.
1716 HBasicBlock* default_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
1717 DCHECK(default_target != nullptr);
1718 current_block_->AddSuccessor(default_target);
1719
1720 // Now add the Switch instruction.
1721 int32_t starting_key = table.GetEntryAt(0);
1722 current_block_->AddInstruction(
1723 new (arena_) HPackedSwitch(starting_key, num_entries, value, dex_pc));
1724 // This block ends with control flow.
1725 current_block_ = nullptr;
1726}
1727
Calin Juravle48c2b032014-12-09 18:11:36 +00001728void HGraphBuilder::BuildPackedSwitch(const Instruction& instruction, uint32_t dex_pc) {
David Brazdil2ef645b2015-06-17 18:20:52 +01001729 // Verifier guarantees that the payload for PackedSwitch contains:
1730 // (a) number of entries (may be zero)
1731 // (b) first and lowest switch case value (entry 0, always present)
1732 // (c) list of target pcs (entries 1 <= i <= N)
Andreas Gamped881df52014-11-24 23:28:39 -08001733 SwitchTable table(instruction, dex_pc, false);
1734
1735 // Value to test against.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001736 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt, dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08001737
Mark Mendellfe57faa2015-09-18 09:26:15 -04001738 // Starting key value.
1739 int32_t starting_key = table.GetEntryAt(0);
1740
David Brazdil2ef645b2015-06-17 18:20:52 +01001741 // Retrieve number of entries.
Andreas Gampee4d4d322014-12-04 09:09:57 -08001742 uint16_t num_entries = table.GetNumEntries();
David Brazdil2ef645b2015-06-17 18:20:52 +01001743 if (num_entries == 0) {
1744 return;
1745 }
Andreas Gampee4d4d322014-12-04 09:09:57 -08001746
Mark Mendellfe57faa2015-09-18 09:26:15 -04001747 // Don't use a packed switch if there are very few entries.
1748 if (num_entries > kSmallSwitchThreshold) {
1749 BuildSwitchJumpTable(table, instruction, value, dex_pc);
1750 } else {
1751 // Chained cmp-and-branch, starting from starting_key.
1752 for (size_t i = 1; i <= num_entries; i++) {
Mark Mendell3b9f3042015-09-24 08:43:40 -04001753 BuildSwitchCaseHelper(instruction,
1754 i,
1755 i == num_entries,
1756 table,
1757 value,
1758 starting_key + i - 1,
1759 table.GetEntryAt(i),
1760 dex_pc);
Mark Mendellfe57faa2015-09-18 09:26:15 -04001761 }
Andreas Gamped881df52014-11-24 23:28:39 -08001762 }
Andreas Gamped881df52014-11-24 23:28:39 -08001763}
1764
Calin Juravle48c2b032014-12-09 18:11:36 +00001765void HGraphBuilder::BuildSparseSwitch(const Instruction& instruction, uint32_t dex_pc) {
David Brazdil2ef645b2015-06-17 18:20:52 +01001766 // Verifier guarantees that the payload for SparseSwitch contains:
1767 // (a) number of entries (may be zero)
1768 // (b) sorted key values (entries 0 <= i < N)
1769 // (c) target pcs corresponding to the switch values (entries N <= i < 2*N)
Andreas Gampee4d4d322014-12-04 09:09:57 -08001770 SwitchTable table(instruction, dex_pc, true);
1771
1772 // Value to test against.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001773 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001774
1775 uint16_t num_entries = table.GetNumEntries();
Andreas Gampee4d4d322014-12-04 09:09:57 -08001776
1777 for (size_t i = 0; i < num_entries; i++) {
1778 BuildSwitchCaseHelper(instruction, i, i == static_cast<size_t>(num_entries) - 1, table, value,
1779 table.GetEntryAt(i), table.GetEntryAt(i + num_entries), dex_pc);
1780 }
Andreas Gampee4d4d322014-12-04 09:09:57 -08001781}
1782
1783void HGraphBuilder::BuildSwitchCaseHelper(const Instruction& instruction, size_t index,
1784 bool is_last_case, const SwitchTable& table,
1785 HInstruction* value, int32_t case_value_int,
1786 int32_t target_offset, uint32_t dex_pc) {
David Brazdil852eaff2015-02-02 15:23:05 +00001787 HBasicBlock* case_target = FindBlockStartingAt(dex_pc + target_offset);
1788 DCHECK(case_target != nullptr);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001789
1790 // The current case's value.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001791 HInstruction* this_case_value = graph_->GetIntConstant(case_value_int, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001792
1793 // Compare value and this_case_value.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001794 HEqual* comparison = new (arena_) HEqual(value, this_case_value, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001795 current_block_->AddInstruction(comparison);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001796 HInstruction* ifinst = new (arena_) HIf(comparison, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001797 current_block_->AddInstruction(ifinst);
1798
1799 // Case hit: use the target offset to determine where to go.
Andreas Gampee4d4d322014-12-04 09:09:57 -08001800 current_block_->AddSuccessor(case_target);
1801
1802 // Case miss: go to the next case (or default fall-through).
1803 // When there is a next case, we use the block stored with the table offset representing this
1804 // case (that is where we registered them in ComputeBranchTargets).
1805 // When there is no next case, we use the following instruction.
1806 // TODO: Find a good way to peel the last iteration to avoid conditional, but still have re-use.
1807 if (!is_last_case) {
1808 HBasicBlock* next_case_target = FindBlockStartingAt(table.GetDexPcForIndex(index));
1809 DCHECK(next_case_target != nullptr);
1810 current_block_->AddSuccessor(next_case_target);
1811
1812 // Need to manually add the block, as there is no dex-pc transition for the cases.
1813 graph_->AddBlock(next_case_target);
1814
1815 current_block_ = next_case_target;
1816 } else {
1817 HBasicBlock* default_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
1818 DCHECK(default_target != nullptr);
1819 current_block_->AddSuccessor(default_target);
1820 current_block_ = nullptr;
1821 }
1822}
1823
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001824bool HGraphBuilder::CanDecodeQuickenedInfo() const {
1825 return interpreter_metadata_ != nullptr;
1826}
1827
1828uint16_t HGraphBuilder::LookupQuickenedInfo(uint32_t dex_pc) {
1829 DCHECK(interpreter_metadata_ != nullptr);
1830 uint32_t dex_pc_in_map = DecodeUnsignedLeb128(&interpreter_metadata_);
1831 DCHECK_EQ(dex_pc, dex_pc_in_map);
1832 return DecodeUnsignedLeb128(&interpreter_metadata_);
1833}
1834
Calin Juravle225ff812014-11-13 16:46:39 +00001835bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001836 if (current_block_ == nullptr) {
1837 return true; // Dead code
1838 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001839
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001840 switch (instruction.Opcode()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001841 case Instruction::CONST_4: {
1842 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001843 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n(), dex_pc);
1844 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001845 break;
1846 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001847
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001848 case Instruction::CONST_16: {
1849 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001850 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s(), dex_pc);
1851 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001852 break;
1853 }
1854
Dave Allison20dfc792014-06-16 20:44:29 -07001855 case Instruction::CONST: {
1856 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001857 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i(), dex_pc);
1858 UpdateLocal(register_index, constant, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001859 break;
1860 }
1861
1862 case Instruction::CONST_HIGH16: {
1863 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001864 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16, dex_pc);
1865 UpdateLocal(register_index, constant, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001866 break;
1867 }
1868
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001869 case Instruction::CONST_WIDE_16: {
1870 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001871 // Get 16 bits of constant value, sign extended to 64 bits.
1872 int64_t value = instruction.VRegB_21s();
1873 value <<= 48;
1874 value >>= 48;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001875 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1876 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001877 break;
1878 }
1879
1880 case Instruction::CONST_WIDE_32: {
1881 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001882 // Get 32 bits of constant value, sign extended to 64 bits.
1883 int64_t value = instruction.VRegB_31i();
1884 value <<= 32;
1885 value >>= 32;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001886 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1887 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001888 break;
1889 }
1890
1891 case Instruction::CONST_WIDE: {
1892 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001893 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l(), dex_pc);
1894 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001895 break;
1896 }
1897
Dave Allison20dfc792014-06-16 20:44:29 -07001898 case Instruction::CONST_WIDE_HIGH16: {
1899 int32_t register_index = instruction.VRegA();
1900 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001901 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1902 UpdateLocal(register_index, constant, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001903 break;
1904 }
1905
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001906 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001907 case Instruction::MOVE:
1908 case Instruction::MOVE_FROM16:
1909 case Instruction::MOVE_16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001910 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
1911 UpdateLocal(instruction.VRegA(), value, dex_pc);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001912 break;
1913 }
1914
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001915 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001916 case Instruction::MOVE_WIDE:
1917 case Instruction::MOVE_WIDE_FROM16:
1918 case Instruction::MOVE_WIDE_16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001919 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong, dex_pc);
1920 UpdateLocal(instruction.VRegA(), value, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001921 break;
1922 }
1923
1924 case Instruction::MOVE_OBJECT:
1925 case Instruction::MOVE_OBJECT_16:
1926 case Instruction::MOVE_OBJECT_FROM16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001927 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot, dex_pc);
1928 UpdateLocal(instruction.VRegA(), value, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001929 break;
1930 }
1931
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001932 case Instruction::RETURN_VOID_NO_BARRIER:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001933 case Instruction::RETURN_VOID: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001934 BuildReturn(instruction, Primitive::kPrimVoid, dex_pc);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001935 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001936 }
1937
Dave Allison20dfc792014-06-16 20:44:29 -07001938#define IF_XX(comparison, cond) \
Calin Juravle225ff812014-11-13 16:46:39 +00001939 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
1940 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001941
Dave Allison20dfc792014-06-16 20:44:29 -07001942 IF_XX(HEqual, EQ);
1943 IF_XX(HNotEqual, NE);
1944 IF_XX(HLessThan, LT);
1945 IF_XX(HLessThanOrEqual, LE);
1946 IF_XX(HGreaterThan, GT);
1947 IF_XX(HGreaterThanOrEqual, GE);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001948
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001949 case Instruction::GOTO:
1950 case Instruction::GOTO_16:
1951 case Instruction::GOTO_32: {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001952 int32_t offset = instruction.GetTargetOffset();
Calin Juravle225ff812014-11-13 16:46:39 +00001953 HBasicBlock* target = FindBlockStartingAt(offset + dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001954 DCHECK(target != nullptr);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001955 current_block_->AddInstruction(new (arena_) HGoto(dex_pc));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001956 current_block_->AddSuccessor(target);
1957 current_block_ = nullptr;
1958 break;
1959 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001960
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001961 case Instruction::RETURN: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001962 BuildReturn(instruction, return_type_, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001963 break;
1964 }
1965
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001966 case Instruction::RETURN_OBJECT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001967 BuildReturn(instruction, return_type_, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001968 break;
1969 }
1970
1971 case Instruction::RETURN_WIDE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001972 BuildReturn(instruction, return_type_, dex_pc);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001973 break;
1974 }
1975
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001976 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001977 case Instruction::INVOKE_INTERFACE:
1978 case Instruction::INVOKE_STATIC:
1979 case Instruction::INVOKE_SUPER:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001980 case Instruction::INVOKE_VIRTUAL:
1981 case Instruction::INVOKE_VIRTUAL_QUICK: {
1982 uint16_t method_idx;
1983 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_QUICK) {
1984 if (!CanDecodeQuickenedInfo()) {
1985 return false;
1986 }
1987 method_idx = LookupQuickenedInfo(dex_pc);
1988 } else {
1989 method_idx = instruction.VRegB_35c();
1990 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001991 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001992 uint32_t args[5];
Ian Rogers29a26482014-05-02 15:27:29 -07001993 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00001994 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001995 number_of_vreg_arguments, false, args, -1)) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001996 return false;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001997 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001998 break;
1999 }
2000
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002001 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00002002 case Instruction::INVOKE_INTERFACE_RANGE:
2003 case Instruction::INVOKE_STATIC_RANGE:
2004 case Instruction::INVOKE_SUPER_RANGE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002005 case Instruction::INVOKE_VIRTUAL_RANGE:
2006 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2007 uint16_t method_idx;
2008 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK) {
2009 if (!CanDecodeQuickenedInfo()) {
2010 return false;
2011 }
2012 method_idx = LookupQuickenedInfo(dex_pc);
2013 } else {
2014 method_idx = instruction.VRegB_3rc();
2015 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002016 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
2017 uint32_t register_index = instruction.VRegC();
Calin Juravle225ff812014-11-13 16:46:39 +00002018 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002019 number_of_vreg_arguments, true, nullptr, register_index)) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002020 return false;
2021 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002022 break;
2023 }
2024
Roland Levillain88cb1752014-10-20 16:36:47 +01002025 case Instruction::NEG_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002026 Unop_12x<HNeg>(instruction, Primitive::kPrimInt, dex_pc);
Roland Levillain88cb1752014-10-20 16:36:47 +01002027 break;
2028 }
2029
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002030 case Instruction::NEG_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002031 Unop_12x<HNeg>(instruction, Primitive::kPrimLong, dex_pc);
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002032 break;
2033 }
2034
Roland Levillain3dbcb382014-10-28 17:30:07 +00002035 case Instruction::NEG_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002036 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat, dex_pc);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002037 break;
2038 }
2039
2040 case Instruction::NEG_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002041 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble, dex_pc);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002042 break;
2043 }
2044
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002045 case Instruction::NOT_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002046 Unop_12x<HNot>(instruction, Primitive::kPrimInt, dex_pc);
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002047 break;
2048 }
2049
Roland Levillain70566432014-10-24 16:20:17 +01002050 case Instruction::NOT_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002051 Unop_12x<HNot>(instruction, Primitive::kPrimLong, dex_pc);
Roland Levillain70566432014-10-24 16:20:17 +01002052 break;
2053 }
2054
Roland Levillaindff1f282014-11-05 14:15:05 +00002055 case Instruction::INT_TO_LONG: {
Roland Levillain624279f2014-12-04 11:54:28 +00002056 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong, dex_pc);
Roland Levillaindff1f282014-11-05 14:15:05 +00002057 break;
2058 }
2059
Roland Levillaincff13742014-11-17 14:32:17 +00002060 case Instruction::INT_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00002061 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00002062 break;
2063 }
2064
2065 case Instruction::INT_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00002066 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00002067 break;
2068 }
2069
Roland Levillain946e1432014-11-11 17:35:19 +00002070 case Instruction::LONG_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00002071 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt, dex_pc);
Roland Levillain946e1432014-11-11 17:35:19 +00002072 break;
2073 }
2074
Roland Levillain6d0e4832014-11-27 18:31:21 +00002075 case Instruction::LONG_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00002076 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimFloat, dex_pc);
Roland Levillain6d0e4832014-11-27 18:31:21 +00002077 break;
2078 }
2079
Roland Levillain647b9ed2014-11-27 12:06:00 +00002080 case Instruction::LONG_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00002081 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble, dex_pc);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002082 break;
2083 }
2084
Roland Levillain3f8f9362014-12-02 17:45:01 +00002085 case Instruction::FLOAT_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00002086 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimInt, dex_pc);
2087 break;
2088 }
2089
2090 case Instruction::FLOAT_TO_LONG: {
2091 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimLong, dex_pc);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002092 break;
2093 }
2094
Roland Levillain8964e2b2014-12-04 12:10:50 +00002095 case Instruction::FLOAT_TO_DOUBLE: {
2096 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimDouble, dex_pc);
2097 break;
2098 }
2099
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002100 case Instruction::DOUBLE_TO_INT: {
2101 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimInt, dex_pc);
2102 break;
2103 }
2104
2105 case Instruction::DOUBLE_TO_LONG: {
2106 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimLong, dex_pc);
2107 break;
2108 }
2109
Roland Levillain8964e2b2014-12-04 12:10:50 +00002110 case Instruction::DOUBLE_TO_FLOAT: {
2111 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimFloat, dex_pc);
2112 break;
2113 }
2114
Roland Levillain51d3fc42014-11-13 14:11:42 +00002115 case Instruction::INT_TO_BYTE: {
Roland Levillain624279f2014-12-04 11:54:28 +00002116 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte, dex_pc);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002117 break;
2118 }
2119
Roland Levillain01a8d712014-11-14 16:27:39 +00002120 case Instruction::INT_TO_SHORT: {
Roland Levillain624279f2014-12-04 11:54:28 +00002121 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort, dex_pc);
Roland Levillain01a8d712014-11-14 16:27:39 +00002122 break;
2123 }
2124
Roland Levillain981e4542014-11-14 11:47:14 +00002125 case Instruction::INT_TO_CHAR: {
Roland Levillain624279f2014-12-04 11:54:28 +00002126 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar, dex_pc);
Roland Levillain981e4542014-11-14 11:47:14 +00002127 break;
2128 }
2129
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002130 case Instruction::ADD_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002131 Binop_23x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002132 break;
2133 }
2134
2135 case Instruction::ADD_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002136 Binop_23x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002137 break;
2138 }
2139
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002140 case Instruction::ADD_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002141 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002142 break;
2143 }
2144
2145 case Instruction::ADD_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002146 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002147 break;
2148 }
2149
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002150 case Instruction::SUB_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002151 Binop_23x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002152 break;
2153 }
2154
2155 case Instruction::SUB_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002156 Binop_23x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002157 break;
2158 }
2159
Calin Juravle096cc022014-10-23 17:01:13 +01002160 case Instruction::SUB_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002161 Binop_23x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01002162 break;
2163 }
2164
2165 case Instruction::SUB_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002166 Binop_23x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01002167 break;
2168 }
2169
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002170 case Instruction::ADD_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002171 Binop_12x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002172 break;
2173 }
2174
Calin Juravle34bacdf2014-10-07 20:23:36 +01002175 case Instruction::MUL_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002176 Binop_23x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002177 break;
2178 }
2179
2180 case Instruction::MUL_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002181 Binop_23x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002182 break;
2183 }
2184
Calin Juravleb5bfa962014-10-21 18:02:24 +01002185 case Instruction::MUL_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002186 Binop_23x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01002187 break;
2188 }
2189
2190 case Instruction::MUL_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002191 Binop_23x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01002192 break;
2193 }
2194
Calin Juravled0d48522014-11-04 16:40:20 +00002195 case Instruction::DIV_INT: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002196 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2197 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravled0d48522014-11-04 16:40:20 +00002198 break;
2199 }
2200
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002201 case Instruction::DIV_LONG: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002202 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2203 dex_pc, Primitive::kPrimLong, false, true);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002204 break;
2205 }
2206
Calin Juravle7c4954d2014-10-28 16:57:40 +00002207 case Instruction::DIV_FLOAT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002208 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002209 break;
2210 }
2211
2212 case Instruction::DIV_DOUBLE: {
Calin Juravle225ff812014-11-13 16:46:39 +00002213 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002214 break;
2215 }
2216
Calin Juravlebacfec32014-11-14 15:54:36 +00002217 case Instruction::REM_INT: {
2218 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2219 dex_pc, Primitive::kPrimInt, false, false);
2220 break;
2221 }
2222
2223 case Instruction::REM_LONG: {
2224 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2225 dex_pc, Primitive::kPrimLong, false, false);
2226 break;
2227 }
2228
Calin Juravled2ec87d2014-12-08 14:24:46 +00002229 case Instruction::REM_FLOAT: {
2230 Binop_23x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2231 break;
2232 }
2233
2234 case Instruction::REM_DOUBLE: {
2235 Binop_23x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2236 break;
2237 }
2238
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002239 case Instruction::AND_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002240 Binop_23x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002241 break;
2242 }
2243
2244 case Instruction::AND_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002245 Binop_23x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002246 break;
2247 }
2248
Calin Juravle9aec02f2014-11-18 23:06:35 +00002249 case Instruction::SHL_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002250 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002251 break;
2252 }
2253
2254 case Instruction::SHL_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002255 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002256 break;
2257 }
2258
2259 case Instruction::SHR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002260 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002261 break;
2262 }
2263
2264 case Instruction::SHR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002265 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002266 break;
2267 }
2268
2269 case Instruction::USHR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002270 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002271 break;
2272 }
2273
2274 case Instruction::USHR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002275 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002276 break;
2277 }
2278
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002279 case Instruction::OR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002280 Binop_23x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002281 break;
2282 }
2283
2284 case Instruction::OR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002285 Binop_23x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002286 break;
2287 }
2288
2289 case Instruction::XOR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002290 Binop_23x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002291 break;
2292 }
2293
2294 case Instruction::XOR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002295 Binop_23x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002296 break;
2297 }
2298
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002299 case Instruction::ADD_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002300 Binop_12x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002301 break;
2302 }
2303
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002304 case Instruction::ADD_DOUBLE_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002305 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002306 break;
2307 }
2308
2309 case Instruction::ADD_FLOAT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002310 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002311 break;
2312 }
2313
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002314 case Instruction::SUB_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002315 Binop_12x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002316 break;
2317 }
2318
2319 case Instruction::SUB_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002320 Binop_12x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002321 break;
2322 }
2323
Calin Juravle096cc022014-10-23 17:01:13 +01002324 case Instruction::SUB_FLOAT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002325 Binop_12x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01002326 break;
2327 }
2328
2329 case Instruction::SUB_DOUBLE_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002330 Binop_12x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01002331 break;
2332 }
2333
Calin Juravle34bacdf2014-10-07 20:23:36 +01002334 case Instruction::MUL_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002335 Binop_12x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002336 break;
2337 }
2338
2339 case Instruction::MUL_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002340 Binop_12x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002341 break;
2342 }
2343
Calin Juravleb5bfa962014-10-21 18:02:24 +01002344 case Instruction::MUL_FLOAT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002345 Binop_12x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01002346 break;
2347 }
2348
2349 case Instruction::MUL_DOUBLE_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002350 Binop_12x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01002351 break;
2352 }
2353
Calin Juravle865fc882014-11-06 17:09:03 +00002354 case Instruction::DIV_INT_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002355 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2356 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravle865fc882014-11-06 17:09:03 +00002357 break;
2358 }
2359
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002360 case Instruction::DIV_LONG_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002361 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2362 dex_pc, Primitive::kPrimLong, false, true);
2363 break;
2364 }
2365
2366 case Instruction::REM_INT_2ADDR: {
2367 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2368 dex_pc, Primitive::kPrimInt, false, false);
2369 break;
2370 }
2371
2372 case Instruction::REM_LONG_2ADDR: {
2373 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2374 dex_pc, Primitive::kPrimLong, false, false);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002375 break;
2376 }
2377
Calin Juravled2ec87d2014-12-08 14:24:46 +00002378 case Instruction::REM_FLOAT_2ADDR: {
2379 Binop_12x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2380 break;
2381 }
2382
2383 case Instruction::REM_DOUBLE_2ADDR: {
2384 Binop_12x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2385 break;
2386 }
2387
Calin Juravle9aec02f2014-11-18 23:06:35 +00002388 case Instruction::SHL_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002389 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002390 break;
2391 }
2392
2393 case Instruction::SHL_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002394 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002395 break;
2396 }
2397
2398 case Instruction::SHR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002399 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002400 break;
2401 }
2402
2403 case Instruction::SHR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002404 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002405 break;
2406 }
2407
2408 case Instruction::USHR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002409 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002410 break;
2411 }
2412
2413 case Instruction::USHR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002414 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002415 break;
2416 }
2417
Calin Juravle7c4954d2014-10-28 16:57:40 +00002418 case Instruction::DIV_FLOAT_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00002419 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002420 break;
2421 }
2422
2423 case Instruction::DIV_DOUBLE_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00002424 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002425 break;
2426 }
2427
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002428 case Instruction::AND_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002429 Binop_12x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002430 break;
2431 }
2432
2433 case Instruction::AND_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002434 Binop_12x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002435 break;
2436 }
2437
2438 case Instruction::OR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002439 Binop_12x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002440 break;
2441 }
2442
2443 case Instruction::OR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002444 Binop_12x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002445 break;
2446 }
2447
2448 case Instruction::XOR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002449 Binop_12x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002450 break;
2451 }
2452
2453 case Instruction::XOR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002454 Binop_12x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002455 break;
2456 }
2457
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002458 case Instruction::ADD_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002459 Binop_22s<HAdd>(instruction, false, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002460 break;
2461 }
2462
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002463 case Instruction::AND_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002464 Binop_22s<HAnd>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002465 break;
2466 }
2467
2468 case Instruction::OR_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002469 Binop_22s<HOr>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002470 break;
2471 }
2472
2473 case Instruction::XOR_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002474 Binop_22s<HXor>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002475 break;
2476 }
2477
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002478 case Instruction::RSUB_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002479 Binop_22s<HSub>(instruction, true, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002480 break;
2481 }
2482
Calin Juravle34bacdf2014-10-07 20:23:36 +01002483 case Instruction::MUL_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002484 Binop_22s<HMul>(instruction, false, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002485 break;
2486 }
2487
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002488 case Instruction::ADD_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002489 Binop_22b<HAdd>(instruction, false, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002490 break;
2491 }
2492
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002493 case Instruction::AND_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002494 Binop_22b<HAnd>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002495 break;
2496 }
2497
2498 case Instruction::OR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002499 Binop_22b<HOr>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002500 break;
2501 }
2502
2503 case Instruction::XOR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002504 Binop_22b<HXor>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002505 break;
2506 }
2507
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002508 case Instruction::RSUB_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002509 Binop_22b<HSub>(instruction, true, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002510 break;
2511 }
2512
Calin Juravle34bacdf2014-10-07 20:23:36 +01002513 case Instruction::MUL_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002514 Binop_22b<HMul>(instruction, false, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002515 break;
2516 }
2517
Calin Juravled0d48522014-11-04 16:40:20 +00002518 case Instruction::DIV_INT_LIT16:
2519 case Instruction::DIV_INT_LIT8: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002520 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2521 dex_pc, Primitive::kPrimInt, true, true);
2522 break;
2523 }
2524
2525 case Instruction::REM_INT_LIT16:
2526 case Instruction::REM_INT_LIT8: {
2527 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2528 dex_pc, Primitive::kPrimInt, true, false);
Calin Juravled0d48522014-11-04 16:40:20 +00002529 break;
2530 }
2531
Calin Juravle9aec02f2014-11-18 23:06:35 +00002532 case Instruction::SHL_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002533 Binop_22b<HShl>(instruction, false, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002534 break;
2535 }
2536
2537 case Instruction::SHR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002538 Binop_22b<HShr>(instruction, false, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002539 break;
2540 }
2541
2542 case Instruction::USHR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002543 Binop_22b<HUShr>(instruction, false, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002544 break;
2545 }
2546
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002547 case Instruction::NEW_INSTANCE: {
David Brazdil6de19382016-01-08 17:37:10 +00002548 if (!BuildNewInstance(instruction.VRegB_21c(), dex_pc)) {
2549 return false;
Jeff Hao848f70a2014-01-15 13:49:50 -08002550 }
David Brazdil6de19382016-01-08 17:37:10 +00002551 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002552 break;
2553 }
2554
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002555 case Instruction::NEW_ARRAY: {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002556 uint16_t type_index = instruction.VRegC_22c();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002557 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt, dex_pc);
Mingyao Yangfb8464a2015-11-02 10:56:59 -08002558 bool finalizable;
2559 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index, &finalizable)
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002560 ? kQuickAllocArrayWithAccessCheck
2561 : kQuickAllocArray;
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002562 current_block_->AddInstruction(new (arena_) HNewArray(length,
2563 graph_->GetCurrentMethod(),
2564 dex_pc,
2565 type_index,
2566 *dex_compilation_unit_->GetDexFile(),
2567 entrypoint));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002568 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002569 break;
2570 }
2571
2572 case Instruction::FILLED_NEW_ARRAY: {
2573 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
2574 uint32_t type_index = instruction.VRegB_35c();
2575 uint32_t args[5];
2576 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00002577 BuildFilledNewArray(dex_pc, type_index, number_of_vreg_arguments, false, args, 0);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002578 break;
2579 }
2580
2581 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2582 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
2583 uint32_t type_index = instruction.VRegB_3rc();
2584 uint32_t register_index = instruction.VRegC_3rc();
2585 BuildFilledNewArray(
Calin Juravle225ff812014-11-13 16:46:39 +00002586 dex_pc, type_index, number_of_vreg_arguments, true, nullptr, register_index);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002587 break;
2588 }
2589
2590 case Instruction::FILL_ARRAY_DATA: {
Calin Juravle225ff812014-11-13 16:46:39 +00002591 BuildFillArrayData(instruction, dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002592 break;
2593 }
2594
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002595 case Instruction::MOVE_RESULT:
Dave Allison20dfc792014-06-16 20:44:29 -07002596 case Instruction::MOVE_RESULT_WIDE:
David Brazdilfc6a86a2015-06-26 10:33:45 +00002597 case Instruction::MOVE_RESULT_OBJECT: {
Nicolas Geoffray1efcc222015-06-24 12:41:20 +01002598 if (latest_result_ == nullptr) {
2599 // Only dead code can lead to this situation, where the verifier
2600 // does not reject the method.
2601 } else {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002602 // An Invoke/FilledNewArray and its MoveResult could have landed in
2603 // different blocks if there was a try/catch block boundary between
2604 // them. For Invoke, we insert a StoreLocal after the instruction. For
2605 // FilledNewArray, the local needs to be updated after the array was
2606 // filled, otherwise we might overwrite an input vreg.
2607 HStoreLocal* update_local =
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002608 new (arena_) HStoreLocal(GetLocalAt(instruction.VRegA()), latest_result_, dex_pc);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002609 HBasicBlock* block = latest_result_->GetBlock();
2610 if (block == current_block_) {
2611 // MoveResult and the previous instruction are in the same block.
2612 current_block_->AddInstruction(update_local);
2613 } else {
2614 // The two instructions are in different blocks. Insert the MoveResult
2615 // before the final control-flow instruction of the previous block.
2616 DCHECK(block->EndsWithControlFlowInstruction());
2617 DCHECK(current_block_->GetInstructions().IsEmpty());
2618 block->InsertInstructionBefore(update_local, block->GetLastInstruction());
2619 }
Nicolas Geoffray1efcc222015-06-24 12:41:20 +01002620 latest_result_ = nullptr;
2621 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002622 break;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002623 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002624
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002625 case Instruction::CMP_LONG: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002626 Binop_23x_cmp(instruction, Primitive::kPrimLong, ComparisonBias::kNoBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002627 break;
2628 }
2629
2630 case Instruction::CMPG_FLOAT: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002631 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kGtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002632 break;
2633 }
2634
2635 case Instruction::CMPG_DOUBLE: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002636 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kGtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002637 break;
2638 }
2639
2640 case Instruction::CMPL_FLOAT: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002641 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kLtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002642 break;
2643 }
2644
2645 case Instruction::CMPL_DOUBLE: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002646 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kLtBias, dex_pc);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002647 break;
2648 }
2649
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002650 case Instruction::NOP:
2651 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002652
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002653 case Instruction::IGET:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002654 case Instruction::IGET_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002655 case Instruction::IGET_WIDE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002656 case Instruction::IGET_WIDE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002657 case Instruction::IGET_OBJECT:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002658 case Instruction::IGET_OBJECT_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002659 case Instruction::IGET_BOOLEAN:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002660 case Instruction::IGET_BOOLEAN_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002661 case Instruction::IGET_BYTE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002662 case Instruction::IGET_BYTE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002663 case Instruction::IGET_CHAR:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002664 case Instruction::IGET_CHAR_QUICK:
2665 case Instruction::IGET_SHORT:
2666 case Instruction::IGET_SHORT_QUICK: {
Calin Juravle225ff812014-11-13 16:46:39 +00002667 if (!BuildInstanceFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002668 return false;
2669 }
2670 break;
2671 }
2672
2673 case Instruction::IPUT:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002674 case Instruction::IPUT_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002675 case Instruction::IPUT_WIDE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002676 case Instruction::IPUT_WIDE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002677 case Instruction::IPUT_OBJECT:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002678 case Instruction::IPUT_OBJECT_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002679 case Instruction::IPUT_BOOLEAN:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002680 case Instruction::IPUT_BOOLEAN_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002681 case Instruction::IPUT_BYTE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002682 case Instruction::IPUT_BYTE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002683 case Instruction::IPUT_CHAR:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002684 case Instruction::IPUT_CHAR_QUICK:
2685 case Instruction::IPUT_SHORT:
2686 case Instruction::IPUT_SHORT_QUICK: {
Calin Juravle225ff812014-11-13 16:46:39 +00002687 if (!BuildInstanceFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002688 return false;
2689 }
2690 break;
2691 }
2692
2693 case Instruction::SGET:
2694 case Instruction::SGET_WIDE:
2695 case Instruction::SGET_OBJECT:
2696 case Instruction::SGET_BOOLEAN:
2697 case Instruction::SGET_BYTE:
2698 case Instruction::SGET_CHAR:
2699 case Instruction::SGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002700 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002701 return false;
2702 }
2703 break;
2704 }
2705
2706 case Instruction::SPUT:
2707 case Instruction::SPUT_WIDE:
2708 case Instruction::SPUT_OBJECT:
2709 case Instruction::SPUT_BOOLEAN:
2710 case Instruction::SPUT_BYTE:
2711 case Instruction::SPUT_CHAR:
2712 case Instruction::SPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002713 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002714 return false;
2715 }
2716 break;
2717 }
2718
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002719#define ARRAY_XX(kind, anticipated_type) \
2720 case Instruction::AGET##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00002721 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002722 break; \
2723 } \
2724 case Instruction::APUT##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00002725 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002726 break; \
2727 }
2728
2729 ARRAY_XX(, Primitive::kPrimInt);
2730 ARRAY_XX(_WIDE, Primitive::kPrimLong);
2731 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
2732 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
2733 ARRAY_XX(_BYTE, Primitive::kPrimByte);
2734 ARRAY_XX(_CHAR, Primitive::kPrimChar);
2735 ARRAY_XX(_SHORT, Primitive::kPrimShort);
2736
Nicolas Geoffray39468442014-09-02 15:17:15 +01002737 case Instruction::ARRAY_LENGTH: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002738 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00002739 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002740 current_block_->AddInstruction(object);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002741 current_block_->AddInstruction(new (arena_) HArrayLength(object, dex_pc));
2742 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002743 break;
2744 }
2745
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002746 case Instruction::CONST_STRING: {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00002747 uint32_t string_index = instruction.VRegB_21c();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002748 current_block_->AddInstruction(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002749 new (arena_) HLoadString(graph_->GetCurrentMethod(), string_index, *dex_file_, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002750 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002751 break;
2752 }
2753
2754 case Instruction::CONST_STRING_JUMBO: {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00002755 uint32_t string_index = instruction.VRegB_31c();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002756 current_block_->AddInstruction(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002757 new (arena_) HLoadString(graph_->GetCurrentMethod(), string_index, *dex_file_, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002758 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002759 break;
2760 }
2761
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002762 case Instruction::CONST_CLASS: {
2763 uint16_t type_index = instruction.VRegB_21c();
2764 bool type_known_final;
2765 bool type_known_abstract;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002766 bool dont_use_is_referrers_class;
2767 // `CanAccessTypeWithoutChecks` will tell whether the method being
2768 // built is trying to access its own class, so that the generated
2769 // code can optimize for this case. However, the optimization does not
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002770 // work for inlining, so we use `IsOutermostCompilingClass` instead.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002771 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
2772 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002773 &type_known_final, &type_known_abstract, &dont_use_is_referrers_class);
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002774 current_block_->AddInstruction(new (arena_) HLoadClass(
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002775 graph_->GetCurrentMethod(),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002776 type_index,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00002777 *dex_file_,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002778 IsOutermostCompilingClass(type_index),
Calin Juravle98893e12015-10-02 21:05:03 +01002779 dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00002780 !can_access,
2781 compiler_driver_->CanAssumeTypeIsPresentInDexCache(*dex_file_, type_index)));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002782 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002783 break;
2784 }
2785
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002786 case Instruction::MOVE_EXCEPTION: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002787 current_block_->AddInstruction(new (arena_) HLoadException(dex_pc));
2788 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction(), dex_pc);
2789 current_block_->AddInstruction(new (arena_) HClearException(dex_pc));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002790 break;
2791 }
2792
2793 case Instruction::THROW: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002794 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00002795 current_block_->AddInstruction(new (arena_) HThrow(exception, dex_pc));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002796 // A throw instruction must branch to the exit block.
2797 current_block_->AddSuccessor(exit_block_);
2798 // We finished building this block. Set the current block to null to avoid
2799 // adding dead instructions to it.
2800 current_block_ = nullptr;
2801 break;
2802 }
2803
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002804 case Instruction::INSTANCE_OF: {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002805 uint8_t destination = instruction.VRegA_22c();
2806 uint8_t reference = instruction.VRegB_22c();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002807 uint16_t type_index = instruction.VRegC_22c();
Calin Juravle98893e12015-10-02 21:05:03 +01002808 BuildTypeCheck(instruction, destination, reference, type_index, dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002809 break;
2810 }
2811
2812 case Instruction::CHECK_CAST: {
2813 uint8_t reference = instruction.VRegA_21c();
2814 uint16_t type_index = instruction.VRegB_21c();
Calin Juravle98893e12015-10-02 21:05:03 +01002815 BuildTypeCheck(instruction, -1, reference, type_index, dex_pc);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002816 break;
2817 }
2818
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002819 case Instruction::MONITOR_ENTER: {
2820 current_block_->AddInstruction(new (arena_) HMonitorOperation(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002821 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot, dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002822 HMonitorOperation::OperationKind::kEnter,
Calin Juravle225ff812014-11-13 16:46:39 +00002823 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002824 break;
2825 }
2826
2827 case Instruction::MONITOR_EXIT: {
2828 current_block_->AddInstruction(new (arena_) HMonitorOperation(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002829 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot, dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002830 HMonitorOperation::OperationKind::kExit,
Calin Juravle225ff812014-11-13 16:46:39 +00002831 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002832 break;
2833 }
2834
Andreas Gamped881df52014-11-24 23:28:39 -08002835 case Instruction::PACKED_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002836 BuildPackedSwitch(instruction, dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08002837 break;
2838 }
2839
Andreas Gampee4d4d322014-12-04 09:09:57 -08002840 case Instruction::SPARSE_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002841 BuildSparseSwitch(instruction, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08002842 break;
2843 }
2844
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002845 default:
Calin Juravle48c2b032014-12-09 18:11:36 +00002846 VLOG(compiler) << "Did not compile "
2847 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
2848 << " because of unhandled instruction "
2849 << instruction.Name();
2850 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnhandledInstruction);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002851 return false;
2852 }
2853 return true;
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00002854} // NOLINT(readability/fn_size)
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002855
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01002856HLocal* HGraphBuilder::GetLocalAt(uint32_t register_index) const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01002857 return locals_[register_index];
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002858}
2859
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01002860void HGraphBuilder::UpdateLocal(uint32_t register_index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002861 HInstruction* instruction,
2862 uint32_t dex_pc) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002863 HLocal* local = GetLocalAt(register_index);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002864 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction, dex_pc));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002865}
2866
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01002867HInstruction* HGraphBuilder::LoadLocal(uint32_t register_index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002868 Primitive::Type type,
2869 uint32_t dex_pc) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002870 HLocal* local = GetLocalAt(register_index);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002871 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type, dex_pc));
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002872 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002873}
2874
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002875} // namespace art