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 Brazdil60328912016-04-04 17:47:42 +000023#include "bytecode_utils.h"
24#include "class_linker.h"
Jeff Hao848f70a2014-01-15 13:49:50 -080025#include "dex/verified_method.h"
David Brazdil60328912016-04-04 17:47:42 +000026#include "dex_file-inl.h"
27#include "dex_instruction-inl.h"
28#include "dex/verified_method.h"
29#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"
David Brazdil60328912016-04-04 17:47:42 +000035#include "scoped_thread_state_change.h"
36#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
David Brazdil60328912016-04-04 17:47:42 +000042void HGraphBuilder::InitializeLocals(uint16_t count) {
43 graph_->SetNumberOfVRegs(count);
44 locals_.resize(count);
45 HBasicBlock* entry_block = graph_->GetEntryBlock();
46 for (int i = 0; i < count; i++) {
47 HLocal* local = new (arena_) HLocal(i);
48 entry_block->AddInstruction(local);
49 locals_[i] = local;
50 }
51}
52
53void HGraphBuilder::InitializeParameters(uint16_t number_of_parameters) {
54 // dex_compilation_unit_ is null only when unit testing.
55 if (dex_compilation_unit_ == nullptr) {
56 return;
57 }
58
59 HBasicBlock* entry_block = graph_->GetEntryBlock();
60
61 graph_->SetNumberOfInVRegs(number_of_parameters);
62 const char* shorty = dex_compilation_unit_->GetShorty();
63 int locals_index = locals_.size() - number_of_parameters;
64 int parameter_index = 0;
65
66 const DexFile::MethodId& referrer_method_id =
67 dex_file_->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
68 if (!dex_compilation_unit_->IsStatic()) {
69 // Add the implicit 'this' argument, not expressed in the signature.
70 HParameterValue* parameter = new (arena_) HParameterValue(*dex_file_,
71 referrer_method_id.class_idx_,
72 parameter_index++,
73 Primitive::kPrimNot,
74 true);
75 entry_block->AddInstruction(parameter);
76 HLocal* local = GetLocalAt(locals_index++);
77 entry_block->AddInstruction(new (arena_) HStoreLocal(local, parameter, local->GetDexPc()));
78 number_of_parameters--;
79 }
80
81 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;
91 entry_block->AddInstruction(parameter);
92 HLocal* local = GetLocalAt(locals_index++);
93 // Store the parameter value in the local that the dex code will use
94 // to reference that parameter.
95 entry_block->AddInstruction(new (arena_) HStoreLocal(local, parameter, local->GetDexPc()));
96 bool is_wide = (parameter->GetType() == Primitive::kPrimLong)
97 || (parameter->GetType() == Primitive::kPrimDouble);
98 if (is_wide) {
99 i++;
100 locals_index++;
101 parameter_index++;
102 }
103 }
104}
105
106template<typename T>
107void HGraphBuilder::If_22t(const Instruction& instruction, uint32_t dex_pc) {
108 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);
111 current_block_->AddInstruction(comparison);
112 HInstruction* ifinst = new (arena_) HIf(comparison, dex_pc);
113 current_block_->AddInstruction(ifinst);
114 current_block_ = nullptr;
115}
116
117template<typename T>
118void HGraphBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) {
119 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt, dex_pc);
120 T* comparison = new (arena_) T(value, graph_->GetIntConstant(0, dex_pc), dex_pc);
121 current_block_->AddInstruction(comparison);
122 HInstruction* ifinst = new (arena_) HIf(comparison, dex_pc);
123 current_block_->AddInstruction(ifinst);
124 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 Brazdil60328912016-04-04 17:47:42 +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();
175 }
176}
David Brazdile3ff7b22016-03-02 16:48:20 +0000177
David Brazdil60328912016-04-04 17:47:42 +0000178bool HGraphBuilder::GenerateInstructions() {
179 // 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) {
186 const uint32_t num_instructions = code_item_.insns_size_in_code_units_;
187 native_debug_info_locations =
188 ArenaBitVector::Create(arena_, num_instructions, false, kArenaAllocGraphBuilder);
189 FindNativeDebugInfoLocations(native_debug_info_locations);
190 }
191
192 InitializeLocals(code_item_.registers_size_);
193 InitializeParameters(code_item_.ins_size_);
194
195 // Add the suspend check to the entry block.
196 current_block_ = graph_->GetEntryBlock();
197 current_block_->AddInstruction(new (arena_) HSuspendCheck(0));
198
199 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) {
204 if (current_block_ != nullptr) {
205 // 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));
209 }
210 DCHECK(BlockIsNotPopulated(next_block));
211 current_block_ = next_block;
212 }
213
214 if (current_block_ == nullptr) {
215 // Unreachable code.
216 continue;
217 }
218
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 }
226 }
227
228 // Add Exit to the exit block.
229 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 }
235
236 return true;
237}
238
239GraphAnalysisResult HGraphBuilder::BuildGraph(StackHandleScopeCollection* handles) {
240 DCHECK(graph_->GetBlocks().empty());
David Brazdil86ea7ee2016-02-16 09:26:07 +0000241 graph_->SetMaximumNumberOfOutVRegs(code_item_.outs_size_);
242 graph_->SetHasTryCatch(code_item_.tries_size_ != 0);
David Brazdil60328912016-04-04 17:47:42 +0000243 graph_->InitializeInexactObjectRTI(handles);
David Brazdil86ea7ee2016-02-16 09:26:07 +0000244
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.
David Brazdil60328912016-04-04 17:47:42 +0000264 if (!GenerateInstructions()) {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000265 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.
David Brazdil60328912016-04-04 17:47:42 +0000269 return SsaBuilder(graph_, code_item_, handles).BuildSsa();
270}
271
272void HGraphBuilder::FindNativeDebugInfoLocations(ArenaBitVector* locations) {
273 // 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 };
281 dex_file_->DecodeDebugPositionInfo(&code_item_, Callback::Position, locations);
282 // Instruction-specific tweaks.
283 const Instruction* const begin = Instruction::At(code_item_.insns_);
284 const Instruction* const end = begin->RelativeAt(code_item_.insns_size_in_code_units_);
285 for (const Instruction* inst = begin; inst < end; inst = inst->Next()) {
286 switch (inst->Opcode()) {
287 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.
291 locations->ClearBit(inst->GetDexPc(code_item_.insns_));
292 const Instruction* next = inst->Next();
293 if (next < end) {
294 locations->SetBit(next->GetDexPc(code_item_.insns_));
295 }
296 break;
297 }
298 default:
299 break;
300 }
301 }
302}
303
304template<typename T>
305void 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);
311}
312
313void HGraphBuilder::Conversion_12x(const Instruction& instruction,
314 Primitive::Type input_type,
315 Primitive::Type result_type,
316 uint32_t dex_pc) {
317 HInstruction* first = LoadLocal(instruction.VRegB(), input_type, dex_pc);
318 current_block_->AddInstruction(new (arena_) HTypeConversion(result_type, first, dex_pc));
319 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
320}
321
322template<typename T>
323void HGraphBuilder::Binop_23x(const Instruction& instruction,
324 Primitive::Type type,
325 uint32_t dex_pc) {
326 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
327 HInstruction* second = LoadLocal(instruction.VRegC(), type, dex_pc);
328 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
329 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
330}
331
332template<typename T>
333void HGraphBuilder::Binop_23x_shift(const Instruction& instruction,
334 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);
340}
341
342void HGraphBuilder::Binop_23x_cmp(const Instruction& instruction,
343 Primitive::Type type,
344 ComparisonBias bias,
345 uint32_t dex_pc) {
346 HInstruction* first = LoadLocal(instruction.VRegB(), type, dex_pc);
347 HInstruction* second = LoadLocal(instruction.VRegC(), type, dex_pc);
348 current_block_->AddInstruction(new (arena_) HCompare(type, first, second, bias, dex_pc));
349 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
350}
351
352template<typename T>
353void 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);
359}
360
361template<typename T>
362void HGraphBuilder::Binop_12x(const Instruction& instruction,
363 Primitive::Type type,
364 uint32_t dex_pc) {
365 HInstruction* first = LoadLocal(instruction.VRegA(), type, dex_pc);
366 HInstruction* second = LoadLocal(instruction.VRegB(), type, dex_pc);
367 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
368 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
369}
370
371template<typename T>
372void 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);
375 if (reverse) {
376 std::swap(first, second);
377 }
378 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second, dex_pc));
379 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
380}
381
382template<typename T>
383void 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);
386 if (reverse) {
387 std::swap(first, second);
388 }
389 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second, dex_pc));
390 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
391}
392
393static bool RequiresConstructorBarrier(const DexCompilationUnit* cu, const CompilerDriver& driver) {
394 Thread* self = Thread::Current();
395 return cu->IsConstructor()
396 && driver.RequiresConstructorBarrier(self, cu->GetDexFile(), cu->GetClassDefIndex());
397}
398
399// 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
436void HGraphBuilder::BuildReturn(const Instruction& instruction,
437 Primitive::Type type,
438 uint32_t dex_pc) {
439 if (type == Primitive::kPrimVoid) {
440 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 }
446 current_block_->AddInstruction(new (arena_) HMemoryBarrier(kStoreStore, dex_pc));
447 }
448 current_block_->AddInstruction(new (arena_) HReturnVoid(dex_pc));
449 } else {
450 HInstruction* value = LoadLocal(instruction.VRegA(), type, dex_pc);
451 current_block_->AddInstruction(new (arena_) HReturn(value, dex_pc));
452 }
453 current_block_ = nullptr;
454}
455
456static 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();
478 }
479}
480
481ArtMethod* HGraphBuilder::ResolveMethod(uint16_t method_idx, InvokeType invoke_type) {
482 ScopedObjectAccess soa(Thread::Current());
483 StackHandleScope<3> hs(soa.Self());
484
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
490 ArtMethod* resolved_method = class_linker->ResolveMethod<ClassLinker::kForceICCECheck>(
491 *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.
518 // 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.
521 if (invoke_type == kSuper) {
522 if (compiling_class.Get() == nullptr) {
523 // We could not determine the method's class we need to wait until runtime.
524 DCHECK(Runtime::Current()->IsAotCompiler());
525 return nullptr;
526 }
527 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());
538 return nullptr;
539 } 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;
567 }
568 }
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
579bool 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) {
586 InvokeType invoke_type = GetInvokeTypeFromOpCode(instruction.Opcode());
587 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;
592 if (invoke_type != kStatic) { // instance call
593 // One extra argument for 'this'.
594 number_of_arguments++;
595 }
596
597 MethodReference target_method(dex_file_, method_idx);
598
599 // 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) {
606 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
607 HInvokeStaticOrDirect::MethodLoadKind::kStringInit,
608 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
609 dchecked_integral_cast<uint64_t>(string_init_offset),
610 0U
611 };
612 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,
620 invoke_type,
621 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
631 ArtMethod* resolved_method = ResolveMethod(method_idx, invoke_type);
632
633 if (UNLIKELY(resolved_method == nullptr)) {
634 MaybeRecordStat(MethodCompilationStat::kUnresolvedMethod);
635 HInvoke* invoke = new (arena_) HInvokeUnresolved(arena_,
636 number_of_arguments,
637 return_type,
638 dex_pc,
639 method_idx,
640 invoke_type);
641 return HandleInvoke(invoke,
642 number_of_vreg_arguments,
643 args,
644 register_index,
645 is_range,
646 descriptor,
647 nullptr /* clinit_check */);
648 }
649
650 // Potential class initialization check, in the case of a static method call.
651 HClinitCheck* clinit_check = nullptr;
652 HInvoke* invoke = nullptr;
653 if (invoke_type == kDirect || invoke_type == kStatic || invoke_type == kSuper) {
654 // 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;
658 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 }
669 }
670
671 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
672 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
673 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
674 0u,
675 0U
676 };
677 invoke = new (arena_) HInvokeStaticOrDirect(arena_,
678 number_of_arguments,
679 return_type,
680 dex_pc,
681 method_idx,
682 target_method,
683 dispatch_info,
684 invoke_type,
685 invoke_type,
686 clinit_check_requirement);
687 } else if (invoke_type == kVirtual) {
688 ScopedObjectAccess soa(Thread::Current()); // Needed for the method index
689 invoke = new (arena_) HInvokeVirtual(arena_,
690 number_of_arguments,
691 return_type,
692 dex_pc,
693 method_idx,
694 resolved_method->GetMethodIndex());
695 } else {
696 DCHECK_EQ(invoke_type, kInterface);
697 ScopedObjectAccess soa(Thread::Current()); // Needed for the method index
698 invoke = new (arena_) HInvokeInterface(arena_,
699 number_of_arguments,
700 return_type,
701 dex_pc,
702 method_idx,
703 resolved_method->GetDexMethodIndex());
704 }
705
706 return HandleInvoke(invoke,
707 number_of_vreg_arguments,
708 args,
709 register_index,
710 is_range,
711 descriptor,
712 clinit_check);
713}
714
715bool 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,
744 outer_dex_file,
745 IsOutermostCompilingClass(type_index),
746 dex_pc,
747 /*needs_access_check*/ can_throw,
748 compiler_driver_->CanAssumeTypeIsPresentInDexCache(outer_dex_file, type_index));
749
750 current_block_->AddInstruction(load_class);
751 HInstruction* cls = load_class;
752 if (!IsInitialized(resolved_class)) {
753 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
769static 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 {
775 if (cls.Get() == nullptr) {
776 return false;
777 }
778
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())) {
783 return true;
784 }
785
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;
797}
798
799HClinitCheck* HGraphBuilder::ProcessClinitCheckForInvoke(
800 uint32_t dex_pc,
801 ArtMethod* resolved_method,
802 uint32_t method_idx,
803 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
804 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
805 Thread* self = Thread::Current();
806 StackHandleScope<4> hs(self);
807 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
808 dex_compilation_unit_->GetClassLinker()->FindDexCache(
809 self, *dex_compilation_unit_->GetDexFile())));
810 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
811 outer_compilation_unit_->GetClassLinker()->FindDexCache(
812 self, outer_dex_file)));
813 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
814 Handle<mirror::Class> resolved_method_class(hs.NewHandle(resolved_method->GetDeclaringClass()));
815
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
832 if (IsInitialized(resolved_method_class)) {
833 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
834 } else if (storage_index != DexFile::kDexNoIndex) {
835 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
836 HLoadClass* load_class = new (arena_) HLoadClass(
837 graph_->GetCurrentMethod(),
838 storage_index,
839 outer_dex_file,
840 is_outer_class,
841 dex_pc,
842 /*needs_access_check*/ false,
843 compiler_driver_->CanAssumeTypeIsPresentInDexCache(outer_dex_file, storage_index));
844 current_block_->AddInstruction(load_class);
845 clinit_check = new (arena_) HClinitCheck(load_class, dex_pc);
846 current_block_->AddInstruction(clinit_check);
847 }
848 return clinit_check;
849}
850
851bool 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) {
859 uint32_t descriptor_index = 1; // Skip the return type.
860 uint32_t dex_pc = invoke->GetDexPc();
861
862 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.
866 (i < number_of_vreg_arguments) && (*argument_index < invoke->GetNumberOfArguments());
867 i++, (*argument_index)++) {
868 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
869 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
870 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 }
882 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type, dex_pc);
883 invoke->SetArgumentAt(*argument_index, arg);
884 if (is_wide) {
885 i++;
886 }
887 }
888
889 if (*argument_index != invoke->GetNumberOfArguments()) {
890 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 }
896
897 if (invoke->IsInvokeStaticOrDirect() &&
898 HInvokeStaticOrDirect::NeedsCurrentMethodInput(
899 invoke->AsInvokeStaticOrDirect()->GetMethodLoadKind())) {
900 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.
919 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);
923 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;
937 }
938
939 if (clinit_check != nullptr) {
940 // Add the class initialization check as last input of `invoke`.
941 DCHECK(invoke->IsInvokeStaticOrDirect());
942 DCHECK(invoke->AsInvokeStaticOrDirect()->GetClinitCheckRequirement()
943 == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit);
944 invoke->SetArgumentAt(argument_index, clinit_check);
945 argument_index++;
946 }
947
948 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;
974 }
975
976 // Add move-result for StringFactory method.
977 uint32_t orig_this_reg = is_range ? register_index : args[0];
978 HInstruction* new_instance = LoadLocal(orig_this_reg, Primitive::kPrimNot, invoke->GetDexPc());
979 invoke->SetArgumentAt(argument_index, new_instance);
980 current_block_->AddInstruction(invoke);
981
982 latest_result_ = invoke;
983 return true;
984}
985
986static 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
992bool HGraphBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
993 uint32_t dex_pc,
994 bool is_put) {
995 uint32_t source_or_dest_reg = instruction.VRegA_22c();
996 uint32_t obj_reg = instruction.VRegB_22c();
997 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 }
1006
1007 ScopedObjectAccess soa(Thread::Current());
1008 ArtField* resolved_field =
1009 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa);
1010
1011
1012 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot, dex_pc);
1013 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();
1019 if (is_put) {
1020 HInstruction* value = LoadLocal(source_or_dest_reg, field_type, dex_pc);
1021 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 {
1030 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
1031 field_set = new (arena_) HInstanceFieldSet(null_check,
1032 value,
1033 field_type,
1034 resolved_field->GetOffset(),
1035 resolved_field->IsVolatile(),
1036 field_index,
1037 class_def_index,
1038 *dex_file_,
1039 dex_compilation_unit_->GetDexCache(),
1040 dex_pc);
1041 }
1042 current_block_->AddInstruction(field_set);
1043 } else {
1044 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 {
1052 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
1053 field_get = new (arena_) HInstanceFieldGet(null_check,
1054 field_type,
1055 resolved_field->GetOffset(),
1056 resolved_field->IsVolatile(),
1057 field_index,
1058 class_def_index,
1059 *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);
1065 }
1066
1067 return true;
1068}
1069
1070static mirror::Class* GetClassFrom(CompilerDriver* driver,
1071 const DexCompilationUnit& compilation_unit) {
1072 ScopedObjectAccess soa(Thread::Current());
1073 StackHandleScope<2> hs(soa.Self());
1074 const DexFile& dex_file = *compilation_unit.GetDexFile();
1075 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1076 soa.Decode<mirror::ClassLoader*>(compilation_unit.GetClassLoader())));
1077 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
1078 compilation_unit.GetClassLinker()->FindDexCache(soa.Self(), dex_file)));
1079
1080 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_);
1089}
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(
1095 dex_compilation_unit_->GetClassLinker()->FindDexCache(
1096 soa.Self(), *dex_compilation_unit_->GetDexFile())));
1097 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_)));
1101 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
1102
1103 // 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());
1110}
1111
1112void 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}
1129bool HGraphBuilder::BuildStaticFieldAccess(const Instruction& instruction,
1130 uint32_t dex_pc,
1131 bool is_put) {
1132 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1133 uint16_t field_index = instruction.VRegB_21c();
1134
1135 ScopedObjectAccess soa(Thread::Current());
1136 StackHandleScope<5> hs(soa.Self());
1137 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
1138 dex_compilation_unit_->GetClassLinker()->FindDexCache(
1139 soa.Self(), *dex_compilation_unit_->GetDexFile())));
1140 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1141 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
1142 ArtField* resolved_field = compiler_driver_->ResolveField(
1143 soa, dex_cache, class_loader, dex_compilation_unit_, field_index, true);
1144
1145 if (resolved_field == nullptr) {
1146 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
1147 Primitive::Type field_type = GetFieldAccessType(*dex_file_, field_index);
1148 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
1149 return true;
1150 }
1151
1152 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
1153 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
1154 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
1155 outer_compilation_unit_->GetClassLinker()->FindDexCache(soa.Self(), outer_dex_file)));
1156 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
1157
1158 // The index at which the field's class is stored in the DexCache's type array.
1159 uint32_t storage_index;
1160 bool is_outer_class = (outer_class.Get() == resolved_field->GetDeclaringClass());
1161 if (is_outer_class) {
1162 storage_index = outer_class->GetDexTypeIndex();
1163 } else if (outer_dex_cache.Get() != dex_cache.Get()) {
1164 // The compiler driver cannot currently understand multiple dex caches involved. Just bailout.
1165 return false;
1166 } else {
1167 // TODO: This is rather expensive. Perf it and cache the results if needed.
1168 std::pair<bool, bool> pair = compiler_driver_->IsFastStaticField(
1169 outer_dex_cache.Get(),
1170 GetCompilingClass(),
1171 resolved_field,
1172 field_index,
1173 &storage_index);
1174 bool can_easily_access = is_put ? pair.second : pair.first;
1175 if (!can_easily_access) {
1176 MaybeRecordStat(MethodCompilationStat::kUnresolvedFieldNotAFastAccess);
1177 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
1178 return true;
1179 }
1180 }
1181
1182 bool is_in_cache =
1183 compiler_driver_->CanAssumeTypeIsPresentInDexCache(outer_dex_file, storage_index);
1184 HLoadClass* constant = new (arena_) HLoadClass(graph_->GetCurrentMethod(),
1185 storage_index,
1186 outer_dex_file,
1187 is_outer_class,
1188 dex_pc,
1189 /*needs_access_check*/ false,
1190 is_in_cache);
1191 current_block_->AddInstruction(constant);
1192
1193 HInstruction* cls = constant;
1194
1195 Handle<mirror::Class> klass(hs.NewHandle(resolved_field->GetDeclaringClass()));
1196 if (!IsInitialized(klass)) {
1197 cls = new (arena_) HClinitCheck(constant, dex_pc);
1198 current_block_->AddInstruction(cls);
1199 }
1200
1201 uint16_t class_def_index = klass->GetDexClassDefIndex();
1202 if (is_put) {
1203 // We need to keep the class alive before loading the value.
1204 HInstruction* value = LoadLocal(source_or_dest_reg, field_type, dex_pc);
1205 DCHECK_EQ(value->GetType(), field_type);
1206 current_block_->AddInstruction(new (arena_) HStaticFieldSet(cls,
1207 value,
1208 field_type,
1209 resolved_field->GetOffset(),
1210 resolved_field->IsVolatile(),
1211 field_index,
1212 class_def_index,
1213 *dex_file_,
1214 dex_cache_,
1215 dex_pc));
1216 } else {
1217 current_block_->AddInstruction(new (arena_) HStaticFieldGet(cls,
1218 field_type,
1219 resolved_field->GetOffset(),
1220 resolved_field->IsVolatile(),
1221 field_index,
1222 class_def_index,
1223 *dex_file_,
1224 dex_cache_,
1225 dex_pc));
1226 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction(), dex_pc);
1227 }
1228 return true;
1229}
1230
1231void 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) {
1238 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1239
1240 HInstruction* first = LoadLocal(first_vreg, type, dex_pc);
1241 HInstruction* second = nullptr;
1242 if (second_is_constant) {
1243 if (type == Primitive::kPrimInt) {
1244 second = graph_->GetIntConstant(second_vreg_or_constant, dex_pc);
1245 } else {
1246 second = graph_->GetLongConstant(second_vreg_or_constant, dex_pc);
1247 }
1248 } else {
1249 second = LoadLocal(second_vreg_or_constant, type, dex_pc);
1250 }
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);
1256 current_block_->AddInstruction(second);
1257 }
1258
1259 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 }
1264 UpdateLocal(out_vreg, current_block_->GetLastInstruction(), dex_pc);
1265}
1266
1267void HGraphBuilder::BuildArrayAccess(const Instruction& instruction,
1268 uint32_t dex_pc,
1269 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
1275 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot, dex_pc);
1276 object = new (arena_) HNullCheck(object, dex_pc);
1277 current_block_->AddInstruction(object);
1278
1279 HInstruction* length = new (arena_) HArrayLength(object, dex_pc);
1280 current_block_->AddInstruction(length);
1281 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt, dex_pc);
1282 index = new (arena_) HBoundsCheck(index, length, dex_pc);
1283 current_block_->AddInstruction(index);
1284 if (is_put) {
1285 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type, dex_pc);
1286 // TODO: Insert a type check node if the type is Object.
1287 current_block_->AddInstruction(new (arena_) HArraySet(
1288 object, index, value, anticipated_type, dex_pc));
1289 } else {
1290 current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type, dex_pc));
1291 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction(), dex_pc);
1292 }
1293 graph_->SetHasBoundsChecks(true);
1294}
1295
1296void HGraphBuilder::BuildFilledNewArray(uint32_t dex_pc,
1297 uint32_t type_index,
1298 uint32_t number_of_vreg_arguments,
1299 bool is_range,
1300 uint32_t* args,
1301 uint32_t register_index) {
1302 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments, dex_pc);
1303 bool finalizable;
1304 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index, &finalizable)
1305 ? kQuickAllocArrayWithAccessCheck
1306 : kQuickAllocArray;
1307 HInstruction* object = new (arena_) HNewArray(length,
1308 graph_->GetCurrentMethod(),
1309 dex_pc,
1310 type_index,
1311 *dex_compilation_unit_->GetDexFile(),
1312 entrypoint);
1313 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
1324 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
1325 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type, dex_pc);
1326 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1327 current_block_->AddInstruction(
1328 new (arena_) HArraySet(object, index, value, type, dex_pc));
1329 }
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,
1338 uint32_t dex_pc) {
1339 for (uint32_t i = 0; i < element_count; ++i) {
1340 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1341 HInstruction* value = graph_->GetIntConstant(data[i], dex_pc);
1342 current_block_->AddInstruction(new (arena_) HArraySet(
1343 object, index, value, anticipated_type, dex_pc));
1344 }
1345}
1346
1347void HGraphBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
1348 HInstruction* array = LoadLocal(instruction.VRegA_31t(), Primitive::kPrimNot, dex_pc);
1349 HNullCheck* null_check = new (arena_) HNullCheck(array, dex_pc);
1350 current_block_->AddInstruction(null_check);
1351
1352 HInstruction* length = new (arena_) HArrayLength(null_check, dex_pc);
1353 current_block_->AddInstruction(length);
1354
1355 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
1356 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.
1363 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1, dex_pc);
1364 current_block_->AddInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
1365
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,
1372 dex_pc);
1373 break;
1374 case 2:
1375 BuildFillArrayData(null_check,
1376 reinterpret_cast<const int16_t*>(data),
1377 element_count,
1378 Primitive::kPrimShort,
1379 dex_pc);
1380 break;
1381 case 4:
1382 BuildFillArrayData(null_check,
1383 reinterpret_cast<const int32_t*>(data),
1384 element_count,
1385 Primitive::kPrimInt,
1386 dex_pc);
1387 break;
1388 case 8:
1389 BuildFillWideArrayData(null_check,
1390 reinterpret_cast<const int64_t*>(data),
1391 element_count,
1392 dex_pc);
1393 break;
1394 default:
1395 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1396 }
1397 graph_->SetHasBoundsChecks(true);
1398}
1399
1400void HGraphBuilder::BuildFillWideArrayData(HInstruction* object,
1401 const int64_t* data,
1402 uint32_t element_count,
1403 uint32_t dex_pc) {
1404 for (uint32_t i = 0; i < element_count; ++i) {
1405 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1406 HInstruction* value = graph_->GetLongConstant(data[i], dex_pc);
1407 current_block_->AddInstruction(new (arena_) HArraySet(
1408 object, index, value, Primitive::kPrimLong, dex_pc));
1409 }
1410}
1411
1412static TypeCheckKind ComputeTypeCheckKind(Handle<mirror::Class> cls)
1413 SHARED_REQUIRES(Locks::mutator_lock_) {
1414 if (cls.Get() == nullptr) {
1415 return TypeCheckKind::kUnresolvedCheck;
1416 } else if (cls->IsInterface()) {
1417 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
1435void HGraphBuilder::BuildTypeCheck(const Instruction& instruction,
1436 uint8_t destination,
1437 uint8_t reference,
1438 uint16_t type_index,
1439 uint32_t dex_pc) {
1440 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
1449 ScopedObjectAccess soa(Thread::Current());
1450 StackHandleScope<2> hs(soa.Self());
1451 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
1452 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
1453 dex_compilation_unit_->GetClassLinker()->FindDexCache(soa.Self(), dex_file)));
1454 Handle<mirror::Class> resolved_class(hs.NewHandle(dex_cache->GetResolvedType(type_index)));
1455
1456 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot, dex_pc);
1457 HLoadClass* cls = new (arena_) HLoadClass(
1458 graph_->GetCurrentMethod(),
1459 type_index,
1460 dex_file,
1461 IsOutermostCompilingClass(type_index),
1462 dex_pc,
1463 !can_access,
1464 compiler_driver_->CanAssumeTypeIsPresentInDexCache(dex_file, type_index));
1465 current_block_->AddInstruction(cls);
1466
1467 TypeCheckKind check_kind = ComputeTypeCheckKind(resolved_class);
1468 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
1469 current_block_->AddInstruction(new (arena_) HInstanceOf(object, cls, check_kind, dex_pc));
1470 UpdateLocal(destination, current_block_->GetLastInstruction(), dex_pc);
1471 } else {
1472 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
1473 // 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.
1476 current_block_->AddInstruction(new (arena_) HCheckCast(object, cls, check_kind, dex_pc));
1477 current_block_->AddInstruction(new (arena_) HBoundType(object, dex_pc));
1478 UpdateLocal(reference, current_block_->GetLastInstruction(), dex_pc);
1479 }
1480}
1481
1482bool HGraphBuilder::NeedsAccessCheck(uint32_t type_index, bool* finalizable) const {
1483 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
1484 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index, finalizable);
1485}
1486
1487bool 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
1498bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_pc) {
1499 switch (instruction.Opcode()) {
1500 case Instruction::CONST_4: {
1501 int32_t register_index = instruction.VRegA();
1502 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n(), dex_pc);
1503 UpdateLocal(register_index, constant, dex_pc);
1504 break;
1505 }
1506
1507 case Instruction::CONST_16: {
1508 int32_t register_index = instruction.VRegA();
1509 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s(), dex_pc);
1510 UpdateLocal(register_index, constant, dex_pc);
1511 break;
1512 }
1513
1514 case Instruction::CONST: {
1515 int32_t register_index = instruction.VRegA();
1516 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i(), dex_pc);
1517 UpdateLocal(register_index, constant, dex_pc);
1518 break;
1519 }
1520
1521 case Instruction::CONST_HIGH16: {
1522 int32_t register_index = instruction.VRegA();
1523 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16, dex_pc);
1524 UpdateLocal(register_index, constant, dex_pc);
1525 break;
1526 }
1527
1528 case Instruction::CONST_WIDE_16: {
1529 int32_t register_index = instruction.VRegA();
1530 // Get 16 bits of constant value, sign extended to 64 bits.
1531 int64_t value = instruction.VRegB_21s();
1532 value <<= 48;
1533 value >>= 48;
1534 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1535 UpdateLocal(register_index, constant, dex_pc);
1536 break;
1537 }
1538
1539 case Instruction::CONST_WIDE_32: {
1540 int32_t register_index = instruction.VRegA();
1541 // Get 32 bits of constant value, sign extended to 64 bits.
1542 int64_t value = instruction.VRegB_31i();
1543 value <<= 32;
1544 value >>= 32;
1545 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1546 UpdateLocal(register_index, constant, dex_pc);
1547 break;
1548 }
1549
1550 case Instruction::CONST_WIDE: {
1551 int32_t register_index = instruction.VRegA();
1552 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l(), dex_pc);
1553 UpdateLocal(register_index, constant, dex_pc);
1554 break;
1555 }
1556
1557 case Instruction::CONST_WIDE_HIGH16: {
1558 int32_t register_index = instruction.VRegA();
1559 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
1560 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1561 UpdateLocal(register_index, constant, dex_pc);
1562 break;
1563 }
1564
1565 // Note that the SSA building will refine the types.
1566 case Instruction::MOVE:
1567 case Instruction::MOVE_FROM16:
1568 case Instruction::MOVE_16: {
1569 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt, dex_pc);
1570 UpdateLocal(instruction.VRegA(), value, dex_pc);
1571 break;
1572 }
1573
1574 // Note that the SSA building will refine the types.
1575 case Instruction::MOVE_WIDE:
1576 case Instruction::MOVE_WIDE_FROM16:
1577 case Instruction::MOVE_WIDE_16: {
1578 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong, dex_pc);
1579 UpdateLocal(instruction.VRegA(), value, dex_pc);
1580 break;
1581 }
1582
1583 case Instruction::MOVE_OBJECT:
1584 case Instruction::MOVE_OBJECT_16:
1585 case Instruction::MOVE_OBJECT_FROM16: {
1586 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot, dex_pc);
1587 UpdateLocal(instruction.VRegA(), value, dex_pc);
1588 break;
1589 }
1590
1591 case Instruction::RETURN_VOID_NO_BARRIER:
1592 case Instruction::RETURN_VOID: {
1593 BuildReturn(instruction, Primitive::kPrimVoid, dex_pc);
1594 break;
1595 }
1596
1597#define IF_XX(comparison, cond) \
1598 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
1599 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
1600
1601 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);
1607
1608 case Instruction::GOTO:
1609 case Instruction::GOTO_16:
1610 case Instruction::GOTO_32: {
1611 current_block_->AddInstruction(new (arena_) HGoto(dex_pc));
1612 current_block_ = nullptr;
1613 break;
1614 }
1615
1616 case Instruction::RETURN: {
1617 BuildReturn(instruction, return_type_, dex_pc);
1618 break;
1619 }
1620
1621 case Instruction::RETURN_OBJECT: {
1622 BuildReturn(instruction, return_type_, dex_pc);
1623 break;
1624 }
1625
1626 case Instruction::RETURN_WIDE: {
1627 BuildReturn(instruction, return_type_, dex_pc);
1628 break;
1629 }
1630
1631 case Instruction::INVOKE_DIRECT:
1632 case Instruction::INVOKE_INTERFACE:
1633 case Instruction::INVOKE_STATIC:
1634 case Instruction::INVOKE_SUPER:
1635 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 }
1646 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
1647 uint32_t args[5];
1648 instruction.GetVarArgs(args);
1649 if (!BuildInvoke(instruction, dex_pc, method_idx,
1650 number_of_vreg_arguments, false, args, -1)) {
1651 return false;
1652 }
1653 break;
1654 }
1655
1656 case Instruction::INVOKE_DIRECT_RANGE:
1657 case Instruction::INVOKE_INTERFACE_RANGE:
1658 case Instruction::INVOKE_STATIC_RANGE:
1659 case Instruction::INVOKE_SUPER_RANGE:
1660 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 }
1671 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1672 uint32_t register_index = instruction.VRegC();
1673 if (!BuildInvoke(instruction, dex_pc, method_idx,
1674 number_of_vreg_arguments, true, nullptr, register_index)) {
1675 return false;
1676 }
1677 break;
1678 }
1679
1680 case Instruction::NEG_INT: {
1681 Unop_12x<HNeg>(instruction, Primitive::kPrimInt, dex_pc);
1682 break;
1683 }
1684
1685 case Instruction::NEG_LONG: {
1686 Unop_12x<HNeg>(instruction, Primitive::kPrimLong, dex_pc);
1687 break;
1688 }
1689
1690 case Instruction::NEG_FLOAT: {
1691 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat, dex_pc);
1692 break;
1693 }
1694
1695 case Instruction::NEG_DOUBLE: {
1696 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble, dex_pc);
1697 break;
1698 }
1699
1700 case Instruction::NOT_INT: {
1701 Unop_12x<HNot>(instruction, Primitive::kPrimInt, dex_pc);
1702 break;
1703 }
1704
1705 case Instruction::NOT_LONG: {
1706 Unop_12x<HNot>(instruction, Primitive::kPrimLong, dex_pc);
1707 break;
1708 }
1709
1710 case Instruction::INT_TO_LONG: {
1711 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong, dex_pc);
1712 break;
1713 }
1714
1715 case Instruction::INT_TO_FLOAT: {
1716 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat, dex_pc);
1717 break;
1718 }
1719
1720 case Instruction::INT_TO_DOUBLE: {
1721 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble, dex_pc);
1722 break;
1723 }
1724
1725 case Instruction::LONG_TO_INT: {
1726 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt, dex_pc);
1727 break;
1728 }
1729
1730 case Instruction::LONG_TO_FLOAT: {
1731 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimFloat, dex_pc);
1732 break;
1733 }
1734
1735 case Instruction::LONG_TO_DOUBLE: {
1736 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble, dex_pc);
1737 break;
1738 }
1739
1740 case Instruction::FLOAT_TO_INT: {
1741 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);
1747 break;
1748 }
1749
1750 case Instruction::FLOAT_TO_DOUBLE: {
1751 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimDouble, dex_pc);
1752 break;
1753 }
1754
1755 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
1765 case Instruction::DOUBLE_TO_FLOAT: {
1766 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimFloat, dex_pc);
1767 break;
1768 }
1769
1770 case Instruction::INT_TO_BYTE: {
1771 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte, dex_pc);
1772 break;
1773 }
1774
1775 case Instruction::INT_TO_SHORT: {
1776 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort, dex_pc);
1777 break;
1778 }
1779
1780 case Instruction::INT_TO_CHAR: {
1781 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar, dex_pc);
1782 break;
1783 }
1784
1785 case Instruction::ADD_INT: {
1786 Binop_23x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
1787 break;
1788 }
1789
1790 case Instruction::ADD_LONG: {
1791 Binop_23x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
1792 break;
1793 }
1794
1795 case Instruction::ADD_DOUBLE: {
1796 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
1797 break;
1798 }
1799
1800 case Instruction::ADD_FLOAT: {
1801 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
1802 break;
1803 }
1804
1805 case Instruction::SUB_INT: {
1806 Binop_23x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
1807 break;
1808 }
1809
1810 case Instruction::SUB_LONG: {
1811 Binop_23x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
1812 break;
1813 }
1814
1815 case Instruction::SUB_FLOAT: {
1816 Binop_23x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
1817 break;
1818 }
1819
1820 case Instruction::SUB_DOUBLE: {
1821 Binop_23x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
1822 break;
1823 }
1824
1825 case Instruction::ADD_INT_2ADDR: {
1826 Binop_12x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
1827 break;
1828 }
1829
1830 case Instruction::MUL_INT: {
1831 Binop_23x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
1832 break;
1833 }
1834
1835 case Instruction::MUL_LONG: {
1836 Binop_23x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
1837 break;
1838 }
1839
1840 case Instruction::MUL_FLOAT: {
1841 Binop_23x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
1842 break;
1843 }
1844
1845 case Instruction::MUL_DOUBLE: {
1846 Binop_23x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
1847 break;
1848 }
1849
1850 case Instruction::DIV_INT: {
1851 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1852 dex_pc, Primitive::kPrimInt, false, true);
1853 break;
1854 }
1855
1856 case Instruction::DIV_LONG: {
1857 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1858 dex_pc, Primitive::kPrimLong, false, true);
1859 break;
1860 }
1861
1862 case Instruction::DIV_FLOAT: {
1863 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
1864 break;
1865 }
1866
1867 case Instruction::DIV_DOUBLE: {
1868 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
1869 break;
1870 }
1871
1872 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
1884 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
1894 case Instruction::AND_INT: {
1895 Binop_23x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
1896 break;
1897 }
1898
1899 case Instruction::AND_LONG: {
1900 Binop_23x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
1901 break;
1902 }
1903
1904 case Instruction::SHL_INT: {
1905 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
1906 break;
1907 }
1908
1909 case Instruction::SHL_LONG: {
1910 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
1911 break;
1912 }
1913
1914 case Instruction::SHR_INT: {
1915 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
1916 break;
1917 }
1918
1919 case Instruction::SHR_LONG: {
1920 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
1921 break;
1922 }
1923
1924 case Instruction::USHR_INT: {
1925 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
1926 break;
1927 }
1928
1929 case Instruction::USHR_LONG: {
1930 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
1931 break;
1932 }
1933
1934 case Instruction::OR_INT: {
1935 Binop_23x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
1936 break;
1937 }
1938
1939 case Instruction::OR_LONG: {
1940 Binop_23x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
1941 break;
1942 }
1943
1944 case Instruction::XOR_INT: {
1945 Binop_23x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
1946 break;
1947 }
1948
1949 case Instruction::XOR_LONG: {
1950 Binop_23x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
1951 break;
1952 }
1953
1954 case Instruction::ADD_LONG_2ADDR: {
1955 Binop_12x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
1956 break;
1957 }
1958
1959 case Instruction::ADD_DOUBLE_2ADDR: {
1960 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
1961 break;
1962 }
1963
1964 case Instruction::ADD_FLOAT_2ADDR: {
1965 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
1966 break;
1967 }
1968
1969 case Instruction::SUB_INT_2ADDR: {
1970 Binop_12x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
1971 break;
1972 }
1973
1974 case Instruction::SUB_LONG_2ADDR: {
1975 Binop_12x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
1976 break;
1977 }
1978
1979 case Instruction::SUB_FLOAT_2ADDR: {
1980 Binop_12x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
1981 break;
1982 }
1983
1984 case Instruction::SUB_DOUBLE_2ADDR: {
1985 Binop_12x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
1986 break;
1987 }
1988
1989 case Instruction::MUL_INT_2ADDR: {
1990 Binop_12x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
1991 break;
1992 }
1993
1994 case Instruction::MUL_LONG_2ADDR: {
1995 Binop_12x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
1996 break;
1997 }
1998
1999 case Instruction::MUL_FLOAT_2ADDR: {
2000 Binop_12x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
2001 break;
2002 }
2003
2004 case Instruction::MUL_DOUBLE_2ADDR: {
2005 Binop_12x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
2006 break;
2007 }
2008
2009 case Instruction::DIV_INT_2ADDR: {
2010 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2011 dex_pc, Primitive::kPrimInt, false, true);
2012 break;
2013 }
2014
2015 case Instruction::DIV_LONG_2ADDR: {
2016 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);
2030 break;
2031 }
2032
2033 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
2043 case Instruction::SHL_INT_2ADDR: {
2044 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
2045 break;
2046 }
2047
2048 case Instruction::SHL_LONG_2ADDR: {
2049 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
2050 break;
2051 }
2052
2053 case Instruction::SHR_INT_2ADDR: {
2054 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
2055 break;
2056 }
2057
2058 case Instruction::SHR_LONG_2ADDR: {
2059 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
2060 break;
2061 }
2062
2063 case Instruction::USHR_INT_2ADDR: {
2064 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
2065 break;
2066 }
2067
2068 case Instruction::USHR_LONG_2ADDR: {
2069 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
2070 break;
2071 }
2072
2073 case Instruction::DIV_FLOAT_2ADDR: {
2074 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
2075 break;
2076 }
2077
2078 case Instruction::DIV_DOUBLE_2ADDR: {
2079 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
2080 break;
2081 }
2082
2083 case Instruction::AND_INT_2ADDR: {
2084 Binop_12x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
2085 break;
2086 }
2087
2088 case Instruction::AND_LONG_2ADDR: {
2089 Binop_12x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
2090 break;
2091 }
2092
2093 case Instruction::OR_INT_2ADDR: {
2094 Binop_12x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
2095 break;
2096 }
2097
2098 case Instruction::OR_LONG_2ADDR: {
2099 Binop_12x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
2100 break;
2101 }
2102
2103 case Instruction::XOR_INT_2ADDR: {
2104 Binop_12x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
2105 break;
2106 }
2107
2108 case Instruction::XOR_LONG_2ADDR: {
2109 Binop_12x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
2110 break;
2111 }
2112
2113 case Instruction::ADD_INT_LIT16: {
2114 Binop_22s<HAdd>(instruction, false, dex_pc);
2115 break;
2116 }
2117
2118 case Instruction::AND_INT_LIT16: {
2119 Binop_22s<HAnd>(instruction, false, dex_pc);
2120 break;
2121 }
2122
2123 case Instruction::OR_INT_LIT16: {
2124 Binop_22s<HOr>(instruction, false, dex_pc);
2125 break;
2126 }
2127
2128 case Instruction::XOR_INT_LIT16: {
2129 Binop_22s<HXor>(instruction, false, dex_pc);
2130 break;
2131 }
2132
2133 case Instruction::RSUB_INT: {
2134 Binop_22s<HSub>(instruction, true, dex_pc);
2135 break;
2136 }
2137
2138 case Instruction::MUL_INT_LIT16: {
2139 Binop_22s<HMul>(instruction, false, dex_pc);
2140 break;
2141 }
2142
2143 case Instruction::ADD_INT_LIT8: {
2144 Binop_22b<HAdd>(instruction, false, dex_pc);
2145 break;
2146 }
2147
2148 case Instruction::AND_INT_LIT8: {
2149 Binop_22b<HAnd>(instruction, false, dex_pc);
2150 break;
2151 }
2152
2153 case Instruction::OR_INT_LIT8: {
2154 Binop_22b<HOr>(instruction, false, dex_pc);
2155 break;
2156 }
2157
2158 case Instruction::XOR_INT_LIT8: {
2159 Binop_22b<HXor>(instruction, false, dex_pc);
2160 break;
2161 }
2162
2163 case Instruction::RSUB_INT_LIT8: {
2164 Binop_22b<HSub>(instruction, true, dex_pc);
2165 break;
2166 }
2167
2168 case Instruction::MUL_INT_LIT8: {
2169 Binop_22b<HMul>(instruction, false, dex_pc);
2170 break;
2171 }
2172
2173 case Instruction::DIV_INT_LIT16:
2174 case Instruction::DIV_INT_LIT8: {
2175 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);
2184 break;
2185 }
2186
2187 case Instruction::SHL_INT_LIT8: {
2188 Binop_22b<HShl>(instruction, false, dex_pc);
2189 break;
2190 }
2191
2192 case Instruction::SHR_INT_LIT8: {
2193 Binop_22b<HShr>(instruction, false, dex_pc);
2194 break;
2195 }
2196
2197 case Instruction::USHR_INT_LIT8: {
2198 Binop_22b<HUShr>(instruction, false, dex_pc);
2199 break;
2200 }
2201
2202 case Instruction::NEW_INSTANCE: {
2203 if (!BuildNewInstance(instruction.VRegB_21c(), dex_pc)) {
2204 return false;
2205 }
2206 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction(), dex_pc);
2207 break;
2208 }
2209
2210 case Instruction::NEW_ARRAY: {
2211 uint16_t type_index = instruction.VRegC_22c();
2212 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt, dex_pc);
2213 bool finalizable;
2214 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index, &finalizable)
2215 ? kQuickAllocArrayWithAccessCheck
2216 : kQuickAllocArray;
2217 current_block_->AddInstruction(new (arena_) HNewArray(length,
2218 graph_->GetCurrentMethod(),
2219 dex_pc,
2220 type_index,
2221 *dex_compilation_unit_->GetDexFile(),
2222 entrypoint));
2223 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction(), dex_pc);
2224 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);
2232 BuildFilledNewArray(dex_pc, type_index, number_of_vreg_arguments, false, args, 0);
2233 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(
2241 dex_pc, type_index, number_of_vreg_arguments, true, nullptr, register_index);
2242 break;
2243 }
2244
2245 case Instruction::FILL_ARRAY_DATA: {
2246 BuildFillArrayData(instruction, dex_pc);
2247 break;
2248 }
2249
2250 case Instruction::MOVE_RESULT:
2251 case Instruction::MOVE_RESULT_WIDE:
2252 case Instruction::MOVE_RESULT_OBJECT: {
2253 if (latest_result_ == nullptr) {
2254 // Only dead code can lead to this situation, where the verifier
2255 // does not reject the method.
2256 } else {
2257 // 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 =
2263 new (arena_) HStoreLocal(GetLocalAt(instruction.VRegA()), latest_result_, dex_pc);
2264 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 }
2275 latest_result_ = nullptr;
2276 }
2277 break;
2278 }
2279
2280 case Instruction::CMP_LONG: {
2281 Binop_23x_cmp(instruction, Primitive::kPrimLong, ComparisonBias::kNoBias, dex_pc);
2282 break;
2283 }
2284
2285 case Instruction::CMPG_FLOAT: {
2286 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kGtBias, dex_pc);
2287 break;
2288 }
2289
2290 case Instruction::CMPG_DOUBLE: {
2291 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kGtBias, dex_pc);
2292 break;
2293 }
2294
2295 case Instruction::CMPL_FLOAT: {
2296 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kLtBias, dex_pc);
2297 break;
2298 }
2299
2300 case Instruction::CMPL_DOUBLE: {
2301 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kLtBias, dex_pc);
2302 break;
2303 }
2304
2305 case Instruction::NOP:
2306 break;
2307
2308 case Instruction::IGET:
2309 case Instruction::IGET_QUICK:
2310 case Instruction::IGET_WIDE:
2311 case Instruction::IGET_WIDE_QUICK:
2312 case Instruction::IGET_OBJECT:
2313 case Instruction::IGET_OBJECT_QUICK:
2314 case Instruction::IGET_BOOLEAN:
2315 case Instruction::IGET_BOOLEAN_QUICK:
2316 case Instruction::IGET_BYTE:
2317 case Instruction::IGET_BYTE_QUICK:
2318 case Instruction::IGET_CHAR:
2319 case Instruction::IGET_CHAR_QUICK:
2320 case Instruction::IGET_SHORT:
2321 case Instruction::IGET_SHORT_QUICK: {
2322 if (!BuildInstanceFieldAccess(instruction, dex_pc, false)) {
2323 return false;
2324 }
2325 break;
2326 }
2327
2328 case Instruction::IPUT:
2329 case Instruction::IPUT_QUICK:
2330 case Instruction::IPUT_WIDE:
2331 case Instruction::IPUT_WIDE_QUICK:
2332 case Instruction::IPUT_OBJECT:
2333 case Instruction::IPUT_OBJECT_QUICK:
2334 case Instruction::IPUT_BOOLEAN:
2335 case Instruction::IPUT_BOOLEAN_QUICK:
2336 case Instruction::IPUT_BYTE:
2337 case Instruction::IPUT_BYTE_QUICK:
2338 case Instruction::IPUT_CHAR:
2339 case Instruction::IPUT_CHAR_QUICK:
2340 case Instruction::IPUT_SHORT:
2341 case Instruction::IPUT_SHORT_QUICK: {
2342 if (!BuildInstanceFieldAccess(instruction, dex_pc, true)) {
2343 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: {
2355 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
2356 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: {
2368 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
2369 return false;
2370 }
2371 break;
2372 }
2373
2374#define ARRAY_XX(kind, anticipated_type) \
2375 case Instruction::AGET##kind: { \
2376 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
2377 break; \
2378 } \
2379 case Instruction::APUT##kind: { \
2380 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
2381 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
2392 case Instruction::ARRAY_LENGTH: {
2393 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot, dex_pc);
2394 object = new (arena_) HNullCheck(object, dex_pc);
2395 current_block_->AddInstruction(object);
2396 current_block_->AddInstruction(new (arena_) HArrayLength(object, dex_pc));
2397 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction(), dex_pc);
2398 break;
2399 }
2400
2401 case Instruction::CONST_STRING: {
2402 uint32_t string_index = instruction.VRegB_21c();
2403 current_block_->AddInstruction(
2404 new (arena_) HLoadString(graph_->GetCurrentMethod(), string_index, *dex_file_, dex_pc));
2405 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction(), dex_pc);
2406 break;
2407 }
2408
2409 case Instruction::CONST_STRING_JUMBO: {
2410 uint32_t string_index = instruction.VRegB_31c();
2411 current_block_->AddInstruction(
2412 new (arena_) HLoadString(graph_->GetCurrentMethod(), string_index, *dex_file_, dex_pc));
2413 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction(), dex_pc);
2414 break;
2415 }
2416
2417 case Instruction::CONST_CLASS: {
2418 uint16_t type_index = instruction.VRegB_21c();
2419 bool type_known_final;
2420 bool type_known_abstract;
2421 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
2425 // work for inlining, so we use `IsOutermostCompilingClass` instead.
2426 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
2427 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
2428 &type_known_final, &type_known_abstract, &dont_use_is_referrers_class);
2429 current_block_->AddInstruction(new (arena_) HLoadClass(
2430 graph_->GetCurrentMethod(),
2431 type_index,
2432 *dex_file_,
2433 IsOutermostCompilingClass(type_index),
2434 dex_pc,
2435 !can_access,
2436 compiler_driver_->CanAssumeTypeIsPresentInDexCache(*dex_file_, type_index)));
2437 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction(), dex_pc);
2438 break;
2439 }
2440
2441 case Instruction::MOVE_EXCEPTION: {
2442 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));
2445 break;
2446 }
2447
2448 case Instruction::THROW: {
2449 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot, dex_pc);
2450 current_block_->AddInstruction(new (arena_) HThrow(exception, dex_pc));
2451 // 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
2457 case Instruction::INSTANCE_OF: {
2458 uint8_t destination = instruction.VRegA_22c();
2459 uint8_t reference = instruction.VRegB_22c();
2460 uint16_t type_index = instruction.VRegC_22c();
2461 BuildTypeCheck(instruction, destination, reference, type_index, dex_pc);
2462 break;
2463 }
2464
2465 case Instruction::CHECK_CAST: {
2466 uint8_t reference = instruction.VRegA_21c();
2467 uint16_t type_index = instruction.VRegB_21c();
2468 BuildTypeCheck(instruction, -1, reference, type_index, dex_pc);
2469 break;
2470 }
2471
2472 case Instruction::MONITOR_ENTER: {
2473 current_block_->AddInstruction(new (arena_) HMonitorOperation(
2474 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot, dex_pc),
2475 HMonitorOperation::OperationKind::kEnter,
2476 dex_pc));
2477 break;
2478 }
2479
2480 case Instruction::MONITOR_EXIT: {
2481 current_block_->AddInstruction(new (arena_) HMonitorOperation(
2482 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot, dex_pc),
2483 HMonitorOperation::OperationKind::kExit,
2484 dex_pc));
2485 break;
2486 }
2487
2488 case Instruction::SPARSE_SWITCH:
2489 case Instruction::PACKED_SWITCH: {
2490 BuildSwitch(instruction, dex_pc);
2491 break;
2492 }
2493
2494 default:
2495 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);
2500 return false;
2501 }
2502 return true;
2503} // NOLINT(readability/fn_size)
2504
2505HLocal* HGraphBuilder::GetLocalAt(uint32_t register_index) const {
2506 return locals_[register_index];
2507}
2508
2509void HGraphBuilder::UpdateLocal(uint32_t register_index,
2510 HInstruction* instruction,
2511 uint32_t dex_pc) const {
2512 HLocal* local = GetLocalAt(register_index);
2513 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction, dex_pc));
2514}
2515
2516HInstruction* HGraphBuilder::LoadLocal(uint32_t register_index,
2517 Primitive::Type type,
2518 uint32_t dex_pc) const {
2519 HLocal* local = GetLocalAt(register_index);
2520 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type, dex_pc));
2521 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002522}
2523
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002524} // namespace art