blob: 53158588de0ed9248f3f4a8390dbd08f4ec82255 [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"
David Brazdil86ea7ee2016-02-16 09:26:07 +000023#include "bytecode_utils.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010024#include "class_linker.h"
Jeff Hao848f70a2014-01-15 13:49:50 -080025#include "dex/verified_method.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000026#include "dex_file-inl.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000027#include "dex_instruction-inl.h"
Roland Levillain4c0eb422015-04-24 16:43:49 +010028#include "dex/verified_method.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010029#include "driver/compiler_driver-inl.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000030#include "driver/compiler_options.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010031#include "mirror/class_loader.h"
32#include "mirror/dex_cache.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000033#include "nodes.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000034#include "primitive.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010035#include "scoped_thread_state_change.h"
David Brazdilbadd8262016-02-02 16:28:56 +000036#include "ssa_builder.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010037#include "thread.h"
Vladimir Marko58155012015-08-19 12:49:41 +000038#include "utils/dex_cache_arrays_layout-inl.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000039
40namespace art {
41
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010042void HGraphBuilder::InitializeLocals(uint16_t count) {
43 graph_->SetNumberOfVRegs(count);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010044 locals_.resize(count);
David Brazdil86ea7ee2016-02-16 09:26:07 +000045 HBasicBlock* entry_block = graph_->GetEntryBlock();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000046 for (int i = 0; i < count; i++) {
47 HLocal* local = new (arena_) HLocal(i);
David Brazdil86ea7ee2016-02-16 09:26:07 +000048 entry_block->AddInstruction(local);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010049 locals_[i] = local;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000050 }
51}
52
Nicolas Geoffray52e832b2014-11-06 15:15:31 +000053void HGraphBuilder::InitializeParameters(uint16_t number_of_parameters) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010054 // dex_compilation_unit_ is null only when unit testing.
55 if (dex_compilation_unit_ == nullptr) {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +000056 return;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010057 }
58
David Brazdil86ea7ee2016-02-16 09:26:07 +000059 HBasicBlock* entry_block = graph_->GetEntryBlock();
60
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010061 graph_->SetNumberOfInVRegs(number_of_parameters);
62 const char* shorty = dex_compilation_unit_->GetShorty();
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010063 int locals_index = locals_.size() - number_of_parameters;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010064 int parameter_index = 0;
65
Calin Juravlee6e3bea2015-10-14 13:53:10 +000066 const DexFile::MethodId& referrer_method_id =
67 dex_file_->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010068 if (!dex_compilation_unit_->IsStatic()) {
69 // Add the implicit 'this' argument, not expressed in the signature.
Calin Juravlee6e3bea2015-10-14 13:53:10 +000070 HParameterValue* parameter = new (arena_) HParameterValue(*dex_file_,
71 referrer_method_id.class_idx_,
72 parameter_index++,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060073 Primitive::kPrimNot,
74 true);
David Brazdil86ea7ee2016-02-16 09:26:07 +000075 entry_block->AddInstruction(parameter);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010076 HLocal* local = GetLocalAt(locals_index++);
David Brazdil86ea7ee2016-02-16 09:26:07 +000077 entry_block->AddInstruction(new (arena_) HStoreLocal(local, parameter, local->GetDexPc()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010078 number_of_parameters--;
79 }
80
Calin Juravlee6e3bea2015-10-14 13:53:10 +000081 const DexFile::ProtoId& proto = dex_file_->GetMethodPrototype(referrer_method_id);
82 const DexFile::TypeList* arg_types = dex_file_->GetProtoParameters(proto);
83 for (int i = 0, shorty_pos = 1; i < number_of_parameters; i++) {
84 HParameterValue* parameter = new (arena_) HParameterValue(
85 *dex_file_,
86 arg_types->GetTypeItem(shorty_pos - 1).type_idx_,
87 parameter_index++,
88 Primitive::GetType(shorty[shorty_pos]),
89 false);
90 ++shorty_pos;
David Brazdil86ea7ee2016-02-16 09:26:07 +000091 entry_block->AddInstruction(parameter);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010092 HLocal* local = GetLocalAt(locals_index++);
93 // Store the parameter value in the local that the dex code will use
94 // to reference that parameter.
David Brazdil86ea7ee2016-02-16 09:26:07 +000095 entry_block->AddInstruction(new (arena_) HStoreLocal(local, parameter, local->GetDexPc()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010096 bool is_wide = (parameter->GetType() == Primitive::kPrimLong)
97 || (parameter->GetType() == Primitive::kPrimDouble);
98 if (is_wide) {
99 i++;
100 locals_index++;
101 parameter_index++;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100102 }
103 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100104}
105
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100106template<typename T>
Calin Juravle225ff812014-11-13 16:46:39 +0000107void HGraphBuilder::If_22t(const Instruction& instruction, uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600108 HInstruction* first = LoadLocal(instruction.VRegA(), Primitive::kPrimInt, dex_pc);
109 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
110 T* comparison = new (arena_) T(first, second, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700111 current_block_->AddInstruction(comparison);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600112 HInstruction* ifinst = new (arena_) HIf(comparison, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700113 current_block_->AddInstruction(ifinst);
Dave Allison20dfc792014-06-16 20:44:29 -0700114 current_block_ = nullptr;
115}
116
117template<typename T>
Calin Juravle225ff812014-11-13 16:46:39 +0000118void HGraphBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600119 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt, dex_pc);
120 T* comparison = new (arena_) T(value, graph_->GetIntConstant(0, dex_pc), dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700121 current_block_->AddInstruction(comparison);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600122 HInstruction* ifinst = new (arena_) HIf(comparison, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700123 current_block_->AddInstruction(ifinst);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100124 current_block_ = nullptr;
125}
126
Calin Juravle48c2b032014-12-09 18:11:36 +0000127void HGraphBuilder::MaybeRecordStat(MethodCompilationStat compilation_stat) {
128 if (compilation_stats_ != nullptr) {
129 compilation_stats_->RecordStat(compilation_stat);
130 }
131}
132
David Brazdil86ea7ee2016-02-16 09:26:07 +0000133bool HGraphBuilder::SkipCompilation(size_t number_of_branches) {
134 if (compiler_driver_ == nullptr) {
135 // Note that the compiler driver is null when unit testing.
136 return false;
137 }
138
Calin Juravle48c2b032014-12-09 18:11:36 +0000139 const CompilerOptions& compiler_options = compiler_driver_->GetCompilerOptions();
Andreas Gampe29d38e72016-03-23 15:31:51 +0000140 CompilerFilter::Filter compiler_filter = compiler_options.GetCompilerFilter();
141 if (compiler_filter == CompilerFilter::kEverything) {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000142 return false;
143 }
144
David Brazdil86ea7ee2016-02-16 09:26:07 +0000145 if (compiler_options.IsHugeMethod(code_item_.insns_size_in_code_units_)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000146 VLOG(compiler) << "Skip compilation of huge method "
147 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
David Brazdil86ea7ee2016-02-16 09:26:07 +0000148 << ": " << code_item_.insns_size_in_code_units_ << " code units";
Calin Juravle48c2b032014-12-09 18:11:36 +0000149 MaybeRecordStat(MethodCompilationStat::kNotCompiledHugeMethod);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000150 return true;
151 }
152
153 // If it's large and contains no branches, it's likely to be machine generated initialization.
David Brazdil86ea7ee2016-02-16 09:26:07 +0000154 if (compiler_options.IsLargeMethod(code_item_.insns_size_in_code_units_)
David Brazdil1b498722015-03-31 11:37:18 +0100155 && (number_of_branches == 0)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000156 VLOG(compiler) << "Skip compilation of large method with no branch "
157 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
David Brazdil86ea7ee2016-02-16 09:26:07 +0000158 << ": " << code_item_.insns_size_in_code_units_ << " code units";
Calin Juravle48c2b032014-12-09 18:11:36 +0000159 MaybeRecordStat(MethodCompilationStat::kNotCompiledLargeMethodNoBranches);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000160 return true;
161 }
162
163 return false;
164}
165
David Brazdil86ea7ee2016-02-16 09:26:07 +0000166static bool BlockIsNotPopulated(HBasicBlock* block) {
167 if (!block->GetPhis().IsEmpty()) {
168 return false;
169 } else if (block->IsLoopHeader()) {
170 // Suspend checks were inserted into loop headers during building of dominator tree.
171 DCHECK(block->GetFirstInstruction()->IsSuspendCheck());
172 return block->GetFirstInstruction() == block->GetLastInstruction();
173 } else {
174 return block->GetInstructions().IsEmpty();
David Brazdilfc6a86a2015-06-26 10:33:45 +0000175 }
176}
177
David Brazdil86ea7ee2016-02-16 09:26:07 +0000178bool HGraphBuilder::GenerateInstructions() {
David Srbecky0cf44932015-12-09 14:09:59 +0000179 // Find locations where we want to generate extra stackmaps for native debugging.
180 // This allows us to generate the info only at interesting points (for example,
181 // at start of java statement) rather than before every dex instruction.
182 const bool native_debuggable = compiler_driver_ != nullptr &&
183 compiler_driver_->GetCompilerOptions().GetNativeDebuggable();
184 ArenaBitVector* native_debug_info_locations;
185 if (native_debuggable) {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000186 const uint32_t num_instructions = code_item_.insns_size_in_code_units_;
Vladimir Markof6a35de2016-03-21 12:01:50 +0000187 native_debug_info_locations =
188 ArenaBitVector::Create(arena_, num_instructions, false, kArenaAllocGraphBuilder);
David Brazdil86ea7ee2016-02-16 09:26:07 +0000189 FindNativeDebugInfoLocations(native_debug_info_locations);
David Srbecky0cf44932015-12-09 14:09:59 +0000190 }
191
David Brazdil86ea7ee2016-02-16 09:26:07 +0000192 InitializeLocals(code_item_.registers_size_);
193 InitializeParameters(code_item_.ins_size_);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000194
David Brazdil86ea7ee2016-02-16 09:26:07 +0000195 // Add the suspend check to the entry block.
196 current_block_ = graph_->GetEntryBlock();
197 current_block_->AddInstruction(new (arena_) HSuspendCheck(0));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100198
David Brazdil86ea7ee2016-02-16 09:26:07 +0000199 for (CodeItemIterator it(code_item_); !it.Done(); it.Advance()) {
200 uint32_t dex_pc = it.CurrentDexPc();
201
202 HBasicBlock* next_block = FindBlockStartingAt(dex_pc);
203 if (next_block != nullptr && next_block->GetGraph() != nullptr) {
David Srbecky0cf44932015-12-09 14:09:59 +0000204 if (current_block_ != nullptr) {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000205 // Branching instructions clear current_block, so we know
206 // the last instruction of the current block is not a branching
207 // instruction. We add an unconditional goto to the found block.
208 current_block_->AddInstruction(new (arena_) HGoto(dex_pc));
David Srbecky0cf44932015-12-09 14:09:59 +0000209 }
David Brazdil86ea7ee2016-02-16 09:26:07 +0000210 DCHECK(BlockIsNotPopulated(next_block));
211 current_block_ = next_block;
David Srbecky0cf44932015-12-09 14:09:59 +0000212 }
David Brazdil86ea7ee2016-02-16 09:26:07 +0000213
214 if (current_block_ == nullptr) {
215 // Unreachable code.
216 continue;
Calin Juravle48c2b032014-12-09 18:11:36 +0000217 }
David Brazdil86ea7ee2016-02-16 09:26:07 +0000218
219 if (native_debuggable && native_debug_info_locations->IsBitSet(dex_pc)) {
220 current_block_->AddInstruction(new (arena_) HNativeDebugInfo(dex_pc));
221 }
222
223 if (!AnalyzeDexInstruction(it.CurrentInstruction(), dex_pc)) {
224 return false;
225 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000226 }
227
David Brazdilfc6a86a2015-06-26 10:33:45 +0000228 // Add Exit to the exit block.
David Brazdil86ea7ee2016-02-16 09:26:07 +0000229 HBasicBlock* exit_block = graph_->GetExitBlock();
230 if (exit_block == nullptr) {
231 // Unreachable exit block was removed.
232 } else {
233 exit_block->AddInstruction(new (arena_) HExit());
234 }
David Brazdil5e8b1372015-01-23 14:39:08 +0000235
David Brazdil86ea7ee2016-02-16 09:26:07 +0000236 return true;
237}
David Brazdilfc6a86a2015-06-26 10:33:45 +0000238
David Brazdil86ea7ee2016-02-16 09:26:07 +0000239GraphAnalysisResult HGraphBuilder::BuildGraph(StackHandleScopeCollection* handles) {
240 DCHECK(graph_->GetBlocks().empty());
241 graph_->SetMaximumNumberOfOutVRegs(code_item_.outs_size_);
242 graph_->SetHasTryCatch(code_item_.tries_size_ != 0);
243 graph_->InitializeInexactObjectRTI(handles);
244
245 // 1) Create basic blocks and link them together. Basic blocks are left
246 // unpopulated with the exception of synthetic blocks, e.g. HTryBoundaries.
247 if (!block_builder_.Build()) {
248 return kAnalysisInvalidBytecode;
249 }
250
251 // 2) Decide whether to skip this method based on its code size and number
252 // of branches.
253 if (SkipCompilation(block_builder_.GetNumberOfBranches())) {
254 return kAnalysisSkipped;
255 }
256
257 // 3) Build the dominator tree and fill in loop and try/catch metadata.
David Brazdilbadd8262016-02-02 16:28:56 +0000258 GraphAnalysisResult result = graph_->BuildDominatorTree();
259 if (result != kAnalysisSuccess) {
260 return result;
261 }
262
David Brazdil86ea7ee2016-02-16 09:26:07 +0000263 // 4) Populate basic blocks with instructions.
264 if (!GenerateInstructions()) {
265 return kAnalysisInvalidBytecode;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000266 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000267
David Brazdil86ea7ee2016-02-16 09:26:07 +0000268 // 5) Type the graph and eliminate dead/redundant phis.
269 return SsaBuilder(graph_, code_item_, handles).BuildSsa();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000270}
271
David Brazdil86ea7ee2016-02-16 09:26:07 +0000272void HGraphBuilder::FindNativeDebugInfoLocations(ArenaBitVector* locations) {
David Srbecky0cf44932015-12-09 14:09:59 +0000273 // The callback gets called when the line number changes.
274 // In other words, it marks the start of new java statement.
275 struct Callback {
276 static bool Position(void* ctx, const DexFile::PositionInfo& entry) {
277 static_cast<ArenaBitVector*>(ctx)->SetBit(entry.address_);
278 return false;
279 }
280 };
David Brazdil86ea7ee2016-02-16 09:26:07 +0000281 dex_file_->DecodeDebugPositionInfo(&code_item_, Callback::Position, locations);
David Srbecky0cf44932015-12-09 14:09:59 +0000282 // Instruction-specific tweaks.
David Brazdil86ea7ee2016-02-16 09:26:07 +0000283 const Instruction* const begin = Instruction::At(code_item_.insns_);
284 const Instruction* const end = begin->RelativeAt(code_item_.insns_size_in_code_units_);
David Srbecky0cf44932015-12-09 14:09:59 +0000285 for (const Instruction* inst = begin; inst < end; inst = inst->Next()) {
286 switch (inst->Opcode()) {
David Srbeckyc7098ff2016-02-09 14:30:11 +0000287 case Instruction::MOVE_EXCEPTION: {
288 // Stop in native debugger after the exception has been moved.
289 // The compiler also expects the move at the start of basic block so
290 // we do not want to interfere by inserting native-debug-info before it.
David Brazdil86ea7ee2016-02-16 09:26:07 +0000291 locations->ClearBit(inst->GetDexPc(code_item_.insns_));
David Srbecky0cf44932015-12-09 14:09:59 +0000292 const Instruction* next = inst->Next();
293 if (next < end) {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000294 locations->SetBit(next->GetDexPc(code_item_.insns_));
David Srbecky0cf44932015-12-09 14:09:59 +0000295 }
296 break;
297 }
298 default:
299 break;
300 }
301 }
302}
303
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100304template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600305void HGraphBuilder::Unop_12x(const Instruction& instruction,
306 Primitive::Type type,
307 uint32_t dex_pc) {
308 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
309 current_block_->AddInstruction(new (arena_) T(type, first, dex_pc));
310 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Roland Levillain88cb1752014-10-20 16:36:47 +0100311}
312
Roland Levillaindff1f282014-11-05 14:15:05 +0000313void HGraphBuilder::Conversion_12x(const Instruction& instruction,
314 Primitive::Type input_type,
Roland Levillain624279f2014-12-04 11:54:28 +0000315 Primitive::Type result_type,
316 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600317 HInstruction* first = LoadLocal(instruction.VRegB(), input_type, dex_pc);
Roland Levillain624279f2014-12-04 11:54:28 +0000318 current_block_->AddInstruction(new (arena_) HTypeConversion(result_type, first, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600319 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100320}
321
322template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000323void HGraphBuilder::Binop_23x(const Instruction& instruction,
324 Primitive::Type type,
325 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600326 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
327 HInstruction* second = LoadLocal(instruction.VRegC(), type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000328 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600329 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000330}
331
332template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000333void HGraphBuilder::Binop_23x_shift(const Instruction& instruction,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600334 Primitive::Type type,
335 uint32_t dex_pc) {
336 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
337 HInstruction* second = LoadLocal(instruction.VRegC(), Primitive::kPrimInt, dex_pc);
338 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
339 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000340}
341
Calin Juravleddb7df22014-11-25 20:56:51 +0000342void HGraphBuilder::Binop_23x_cmp(const Instruction& instruction,
343 Primitive::Type type,
Mark Mendellc4701932015-04-10 13:18:51 -0400344 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700345 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600346 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
347 HInstruction* second = LoadLocal(instruction.VRegC(), type, dex_pc);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700348 current_block_->AddInstruction(new (arena_) HCompare(type, first, second, bias, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600349 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +0000350}
351
Calin Juravle9aec02f2014-11-18 23:06:35 +0000352template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600353void HGraphBuilder::Binop_12x_shift(const Instruction& instruction, Primitive::Type type,
354 uint32_t dex_pc) {
355 HInstruction* first = LoadLocal(instruction.VRegA(), type, dex_pc);
356 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
357 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
358 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000359}
360
361template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000362void HGraphBuilder::Binop_12x(const Instruction& instruction,
363 Primitive::Type type,
364 uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600365 HInstruction* first = LoadLocal(instruction.VRegA(), type, dex_pc);
366 HInstruction* second = LoadLocal(instruction.VRegB(), type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000367 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600368 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000369}
370
371template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600372void HGraphBuilder::Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
373 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
374 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100375 if (reverse) {
376 std::swap(first, second);
377 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600378 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second, dex_pc));
379 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100380}
381
382template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600383void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
384 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
385 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100386 if (reverse) {
387 std::swap(first, second);
388 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600389 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second, dex_pc));
390 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100391}
392
Calin Juravle0c25d102015-04-20 14:49:09 +0100393static bool RequiresConstructorBarrier(const DexCompilationUnit* cu, const CompilerDriver& driver) {
Calin Juravle27df7582015-04-17 19:12:31 +0100394 Thread* self = Thread::Current();
Calin Juravle0c25d102015-04-20 14:49:09 +0100395 return cu->IsConstructor()
396 && driver.RequiresConstructorBarrier(self, cu->GetDexFile(), cu->GetClassDefIndex());
Calin Juravle27df7582015-04-17 19:12:31 +0100397}
398
David Brazdil86ea7ee2016-02-16 09:26:07 +0000399// Returns true if `block` has only one successor which starts at the next
400// dex_pc after `instruction` at `dex_pc`.
401static bool IsFallthroughInstruction(const Instruction& instruction,
402 uint32_t dex_pc,
403 HBasicBlock* block) {
404 uint32_t next_dex_pc = dex_pc + instruction.SizeInCodeUnits();
405 return block->GetSingleSuccessor()->GetDexPc() == next_dex_pc;
406}
407
408void HGraphBuilder::BuildSwitch(const Instruction& instruction, uint32_t dex_pc) {
409 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt, dex_pc);
410 DexSwitchTable table(instruction, dex_pc);
411
412 if (table.GetNumEntries() == 0) {
413 // Empty Switch. Code falls through to the next block.
414 DCHECK(IsFallthroughInstruction(instruction, dex_pc, current_block_));
415 current_block_->AddInstruction(new (arena_) HGoto(dex_pc));
416 } else if (table.ShouldBuildDecisionTree()) {
417 for (DexSwitchTableIterator it(table); !it.Done(); it.Advance()) {
418 HInstruction* case_value = graph_->GetIntConstant(it.CurrentKey(), dex_pc);
419 HEqual* comparison = new (arena_) HEqual(value, case_value, dex_pc);
420 current_block_->AddInstruction(comparison);
421 HInstruction* ifinst = new (arena_) HIf(comparison, dex_pc);
422 current_block_->AddInstruction(ifinst);
423
424 if (!it.IsLast()) {
425 current_block_ = FindBlockStartingAt(it.GetDexPcForCurrentIndex());
426 }
427 }
428 } else {
429 current_block_->AddInstruction(
430 new (arena_) HPackedSwitch(table.GetEntryAt(0), table.GetNumEntries(), value, dex_pc));
431 }
432
433 current_block_ = nullptr;
434}
435
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600436void HGraphBuilder::BuildReturn(const Instruction& instruction,
437 Primitive::Type type,
438 uint32_t dex_pc) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100439 if (type == Primitive::kPrimVoid) {
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100440 if (graph_->ShouldGenerateConstructorBarrier()) {
441 // The compilation unit is null during testing.
442 if (dex_compilation_unit_ != nullptr) {
443 DCHECK(RequiresConstructorBarrier(dex_compilation_unit_, *compiler_driver_))
444 << "Inconsistent use of ShouldGenerateConstructorBarrier. Should not generate a barrier.";
445 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600446 current_block_->AddInstruction(new (arena_) HMemoryBarrier(kStoreStore, dex_pc));
Calin Juravle27df7582015-04-17 19:12:31 +0100447 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600448 current_block_->AddInstruction(new (arena_) HReturnVoid(dex_pc));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100449 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600450 HInstruction* value = LoadLocal(instruction.VRegA(), type, dex_pc);
451 current_block_->AddInstruction(new (arena_) HReturn(value, dex_pc));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100452 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100453 current_block_ = nullptr;
454}
455
Calin Juravle68ad6492015-08-18 17:08:12 +0100456static InvokeType GetInvokeTypeFromOpCode(Instruction::Code opcode) {
457 switch (opcode) {
458 case Instruction::INVOKE_STATIC:
459 case Instruction::INVOKE_STATIC_RANGE:
460 return kStatic;
461 case Instruction::INVOKE_DIRECT:
462 case Instruction::INVOKE_DIRECT_RANGE:
463 return kDirect;
464 case Instruction::INVOKE_VIRTUAL:
465 case Instruction::INVOKE_VIRTUAL_QUICK:
466 case Instruction::INVOKE_VIRTUAL_RANGE:
467 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
468 return kVirtual;
469 case Instruction::INVOKE_INTERFACE:
470 case Instruction::INVOKE_INTERFACE_RANGE:
471 return kInterface;
472 case Instruction::INVOKE_SUPER_RANGE:
473 case Instruction::INVOKE_SUPER:
474 return kSuper;
475 default:
476 LOG(FATAL) << "Unexpected invoke opcode: " << opcode;
477 UNREACHABLE();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100478 }
Calin Juravle68ad6492015-08-18 17:08:12 +0100479}
480
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000481ArtMethod* HGraphBuilder::ResolveMethod(uint16_t method_idx, InvokeType invoke_type) {
482 ScopedObjectAccess soa(Thread::Current());
Alex Lightfedd91d2016-01-07 14:49:16 -0800483 StackHandleScope<3> hs(soa.Self());
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000484
485 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
486 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
487 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
488 Handle<mirror::Class> compiling_class(hs.NewHandle(GetCompilingClass()));
489
Andreas Gampedae24142015-12-03 17:27:32 -0800490 ArtMethod* resolved_method = class_linker->ResolveMethod<ClassLinker::kForceICCECheck>(
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000491 *dex_compilation_unit_->GetDexFile(),
492 method_idx,
493 dex_compilation_unit_->GetDexCache(),
494 class_loader,
495 /* referrer */ nullptr,
496 invoke_type);
497
498 if (UNLIKELY(resolved_method == nullptr)) {
499 // Clean up any exception left by type resolution.
500 soa.Self()->ClearException();
501 return nullptr;
502 }
503
504 // Check access. The class linker has a fast path for looking into the dex cache
505 // and does not check the access if it hits it.
506 if (compiling_class.Get() == nullptr) {
507 if (!resolved_method->IsPublic()) {
508 return nullptr;
509 }
510 } else if (!compiling_class->CanAccessResolvedMethod(resolved_method->GetDeclaringClass(),
511 resolved_method,
512 dex_compilation_unit_->GetDexCache().Get(),
513 method_idx)) {
514 return nullptr;
515 }
516
517 // We have to special case the invoke-super case, as ClassLinker::ResolveMethod does not.
Alex Lightfedd91d2016-01-07 14:49:16 -0800518 // We need to look at the referrer's super class vtable. We need to do this to know if we need to
519 // make this an invoke-unresolved to handle cross-dex invokes or abstract super methods, both of
520 // which require runtime handling.
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000521 if (invoke_type == kSuper) {
522 if (compiling_class.Get() == nullptr) {
Alex Lightfedd91d2016-01-07 14:49:16 -0800523 // We could not determine the method's class we need to wait until runtime.
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000524 DCHECK(Runtime::Current()->IsAotCompiler());
525 return nullptr;
526 }
Alex Lightfedd91d2016-01-07 14:49:16 -0800527 ArtMethod* current_method = graph_->GetArtMethod();
528 DCHECK(current_method != nullptr);
529 Handle<mirror::Class> methods_class(hs.NewHandle(
530 dex_compilation_unit_->GetClassLinker()->ResolveReferencedClassOfMethod(Thread::Current(),
531 method_idx,
532 current_method)));
533 if (methods_class.Get() == nullptr) {
534 // Invoking a super method requires knowing the actual super class. If we did not resolve
535 // the compiling method's declaring class (which only happens for ahead of time
536 // compilation), bail out.
537 DCHECK(Runtime::Current()->IsAotCompiler());
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000538 return nullptr;
Alex Lightfedd91d2016-01-07 14:49:16 -0800539 } else {
540 ArtMethod* actual_method;
541 if (methods_class->IsInterface()) {
542 actual_method = methods_class->FindVirtualMethodForInterfaceSuper(
543 resolved_method, class_linker->GetImagePointerSize());
544 } else {
545 uint16_t vtable_index = resolved_method->GetMethodIndex();
546 actual_method = compiling_class->GetSuperClass()->GetVTableEntry(
547 vtable_index, class_linker->GetImagePointerSize());
548 }
549 if (actual_method != resolved_method &&
550 !IsSameDexFile(*actual_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
551 // The back-end code generator relies on this check in order to ensure that it will not
552 // attempt to read the dex_cache with a dex_method_index that is not from the correct
553 // dex_file. If we didn't do this check then the dex_method_index will not be updated in the
554 // builder, which means that the code-generator (and compiler driver during sharpening and
555 // inliner, maybe) might invoke an incorrect method.
556 // TODO: The actual method could still be referenced in the current dex file, so we
557 // could try locating it.
558 // TODO: Remove the dex_file restriction.
559 return nullptr;
560 }
561 if (!actual_method->IsInvokable()) {
562 // Fail if the actual method cannot be invoked. Otherwise, the runtime resolution stub
563 // could resolve the callee to the wrong method.
564 return nullptr;
565 }
566 resolved_method = actual_method;
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000567 }
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000568 }
569
570 // Check for incompatible class changes. The class linker has a fast path for
571 // looking into the dex cache and does not check incompatible class changes if it hits it.
572 if (resolved_method->CheckIncompatibleClassChange(invoke_type)) {
573 return nullptr;
574 }
575
576 return resolved_method;
577}
578
Calin Juravle68ad6492015-08-18 17:08:12 +0100579bool HGraphBuilder::BuildInvoke(const Instruction& instruction,
580 uint32_t dex_pc,
581 uint32_t method_idx,
582 uint32_t number_of_vreg_arguments,
583 bool is_range,
584 uint32_t* args,
585 uint32_t register_index) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000586 InvokeType invoke_type = GetInvokeTypeFromOpCode(instruction.Opcode());
Calin Juravle68ad6492015-08-18 17:08:12 +0100587 const char* descriptor = dex_file_->GetMethodShorty(method_idx);
588 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
589
590 // Remove the return type from the 'proto'.
591 size_t number_of_arguments = strlen(descriptor) - 1;
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000592 if (invoke_type != kStatic) { // instance call
Calin Juravle68ad6492015-08-18 17:08:12 +0100593 // One extra argument for 'this'.
594 number_of_arguments++;
595 }
596
597 MethodReference target_method(dex_file_, method_idx);
Calin Juravle68ad6492015-08-18 17:08:12 +0100598
Calin Juravle5d01db12015-08-25 15:02:42 +0100599 // Special handling for string init.
600 int32_t string_init_offset = 0;
601 bool is_string_init = compiler_driver_->IsStringInit(method_idx,
602 dex_file_,
603 &string_init_offset);
604 // Replace calls to String.<init> with StringFactory.
605 if (is_string_init) {
Vladimir Markodc151b22015-10-15 18:02:30 +0100606 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
607 HInvokeStaticOrDirect::MethodLoadKind::kStringInit,
608 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
609 dchecked_integral_cast<uint64_t>(string_init_offset),
610 0U
611 };
Calin Juravle5d01db12015-08-25 15:02:42 +0100612 HInvoke* invoke = new (arena_) HInvokeStaticOrDirect(
613 arena_,
614 number_of_arguments - 1,
615 Primitive::kPrimNot /*return_type */,
616 dex_pc,
617 method_idx,
618 target_method,
619 dispatch_info,
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000620 invoke_type,
Calin Juravle5d01db12015-08-25 15:02:42 +0100621 kStatic /* optimized_invoke_type */,
622 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit);
623 return HandleStringInit(invoke,
624 number_of_vreg_arguments,
625 args,
626 register_index,
627 is_range,
628 descriptor);
629 }
630
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000631 ArtMethod* resolved_method = ResolveMethod(method_idx, invoke_type);
632
Alex Lightfedd91d2016-01-07 14:49:16 -0800633 if (UNLIKELY(resolved_method == nullptr)) {
Calin Juravle175dc732015-08-25 15:42:32 +0100634 MaybeRecordStat(MethodCompilationStat::kUnresolvedMethod);
635 HInvoke* invoke = new (arena_) HInvokeUnresolved(arena_,
636 number_of_arguments,
637 return_type,
638 dex_pc,
639 method_idx,
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000640 invoke_type);
Calin Juravle175dc732015-08-25 15:42:32 +0100641 return HandleInvoke(invoke,
642 number_of_vreg_arguments,
643 args,
644 register_index,
645 is_range,
646 descriptor,
647 nullptr /* clinit_check */);
Calin Juravle68ad6492015-08-18 17:08:12 +0100648 }
649
Calin Juravle68ad6492015-08-18 17:08:12 +0100650 // Potential class initialization check, in the case of a static method call.
651 HClinitCheck* clinit_check = nullptr;
652 HInvoke* invoke = nullptr;
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000653 if (invoke_type == kDirect || invoke_type == kStatic || invoke_type == kSuper) {
Calin Juravle68ad6492015-08-18 17:08:12 +0100654 // By default, consider that the called method implicitly requires
655 // an initialization check of its declaring method.
656 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement
657 = HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000658 ScopedObjectAccess soa(Thread::Current());
659 if (invoke_type == kStatic) {
660 clinit_check = ProcessClinitCheckForInvoke(
661 dex_pc, resolved_method, method_idx, &clinit_check_requirement);
662 } else if (invoke_type == kSuper) {
663 if (IsSameDexFile(*resolved_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
664 // Update the target method to the one resolved. Note that this may be a no-op if
665 // we resolved to the method referenced by the instruction.
666 method_idx = resolved_method->GetDexMethodIndex();
667 target_method = MethodReference(dex_file_, method_idx);
668 }
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100669 }
Calin Juravle68ad6492015-08-18 17:08:12 +0100670
Vladimir Markodc151b22015-10-15 18:02:30 +0100671 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
672 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
673 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
674 0u,
675 0U
676 };
Calin Juravle68ad6492015-08-18 17:08:12 +0100677 invoke = new (arena_) HInvokeStaticOrDirect(arena_,
678 number_of_arguments,
679 return_type,
680 dex_pc,
681 method_idx,
682 target_method,
683 dispatch_info,
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000684 invoke_type,
685 invoke_type,
Calin Juravle68ad6492015-08-18 17:08:12 +0100686 clinit_check_requirement);
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000687 } else if (invoke_type == kVirtual) {
688 ScopedObjectAccess soa(Thread::Current()); // Needed for the method index
Calin Juravle68ad6492015-08-18 17:08:12 +0100689 invoke = new (arena_) HInvokeVirtual(arena_,
690 number_of_arguments,
691 return_type,
692 dex_pc,
693 method_idx,
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000694 resolved_method->GetMethodIndex());
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100695 } else {
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000696 DCHECK_EQ(invoke_type, kInterface);
697 ScopedObjectAccess soa(Thread::Current()); // Needed for the method index
Calin Juravle68ad6492015-08-18 17:08:12 +0100698 invoke = new (arena_) HInvokeInterface(arena_,
699 number_of_arguments,
700 return_type,
701 dex_pc,
702 method_idx,
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000703 resolved_method->GetDexMethodIndex());
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100704 }
Calin Juravle68ad6492015-08-18 17:08:12 +0100705
Calin Juravle5d01db12015-08-25 15:02:42 +0100706 return HandleInvoke(invoke,
707 number_of_vreg_arguments,
708 args,
709 register_index,
710 is_range,
711 descriptor,
712 clinit_check);
Calin Juravle68ad6492015-08-18 17:08:12 +0100713}
714
Nicolas Geoffray729645a2015-11-19 13:29:02 +0000715bool HGraphBuilder::BuildNewInstance(uint16_t type_index, uint32_t dex_pc) {
716 bool finalizable;
717 bool can_throw = NeedsAccessCheck(type_index, &finalizable);
718
719 // Only the non-resolved entrypoint handles the finalizable class case. If we
720 // need access checks, then we haven't resolved the method and the class may
721 // again be finalizable.
722 QuickEntrypointEnum entrypoint = (finalizable || can_throw)
723 ? kQuickAllocObject
724 : kQuickAllocObjectInitialized;
725
726 ScopedObjectAccess soa(Thread::Current());
727 StackHandleScope<3> hs(soa.Self());
728 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
729 dex_compilation_unit_->GetClassLinker()->FindDexCache(
730 soa.Self(), *dex_compilation_unit_->GetDexFile())));
731 Handle<mirror::Class> resolved_class(hs.NewHandle(dex_cache->GetResolvedType(type_index)));
732 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
733 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
734 outer_compilation_unit_->GetClassLinker()->FindDexCache(soa.Self(), outer_dex_file)));
735
736 if (outer_dex_cache.Get() != dex_cache.Get()) {
737 // We currently do not support inlining allocations across dex files.
738 return false;
739 }
740
741 HLoadClass* load_class = new (arena_) HLoadClass(
742 graph_->GetCurrentMethod(),
743 type_index,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +0000744 outer_dex_file,
Nicolas Geoffray729645a2015-11-19 13:29:02 +0000745 IsOutermostCompilingClass(type_index),
746 dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +0000747 /*needs_access_check*/ can_throw,
748 compiler_driver_->CanAssumeTypeIsPresentInDexCache(outer_dex_file, type_index));
Nicolas Geoffray729645a2015-11-19 13:29:02 +0000749
750 current_block_->AddInstruction(load_class);
751 HInstruction* cls = load_class;
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +0000752 if (!IsInitialized(resolved_class)) {
Nicolas Geoffray729645a2015-11-19 13:29:02 +0000753 cls = new (arena_) HClinitCheck(load_class, dex_pc);
754 current_block_->AddInstruction(cls);
755 }
756
757 current_block_->AddInstruction(new (arena_) HNewInstance(
758 cls,
759 graph_->GetCurrentMethod(),
760 dex_pc,
761 type_index,
762 *dex_compilation_unit_->GetDexFile(),
763 can_throw,
764 finalizable,
765 entrypoint));
766 return true;
767}
768
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +0000769static bool IsSubClass(mirror::Class* to_test, mirror::Class* super_class)
770 SHARED_REQUIRES(Locks::mutator_lock_) {
771 return to_test != nullptr && !to_test->IsInterface() && to_test->IsSubClass(super_class);
772}
773
774bool HGraphBuilder::IsInitialized(Handle<mirror::Class> cls) const {
Nicolas Geoffray729645a2015-11-19 13:29:02 +0000775 if (cls.Get() == nullptr) {
776 return false;
777 }
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +0000778
779 // `CanAssumeClassIsLoaded` will return true if we're JITting, or will
780 // check whether the class is in an image for the AOT compilation.
781 if (cls->IsInitialized() &&
782 compiler_driver_->CanAssumeClassIsLoaded(cls.Get())) {
Nicolas Geoffray729645a2015-11-19 13:29:02 +0000783 return true;
784 }
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +0000785
786 if (IsSubClass(GetOutermostCompilingClass(), cls.Get())) {
787 return true;
788 }
789
790 // TODO: We should walk over the inlined methods, but we don't pass
791 // that information to the builder.
792 if (IsSubClass(GetCompilingClass(), cls.Get())) {
793 return true;
794 }
795
796 return false;
Nicolas Geoffray729645a2015-11-19 13:29:02 +0000797}
798
Calin Juravle68ad6492015-08-18 17:08:12 +0100799HClinitCheck* HGraphBuilder::ProcessClinitCheckForInvoke(
800 uint32_t dex_pc,
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000801 ArtMethod* resolved_method,
Calin Juravle68ad6492015-08-18 17:08:12 +0100802 uint32_t method_idx,
803 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000804 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
805 Thread* self = Thread::Current();
806 StackHandleScope<4> hs(self);
Calin Juravle68ad6492015-08-18 17:08:12 +0100807 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
808 dex_compilation_unit_->GetClassLinker()->FindDexCache(
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000809 self, *dex_compilation_unit_->GetDexFile())));
Calin Juravle68ad6492015-08-18 17:08:12 +0100810 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000811 outer_compilation_unit_->GetClassLinker()->FindDexCache(
812 self, outer_dex_file)));
Calin Juravle68ad6492015-08-18 17:08:12 +0100813 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +0000814 Handle<mirror::Class> resolved_method_class(hs.NewHandle(resolved_method->GetDeclaringClass()));
Calin Juravle68ad6492015-08-18 17:08:12 +0100815
816 // The index at which the method's class is stored in the DexCache's type array.
817 uint32_t storage_index = DexFile::kDexNoIndex;
818 bool is_outer_class = (resolved_method->GetDeclaringClass() == outer_class.Get());
819 if (is_outer_class) {
820 storage_index = outer_class->GetDexTypeIndex();
821 } else if (outer_dex_cache.Get() == dex_cache.Get()) {
822 // Get `storage_index` from IsClassOfStaticMethodAvailableToReferrer.
823 compiler_driver_->IsClassOfStaticMethodAvailableToReferrer(outer_dex_cache.Get(),
824 GetCompilingClass(),
825 resolved_method,
826 method_idx,
827 &storage_index);
828 }
829
830 HClinitCheck* clinit_check = nullptr;
831
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +0000832 if (IsInitialized(resolved_method_class)) {
Calin Juravle68ad6492015-08-18 17:08:12 +0100833 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
834 } else if (storage_index != DexFile::kDexNoIndex) {
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +0000835 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
836 HLoadClass* load_class = new (arena_) HLoadClass(
837 graph_->GetCurrentMethod(),
838 storage_index,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +0000839 outer_dex_file,
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +0000840 is_outer_class,
841 dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +0000842 /*needs_access_check*/ false,
843 compiler_driver_->CanAssumeTypeIsPresentInDexCache(outer_dex_file, storage_index));
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +0000844 current_block_->AddInstruction(load_class);
845 clinit_check = new (arena_) HClinitCheck(load_class, dex_pc);
846 current_block_->AddInstruction(clinit_check);
Calin Juravle68ad6492015-08-18 17:08:12 +0100847 }
848 return clinit_check;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100849}
850
Calin Juravle5d01db12015-08-25 15:02:42 +0100851bool HGraphBuilder::SetupInvokeArguments(HInvoke* invoke,
852 uint32_t number_of_vreg_arguments,
853 uint32_t* args,
854 uint32_t register_index,
855 bool is_range,
856 const char* descriptor,
857 size_t start_index,
858 size_t* argument_index) {
Calin Juravle68ad6492015-08-18 17:08:12 +0100859 uint32_t descriptor_index = 1; // Skip the return type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600860 uint32_t dex_pc = invoke->GetDexPc();
Calin Juravle68ad6492015-08-18 17:08:12 +0100861
Nicolas Geoffray2e335252015-06-18 11:11:27 +0100862 for (size_t i = start_index;
863 // Make sure we don't go over the expected arguments or over the number of
864 // dex registers given. If the instruction was seen as dead by the verifier,
865 // it hasn't been properly checked.
Calin Juravle5d01db12015-08-25 15:02:42 +0100866 (i < number_of_vreg_arguments) && (*argument_index < invoke->GetNumberOfArguments());
867 i++, (*argument_index)++) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100868 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100869 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
Nicolas Geoffray2e335252015-06-18 11:11:27 +0100870 if (!is_range
871 && is_wide
872 && ((i + 1 == number_of_vreg_arguments) || (args[i] + 1 != args[i + 1]))) {
873 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
874 // reject any class where this is violated. However, the verifier only does these checks
875 // on non trivially dead instructions, so we just bailout the compilation.
876 VLOG(compiler) << "Did not compile "
877 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
878 << " because of non-sequential dex register pair in wide argument";
879 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
880 return false;
881 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600882 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type, dex_pc);
Calin Juravle5d01db12015-08-25 15:02:42 +0100883 invoke->SetArgumentAt(*argument_index, arg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100884 if (is_wide) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100885 i++;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100886 }
887 }
Nicolas Geoffray2e335252015-06-18 11:11:27 +0100888
Calin Juravle5d01db12015-08-25 15:02:42 +0100889 if (*argument_index != invoke->GetNumberOfArguments()) {
Nicolas Geoffray2e335252015-06-18 11:11:27 +0100890 VLOG(compiler) << "Did not compile "
891 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
892 << " because of wrong number of arguments in invoke instruction";
893 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
894 return false;
895 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100896
Vladimir Markob554b5a2015-11-06 12:57:55 +0000897 if (invoke->IsInvokeStaticOrDirect() &&
898 HInvokeStaticOrDirect::NeedsCurrentMethodInput(
899 invoke->AsInvokeStaticOrDirect()->GetMethodLoadKind())) {
Calin Juravle5d01db12015-08-25 15:02:42 +0100900 invoke->SetArgumentAt(*argument_index, graph_->GetCurrentMethod());
901 (*argument_index)++;
902 }
903
904 return true;
905}
906
907bool HGraphBuilder::HandleInvoke(HInvoke* invoke,
908 uint32_t number_of_vreg_arguments,
909 uint32_t* args,
910 uint32_t register_index,
911 bool is_range,
912 const char* descriptor,
913 HClinitCheck* clinit_check) {
914 DCHECK(!invoke->IsInvokeStaticOrDirect() || !invoke->AsInvokeStaticOrDirect()->IsStringInit());
915
916 size_t start_index = 0;
917 size_t argument_index = 0;
918 if (invoke->GetOriginalInvokeType() != InvokeType::kStatic) { // Instance call.
Calin Juravle5d01db12015-08-25 15:02:42 +0100919 HInstruction* arg = LoadLocal(
920 is_range ? register_index : args[0], Primitive::kPrimNot, invoke->GetDexPc());
921 HNullCheck* null_check = new (arena_) HNullCheck(arg, invoke->GetDexPc());
922 current_block_->AddInstruction(null_check);
Calin Juravle5d01db12015-08-25 15:02:42 +0100923 invoke->SetArgumentAt(0, null_check);
924 start_index = 1;
925 argument_index = 1;
926 }
927
928 if (!SetupInvokeArguments(invoke,
929 number_of_vreg_arguments,
930 args,
931 register_index,
932 is_range,
933 descriptor,
934 start_index,
935 &argument_index)) {
936 return false;
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100937 }
938
Calin Juravle68ad6492015-08-18 17:08:12 +0100939 if (clinit_check != nullptr) {
Roland Levillain4c0eb422015-04-24 16:43:49 +0100940 // Add the class initialization check as last input of `invoke`.
Calin Juravle68ad6492015-08-18 17:08:12 +0100941 DCHECK(invoke->IsInvokeStaticOrDirect());
942 DCHECK(invoke->AsInvokeStaticOrDirect()->GetClinitCheckRequirement()
943 == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit);
Roland Levillain3e3d7332015-04-28 11:00:54 +0100944 invoke->SetArgumentAt(argument_index, clinit_check);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100945 argument_index++;
Roland Levillain4c0eb422015-04-24 16:43:49 +0100946 }
947
Calin Juravle5d01db12015-08-25 15:02:42 +0100948 current_block_->AddInstruction(invoke);
949 latest_result_ = invoke;
950
951 return true;
952}
953
954bool HGraphBuilder::HandleStringInit(HInvoke* invoke,
955 uint32_t number_of_vreg_arguments,
956 uint32_t* args,
957 uint32_t register_index,
958 bool is_range,
959 const char* descriptor) {
960 DCHECK(invoke->IsInvokeStaticOrDirect());
961 DCHECK(invoke->AsInvokeStaticOrDirect()->IsStringInit());
962
963 size_t start_index = 1;
964 size_t argument_index = 0;
965 if (!SetupInvokeArguments(invoke,
966 number_of_vreg_arguments,
967 args,
968 register_index,
969 is_range,
970 descriptor,
971 start_index,
972 &argument_index)) {
973 return false;
Jeff Hao848f70a2014-01-15 13:49:50 -0800974 }
Calin Juravle0eedd7e2015-08-20 14:48:00 +0100975
Calin Juravle5d01db12015-08-25 15:02:42 +0100976 // Add move-result for StringFactory method.
977 uint32_t orig_this_reg = is_range ? register_index : args[0];
David Brazdil6de19382016-01-08 17:37:10 +0000978 HInstruction* new_instance = LoadLocal(orig_this_reg, Primitive::kPrimNot, invoke->GetDexPc());
979 invoke->SetArgumentAt(argument_index, new_instance);
Calin Juravle5d01db12015-08-25 15:02:42 +0100980 current_block_->AddInstruction(invoke);
Calin Juravle5d01db12015-08-25 15:02:42 +0100981
Calin Juravle0eedd7e2015-08-20 14:48:00 +0100982 latest_result_ = invoke;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100983 return true;
984}
985
Calin Juravlee460d1d2015-09-29 04:52:17 +0100986static Primitive::Type GetFieldAccessType(const DexFile& dex_file, uint16_t field_index) {
987 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
988 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
989 return Primitive::GetType(type[0]);
990}
991
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100992bool HGraphBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000993 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100994 bool is_put) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100995 uint32_t source_or_dest_reg = instruction.VRegA_22c();
996 uint32_t obj_reg = instruction.VRegB_22c();
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +0000997 uint16_t field_index;
998 if (instruction.IsQuickened()) {
999 if (!CanDecodeQuickenedInfo()) {
1000 return false;
1001 }
1002 field_index = LookupQuickenedInfo(dex_pc);
1003 } else {
1004 field_index = instruction.VRegC_22c();
1005 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001006
1007 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierc7853442015-03-27 14:35:38 -07001008 ArtField* resolved_field =
1009 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001010
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001011
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001012 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot, dex_pc);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001013 HInstruction* null_check = new (arena_) HNullCheck(object, dex_pc);
1014 current_block_->AddInstruction(null_check);
1015
1016 Primitive::Type field_type = (resolved_field == nullptr)
1017 ? GetFieldAccessType(*dex_file_, field_index)
1018 : resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001019 if (is_put) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001020 HInstruction* value = LoadLocal(source_or_dest_reg, field_type, dex_pc);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001021 HInstruction* field_set = nullptr;
1022 if (resolved_field == nullptr) {
1023 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
1024 field_set = new (arena_) HUnresolvedInstanceFieldSet(null_check,
1025 value,
1026 field_type,
1027 field_index,
1028 dex_pc);
1029 } else {
Mingyao Yang8df69d42015-10-22 15:40:58 -07001030 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Calin Juravlee460d1d2015-09-29 04:52:17 +01001031 field_set = new (arena_) HInstanceFieldSet(null_check,
1032 value,
1033 field_type,
1034 resolved_field->GetOffset(),
1035 resolved_field->IsVolatile(),
1036 field_index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07001037 class_def_index,
Calin Juravlee460d1d2015-09-29 04:52:17 +01001038 *dex_file_,
1039 dex_compilation_unit_->GetDexCache(),
1040 dex_pc);
1041 }
1042 current_block_->AddInstruction(field_set);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001043 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001044 HInstruction* field_get = nullptr;
1045 if (resolved_field == nullptr) {
1046 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
1047 field_get = new (arena_) HUnresolvedInstanceFieldGet(null_check,
1048 field_type,
1049 field_index,
1050 dex_pc);
1051 } else {
Mingyao Yang8df69d42015-10-22 15:40:58 -07001052 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Calin Juravlee460d1d2015-09-29 04:52:17 +01001053 field_get = new (arena_) HInstanceFieldGet(null_check,
1054 field_type,
1055 resolved_field->GetOffset(),
1056 resolved_field->IsVolatile(),
1057 field_index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07001058 class_def_index,
Calin Juravlee460d1d2015-09-29 04:52:17 +01001059 *dex_file_,
1060 dex_compilation_unit_->GetDexCache(),
1061 dex_pc);
1062 }
1063 current_block_->AddInstruction(field_get);
1064 UpdateLocal(source_or_dest_reg, field_get, dex_pc);
Calin Juravlee6f49b42015-09-17 14:04:33 +00001065 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001066
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001067 return true;
1068}
1069
Nicolas Geoffray30451742015-06-19 13:32:41 +01001070static mirror::Class* GetClassFrom(CompilerDriver* driver,
1071 const DexCompilationUnit& compilation_unit) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001072 ScopedObjectAccess soa(Thread::Current());
1073 StackHandleScope<2> hs(soa.Self());
Nicolas Geoffray30451742015-06-19 13:32:41 +01001074 const DexFile& dex_file = *compilation_unit.GetDexFile();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001075 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Nicolas Geoffray30451742015-06-19 13:32:41 +01001076 soa.Decode<mirror::ClassLoader*>(compilation_unit.GetClassLoader())));
1077 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001078 compilation_unit.GetClassLinker()->FindDexCache(soa.Self(), dex_file)));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001079
Nicolas Geoffray30451742015-06-19 13:32:41 +01001080 return driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, &compilation_unit);
1081}
1082
1083mirror::Class* HGraphBuilder::GetOutermostCompilingClass() const {
1084 return GetClassFrom(compiler_driver_, *outer_compilation_unit_);
1085}
1086
1087mirror::Class* HGraphBuilder::GetCompilingClass() const {
1088 return GetClassFrom(compiler_driver_, *dex_compilation_unit_);
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001089}
1090
1091bool HGraphBuilder::IsOutermostCompilingClass(uint16_t type_index) const {
1092 ScopedObjectAccess soa(Thread::Current());
1093 StackHandleScope<4> hs(soa.Self());
1094 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001095 dex_compilation_unit_->GetClassLinker()->FindDexCache(
1096 soa.Self(), *dex_compilation_unit_->GetDexFile())));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001097 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1098 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
1099 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
1100 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
Nicolas Geoffrayafd06412015-06-20 22:44:47 +01001101 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001102
Calin Juravle4e2a5572015-10-07 18:55:43 +01001103 // GetOutermostCompilingClass returns null when the class is unresolved
1104 // (e.g. if it derives from an unresolved class). This is bogus knowing that
1105 // we are compiling it.
1106 // When this happens we cannot establish a direct relation between the current
1107 // class and the outer class, so we return false.
1108 // (Note that this is only used for optimizing invokes and field accesses)
1109 return (cls.Get() != nullptr) && (outer_class.Get() == cls.Get());
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001110}
1111
Calin Juravle07380a22015-09-17 14:15:12 +01001112void HGraphBuilder::BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
1113 uint32_t dex_pc,
1114 bool is_put,
1115 Primitive::Type field_type) {
1116 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1117 uint16_t field_index = instruction.VRegB_21c();
1118
1119 if (is_put) {
1120 HInstruction* value = LoadLocal(source_or_dest_reg, field_type, dex_pc);
1121 current_block_->AddInstruction(
1122 new (arena_) HUnresolvedStaticFieldSet(value, field_type, field_index, dex_pc));
1123 } else {
1124 current_block_->AddInstruction(
1125 new (arena_) HUnresolvedStaticFieldGet(field_type, field_index, dex_pc));
1126 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction(), dex_pc);
1127 }
1128}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001129bool HGraphBuilder::BuildStaticFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001130 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001131 bool is_put) {
1132 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1133 uint16_t field_index = instruction.VRegB_21c();
1134
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001135 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray729645a2015-11-19 13:29:02 +00001136 StackHandleScope<5> hs(soa.Self());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001137 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001138 dex_compilation_unit_->GetClassLinker()->FindDexCache(
1139 soa.Self(), *dex_compilation_unit_->GetDexFile())));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001140 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1141 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
Mathieu Chartierc7853442015-03-27 14:35:38 -07001142 ArtField* resolved_field = compiler_driver_->ResolveField(
1143 soa, dex_cache, class_loader, dex_compilation_unit_, field_index, true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001144
Mathieu Chartierc7853442015-03-27 14:35:38 -07001145 if (resolved_field == nullptr) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001146 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
1147 Primitive::Type field_type = GetFieldAccessType(*dex_file_, field_index);
Calin Juravle07380a22015-09-17 14:15:12 +01001148 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001149 return true;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001150 }
1151
Calin Juravle07380a22015-09-17 14:15:12 +01001152 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001153 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
1154 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001155 outer_compilation_unit_->GetClassLinker()->FindDexCache(soa.Self(), outer_dex_file)));
Nicolas Geoffray30451742015-06-19 13:32:41 +01001156 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001157
1158 // The index at which the field's class is stored in the DexCache's type array.
1159 uint32_t storage_index;
Nicolas Geoffray30451742015-06-19 13:32:41 +01001160 bool is_outer_class = (outer_class.Get() == resolved_field->GetDeclaringClass());
1161 if (is_outer_class) {
1162 storage_index = outer_class->GetDexTypeIndex();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001163 } else if (outer_dex_cache.Get() != dex_cache.Get()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001164 // The compiler driver cannot currently understand multiple dex caches involved. Just bailout.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001165 return false;
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001166 } else {
Calin Juravle07380a22015-09-17 14:15:12 +01001167 // TODO: This is rather expensive. Perf it and cache the results if needed.
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001168 std::pair<bool, bool> pair = compiler_driver_->IsFastStaticField(
1169 outer_dex_cache.Get(),
Nicolas Geoffray30451742015-06-19 13:32:41 +01001170 GetCompilingClass(),
Mathieu Chartierc7853442015-03-27 14:35:38 -07001171 resolved_field,
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001172 field_index,
1173 &storage_index);
1174 bool can_easily_access = is_put ? pair.second : pair.first;
1175 if (!can_easily_access) {
Calin Juravle07380a22015-09-17 14:15:12 +01001176 MaybeRecordStat(MethodCompilationStat::kUnresolvedFieldNotAFastAccess);
1177 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
1178 return true;
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001179 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001180 }
1181
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001182 bool is_in_cache =
1183 compiler_driver_->CanAssumeTypeIsPresentInDexCache(outer_dex_file, storage_index);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001184 HLoadClass* constant = new (arena_) HLoadClass(graph_->GetCurrentMethod(),
1185 storage_index,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001186 outer_dex_file,
Nicolas Geoffray30451742015-06-19 13:32:41 +01001187 is_outer_class,
Calin Juravle98893e12015-10-02 21:05:03 +01001188 dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001189 /*needs_access_check*/ false,
1190 is_in_cache);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001191 current_block_->AddInstruction(constant);
1192
1193 HInstruction* cls = constant;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00001194
1195 Handle<mirror::Class> klass(hs.NewHandle(resolved_field->GetDeclaringClass()));
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +00001196 if (!IsInitialized(klass)) {
Calin Juravle225ff812014-11-13 16:46:39 +00001197 cls = new (arena_) HClinitCheck(constant, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001198 current_block_->AddInstruction(cls);
1199 }
Mingyao Yang8df69d42015-10-22 15:40:58 -07001200
Nicolas Geoffray729645a2015-11-19 13:29:02 +00001201 uint16_t class_def_index = klass->GetDexClassDefIndex();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001202 if (is_put) {
1203 // We need to keep the class alive before loading the value.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001204 HInstruction* value = LoadLocal(source_or_dest_reg, field_type, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001205 DCHECK_EQ(value->GetType(), field_type);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001206 current_block_->AddInstruction(new (arena_) HStaticFieldSet(cls,
1207 value,
1208 field_type,
1209 resolved_field->GetOffset(),
1210 resolved_field->IsVolatile(),
1211 field_index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07001212 class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07001213 *dex_file_,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001214 dex_cache_,
1215 dex_pc));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001216 } else {
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001217 current_block_->AddInstruction(new (arena_) HStaticFieldGet(cls,
1218 field_type,
1219 resolved_field->GetOffset(),
1220 resolved_field->IsVolatile(),
1221 field_index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07001222 class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07001223 *dex_file_,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001224 dex_cache_,
1225 dex_pc));
1226 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001227 }
1228 return true;
1229}
1230
Calin Juravlebacfec32014-11-14 15:54:36 +00001231void HGraphBuilder::BuildCheckedDivRem(uint16_t out_vreg,
1232 uint16_t first_vreg,
1233 int64_t second_vreg_or_constant,
1234 uint32_t dex_pc,
1235 Primitive::Type type,
1236 bool second_is_constant,
1237 bool isDiv) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001238 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Calin Juravled0d48522014-11-04 16:40:20 +00001239
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001240 HInstruction* first = LoadLocal(first_vreg, type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001241 HInstruction* second = nullptr;
1242 if (second_is_constant) {
1243 if (type == Primitive::kPrimInt) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001244 second = graph_->GetIntConstant(second_vreg_or_constant, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001245 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001246 second = graph_->GetLongConstant(second_vreg_or_constant, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001247 }
1248 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001249 second = LoadLocal(second_vreg_or_constant, type, dex_pc);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001250 }
1251
1252 if (!second_is_constant
1253 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0)
1254 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) {
1255 second = new (arena_) HDivZeroCheck(second, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001256 current_block_->AddInstruction(second);
Calin Juravled0d48522014-11-04 16:40:20 +00001257 }
1258
Calin Juravlebacfec32014-11-14 15:54:36 +00001259 if (isDiv) {
1260 current_block_->AddInstruction(new (arena_) HDiv(type, first, second, dex_pc));
1261 } else {
1262 current_block_->AddInstruction(new (arena_) HRem(type, first, second, dex_pc));
1263 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001264 UpdateLocal(out_vreg, current_block_->GetLastInstruction(), dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001265}
1266
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001267void HGraphBuilder::BuildArrayAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001268 uint32_t dex_pc,
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001269 bool is_put,
1270 Primitive::Type anticipated_type) {
1271 uint8_t source_or_dest_reg = instruction.VRegA_23x();
1272 uint8_t array_reg = instruction.VRegB_23x();
1273 uint8_t index_reg = instruction.VRegC_23x();
1274
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001275 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001276 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001277 current_block_->AddInstruction(object);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001278
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001279 HInstruction* length = new (arena_) HArrayLength(object, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001280 current_block_->AddInstruction(length);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001281 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001282 index = new (arena_) HBoundsCheck(index, length, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001283 current_block_->AddInstruction(index);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001284 if (is_put) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001285 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001286 // TODO: Insert a type check node if the type is Object.
Nicolas Geoffray39468442014-09-02 15:17:15 +01001287 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001288 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001289 } else {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001290 current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type, dex_pc));
1291 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001292 }
Mark Mendell1152c922015-04-24 17:06:35 -04001293 graph_->SetHasBoundsChecks(true);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001294}
1295
Calin Juravle225ff812014-11-13 16:46:39 +00001296void HGraphBuilder::BuildFilledNewArray(uint32_t dex_pc,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001297 uint32_t type_index,
1298 uint32_t number_of_vreg_arguments,
1299 bool is_range,
1300 uint32_t* args,
1301 uint32_t register_index) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001302 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments, dex_pc);
Mingyao Yangfb8464a2015-11-02 10:56:59 -08001303 bool finalizable;
1304 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index, &finalizable)
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001305 ? kQuickAllocArrayWithAccessCheck
1306 : kQuickAllocArray;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01001307 HInstruction* object = new (arena_) HNewArray(length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01001308 graph_->GetCurrentMethod(),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01001309 dex_pc,
1310 type_index,
1311 *dex_compilation_unit_->GetDexFile(),
1312 entrypoint);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001313 current_block_->AddInstruction(object);
1314
1315 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1316 DCHECK_EQ(descriptor[0], '[') << descriptor;
1317 char primitive = descriptor[1];
1318 DCHECK(primitive == 'I'
1319 || primitive == 'L'
1320 || primitive == '[') << descriptor;
1321 bool is_reference_array = (primitive == 'L') || (primitive == '[');
1322 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
1323
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001324 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001325 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type, dex_pc);
1326 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001327 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001328 new (arena_) HArraySet(object, index, value, type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001329 }
1330 latest_result_ = object;
1331}
1332
1333template <typename T>
1334void HGraphBuilder::BuildFillArrayData(HInstruction* object,
1335 const T* data,
1336 uint32_t element_count,
1337 Primitive::Type anticipated_type,
Calin Juravle225ff812014-11-13 16:46:39 +00001338 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001339 for (uint32_t i = 0; i < element_count; ++i) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001340 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1341 HInstruction* value = graph_->GetIntConstant(data[i], dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001342 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001343 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001344 }
1345}
1346
Calin Juravle225ff812014-11-13 16:46:39 +00001347void HGraphBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001348 HInstruction* array = LoadLocal(instruction.VRegA_31t(), Primitive::kPrimNot, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001349 HNullCheck* null_check = new (arena_) HNullCheck(array, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001350 current_block_->AddInstruction(null_check);
Calin Juravled0d48522014-11-04 16:40:20 +00001351
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001352 HInstruction* length = new (arena_) HArrayLength(null_check, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001353 current_block_->AddInstruction(length);
1354
Calin Juravle225ff812014-11-13 16:46:39 +00001355 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
Calin Juravled0d48522014-11-04 16:40:20 +00001356 const Instruction::ArrayDataPayload* payload =
1357 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_start_ + payload_offset);
1358 const uint8_t* data = payload->data;
1359 uint32_t element_count = payload->element_count;
1360
1361 // Implementation of this DEX instruction seems to be that the bounds check is
1362 // done before doing any stores.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001363 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00001364 current_block_->AddInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
Calin Juravled0d48522014-11-04 16:40:20 +00001365
1366 switch (payload->element_width) {
1367 case 1:
1368 BuildFillArrayData(null_check,
1369 reinterpret_cast<const int8_t*>(data),
1370 element_count,
1371 Primitive::kPrimByte,
Calin Juravle225ff812014-11-13 16:46:39 +00001372 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001373 break;
1374 case 2:
1375 BuildFillArrayData(null_check,
1376 reinterpret_cast<const int16_t*>(data),
1377 element_count,
1378 Primitive::kPrimShort,
Calin Juravle225ff812014-11-13 16:46:39 +00001379 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001380 break;
1381 case 4:
1382 BuildFillArrayData(null_check,
1383 reinterpret_cast<const int32_t*>(data),
1384 element_count,
1385 Primitive::kPrimInt,
Calin Juravle225ff812014-11-13 16:46:39 +00001386 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001387 break;
1388 case 8:
1389 BuildFillWideArrayData(null_check,
1390 reinterpret_cast<const int64_t*>(data),
1391 element_count,
Calin Juravle225ff812014-11-13 16:46:39 +00001392 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001393 break;
1394 default:
1395 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1396 }
Mark Mendell1152c922015-04-24 17:06:35 -04001397 graph_->SetHasBoundsChecks(true);
Calin Juravled0d48522014-11-04 16:40:20 +00001398}
1399
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001400void HGraphBuilder::BuildFillWideArrayData(HInstruction* object,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +01001401 const int64_t* data,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001402 uint32_t element_count,
Calin Juravle225ff812014-11-13 16:46:39 +00001403 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001404 for (uint32_t i = 0; i < element_count; ++i) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001405 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1406 HInstruction* value = graph_->GetLongConstant(data[i], dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001407 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001408 object, index, value, Primitive::kPrimLong, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001409 }
1410}
1411
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001412static TypeCheckKind ComputeTypeCheckKind(Handle<mirror::Class> cls)
1413 SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle98893e12015-10-02 21:05:03 +01001414 if (cls.Get() == nullptr) {
1415 return TypeCheckKind::kUnresolvedCheck;
1416 } else if (cls->IsInterface()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001417 return TypeCheckKind::kInterfaceCheck;
1418 } else if (cls->IsArrayClass()) {
1419 if (cls->GetComponentType()->IsObjectClass()) {
1420 return TypeCheckKind::kArrayObjectCheck;
1421 } else if (cls->CannotBeAssignedFromOtherTypes()) {
1422 return TypeCheckKind::kExactCheck;
1423 } else {
1424 return TypeCheckKind::kArrayCheck;
1425 }
1426 } else if (cls->IsFinal()) {
1427 return TypeCheckKind::kExactCheck;
1428 } else if (cls->IsAbstract()) {
1429 return TypeCheckKind::kAbstractClassCheck;
1430 } else {
1431 return TypeCheckKind::kClassHierarchyCheck;
1432 }
1433}
1434
Calin Juravle98893e12015-10-02 21:05:03 +01001435void HGraphBuilder::BuildTypeCheck(const Instruction& instruction,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001436 uint8_t destination,
1437 uint8_t reference,
1438 uint16_t type_index,
Calin Juravle225ff812014-11-13 16:46:39 +00001439 uint32_t dex_pc) {
Calin Juravle98893e12015-10-02 21:05:03 +01001440 bool type_known_final, type_known_abstract, use_declaring_class;
1441 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
1442 dex_compilation_unit_->GetDexMethodIndex(),
1443 *dex_compilation_unit_->GetDexFile(),
1444 type_index,
1445 &type_known_final,
1446 &type_known_abstract,
1447 &use_declaring_class);
1448
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001449 ScopedObjectAccess soa(Thread::Current());
1450 StackHandleScope<2> hs(soa.Self());
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001451 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001452 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001453 dex_compilation_unit_->GetClassLinker()->FindDexCache(soa.Self(), dex_file)));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001454 Handle<mirror::Class> resolved_class(hs.NewHandle(dex_cache->GetResolvedType(type_index)));
1455
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001456 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot, dex_pc);
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001457 HLoadClass* cls = new (arena_) HLoadClass(
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001458 graph_->GetCurrentMethod(),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01001459 type_index,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001460 dex_file,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01001461 IsOutermostCompilingClass(type_index),
Calin Juravle98893e12015-10-02 21:05:03 +01001462 dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00001463 !can_access,
1464 compiler_driver_->CanAssumeTypeIsPresentInDexCache(dex_file, type_index));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001465 current_block_->AddInstruction(cls);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001466
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001467 TypeCheckKind check_kind = ComputeTypeCheckKind(resolved_class);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001468 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001469 current_block_->AddInstruction(new (arena_) HInstanceOf(object, cls, check_kind, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001470 UpdateLocal(destination, current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001471 } else {
1472 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
David Brazdilf5552582015-12-27 13:36:12 +00001473 // We emit a CheckCast followed by a BoundType. CheckCast is a statement
1474 // which may throw. If it succeeds BoundType sets the new type of `object`
1475 // for all subsequent uses.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00001476 current_block_->AddInstruction(new (arena_) HCheckCast(object, cls, check_kind, dex_pc));
David Brazdilf5552582015-12-27 13:36:12 +00001477 current_block_->AddInstruction(new (arena_) HBoundType(object, dex_pc));
1478 UpdateLocal(reference, current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001479 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001480}
1481
Mingyao Yangfb8464a2015-11-02 10:56:59 -08001482bool HGraphBuilder::NeedsAccessCheck(uint32_t type_index, bool* finalizable) const {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001483 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
Mingyao Yangfb8464a2015-11-02 10:56:59 -08001484 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index, finalizable);
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001485}
1486
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001487bool HGraphBuilder::CanDecodeQuickenedInfo() const {
1488 return interpreter_metadata_ != nullptr;
1489}
1490
1491uint16_t HGraphBuilder::LookupQuickenedInfo(uint32_t dex_pc) {
1492 DCHECK(interpreter_metadata_ != nullptr);
1493 uint32_t dex_pc_in_map = DecodeUnsignedLeb128(&interpreter_metadata_);
1494 DCHECK_EQ(dex_pc, dex_pc_in_map);
1495 return DecodeUnsignedLeb128(&interpreter_metadata_);
1496}
1497
Calin Juravle225ff812014-11-13 16:46:39 +00001498bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001499 switch (instruction.Opcode()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001500 case Instruction::CONST_4: {
1501 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001502 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n(), dex_pc);
1503 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001504 break;
1505 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001506
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001507 case Instruction::CONST_16: {
1508 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001509 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s(), dex_pc);
1510 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001511 break;
1512 }
1513
Dave Allison20dfc792014-06-16 20:44:29 -07001514 case Instruction::CONST: {
1515 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001516 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i(), dex_pc);
1517 UpdateLocal(register_index, constant, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001518 break;
1519 }
1520
1521 case Instruction::CONST_HIGH16: {
1522 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001523 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16, dex_pc);
1524 UpdateLocal(register_index, constant, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001525 break;
1526 }
1527
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001528 case Instruction::CONST_WIDE_16: {
1529 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001530 // Get 16 bits of constant value, sign extended to 64 bits.
1531 int64_t value = instruction.VRegB_21s();
1532 value <<= 48;
1533 value >>= 48;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001534 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1535 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001536 break;
1537 }
1538
1539 case Instruction::CONST_WIDE_32: {
1540 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001541 // Get 32 bits of constant value, sign extended to 64 bits.
1542 int64_t value = instruction.VRegB_31i();
1543 value <<= 32;
1544 value >>= 32;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001545 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1546 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001547 break;
1548 }
1549
1550 case Instruction::CONST_WIDE: {
1551 int32_t register_index = instruction.VRegA();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001552 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l(), dex_pc);
1553 UpdateLocal(register_index, constant, dex_pc);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001554 break;
1555 }
1556
Dave Allison20dfc792014-06-16 20:44:29 -07001557 case Instruction::CONST_WIDE_HIGH16: {
1558 int32_t register_index = instruction.VRegA();
1559 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001560 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1561 UpdateLocal(register_index, constant, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001562 break;
1563 }
1564
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001565 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001566 case Instruction::MOVE:
1567 case Instruction::MOVE_FROM16:
1568 case Instruction::MOVE_16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001569 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
1570 UpdateLocal(instruction.VRegA(), value, dex_pc);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001571 break;
1572 }
1573
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001574 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001575 case Instruction::MOVE_WIDE:
1576 case Instruction::MOVE_WIDE_FROM16:
1577 case Instruction::MOVE_WIDE_16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001578 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong, dex_pc);
1579 UpdateLocal(instruction.VRegA(), value, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001580 break;
1581 }
1582
1583 case Instruction::MOVE_OBJECT:
1584 case Instruction::MOVE_OBJECT_16:
1585 case Instruction::MOVE_OBJECT_FROM16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001586 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot, dex_pc);
1587 UpdateLocal(instruction.VRegA(), value, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -07001588 break;
1589 }
1590
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001591 case Instruction::RETURN_VOID_NO_BARRIER:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001592 case Instruction::RETURN_VOID: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001593 BuildReturn(instruction, Primitive::kPrimVoid, dex_pc);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001594 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001595 }
1596
Dave Allison20dfc792014-06-16 20:44:29 -07001597#define IF_XX(comparison, cond) \
Calin Juravle225ff812014-11-13 16:46:39 +00001598 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
1599 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001600
Dave Allison20dfc792014-06-16 20:44:29 -07001601 IF_XX(HEqual, EQ);
1602 IF_XX(HNotEqual, NE);
1603 IF_XX(HLessThan, LT);
1604 IF_XX(HLessThanOrEqual, LE);
1605 IF_XX(HGreaterThan, GT);
1606 IF_XX(HGreaterThanOrEqual, GE);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001607
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001608 case Instruction::GOTO:
1609 case Instruction::GOTO_16:
1610 case Instruction::GOTO_32: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001611 current_block_->AddInstruction(new (arena_) HGoto(dex_pc));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001612 current_block_ = nullptr;
1613 break;
1614 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001615
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001616 case Instruction::RETURN: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001617 BuildReturn(instruction, return_type_, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001618 break;
1619 }
1620
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001621 case Instruction::RETURN_OBJECT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001622 BuildReturn(instruction, return_type_, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001623 break;
1624 }
1625
1626 case Instruction::RETURN_WIDE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001627 BuildReturn(instruction, return_type_, dex_pc);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001628 break;
1629 }
1630
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001631 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001632 case Instruction::INVOKE_INTERFACE:
1633 case Instruction::INVOKE_STATIC:
1634 case Instruction::INVOKE_SUPER:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001635 case Instruction::INVOKE_VIRTUAL:
1636 case Instruction::INVOKE_VIRTUAL_QUICK: {
1637 uint16_t method_idx;
1638 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_QUICK) {
1639 if (!CanDecodeQuickenedInfo()) {
1640 return false;
1641 }
1642 method_idx = LookupQuickenedInfo(dex_pc);
1643 } else {
1644 method_idx = instruction.VRegB_35c();
1645 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001646 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001647 uint32_t args[5];
Ian Rogers29a26482014-05-02 15:27:29 -07001648 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00001649 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001650 number_of_vreg_arguments, false, args, -1)) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001651 return false;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001652 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001653 break;
1654 }
1655
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001656 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001657 case Instruction::INVOKE_INTERFACE_RANGE:
1658 case Instruction::INVOKE_STATIC_RANGE:
1659 case Instruction::INVOKE_SUPER_RANGE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001660 case Instruction::INVOKE_VIRTUAL_RANGE:
1661 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
1662 uint16_t method_idx;
1663 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK) {
1664 if (!CanDecodeQuickenedInfo()) {
1665 return false;
1666 }
1667 method_idx = LookupQuickenedInfo(dex_pc);
1668 } else {
1669 method_idx = instruction.VRegB_3rc();
1670 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001671 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1672 uint32_t register_index = instruction.VRegC();
Calin Juravle225ff812014-11-13 16:46:39 +00001673 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001674 number_of_vreg_arguments, true, nullptr, register_index)) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001675 return false;
1676 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001677 break;
1678 }
1679
Roland Levillain88cb1752014-10-20 16:36:47 +01001680 case Instruction::NEG_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001681 Unop_12x<HNeg>(instruction, Primitive::kPrimInt, dex_pc);
Roland Levillain88cb1752014-10-20 16:36:47 +01001682 break;
1683 }
1684
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001685 case Instruction::NEG_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001686 Unop_12x<HNeg>(instruction, Primitive::kPrimLong, dex_pc);
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001687 break;
1688 }
1689
Roland Levillain3dbcb382014-10-28 17:30:07 +00001690 case Instruction::NEG_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001691 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat, dex_pc);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001692 break;
1693 }
1694
1695 case Instruction::NEG_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001696 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble, dex_pc);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001697 break;
1698 }
1699
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001700 case Instruction::NOT_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001701 Unop_12x<HNot>(instruction, Primitive::kPrimInt, dex_pc);
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001702 break;
1703 }
1704
Roland Levillain70566432014-10-24 16:20:17 +01001705 case Instruction::NOT_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001706 Unop_12x<HNot>(instruction, Primitive::kPrimLong, dex_pc);
Roland Levillain70566432014-10-24 16:20:17 +01001707 break;
1708 }
1709
Roland Levillaindff1f282014-11-05 14:15:05 +00001710 case Instruction::INT_TO_LONG: {
Roland Levillain624279f2014-12-04 11:54:28 +00001711 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong, dex_pc);
Roland Levillaindff1f282014-11-05 14:15:05 +00001712 break;
1713 }
1714
Roland Levillaincff13742014-11-17 14:32:17 +00001715 case Instruction::INT_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001716 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00001717 break;
1718 }
1719
1720 case Instruction::INT_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001721 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00001722 break;
1723 }
1724
Roland Levillain946e1432014-11-11 17:35:19 +00001725 case Instruction::LONG_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001726 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt, dex_pc);
Roland Levillain946e1432014-11-11 17:35:19 +00001727 break;
1728 }
1729
Roland Levillain6d0e4832014-11-27 18:31:21 +00001730 case Instruction::LONG_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001731 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimFloat, dex_pc);
Roland Levillain6d0e4832014-11-27 18:31:21 +00001732 break;
1733 }
1734
Roland Levillain647b9ed2014-11-27 12:06:00 +00001735 case Instruction::LONG_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001736 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble, dex_pc);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001737 break;
1738 }
1739
Roland Levillain3f8f9362014-12-02 17:45:01 +00001740 case Instruction::FLOAT_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001741 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimInt, dex_pc);
1742 break;
1743 }
1744
1745 case Instruction::FLOAT_TO_LONG: {
1746 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimLong, dex_pc);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001747 break;
1748 }
1749
Roland Levillain8964e2b2014-12-04 12:10:50 +00001750 case Instruction::FLOAT_TO_DOUBLE: {
1751 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimDouble, dex_pc);
1752 break;
1753 }
1754
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001755 case Instruction::DOUBLE_TO_INT: {
1756 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimInt, dex_pc);
1757 break;
1758 }
1759
1760 case Instruction::DOUBLE_TO_LONG: {
1761 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimLong, dex_pc);
1762 break;
1763 }
1764
Roland Levillain8964e2b2014-12-04 12:10:50 +00001765 case Instruction::DOUBLE_TO_FLOAT: {
1766 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimFloat, dex_pc);
1767 break;
1768 }
1769
Roland Levillain51d3fc42014-11-13 14:11:42 +00001770 case Instruction::INT_TO_BYTE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001771 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte, dex_pc);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001772 break;
1773 }
1774
Roland Levillain01a8d712014-11-14 16:27:39 +00001775 case Instruction::INT_TO_SHORT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001776 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort, dex_pc);
Roland Levillain01a8d712014-11-14 16:27:39 +00001777 break;
1778 }
1779
Roland Levillain981e4542014-11-14 11:47:14 +00001780 case Instruction::INT_TO_CHAR: {
Roland Levillain624279f2014-12-04 11:54:28 +00001781 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar, dex_pc);
Roland Levillain981e4542014-11-14 11:47:14 +00001782 break;
1783 }
1784
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001785 case Instruction::ADD_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001786 Binop_23x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001787 break;
1788 }
1789
1790 case Instruction::ADD_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001791 Binop_23x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001792 break;
1793 }
1794
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001795 case Instruction::ADD_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001796 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001797 break;
1798 }
1799
1800 case Instruction::ADD_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001801 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001802 break;
1803 }
1804
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001805 case Instruction::SUB_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001806 Binop_23x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001807 break;
1808 }
1809
1810 case Instruction::SUB_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001811 Binop_23x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001812 break;
1813 }
1814
Calin Juravle096cc022014-10-23 17:01:13 +01001815 case Instruction::SUB_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001816 Binop_23x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01001817 break;
1818 }
1819
1820 case Instruction::SUB_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001821 Binop_23x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01001822 break;
1823 }
1824
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001825 case Instruction::ADD_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001826 Binop_12x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001827 break;
1828 }
1829
Calin Juravle34bacdf2014-10-07 20:23:36 +01001830 case Instruction::MUL_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001831 Binop_23x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01001832 break;
1833 }
1834
1835 case Instruction::MUL_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001836 Binop_23x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01001837 break;
1838 }
1839
Calin Juravleb5bfa962014-10-21 18:02:24 +01001840 case Instruction::MUL_FLOAT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001841 Binop_23x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01001842 break;
1843 }
1844
1845 case Instruction::MUL_DOUBLE: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001846 Binop_23x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01001847 break;
1848 }
1849
Calin Juravled0d48522014-11-04 16:40:20 +00001850 case Instruction::DIV_INT: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001851 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1852 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravled0d48522014-11-04 16:40:20 +00001853 break;
1854 }
1855
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001856 case Instruction::DIV_LONG: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001857 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1858 dex_pc, Primitive::kPrimLong, false, true);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001859 break;
1860 }
1861
Calin Juravle7c4954d2014-10-28 16:57:40 +00001862 case Instruction::DIV_FLOAT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001863 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001864 break;
1865 }
1866
1867 case Instruction::DIV_DOUBLE: {
Calin Juravle225ff812014-11-13 16:46:39 +00001868 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001869 break;
1870 }
1871
Calin Juravlebacfec32014-11-14 15:54:36 +00001872 case Instruction::REM_INT: {
1873 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1874 dex_pc, Primitive::kPrimInt, false, false);
1875 break;
1876 }
1877
1878 case Instruction::REM_LONG: {
1879 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1880 dex_pc, Primitive::kPrimLong, false, false);
1881 break;
1882 }
1883
Calin Juravled2ec87d2014-12-08 14:24:46 +00001884 case Instruction::REM_FLOAT: {
1885 Binop_23x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
1886 break;
1887 }
1888
1889 case Instruction::REM_DOUBLE: {
1890 Binop_23x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
1891 break;
1892 }
1893
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001894 case Instruction::AND_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001895 Binop_23x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001896 break;
1897 }
1898
1899 case Instruction::AND_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001900 Binop_23x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001901 break;
1902 }
1903
Calin Juravle9aec02f2014-11-18 23:06:35 +00001904 case Instruction::SHL_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001905 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00001906 break;
1907 }
1908
1909 case Instruction::SHL_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001910 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00001911 break;
1912 }
1913
1914 case Instruction::SHR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001915 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00001916 break;
1917 }
1918
1919 case Instruction::SHR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001920 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00001921 break;
1922 }
1923
1924 case Instruction::USHR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001925 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00001926 break;
1927 }
1928
1929 case Instruction::USHR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001930 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00001931 break;
1932 }
1933
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001934 case Instruction::OR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001935 Binop_23x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001936 break;
1937 }
1938
1939 case Instruction::OR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001940 Binop_23x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001941 break;
1942 }
1943
1944 case Instruction::XOR_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001945 Binop_23x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001946 break;
1947 }
1948
1949 case Instruction::XOR_LONG: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001950 Binop_23x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001951 break;
1952 }
1953
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001954 case Instruction::ADD_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001955 Binop_12x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001956 break;
1957 }
1958
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001959 case Instruction::ADD_DOUBLE_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001960 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001961 break;
1962 }
1963
1964 case Instruction::ADD_FLOAT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001965 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001966 break;
1967 }
1968
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001969 case Instruction::SUB_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001970 Binop_12x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001971 break;
1972 }
1973
1974 case Instruction::SUB_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001975 Binop_12x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001976 break;
1977 }
1978
Calin Juravle096cc022014-10-23 17:01:13 +01001979 case Instruction::SUB_FLOAT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001980 Binop_12x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01001981 break;
1982 }
1983
1984 case Instruction::SUB_DOUBLE_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001985 Binop_12x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle096cc022014-10-23 17:01:13 +01001986 break;
1987 }
1988
Calin Juravle34bacdf2014-10-07 20:23:36 +01001989 case Instruction::MUL_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001990 Binop_12x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01001991 break;
1992 }
1993
1994 case Instruction::MUL_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001995 Binop_12x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01001996 break;
1997 }
1998
Calin Juravleb5bfa962014-10-21 18:02:24 +01001999 case Instruction::MUL_FLOAT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002000 Binop_12x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01002001 break;
2002 }
2003
2004 case Instruction::MUL_DOUBLE_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002005 Binop_12x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravleb5bfa962014-10-21 18:02:24 +01002006 break;
2007 }
2008
Calin Juravle865fc882014-11-06 17:09:03 +00002009 case Instruction::DIV_INT_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002010 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2011 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravle865fc882014-11-06 17:09:03 +00002012 break;
2013 }
2014
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002015 case Instruction::DIV_LONG_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002016 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2017 dex_pc, Primitive::kPrimLong, false, true);
2018 break;
2019 }
2020
2021 case Instruction::REM_INT_2ADDR: {
2022 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2023 dex_pc, Primitive::kPrimInt, false, false);
2024 break;
2025 }
2026
2027 case Instruction::REM_LONG_2ADDR: {
2028 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2029 dex_pc, Primitive::kPrimLong, false, false);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002030 break;
2031 }
2032
Calin Juravled2ec87d2014-12-08 14:24:46 +00002033 case Instruction::REM_FLOAT_2ADDR: {
2034 Binop_12x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2035 break;
2036 }
2037
2038 case Instruction::REM_DOUBLE_2ADDR: {
2039 Binop_12x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2040 break;
2041 }
2042
Calin Juravle9aec02f2014-11-18 23:06:35 +00002043 case Instruction::SHL_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002044 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002045 break;
2046 }
2047
2048 case Instruction::SHL_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002049 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002050 break;
2051 }
2052
2053 case Instruction::SHR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002054 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002055 break;
2056 }
2057
2058 case Instruction::SHR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002059 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002060 break;
2061 }
2062
2063 case Instruction::USHR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002064 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002065 break;
2066 }
2067
2068 case Instruction::USHR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002069 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002070 break;
2071 }
2072
Calin Juravle7c4954d2014-10-28 16:57:40 +00002073 case Instruction::DIV_FLOAT_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00002074 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002075 break;
2076 }
2077
2078 case Instruction::DIV_DOUBLE_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00002079 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002080 break;
2081 }
2082
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002083 case Instruction::AND_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002084 Binop_12x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002085 break;
2086 }
2087
2088 case Instruction::AND_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002089 Binop_12x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002090 break;
2091 }
2092
2093 case Instruction::OR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002094 Binop_12x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002095 break;
2096 }
2097
2098 case Instruction::OR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002099 Binop_12x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002100 break;
2101 }
2102
2103 case Instruction::XOR_INT_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002104 Binop_12x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002105 break;
2106 }
2107
2108 case Instruction::XOR_LONG_2ADDR: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002109 Binop_12x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002110 break;
2111 }
2112
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002113 case Instruction::ADD_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002114 Binop_22s<HAdd>(instruction, false, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002115 break;
2116 }
2117
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002118 case Instruction::AND_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002119 Binop_22s<HAnd>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002120 break;
2121 }
2122
2123 case Instruction::OR_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002124 Binop_22s<HOr>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002125 break;
2126 }
2127
2128 case Instruction::XOR_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002129 Binop_22s<HXor>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002130 break;
2131 }
2132
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002133 case Instruction::RSUB_INT: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002134 Binop_22s<HSub>(instruction, true, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002135 break;
2136 }
2137
Calin Juravle34bacdf2014-10-07 20:23:36 +01002138 case Instruction::MUL_INT_LIT16: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002139 Binop_22s<HMul>(instruction, false, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002140 break;
2141 }
2142
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002143 case Instruction::ADD_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002144 Binop_22b<HAdd>(instruction, false, dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002145 break;
2146 }
2147
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002148 case Instruction::AND_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002149 Binop_22b<HAnd>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002150 break;
2151 }
2152
2153 case Instruction::OR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002154 Binop_22b<HOr>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002155 break;
2156 }
2157
2158 case Instruction::XOR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002159 Binop_22b<HXor>(instruction, false, dex_pc);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002160 break;
2161 }
2162
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002163 case Instruction::RSUB_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002164 Binop_22b<HSub>(instruction, true, dex_pc);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002165 break;
2166 }
2167
Calin Juravle34bacdf2014-10-07 20:23:36 +01002168 case Instruction::MUL_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002169 Binop_22b<HMul>(instruction, false, dex_pc);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002170 break;
2171 }
2172
Calin Juravled0d48522014-11-04 16:40:20 +00002173 case Instruction::DIV_INT_LIT16:
2174 case Instruction::DIV_INT_LIT8: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002175 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2176 dex_pc, Primitive::kPrimInt, true, true);
2177 break;
2178 }
2179
2180 case Instruction::REM_INT_LIT16:
2181 case Instruction::REM_INT_LIT8: {
2182 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2183 dex_pc, Primitive::kPrimInt, true, false);
Calin Juravled0d48522014-11-04 16:40:20 +00002184 break;
2185 }
2186
Calin Juravle9aec02f2014-11-18 23:06:35 +00002187 case Instruction::SHL_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002188 Binop_22b<HShl>(instruction, false, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002189 break;
2190 }
2191
2192 case Instruction::SHR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002193 Binop_22b<HShr>(instruction, false, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002194 break;
2195 }
2196
2197 case Instruction::USHR_INT_LIT8: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002198 Binop_22b<HUShr>(instruction, false, dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002199 break;
2200 }
2201
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002202 case Instruction::NEW_INSTANCE: {
David Brazdil6de19382016-01-08 17:37:10 +00002203 if (!BuildNewInstance(instruction.VRegB_21c(), dex_pc)) {
2204 return false;
Jeff Hao848f70a2014-01-15 13:49:50 -08002205 }
David Brazdil6de19382016-01-08 17:37:10 +00002206 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002207 break;
2208 }
2209
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002210 case Instruction::NEW_ARRAY: {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002211 uint16_t type_index = instruction.VRegC_22c();
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002212 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt, dex_pc);
Mingyao Yangfb8464a2015-11-02 10:56:59 -08002213 bool finalizable;
2214 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index, &finalizable)
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002215 ? kQuickAllocArrayWithAccessCheck
2216 : kQuickAllocArray;
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002217 current_block_->AddInstruction(new (arena_) HNewArray(length,
2218 graph_->GetCurrentMethod(),
2219 dex_pc,
2220 type_index,
2221 *dex_compilation_unit_->GetDexFile(),
2222 entrypoint));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002223 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002224 break;
2225 }
2226
2227 case Instruction::FILLED_NEW_ARRAY: {
2228 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
2229 uint32_t type_index = instruction.VRegB_35c();
2230 uint32_t args[5];
2231 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00002232 BuildFilledNewArray(dex_pc, type_index, number_of_vreg_arguments, false, args, 0);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002233 break;
2234 }
2235
2236 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2237 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
2238 uint32_t type_index = instruction.VRegB_3rc();
2239 uint32_t register_index = instruction.VRegC_3rc();
2240 BuildFilledNewArray(
Calin Juravle225ff812014-11-13 16:46:39 +00002241 dex_pc, type_index, number_of_vreg_arguments, true, nullptr, register_index);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002242 break;
2243 }
2244
2245 case Instruction::FILL_ARRAY_DATA: {
Calin Juravle225ff812014-11-13 16:46:39 +00002246 BuildFillArrayData(instruction, dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002247 break;
2248 }
2249
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002250 case Instruction::MOVE_RESULT:
Dave Allison20dfc792014-06-16 20:44:29 -07002251 case Instruction::MOVE_RESULT_WIDE:
David Brazdilfc6a86a2015-06-26 10:33:45 +00002252 case Instruction::MOVE_RESULT_OBJECT: {
Nicolas Geoffray1efcc222015-06-24 12:41:20 +01002253 if (latest_result_ == nullptr) {
2254 // Only dead code can lead to this situation, where the verifier
2255 // does not reject the method.
2256 } else {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002257 // An Invoke/FilledNewArray and its MoveResult could have landed in
2258 // different blocks if there was a try/catch block boundary between
2259 // them. For Invoke, we insert a StoreLocal after the instruction. For
2260 // FilledNewArray, the local needs to be updated after the array was
2261 // filled, otherwise we might overwrite an input vreg.
2262 HStoreLocal* update_local =
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002263 new (arena_) HStoreLocal(GetLocalAt(instruction.VRegA()), latest_result_, dex_pc);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002264 HBasicBlock* block = latest_result_->GetBlock();
2265 if (block == current_block_) {
2266 // MoveResult and the previous instruction are in the same block.
2267 current_block_->AddInstruction(update_local);
2268 } else {
2269 // The two instructions are in different blocks. Insert the MoveResult
2270 // before the final control-flow instruction of the previous block.
2271 DCHECK(block->EndsWithControlFlowInstruction());
2272 DCHECK(current_block_->GetInstructions().IsEmpty());
2273 block->InsertInstructionBefore(update_local, block->GetLastInstruction());
2274 }
Nicolas Geoffray1efcc222015-06-24 12:41:20 +01002275 latest_result_ = nullptr;
2276 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002277 break;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002278 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002279
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002280 case Instruction::CMP_LONG: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002281 Binop_23x_cmp(instruction, Primitive::kPrimLong, ComparisonBias::kNoBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002282 break;
2283 }
2284
2285 case Instruction::CMPG_FLOAT: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002286 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kGtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002287 break;
2288 }
2289
2290 case Instruction::CMPG_DOUBLE: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002291 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kGtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002292 break;
2293 }
2294
2295 case Instruction::CMPL_FLOAT: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002296 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kLtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002297 break;
2298 }
2299
2300 case Instruction::CMPL_DOUBLE: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002301 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kLtBias, dex_pc);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002302 break;
2303 }
2304
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002305 case Instruction::NOP:
2306 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002307
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002308 case Instruction::IGET:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002309 case Instruction::IGET_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002310 case Instruction::IGET_WIDE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002311 case Instruction::IGET_WIDE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002312 case Instruction::IGET_OBJECT:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002313 case Instruction::IGET_OBJECT_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002314 case Instruction::IGET_BOOLEAN:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002315 case Instruction::IGET_BOOLEAN_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002316 case Instruction::IGET_BYTE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002317 case Instruction::IGET_BYTE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002318 case Instruction::IGET_CHAR:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002319 case Instruction::IGET_CHAR_QUICK:
2320 case Instruction::IGET_SHORT:
2321 case Instruction::IGET_SHORT_QUICK: {
Calin Juravle225ff812014-11-13 16:46:39 +00002322 if (!BuildInstanceFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002323 return false;
2324 }
2325 break;
2326 }
2327
2328 case Instruction::IPUT:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002329 case Instruction::IPUT_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002330 case Instruction::IPUT_WIDE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002331 case Instruction::IPUT_WIDE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002332 case Instruction::IPUT_OBJECT:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002333 case Instruction::IPUT_OBJECT_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002334 case Instruction::IPUT_BOOLEAN:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002335 case Instruction::IPUT_BOOLEAN_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002336 case Instruction::IPUT_BYTE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002337 case Instruction::IPUT_BYTE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002338 case Instruction::IPUT_CHAR:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002339 case Instruction::IPUT_CHAR_QUICK:
2340 case Instruction::IPUT_SHORT:
2341 case Instruction::IPUT_SHORT_QUICK: {
Calin Juravle225ff812014-11-13 16:46:39 +00002342 if (!BuildInstanceFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002343 return false;
2344 }
2345 break;
2346 }
2347
2348 case Instruction::SGET:
2349 case Instruction::SGET_WIDE:
2350 case Instruction::SGET_OBJECT:
2351 case Instruction::SGET_BOOLEAN:
2352 case Instruction::SGET_BYTE:
2353 case Instruction::SGET_CHAR:
2354 case Instruction::SGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002355 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002356 return false;
2357 }
2358 break;
2359 }
2360
2361 case Instruction::SPUT:
2362 case Instruction::SPUT_WIDE:
2363 case Instruction::SPUT_OBJECT:
2364 case Instruction::SPUT_BOOLEAN:
2365 case Instruction::SPUT_BYTE:
2366 case Instruction::SPUT_CHAR:
2367 case Instruction::SPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002368 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002369 return false;
2370 }
2371 break;
2372 }
2373
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002374#define ARRAY_XX(kind, anticipated_type) \
2375 case Instruction::AGET##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00002376 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002377 break; \
2378 } \
2379 case Instruction::APUT##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00002380 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002381 break; \
2382 }
2383
2384 ARRAY_XX(, Primitive::kPrimInt);
2385 ARRAY_XX(_WIDE, Primitive::kPrimLong);
2386 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
2387 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
2388 ARRAY_XX(_BYTE, Primitive::kPrimByte);
2389 ARRAY_XX(_CHAR, Primitive::kPrimChar);
2390 ARRAY_XX(_SHORT, Primitive::kPrimShort);
2391
Nicolas Geoffray39468442014-09-02 15:17:15 +01002392 case Instruction::ARRAY_LENGTH: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002393 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00002394 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002395 current_block_->AddInstruction(object);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002396 current_block_->AddInstruction(new (arena_) HArrayLength(object, dex_pc));
2397 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002398 break;
2399 }
2400
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002401 case Instruction::CONST_STRING: {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00002402 uint32_t string_index = instruction.VRegB_21c();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002403 current_block_->AddInstruction(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002404 new (arena_) HLoadString(graph_->GetCurrentMethod(), string_index, *dex_file_, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002405 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002406 break;
2407 }
2408
2409 case Instruction::CONST_STRING_JUMBO: {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00002410 uint32_t string_index = instruction.VRegB_31c();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002411 current_block_->AddInstruction(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002412 new (arena_) HLoadString(graph_->GetCurrentMethod(), string_index, *dex_file_, dex_pc));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002413 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002414 break;
2415 }
2416
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002417 case Instruction::CONST_CLASS: {
2418 uint16_t type_index = instruction.VRegB_21c();
2419 bool type_known_final;
2420 bool type_known_abstract;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002421 bool dont_use_is_referrers_class;
2422 // `CanAccessTypeWithoutChecks` will tell whether the method being
2423 // built is trying to access its own class, so that the generated
2424 // code can optimize for this case. However, the optimization does not
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002425 // work for inlining, so we use `IsOutermostCompilingClass` instead.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002426 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
2427 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002428 &type_known_final, &type_known_abstract, &dont_use_is_referrers_class);
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002429 current_block_->AddInstruction(new (arena_) HLoadClass(
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002430 graph_->GetCurrentMethod(),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002431 type_index,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00002432 *dex_file_,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002433 IsOutermostCompilingClass(type_index),
Calin Juravle98893e12015-10-02 21:05:03 +01002434 dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00002435 !can_access,
2436 compiler_driver_->CanAssumeTypeIsPresentInDexCache(*dex_file_, type_index)));
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002437 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction(), dex_pc);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002438 break;
2439 }
2440
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002441 case Instruction::MOVE_EXCEPTION: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002442 current_block_->AddInstruction(new (arena_) HLoadException(dex_pc));
2443 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction(), dex_pc);
2444 current_block_->AddInstruction(new (arena_) HClearException(dex_pc));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002445 break;
2446 }
2447
2448 case Instruction::THROW: {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002449 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot, dex_pc);
Calin Juravle225ff812014-11-13 16:46:39 +00002450 current_block_->AddInstruction(new (arena_) HThrow(exception, dex_pc));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002451 // We finished building this block. Set the current block to null to avoid
2452 // adding dead instructions to it.
2453 current_block_ = nullptr;
2454 break;
2455 }
2456
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002457 case Instruction::INSTANCE_OF: {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002458 uint8_t destination = instruction.VRegA_22c();
2459 uint8_t reference = instruction.VRegB_22c();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002460 uint16_t type_index = instruction.VRegC_22c();
Calin Juravle98893e12015-10-02 21:05:03 +01002461 BuildTypeCheck(instruction, destination, reference, type_index, dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002462 break;
2463 }
2464
2465 case Instruction::CHECK_CAST: {
2466 uint8_t reference = instruction.VRegA_21c();
2467 uint16_t type_index = instruction.VRegB_21c();
Calin Juravle98893e12015-10-02 21:05:03 +01002468 BuildTypeCheck(instruction, -1, reference, type_index, dex_pc);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002469 break;
2470 }
2471
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002472 case Instruction::MONITOR_ENTER: {
2473 current_block_->AddInstruction(new (arena_) HMonitorOperation(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002474 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot, dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002475 HMonitorOperation::OperationKind::kEnter,
Calin Juravle225ff812014-11-13 16:46:39 +00002476 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002477 break;
2478 }
2479
2480 case Instruction::MONITOR_EXIT: {
2481 current_block_->AddInstruction(new (arena_) HMonitorOperation(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002482 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot, dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002483 HMonitorOperation::OperationKind::kExit,
Calin Juravle225ff812014-11-13 16:46:39 +00002484 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002485 break;
2486 }
2487
David Brazdil86ea7ee2016-02-16 09:26:07 +00002488 case Instruction::SPARSE_SWITCH:
Andreas Gamped881df52014-11-24 23:28:39 -08002489 case Instruction::PACKED_SWITCH: {
David Brazdil86ea7ee2016-02-16 09:26:07 +00002490 BuildSwitch(instruction, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08002491 break;
2492 }
2493
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002494 default:
Calin Juravle48c2b032014-12-09 18:11:36 +00002495 VLOG(compiler) << "Did not compile "
2496 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
2497 << " because of unhandled instruction "
2498 << instruction.Name();
2499 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnhandledInstruction);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002500 return false;
2501 }
2502 return true;
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00002503} // NOLINT(readability/fn_size)
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002504
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01002505HLocal* HGraphBuilder::GetLocalAt(uint32_t register_index) const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01002506 return locals_[register_index];
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002507}
2508
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01002509void HGraphBuilder::UpdateLocal(uint32_t register_index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002510 HInstruction* instruction,
2511 uint32_t dex_pc) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002512 HLocal* local = GetLocalAt(register_index);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002513 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction, dex_pc));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002514}
2515
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01002516HInstruction* HGraphBuilder::LoadLocal(uint32_t register_index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002517 Primitive::Type type,
2518 uint32_t dex_pc) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002519 HLocal* local = GetLocalAt(register_index);
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002520 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type, dex_pc));
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002521 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002522}
2523
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002524} // namespace art