blob: 52a3a1534a93608a09e83c87959f7ca49872b1cf [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"
Andreas Gamped881df52014-11-24 23:28:39 -080020#include "base/logging.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010021#include "class_linker.h"
Jeff Hao848f70a2014-01-15 13:49:50 -080022#include "dex/verified_method.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000023#include "dex_file-inl.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000024#include "dex_instruction-inl.h"
Roland Levillain4c0eb422015-04-24 16:43:49 +010025#include "dex/verified_method.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010026#include "driver/compiler_driver-inl.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000027#include "driver/compiler_options.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010028#include "mirror/class_loader.h"
29#include "mirror/dex_cache.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000030#include "nodes.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000031#include "primitive.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010032#include "scoped_thread_state_change.h"
33#include "thread.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000034
35namespace art {
36
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010037/**
38 * Helper class to add HTemporary instructions. This class is used when
39 * converting a DEX instruction to multiple HInstruction, and where those
40 * instructions do not die at the following instruction, but instead spans
41 * multiple instructions.
42 */
43class Temporaries : public ValueObject {
44 public:
Calin Juravlef97f9fb2014-11-11 15:38:19 +000045 explicit Temporaries(HGraph* graph) : graph_(graph), index_(0) {}
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010046
47 void Add(HInstruction* instruction) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +000048 HInstruction* temp = new (graph_->GetArena()) HTemporary(index_);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010049 instruction->GetBlock()->AddInstruction(temp);
Calin Juravlef97f9fb2014-11-11 15:38:19 +000050
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010051 DCHECK(temp->GetPrevious() == instruction);
Calin Juravlef97f9fb2014-11-11 15:38:19 +000052
53 size_t offset;
54 if (instruction->GetType() == Primitive::kPrimLong
55 || instruction->GetType() == Primitive::kPrimDouble) {
56 offset = 2;
57 } else {
58 offset = 1;
59 }
60 index_ += offset;
61
62 graph_->UpdateTemporariesVRegSlots(index_);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010063 }
64
65 private:
66 HGraph* const graph_;
67
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010068 // Current index in the temporary stack, updated by `Add`.
69 size_t index_;
70};
71
Andreas Gamped881df52014-11-24 23:28:39 -080072class SwitchTable : public ValueObject {
73 public:
74 SwitchTable(const Instruction& instruction, uint32_t dex_pc, bool sparse)
75 : instruction_(instruction), dex_pc_(dex_pc), sparse_(sparse) {
76 int32_t table_offset = instruction.VRegB_31t();
77 const uint16_t* table = reinterpret_cast<const uint16_t*>(&instruction) + table_offset;
78 if (sparse) {
79 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
80 } else {
81 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
82 }
83 num_entries_ = table[1];
84 values_ = reinterpret_cast<const int32_t*>(&table[2]);
85 }
86
87 uint16_t GetNumEntries() const {
88 return num_entries_;
89 }
90
Andreas Gampee4d4d322014-12-04 09:09:57 -080091 void CheckIndex(size_t index) const {
92 if (sparse_) {
93 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
94 DCHECK_LT(index, 2 * static_cast<size_t>(num_entries_));
95 } else {
96 // In a packed table, we have the starting key and num_entries_ values.
97 DCHECK_LT(index, 1 + static_cast<size_t>(num_entries_));
98 }
99 }
100
Andreas Gamped881df52014-11-24 23:28:39 -0800101 int32_t GetEntryAt(size_t index) const {
Andreas Gampee4d4d322014-12-04 09:09:57 -0800102 CheckIndex(index);
Andreas Gamped881df52014-11-24 23:28:39 -0800103 return values_[index];
104 }
105
106 uint32_t GetDexPcForIndex(size_t index) const {
Andreas Gampee4d4d322014-12-04 09:09:57 -0800107 CheckIndex(index);
Andreas Gamped881df52014-11-24 23:28:39 -0800108 return dex_pc_ +
109 (reinterpret_cast<const int16_t*>(values_ + index) -
110 reinterpret_cast<const int16_t*>(&instruction_));
111 }
112
Andreas Gampee4d4d322014-12-04 09:09:57 -0800113 // Index of the first value in the table.
114 size_t GetFirstValueIndex() const {
115 if (sparse_) {
116 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
117 return num_entries_;
118 } else {
119 // In a packed table, we have the starting key and num_entries_ values.
120 return 1;
121 }
122 }
123
Andreas Gamped881df52014-11-24 23:28:39 -0800124 private:
125 const Instruction& instruction_;
126 const uint32_t dex_pc_;
127
128 // Whether this is a sparse-switch table (or a packed-switch one).
129 const bool sparse_;
130
131 // This can't be const as it needs to be computed off of the given instruction, and complicated
132 // expressions in the initializer list seemed very ugly.
133 uint16_t num_entries_;
134
135 const int32_t* values_;
136
137 DISALLOW_COPY_AND_ASSIGN(SwitchTable);
138};
139
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100140void HGraphBuilder::InitializeLocals(uint16_t count) {
141 graph_->SetNumberOfVRegs(count);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000142 locals_.SetSize(count);
143 for (int i = 0; i < count; i++) {
144 HLocal* local = new (arena_) HLocal(i);
145 entry_block_->AddInstruction(local);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000146 locals_.Put(i, local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000147 }
148}
149
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000150void HGraphBuilder::InitializeParameters(uint16_t number_of_parameters) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100151 // dex_compilation_unit_ is null only when unit testing.
152 if (dex_compilation_unit_ == nullptr) {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000153 return;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100154 }
155
156 graph_->SetNumberOfInVRegs(number_of_parameters);
157 const char* shorty = dex_compilation_unit_->GetShorty();
158 int locals_index = locals_.Size() - number_of_parameters;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100159 int parameter_index = 0;
160
161 if (!dex_compilation_unit_->IsStatic()) {
162 // Add the implicit 'this' argument, not expressed in the signature.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100163 HParameterValue* parameter =
Calin Juravle10e244f2015-01-26 18:54:32 +0000164 new (arena_) HParameterValue(parameter_index++, Primitive::kPrimNot, true);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100165 entry_block_->AddInstruction(parameter);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100166 HLocal* local = GetLocalAt(locals_index++);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100167 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100168 number_of_parameters--;
169 }
170
171 uint32_t pos = 1;
172 for (int i = 0; i < number_of_parameters; i++) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100173 HParameterValue* parameter =
174 new (arena_) HParameterValue(parameter_index++, Primitive::GetType(shorty[pos++]));
175 entry_block_->AddInstruction(parameter);
176 HLocal* local = GetLocalAt(locals_index++);
177 // Store the parameter value in the local that the dex code will use
178 // to reference that parameter.
179 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter));
180 bool is_wide = (parameter->GetType() == Primitive::kPrimLong)
181 || (parameter->GetType() == Primitive::kPrimDouble);
182 if (is_wide) {
183 i++;
184 locals_index++;
185 parameter_index++;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100186 }
187 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100188}
189
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100190template<typename T>
Calin Juravle225ff812014-11-13 16:46:39 +0000191void HGraphBuilder::If_22t(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000192 int32_t target_offset = instruction.GetTargetOffset();
David Brazdil852eaff2015-02-02 15:23:05 +0000193 HBasicBlock* branch_target = FindBlockStartingAt(dex_pc + target_offset);
194 HBasicBlock* fallthrough_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
195 DCHECK(branch_target != nullptr);
196 DCHECK(fallthrough_target != nullptr);
197 PotentiallyAddSuspendCheck(branch_target, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100198 HInstruction* first = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
199 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Dave Allison20dfc792014-06-16 20:44:29 -0700200 T* comparison = new (arena_) T(first, second);
201 current_block_->AddInstruction(comparison);
202 HInstruction* ifinst = new (arena_) HIf(comparison);
203 current_block_->AddInstruction(ifinst);
David Brazdil852eaff2015-02-02 15:23:05 +0000204 current_block_->AddSuccessor(branch_target);
205 current_block_->AddSuccessor(fallthrough_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700206 current_block_ = nullptr;
207}
208
209template<typename T>
Calin Juravle225ff812014-11-13 16:46:39 +0000210void HGraphBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000211 int32_t target_offset = instruction.GetTargetOffset();
David Brazdil852eaff2015-02-02 15:23:05 +0000212 HBasicBlock* branch_target = FindBlockStartingAt(dex_pc + target_offset);
213 HBasicBlock* fallthrough_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
214 DCHECK(branch_target != nullptr);
215 DCHECK(fallthrough_target != nullptr);
216 PotentiallyAddSuspendCheck(branch_target, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700217 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000218 T* comparison = new (arena_) T(value, graph_->GetIntConstant(0));
Dave Allison20dfc792014-06-16 20:44:29 -0700219 current_block_->AddInstruction(comparison);
220 HInstruction* ifinst = new (arena_) HIf(comparison);
221 current_block_->AddInstruction(ifinst);
David Brazdil852eaff2015-02-02 15:23:05 +0000222 current_block_->AddSuccessor(branch_target);
223 current_block_->AddSuccessor(fallthrough_target);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100224 current_block_ = nullptr;
225}
226
Calin Juravle48c2b032014-12-09 18:11:36 +0000227void HGraphBuilder::MaybeRecordStat(MethodCompilationStat compilation_stat) {
228 if (compilation_stats_ != nullptr) {
229 compilation_stats_->RecordStat(compilation_stat);
230 }
231}
232
David Brazdil1b498722015-03-31 11:37:18 +0100233bool HGraphBuilder::SkipCompilation(const DexFile::CodeItem& code_item,
Calin Juravle48c2b032014-12-09 18:11:36 +0000234 size_t number_of_branches) {
235 const CompilerOptions& compiler_options = compiler_driver_->GetCompilerOptions();
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000236 CompilerOptions::CompilerFilter compiler_filter = compiler_options.GetCompilerFilter();
237 if (compiler_filter == CompilerOptions::kEverything) {
238 return false;
239 }
240
David Brazdil1b498722015-03-31 11:37:18 +0100241 if (compiler_options.IsHugeMethod(code_item.insns_size_in_code_units_)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000242 VLOG(compiler) << "Skip compilation of huge method "
243 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
David Brazdil1b498722015-03-31 11:37:18 +0100244 << ": " << code_item.insns_size_in_code_units_ << " code units";
Calin Juravle48c2b032014-12-09 18:11:36 +0000245 MaybeRecordStat(MethodCompilationStat::kNotCompiledHugeMethod);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000246 return true;
247 }
248
249 // If it's large and contains no branches, it's likely to be machine generated initialization.
David Brazdil1b498722015-03-31 11:37:18 +0100250 if (compiler_options.IsLargeMethod(code_item.insns_size_in_code_units_)
251 && (number_of_branches == 0)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000252 VLOG(compiler) << "Skip compilation of large method with no branch "
253 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
David Brazdil1b498722015-03-31 11:37:18 +0100254 << ": " << code_item.insns_size_in_code_units_ << " code units";
Calin Juravle48c2b032014-12-09 18:11:36 +0000255 MaybeRecordStat(MethodCompilationStat::kNotCompiledLargeMethodNoBranches);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000256 return true;
257 }
258
259 return false;
260}
261
David Brazdilbff75032015-07-08 17:26:51 +0000262static const DexFile::TryItem* GetTryItem(HBasicBlock* block,
263 const DexFile::CodeItem& code_item,
264 const ArenaBitVector& can_block_throw) {
265 DCHECK(!block->IsSingleTryBoundary());
266
267 // Block does not contain throwing instructions. Even if it is covered by
268 // a TryItem, we will consider it not in a try block.
269 if (!can_block_throw.IsBitSet(block->GetBlockId())) {
270 return nullptr;
271 }
272
273 // Instructions in the block may throw. Find a TryItem covering this block.
274 int32_t try_item_idx = DexFile::FindTryItem(code_item, block->GetDexPc());
David Brazdil6cd788f2015-07-08 16:44:00 +0100275 return (try_item_idx == -1) ? nullptr : DexFile::GetTryItems(code_item, try_item_idx);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000276}
277
278void HGraphBuilder::CreateBlocksForTryCatch(const DexFile::CodeItem& code_item) {
279 if (code_item.tries_size_ == 0) {
280 return;
281 }
282
283 // Create branch targets at the start/end of the TryItem range. These are
284 // places where the program might fall through into/out of the a block and
285 // where TryBoundary instructions will be inserted later. Other edges which
286 // enter/exit the try blocks are a result of branches/switches.
287 for (size_t idx = 0; idx < code_item.tries_size_; ++idx) {
288 const DexFile::TryItem* try_item = DexFile::GetTryItems(code_item, idx);
289 uint32_t dex_pc_start = try_item->start_addr_;
290 uint32_t dex_pc_end = dex_pc_start + try_item->insn_count_;
291 FindOrCreateBlockStartingAt(dex_pc_start);
292 if (dex_pc_end < code_item.insns_size_in_code_units_) {
293 // TODO: Do not create block if the last instruction cannot fall through.
294 FindOrCreateBlockStartingAt(dex_pc_end);
295 } else {
296 // The TryItem spans until the very end of the CodeItem (or beyond if
297 // invalid) and therefore cannot have any code afterwards.
298 }
299 }
300
301 // Create branch targets for exception handlers.
302 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(code_item, 0);
303 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
304 for (uint32_t idx = 0; idx < handlers_size; ++idx) {
305 CatchHandlerIterator iterator(handlers_ptr);
306 for (; iterator.HasNext(); iterator.Next()) {
307 uint32_t address = iterator.GetHandlerAddress();
308 HBasicBlock* block = FindOrCreateBlockStartingAt(address);
309 block->SetIsCatchBlock();
310 }
311 handlers_ptr = iterator.EndDataPointer();
312 }
313}
314
David Brazdil56e1acc2015-06-30 15:41:36 +0100315void HGraphBuilder::SplitTryBoundaryEdge(HBasicBlock* predecessor,
316 HBasicBlock* successor,
317 HTryBoundary::BoundaryKind kind,
318 const DexFile::CodeItem& code_item,
319 const DexFile::TryItem& try_item) {
320 // Split the edge with a single TryBoundary instruction.
321 HTryBoundary* try_boundary = new (arena_) HTryBoundary(kind);
322 HBasicBlock* try_entry_block = graph_->SplitEdge(predecessor, successor);
323 try_entry_block->AddInstruction(try_boundary);
324
325 // Link the TryBoundary to the handlers of `try_item`.
326 for (CatchHandlerIterator it(code_item, try_item); it.HasNext(); it.Next()) {
327 try_boundary->AddExceptionHandler(FindBlockStartingAt(it.GetHandlerAddress()));
328 }
329}
330
David Brazdilfc6a86a2015-06-26 10:33:45 +0000331void HGraphBuilder::InsertTryBoundaryBlocks(const DexFile::CodeItem& code_item) {
332 if (code_item.tries_size_ == 0) {
333 return;
334 }
335
David Brazdil72783ff2015-07-09 14:36:05 +0100336 // Bit vector stores information on which blocks contain throwing instructions.
337 // Must be expandable because catch blocks may be split into two.
338 ArenaBitVector can_block_throw(arena_, graph_->GetBlocks().Size(), /* expandable */ true);
David Brazdilbff75032015-07-08 17:26:51 +0000339
340 // Scan blocks and mark those which contain throwing instructions.
David Brazdil72783ff2015-07-09 14:36:05 +0100341 for (size_t block_id = 0, e = graph_->GetBlocks().Size(); block_id < e; ++block_id) {
David Brazdilbff75032015-07-08 17:26:51 +0000342 HBasicBlock* block = graph_->GetBlocks().Get(block_id);
David Brazdil72783ff2015-07-09 14:36:05 +0100343 bool can_throw = false;
David Brazdilbff75032015-07-08 17:26:51 +0000344 for (HInstructionIterator insn(block->GetInstructions()); !insn.Done(); insn.Advance()) {
345 if (insn.Current()->CanThrow()) {
David Brazdil72783ff2015-07-09 14:36:05 +0100346 can_throw = true;
David Brazdilbff75032015-07-08 17:26:51 +0000347 break;
348 }
349 }
David Brazdil72783ff2015-07-09 14:36:05 +0100350
351 if (can_throw) {
352 if (block->IsCatchBlock()) {
353 // Catch blocks are always considered an entry point into the TryItem in
354 // order to avoid splitting exceptional edges. We split the block after
355 // the move-exception (if present) and mark the first part non-throwing.
356 // Later on, a TryBoundary will be inserted between the two blocks.
357 HInstruction* first_insn = block->GetFirstInstruction();
358 if (first_insn->IsLoadException()) {
359 // Catch block starts with a LoadException. Split the block after the
360 // StoreLocal that must come after the load.
361 DCHECK(first_insn->GetNext()->IsStoreLocal());
362 block = block->SplitBefore(first_insn->GetNext()->GetNext());
363 } else {
364 // Catch block does not load the exception. Split at the beginning to
365 // create an empty catch block.
366 block = block->SplitBefore(first_insn);
367 }
368 }
369 can_block_throw.SetBit(block->GetBlockId());
370 }
David Brazdilbff75032015-07-08 17:26:51 +0000371 }
372
David Brazdil281a6322015-07-03 10:34:57 +0100373 // Iterate over all blocks, find those covered by some TryItem and:
374 // (a) split edges which enter/exit the try range,
375 // (b) create TryBoundary instructions in the new blocks,
376 // (c) link the new blocks to corresponding exception handlers.
377 // We cannot iterate only over blocks in `branch_targets_` because switch-case
378 // blocks share the same dex_pc.
David Brazdil72783ff2015-07-09 14:36:05 +0100379 for (size_t block_id = 0, e = graph_->GetBlocks().Size(); block_id < e; ++block_id) {
David Brazdil281a6322015-07-03 10:34:57 +0100380 HBasicBlock* try_block = graph_->GetBlocks().Get(block_id);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000381
David Brazdil281a6322015-07-03 10:34:57 +0100382 // TryBoundary blocks are added at the end of the list and not iterated over.
383 DCHECK(!try_block->IsSingleTryBoundary());
David Brazdilfc6a86a2015-06-26 10:33:45 +0000384
David Brazdil281a6322015-07-03 10:34:57 +0100385 // Find the TryItem for this block.
David Brazdilbff75032015-07-08 17:26:51 +0000386 const DexFile::TryItem* try_item = GetTryItem(try_block, code_item, can_block_throw);
387 if (try_item == nullptr) {
David Brazdil281a6322015-07-03 10:34:57 +0100388 continue;
389 }
David Brazdil281a6322015-07-03 10:34:57 +0100390
David Brazdil72783ff2015-07-09 14:36:05 +0100391 // Catch blocks were split earlier and cannot throw.
392 DCHECK(!try_block->IsCatchBlock());
393
394 // Find predecessors which are not covered by the same TryItem range. Such
395 // edges enter the try block and will have a TryBoundary inserted.
396 for (size_t i = 0; i < try_block->GetPredecessors().Size(); ++i) {
397 HBasicBlock* predecessor = try_block->GetPredecessors().Get(i);
398 if (predecessor->IsSingleTryBoundary()) {
399 // The edge was already split because of an exit from a neighbouring
400 // TryItem. We split it again and insert an entry point.
401 if (kIsDebugBuild) {
402 HTryBoundary* last_insn = predecessor->GetLastInstruction()->AsTryBoundary();
403 const DexFile::TryItem* predecessor_try_item =
404 GetTryItem(predecessor->GetSinglePredecessor(), code_item, can_block_throw);
405 DCHECK(!last_insn->IsEntry());
406 DCHECK_EQ(last_insn->GetNormalFlowSuccessor(), try_block);
407 DCHECK(try_block->IsFirstIndexOfPredecessor(predecessor, i));
408 DCHECK_NE(try_item, predecessor_try_item);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000409 }
David Brazdil72783ff2015-07-09 14:36:05 +0100410 } else if (GetTryItem(predecessor, code_item, can_block_throw) != try_item) {
411 // This is an entry point into the TryItem and the edge has not been
412 // split yet. That means that `predecessor` is not in a TryItem, or
413 // it is in a different TryItem and we happened to iterate over this
414 // block first. We split the edge and insert an entry point.
415 } else {
416 // Not an edge on the boundary of the try block.
417 continue;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000418 }
David Brazdil72783ff2015-07-09 14:36:05 +0100419 SplitTryBoundaryEdge(predecessor, try_block, HTryBoundary::kEntry, code_item, *try_item);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000420 }
David Brazdil281a6322015-07-03 10:34:57 +0100421
422 // Find successors which are not covered by the same TryItem range. Such
423 // edges exit the try block and will have a TryBoundary inserted.
424 for (size_t i = 0; i < try_block->GetSuccessors().Size(); ++i) {
425 HBasicBlock* successor = try_block->GetSuccessors().Get(i);
426 if (successor->IsCatchBlock()) {
427 // A catch block is always considered an entry point into its TryItem.
428 // We therefore assume this is an exit point, regardless of whether
429 // the catch block is in a different TryItem or not.
430 } else if (successor->IsSingleTryBoundary()) {
431 // The edge was already split because of an entry into a neighbouring
432 // TryItem. We split it again and insert an exit.
433 if (kIsDebugBuild) {
434 HTryBoundary* last_insn = successor->GetLastInstruction()->AsTryBoundary();
David Brazdilbff75032015-07-08 17:26:51 +0000435 const DexFile::TryItem* successor_try_item =
436 GetTryItem(last_insn->GetNormalFlowSuccessor(), code_item, can_block_throw);
David Brazdil281a6322015-07-03 10:34:57 +0100437 DCHECK_EQ(try_block, successor->GetSinglePredecessor());
438 DCHECK(last_insn->IsEntry());
David Brazdilbff75032015-07-08 17:26:51 +0000439 DCHECK_NE(try_item, successor_try_item);
David Brazdil281a6322015-07-03 10:34:57 +0100440 }
David Brazdilbff75032015-07-08 17:26:51 +0000441 } else if (GetTryItem(successor, code_item, can_block_throw) != try_item) {
David Brazdil281a6322015-07-03 10:34:57 +0100442 // This is an exit out of the TryItem and the edge has not been split
443 // yet. That means that either `successor` is not in a TryItem, or it
444 // is in a different TryItem and we happened to iterate over this
445 // block first. We split the edge and insert an exit.
446 HInstruction* last_instruction = try_block->GetLastInstruction();
447 if (last_instruction->IsReturn() || last_instruction->IsReturnVoid()) {
448 DCHECK_EQ(successor, exit_block_);
449 // Control flow exits the try block with a Return(Void). Because
450 // splitting the edge would invalidate the invariant that Return
451 // always jumps to Exit, we move the Return outside the try block.
452 successor = try_block->SplitBefore(last_instruction);
453 }
454 } else {
455 // Not an edge on the boundary of the try block.
456 continue;
457 }
David Brazdilbff75032015-07-08 17:26:51 +0000458 SplitTryBoundaryEdge(try_block, successor, HTryBoundary::kExit, code_item, *try_item);
David Brazdil281a6322015-07-03 10:34:57 +0100459 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000460 }
461}
462
David Brazdil5e8b1372015-01-23 14:39:08 +0000463bool HGraphBuilder::BuildGraph(const DexFile::CodeItem& code_item) {
464 DCHECK(graph_->GetBlocks().IsEmpty());
465
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000466 const uint16_t* code_ptr = code_item.insns_;
467 const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100468 code_start_ = code_ptr;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000469
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000470 // Setup the graph with the entry block and exit block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100471 entry_block_ = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000472 graph_->AddBlock(entry_block_);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100473 exit_block_ = new (arena_) HBasicBlock(graph_, kNoDexPc);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000474 graph_->SetEntryBlock(entry_block_);
475 graph_->SetExitBlock(exit_block_);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000476
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000477 InitializeLocals(code_item.registers_size_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000478 graph_->SetMaximumNumberOfOutVRegs(code_item.outs_size_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000479
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000480 // Compute the number of dex instructions, blocks, and branches. We will
481 // check these values against limits given to the compiler.
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000482 size_t number_of_branches = 0;
483
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000484 // To avoid splitting blocks, we compute ahead of time the instructions that
485 // start a new block, and create these blocks.
Calin Juravle702d2602015-04-30 19:28:21 +0100486 if (!ComputeBranchTargets(code_ptr, code_end, &number_of_branches)) {
487 MaybeRecordStat(MethodCompilationStat::kNotCompiledBranchOutsideMethodCode);
488 return false;
489 }
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000490
491 // Note that the compiler driver is null when unit testing.
David Brazdil1b498722015-03-31 11:37:18 +0100492 if ((compiler_driver_ != nullptr) && SkipCompilation(code_item, number_of_branches)) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000493 return false;
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000494 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000495
David Brazdilfc6a86a2015-06-26 10:33:45 +0000496 CreateBlocksForTryCatch(code_item);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000497
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000498 InitializeParameters(code_item.ins_size_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100499
Calin Juravle225ff812014-11-13 16:46:39 +0000500 size_t dex_pc = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000501 while (code_ptr < code_end) {
Calin Juravle225ff812014-11-13 16:46:39 +0000502 // Update the current block if dex_pc starts a new block.
503 MaybeUpdateCurrentBlock(dex_pc);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000504 const Instruction& instruction = *Instruction::At(code_ptr);
Calin Juravle48c2b032014-12-09 18:11:36 +0000505 if (!AnalyzeDexInstruction(instruction, dex_pc)) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000506 return false;
Calin Juravle48c2b032014-12-09 18:11:36 +0000507 }
Calin Juravle225ff812014-11-13 16:46:39 +0000508 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000509 code_ptr += instruction.SizeInCodeUnits();
510 }
511
David Brazdilfc6a86a2015-06-26 10:33:45 +0000512 // Add Exit to the exit block.
David Brazdil3e187382015-06-26 09:59:52 +0000513 exit_block_->AddInstruction(new (arena_) HExit());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000514 // Add the suspend check to the entry block.
515 entry_block_->AddInstruction(new (arena_) HSuspendCheck(0));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000516 entry_block_->AddInstruction(new (arena_) HGoto());
David Brazdilbff75032015-07-08 17:26:51 +0000517 // Add the exit block at the end.
518 graph_->AddBlock(exit_block_);
David Brazdil5e8b1372015-01-23 14:39:08 +0000519
David Brazdilfc6a86a2015-06-26 10:33:45 +0000520 // Iterate over blocks covered by TryItems and insert TryBoundaries at entry
521 // and exit points. This requires all control-flow instructions and
522 // non-exceptional edges to have been created.
523 InsertTryBoundaryBlocks(code_item);
524
David Brazdil5e8b1372015-01-23 14:39:08 +0000525 return true;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000526}
527
David Brazdilfc6a86a2015-06-26 10:33:45 +0000528void HGraphBuilder::MaybeUpdateCurrentBlock(size_t dex_pc) {
529 HBasicBlock* block = FindBlockStartingAt(dex_pc);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000530 if (block == nullptr) {
531 return;
532 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000533
534 if (current_block_ != nullptr) {
535 // Branching instructions clear current_block, so we know
536 // the last instruction of the current block is not a branching
537 // instruction. We add an unconditional goto to the found block.
538 current_block_->AddInstruction(new (arena_) HGoto());
539 current_block_->AddSuccessor(block);
540 }
541 graph_->AddBlock(block);
542 current_block_ = block;
543}
544
Calin Juravle702d2602015-04-30 19:28:21 +0100545bool HGraphBuilder::ComputeBranchTargets(const uint16_t* code_ptr,
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000546 const uint16_t* code_end,
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000547 size_t* number_of_branches) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000548 branch_targets_.SetSize(code_end - code_ptr);
549
550 // Create the first block for the dex instructions, single successor of the entry block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100551 HBasicBlock* block = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000552 branch_targets_.Put(0, block);
553 entry_block_->AddSuccessor(block);
554
555 // Iterate over all instructions and find branching instructions. Create blocks for
556 // the locations these instructions branch to.
Andreas Gamped881df52014-11-24 23:28:39 -0800557 uint32_t dex_pc = 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000558 while (code_ptr < code_end) {
559 const Instruction& instruction = *Instruction::At(code_ptr);
560 if (instruction.IsBranch()) {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000561 (*number_of_branches)++;
Calin Juravle225ff812014-11-13 16:46:39 +0000562 int32_t target = instruction.GetTargetOffset() + dex_pc;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000563 // Create a block for the target instruction.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000564 FindOrCreateBlockStartingAt(target);
565
Calin Juravle225ff812014-11-13 16:46:39 +0000566 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000567 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle702d2602015-04-30 19:28:21 +0100568
David Brazdilfe659462015-06-24 14:23:56 +0100569 if (instruction.CanFlowThrough()) {
570 if (code_ptr >= code_end) {
Calin Juravle702d2602015-04-30 19:28:21 +0100571 // In the normal case we should never hit this but someone can artificially forge a dex
572 // file to fall-through out the method code. In this case we bail out compilation.
573 return false;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000574 } else {
575 FindOrCreateBlockStartingAt(dex_pc);
Calin Juravle702d2602015-04-30 19:28:21 +0100576 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000577 }
Andreas Gampee4d4d322014-12-04 09:09:57 -0800578 } else if (instruction.IsSwitch()) {
579 SwitchTable table(instruction, dex_pc, instruction.Opcode() == Instruction::SPARSE_SWITCH);
Andreas Gamped881df52014-11-24 23:28:39 -0800580
581 uint16_t num_entries = table.GetNumEntries();
582
Andreas Gampee4d4d322014-12-04 09:09:57 -0800583 // In a packed-switch, the entry at index 0 is the starting key. In a sparse-switch, the
584 // entry at index 0 is the first key, and values are after *all* keys.
585 size_t offset = table.GetFirstValueIndex();
586
587 // Use a larger loop counter type to avoid overflow issues.
588 for (size_t i = 0; i < num_entries; ++i) {
Andreas Gamped881df52014-11-24 23:28:39 -0800589 // The target of the case.
Andreas Gampee4d4d322014-12-04 09:09:57 -0800590 uint32_t target = dex_pc + table.GetEntryAt(i + offset);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000591 FindOrCreateBlockStartingAt(target);
Andreas Gamped881df52014-11-24 23:28:39 -0800592
David Brazdil281a6322015-07-03 10:34:57 +0100593 // Create a block for the switch-case logic. The block gets the dex_pc
594 // of the SWITCH instruction because it is part of its semantics.
595 block = new (arena_) HBasicBlock(graph_, dex_pc);
596 branch_targets_.Put(table.GetDexPcForIndex(i), block);
Andreas Gamped881df52014-11-24 23:28:39 -0800597 }
598
599 // Fall-through. Add a block if there is more code afterwards.
600 dex_pc += instruction.SizeInCodeUnits();
601 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle702d2602015-04-30 19:28:21 +0100602 if (code_ptr >= code_end) {
603 // In the normal case we should never hit this but someone can artificially forge a dex
604 // file to fall-through out the method code. In this case we bail out compilation.
605 // (A switch can fall-through so we don't need to check CanFlowThrough().)
606 return false;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000607 } else {
608 FindOrCreateBlockStartingAt(dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -0800609 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000610 } else {
611 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle225ff812014-11-13 16:46:39 +0000612 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000613 }
614 }
Calin Juravle702d2602015-04-30 19:28:21 +0100615 return true;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000616}
617
David Brazdilfc6a86a2015-06-26 10:33:45 +0000618HBasicBlock* HGraphBuilder::FindBlockStartingAt(int32_t dex_pc) const {
619 DCHECK_GE(dex_pc, 0);
620 DCHECK_LT(static_cast<size_t>(dex_pc), branch_targets_.Size());
621 return branch_targets_.Get(dex_pc);
622}
623
624HBasicBlock* HGraphBuilder::FindOrCreateBlockStartingAt(int32_t dex_pc) {
625 HBasicBlock* block = FindBlockStartingAt(dex_pc);
626 if (block == nullptr) {
627 block = new (arena_) HBasicBlock(graph_, dex_pc);
628 branch_targets_.Put(dex_pc, block);
629 }
630 return block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000631}
632
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100633template<typename T>
Roland Levillain88cb1752014-10-20 16:36:47 +0100634void HGraphBuilder::Unop_12x(const Instruction& instruction, Primitive::Type type) {
635 HInstruction* first = LoadLocal(instruction.VRegB(), type);
636 current_block_->AddInstruction(new (arena_) T(type, first));
637 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
638}
639
Roland Levillaindff1f282014-11-05 14:15:05 +0000640void HGraphBuilder::Conversion_12x(const Instruction& instruction,
641 Primitive::Type input_type,
Roland Levillain624279f2014-12-04 11:54:28 +0000642 Primitive::Type result_type,
643 uint32_t dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +0000644 HInstruction* first = LoadLocal(instruction.VRegB(), input_type);
Roland Levillain624279f2014-12-04 11:54:28 +0000645 current_block_->AddInstruction(new (arena_) HTypeConversion(result_type, first, dex_pc));
Roland Levillaindff1f282014-11-05 14:15:05 +0000646 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
647}
648
Roland Levillain88cb1752014-10-20 16:36:47 +0100649template<typename T>
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100650void HGraphBuilder::Binop_23x(const Instruction& instruction, Primitive::Type type) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100651 HInstruction* first = LoadLocal(instruction.VRegB(), type);
652 HInstruction* second = LoadLocal(instruction.VRegC(), type);
653 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100654 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
655}
656
657template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000658void HGraphBuilder::Binop_23x(const Instruction& instruction,
659 Primitive::Type type,
660 uint32_t dex_pc) {
661 HInstruction* first = LoadLocal(instruction.VRegB(), type);
662 HInstruction* second = LoadLocal(instruction.VRegC(), type);
663 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
664 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
665}
666
667template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000668void HGraphBuilder::Binop_23x_shift(const Instruction& instruction,
669 Primitive::Type type) {
670 HInstruction* first = LoadLocal(instruction.VRegB(), type);
671 HInstruction* second = LoadLocal(instruction.VRegC(), Primitive::kPrimInt);
672 current_block_->AddInstruction(new (arena_) T(type, first, second));
673 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
674}
675
Calin Juravleddb7df22014-11-25 20:56:51 +0000676void HGraphBuilder::Binop_23x_cmp(const Instruction& instruction,
677 Primitive::Type type,
Mark Mendellc4701932015-04-10 13:18:51 -0400678 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700679 uint32_t dex_pc) {
Calin Juravleddb7df22014-11-25 20:56:51 +0000680 HInstruction* first = LoadLocal(instruction.VRegB(), type);
681 HInstruction* second = LoadLocal(instruction.VRegC(), type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700682 current_block_->AddInstruction(new (arena_) HCompare(type, first, second, bias, dex_pc));
Calin Juravleddb7df22014-11-25 20:56:51 +0000683 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
684}
685
Calin Juravle9aec02f2014-11-18 23:06:35 +0000686template<typename T>
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100687void HGraphBuilder::Binop_12x(const Instruction& instruction, Primitive::Type type) {
688 HInstruction* first = LoadLocal(instruction.VRegA(), type);
689 HInstruction* second = LoadLocal(instruction.VRegB(), type);
690 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100691 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
692}
693
694template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000695void HGraphBuilder::Binop_12x_shift(const Instruction& instruction, Primitive::Type type) {
696 HInstruction* first = LoadLocal(instruction.VRegA(), type);
697 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
698 current_block_->AddInstruction(new (arena_) T(type, first, second));
699 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
700}
701
702template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000703void HGraphBuilder::Binop_12x(const Instruction& instruction,
704 Primitive::Type type,
705 uint32_t dex_pc) {
706 HInstruction* first = LoadLocal(instruction.VRegA(), type);
707 HInstruction* second = LoadLocal(instruction.VRegB(), type);
708 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
709 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
710}
711
712template<typename T>
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100713void HGraphBuilder::Binop_22s(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100714 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000715 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100716 if (reverse) {
717 std::swap(first, second);
718 }
719 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
720 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
721}
722
723template<typename T>
724void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100725 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000726 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100727 if (reverse) {
728 std::swap(first, second);
729 }
730 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
731 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
732}
733
Calin Juravle0c25d102015-04-20 14:49:09 +0100734static bool RequiresConstructorBarrier(const DexCompilationUnit* cu, const CompilerDriver& driver) {
Calin Juravle27df7582015-04-17 19:12:31 +0100735 Thread* self = Thread::Current();
Calin Juravle0c25d102015-04-20 14:49:09 +0100736 return cu->IsConstructor()
737 && driver.RequiresConstructorBarrier(self, cu->GetDexFile(), cu->GetClassDefIndex());
Calin Juravle27df7582015-04-17 19:12:31 +0100738}
739
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100740void HGraphBuilder::BuildReturn(const Instruction& instruction, Primitive::Type type) {
741 if (type == Primitive::kPrimVoid) {
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100742 if (graph_->ShouldGenerateConstructorBarrier()) {
743 // The compilation unit is null during testing.
744 if (dex_compilation_unit_ != nullptr) {
745 DCHECK(RequiresConstructorBarrier(dex_compilation_unit_, *compiler_driver_))
746 << "Inconsistent use of ShouldGenerateConstructorBarrier. Should not generate a barrier.";
747 }
Calin Juravle27df7582015-04-17 19:12:31 +0100748 current_block_->AddInstruction(new (arena_) HMemoryBarrier(kStoreStore));
749 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100750 current_block_->AddInstruction(new (arena_) HReturnVoid());
751 } else {
752 HInstruction* value = LoadLocal(instruction.VRegA(), type);
753 current_block_->AddInstruction(new (arena_) HReturn(value));
754 }
755 current_block_->AddSuccessor(exit_block_);
756 current_block_ = nullptr;
757}
758
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100759void HGraphBuilder::PotentiallySimplifyFakeString(uint16_t original_dex_register,
760 uint32_t dex_pc,
761 HInvoke* actual_string) {
762 if (!graph_->IsDebuggable()) {
763 // Notify that we cannot compile with baseline. The dex registers aliasing
764 // with `original_dex_register` will be handled when we optimize
765 // (see HInstructionSimplifer::VisitFakeString).
766 can_use_baseline_for_string_init_ = false;
767 return;
768 }
769 const VerifiedMethod* verified_method =
770 compiler_driver_->GetVerifiedMethod(dex_file_, dex_compilation_unit_->GetDexMethodIndex());
771 if (verified_method != nullptr) {
772 UpdateLocal(original_dex_register, actual_string);
773 const SafeMap<uint32_t, std::set<uint32_t>>& string_init_map =
774 verified_method->GetStringInitPcRegMap();
775 auto map_it = string_init_map.find(dex_pc);
776 if (map_it != string_init_map.end()) {
777 std::set<uint32_t> reg_set = map_it->second;
778 for (auto set_it = reg_set.begin(); set_it != reg_set.end(); ++set_it) {
779 HInstruction* load_local = LoadLocal(original_dex_register, Primitive::kPrimNot);
780 UpdateLocal(*set_it, load_local);
781 }
782 }
783 } else {
784 can_use_baseline_for_string_init_ = false;
785 }
786}
787
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100788bool HGraphBuilder::BuildInvoke(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000789 uint32_t dex_pc,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100790 uint32_t method_idx,
791 uint32_t number_of_vreg_arguments,
792 bool is_range,
793 uint32_t* args,
794 uint32_t register_index) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100795 Instruction::Code opcode = instruction.Opcode();
796 InvokeType invoke_type;
797 switch (opcode) {
798 case Instruction::INVOKE_STATIC:
799 case Instruction::INVOKE_STATIC_RANGE:
800 invoke_type = kStatic;
801 break;
802 case Instruction::INVOKE_DIRECT:
803 case Instruction::INVOKE_DIRECT_RANGE:
804 invoke_type = kDirect;
805 break;
806 case Instruction::INVOKE_VIRTUAL:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +0000807 case Instruction::INVOKE_VIRTUAL_QUICK:
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100808 case Instruction::INVOKE_VIRTUAL_RANGE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +0000809 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100810 invoke_type = kVirtual;
811 break;
812 case Instruction::INVOKE_INTERFACE:
813 case Instruction::INVOKE_INTERFACE_RANGE:
814 invoke_type = kInterface;
815 break;
816 case Instruction::INVOKE_SUPER_RANGE:
817 case Instruction::INVOKE_SUPER:
818 invoke_type = kSuper;
819 break;
820 default:
821 LOG(FATAL) << "Unexpected invoke op: " << opcode;
822 return false;
823 }
824
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100825 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
826 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(method_id.proto_idx_);
827 const char* descriptor = dex_file_->StringDataByIdx(proto_id.shorty_idx_);
828 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100829 bool is_instance_call = invoke_type != kStatic;
Nicolas Geoffray2e335252015-06-18 11:11:27 +0100830 // Remove the return type from the 'proto'.
831 size_t number_of_arguments = strlen(descriptor) - 1;
832 if (is_instance_call) {
833 // One extra argument for 'this'.
834 ++number_of_arguments;
835 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100836
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000837 MethodReference target_method(dex_file_, method_idx);
838 uintptr_t direct_code;
839 uintptr_t direct_method;
840 int table_index;
841 InvokeType optimized_invoke_type = invoke_type;
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000842
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000843 if (!compiler_driver_->ComputeInvokeInfo(dex_compilation_unit_, dex_pc, true, true,
844 &optimized_invoke_type, &target_method, &table_index,
845 &direct_code, &direct_method)) {
Nicolas Geoffray2e335252015-06-18 11:11:27 +0100846 VLOG(compiler) << "Did not compile "
847 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
Calin Juravle48c2b032014-12-09 18:11:36 +0000848 << " because a method call could not be resolved";
849 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedMethod);
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000850 return false;
851 }
852 DCHECK(optimized_invoke_type != kSuper);
853
Roland Levillain4c0eb422015-04-24 16:43:49 +0100854 // By default, consider that the called method implicitly requires
855 // an initialization check of its declaring method.
856 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement =
857 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
858 // Potential class initialization check, in the case of a static method call.
859 HClinitCheck* clinit_check = nullptr;
Jeff Hao848f70a2014-01-15 13:49:50 -0800860 // Replace calls to String.<init> with StringFactory.
861 int32_t string_init_offset = 0;
862 bool is_string_init = compiler_driver_->IsStringInit(method_idx, dex_file_, &string_init_offset);
863 if (is_string_init) {
864 return_type = Primitive::kPrimNot;
865 is_instance_call = false;
866 number_of_arguments--;
867 invoke_type = kStatic;
868 optimized_invoke_type = kStatic;
869 }
Roland Levillain4c0eb422015-04-24 16:43:49 +0100870
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000871 HInvoke* invoke = nullptr;
Roland Levillain4c0eb422015-04-24 16:43:49 +0100872
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000873 if (optimized_invoke_type == kVirtual) {
874 invoke = new (arena_) HInvokeVirtual(
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800875 arena_, number_of_arguments, return_type, dex_pc, method_idx, table_index);
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000876 } else if (optimized_invoke_type == kInterface) {
877 invoke = new (arena_) HInvokeInterface(
878 arena_, number_of_arguments, return_type, dex_pc, method_idx, table_index);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100879 } else {
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000880 DCHECK(optimized_invoke_type == kDirect || optimized_invoke_type == kStatic);
881 // Sharpening to kDirect only works if we compile PIC.
882 DCHECK((optimized_invoke_type == invoke_type) || (optimized_invoke_type != kDirect)
883 || compiler_driver_->GetCompilerOptions().GetCompilePic());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000884 bool is_recursive =
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100885 (target_method.dex_method_index == outer_compilation_unit_->GetDexMethodIndex())
886 && (target_method.dex_file == outer_compilation_unit_->GetDexFile());
Roland Levillain4c0eb422015-04-24 16:43:49 +0100887
Jeff Haocad65422015-06-18 21:16:08 -0700888 if (optimized_invoke_type == kStatic && !is_string_init) {
Roland Levillain4c0eb422015-04-24 16:43:49 +0100889 ScopedObjectAccess soa(Thread::Current());
890 StackHandleScope<4> hs(soa.Self());
891 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
892 dex_compilation_unit_->GetClassLinker()->FindDexCache(
893 *dex_compilation_unit_->GetDexFile())));
894 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
895 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700896 ArtMethod* resolved_method = compiler_driver_->ResolveMethod(
897 soa, dex_cache, class_loader, dex_compilation_unit_, method_idx, optimized_invoke_type);
Roland Levillain4c0eb422015-04-24 16:43:49 +0100898
899 if (resolved_method == nullptr) {
900 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedMethod);
901 return false;
902 }
903
904 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
905 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
906 outer_compilation_unit_->GetClassLinker()->FindDexCache(outer_dex_file)));
Nicolas Geoffrayafd06412015-06-20 22:44:47 +0100907 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Roland Levillain4c0eb422015-04-24 16:43:49 +0100908
909 // The index at which the method's class is stored in the DexCache's type array.
910 uint32_t storage_index = DexFile::kDexNoIndex;
Nicolas Geoffrayafd06412015-06-20 22:44:47 +0100911 bool is_outer_class = (resolved_method->GetDeclaringClass() == outer_class.Get());
912 if (is_outer_class) {
913 storage_index = outer_class->GetDexTypeIndex();
Roland Levillain4c0eb422015-04-24 16:43:49 +0100914 } else if (outer_dex_cache.Get() == dex_cache.Get()) {
915 // Get `storage_index` from IsClassOfStaticMethodAvailableToReferrer.
916 compiler_driver_->IsClassOfStaticMethodAvailableToReferrer(outer_dex_cache.Get(),
Nicolas Geoffrayafd06412015-06-20 22:44:47 +0100917 GetCompilingClass(),
Roland Levillain4c0eb422015-04-24 16:43:49 +0100918 resolved_method,
919 method_idx,
920 &storage_index);
921 }
922
Nicolas Geoffrayb783b402015-06-22 11:06:43 +0100923 if (!outer_class->IsInterface()
924 && outer_class->IsSubClass(resolved_method->GetDeclaringClass())) {
Nicolas Geoffrayafd06412015-06-20 22:44:47 +0100925 // If the outer class is the declaring class or a subclass
Roland Levillain5f02c6c2015-04-24 19:14:22 +0100926 // of the declaring class, no class initialization is needed
927 // before the static method call.
Nicolas Geoffrayafd06412015-06-20 22:44:47 +0100928 // Note that in case of inlining, we do not need to add clinit checks
929 // to calls that satisfy this subclass check with any inlined methods. This
930 // will be detected by the optimization passes.
Roland Levillain4c0eb422015-04-24 16:43:49 +0100931 clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
932 } else if (storage_index != DexFile::kDexNoIndex) {
933 // If the method's class type index is available, check
934 // whether we should add an explicit class initialization
935 // check for its declaring class before the static method call.
936
937 // TODO: find out why this check is needed.
938 bool is_in_dex_cache = compiler_driver_->CanAssumeTypeIsPresentInDexCache(
939 *outer_compilation_unit_->GetDexFile(), storage_index);
940 bool is_initialized =
941 resolved_method->GetDeclaringClass()->IsInitialized() && is_in_dex_cache;
942
943 if (is_initialized) {
944 clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
945 } else {
946 clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +0100947 HLoadClass* load_class = new (arena_) HLoadClass(
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100948 graph_->GetCurrentMethod(),
949 storage_index,
950 *dex_compilation_unit_->GetDexFile(),
Nicolas Geoffrayafd06412015-06-20 22:44:47 +0100951 is_outer_class,
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100952 dex_pc);
Roland Levillain4c0eb422015-04-24 16:43:49 +0100953 current_block_->AddInstruction(load_class);
954 clinit_check = new (arena_) HClinitCheck(load_class, dex_pc);
955 current_block_->AddInstruction(clinit_check);
Roland Levillain4c0eb422015-04-24 16:43:49 +0100956 }
957 }
958 }
959
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100960 invoke = new (arena_) HInvokeStaticOrDirect(arena_,
961 number_of_arguments,
962 return_type,
963 dex_pc,
964 target_method.dex_method_index,
965 is_recursive,
966 string_init_offset,
967 invoke_type,
968 optimized_invoke_type,
969 clinit_check_requirement);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100970 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100971
972 size_t start_index = 0;
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000973 Temporaries temps(graph_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100974 if (is_instance_call) {
975 HInstruction* arg = LoadLocal(is_range ? register_index : args[0], Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000976 HNullCheck* null_check = new (arena_) HNullCheck(arg, dex_pc);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100977 current_block_->AddInstruction(null_check);
978 temps.Add(null_check);
979 invoke->SetArgumentAt(0, null_check);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100980 start_index = 1;
981 }
982
Nicolas Geoffray2e335252015-06-18 11:11:27 +0100983 uint32_t descriptor_index = 1; // Skip the return type.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100984 uint32_t argument_index = start_index;
Jeff Hao848f70a2014-01-15 13:49:50 -0800985 if (is_string_init) {
986 start_index = 1;
987 }
Nicolas Geoffray2e335252015-06-18 11:11:27 +0100988 for (size_t i = start_index;
989 // Make sure we don't go over the expected arguments or over the number of
990 // dex registers given. If the instruction was seen as dead by the verifier,
991 // it hasn't been properly checked.
992 (i < number_of_vreg_arguments) && (argument_index < number_of_arguments);
993 i++, argument_index++) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100994 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100995 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
Nicolas Geoffray2e335252015-06-18 11:11:27 +0100996 if (!is_range
997 && is_wide
998 && ((i + 1 == number_of_vreg_arguments) || (args[i] + 1 != args[i + 1]))) {
999 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
1000 // reject any class where this is violated. However, the verifier only does these checks
1001 // on non trivially dead instructions, so we just bailout the compilation.
1002 VLOG(compiler) << "Did not compile "
1003 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
1004 << " because of non-sequential dex register pair in wide argument";
1005 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1006 return false;
1007 }
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001008 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
1009 invoke->SetArgumentAt(argument_index, arg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001010 if (is_wide) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001011 i++;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001012 }
1013 }
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001014
1015 if (argument_index != number_of_arguments) {
1016 VLOG(compiler) << "Did not compile "
1017 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
1018 << " because of wrong number of arguments in invoke instruction";
1019 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1020 return false;
1021 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001022
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001023 if (invoke->IsInvokeStaticOrDirect()) {
1024 invoke->SetArgumentAt(argument_index, graph_->GetCurrentMethod());
1025 argument_index++;
1026 }
1027
Roland Levillain4c0eb422015-04-24 16:43:49 +01001028 if (clinit_check_requirement == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit) {
1029 // Add the class initialization check as last input of `invoke`.
1030 DCHECK(clinit_check != nullptr);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001031 DCHECK(!is_string_init);
Roland Levillain3e3d7332015-04-28 11:00:54 +01001032 invoke->SetArgumentAt(argument_index, clinit_check);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001033 argument_index++;
Roland Levillain4c0eb422015-04-24 16:43:49 +01001034 }
1035
Jeff Hao848f70a2014-01-15 13:49:50 -08001036 // Add move-result for StringFactory method.
1037 if (is_string_init) {
1038 uint32_t orig_this_reg = is_range ? register_index : args[0];
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001039 HInstruction* fake_string = LoadLocal(orig_this_reg, Primitive::kPrimNot);
1040 invoke->SetArgumentAt(argument_index, fake_string);
1041 current_block_->AddInstruction(invoke);
1042 PotentiallySimplifyFakeString(orig_this_reg, dex_pc, invoke);
1043 } else {
1044 current_block_->AddInstruction(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001045 }
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001046 latest_result_ = invoke;
1047
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001048 return true;
1049}
1050
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001051bool HGraphBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001052 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001053 bool is_put) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001054 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1055 uint32_t obj_reg = instruction.VRegB_22c();
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001056 uint16_t field_index;
1057 if (instruction.IsQuickened()) {
1058 if (!CanDecodeQuickenedInfo()) {
1059 return false;
1060 }
1061 field_index = LookupQuickenedInfo(dex_pc);
1062 } else {
1063 field_index = instruction.VRegC_22c();
1064 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001065
1066 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierc7853442015-03-27 14:35:38 -07001067 ArtField* resolved_field =
1068 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001069
Mathieu Chartierc7853442015-03-27 14:35:38 -07001070 if (resolved_field == nullptr) {
Calin Juravle48c2b032014-12-09 18:11:36 +00001071 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedField);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001072 return false;
1073 }
Calin Juravle52c48962014-12-16 17:02:57 +00001074
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001075 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001076
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001077 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00001078 current_block_->AddInstruction(new (arena_) HNullCheck(object, dex_pc));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001079 if (is_put) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001080 Temporaries temps(graph_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001081 HInstruction* null_check = current_block_->GetLastInstruction();
1082 // We need one temporary for the null check.
1083 temps.Add(null_check);
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001084 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001085 current_block_->AddInstruction(new (arena_) HInstanceFieldSet(
1086 null_check,
1087 value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001088 field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00001089 resolved_field->GetOffset(),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001090 resolved_field->IsVolatile(),
1091 field_index,
1092 *dex_file_));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001093 } else {
1094 current_block_->AddInstruction(new (arena_) HInstanceFieldGet(
1095 current_block_->GetLastInstruction(),
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001096 field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00001097 resolved_field->GetOffset(),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001098 resolved_field->IsVolatile(),
1099 field_index,
1100 *dex_file_));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001101
1102 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1103 }
1104 return true;
1105}
1106
Nicolas Geoffray30451742015-06-19 13:32:41 +01001107static mirror::Class* GetClassFrom(CompilerDriver* driver,
1108 const DexCompilationUnit& compilation_unit) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001109 ScopedObjectAccess soa(Thread::Current());
1110 StackHandleScope<2> hs(soa.Self());
Nicolas Geoffray30451742015-06-19 13:32:41 +01001111 const DexFile& dex_file = *compilation_unit.GetDexFile();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001112 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Nicolas Geoffray30451742015-06-19 13:32:41 +01001113 soa.Decode<mirror::ClassLoader*>(compilation_unit.GetClassLoader())));
1114 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
1115 compilation_unit.GetClassLinker()->FindDexCache(dex_file)));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001116
Nicolas Geoffray30451742015-06-19 13:32:41 +01001117 return driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, &compilation_unit);
1118}
1119
1120mirror::Class* HGraphBuilder::GetOutermostCompilingClass() const {
1121 return GetClassFrom(compiler_driver_, *outer_compilation_unit_);
1122}
1123
1124mirror::Class* HGraphBuilder::GetCompilingClass() const {
1125 return GetClassFrom(compiler_driver_, *dex_compilation_unit_);
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001126}
1127
1128bool HGraphBuilder::IsOutermostCompilingClass(uint16_t type_index) const {
1129 ScopedObjectAccess soa(Thread::Current());
1130 StackHandleScope<4> hs(soa.Self());
1131 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
1132 dex_compilation_unit_->GetClassLinker()->FindDexCache(*dex_compilation_unit_->GetDexFile())));
1133 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1134 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
1135 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
1136 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
Nicolas Geoffrayafd06412015-06-20 22:44:47 +01001137 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001138
Nicolas Geoffrayafd06412015-06-20 22:44:47 +01001139 return outer_class.Get() == cls.Get();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001140}
1141
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001142bool HGraphBuilder::BuildStaticFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001143 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001144 bool is_put) {
1145 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1146 uint16_t field_index = instruction.VRegB_21c();
1147
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001148 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierc7853442015-03-27 14:35:38 -07001149 StackHandleScope<4> hs(soa.Self());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001150 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
1151 dex_compilation_unit_->GetClassLinker()->FindDexCache(*dex_compilation_unit_->GetDexFile())));
1152 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1153 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
Mathieu Chartierc7853442015-03-27 14:35:38 -07001154 ArtField* resolved_field = compiler_driver_->ResolveField(
1155 soa, dex_cache, class_loader, dex_compilation_unit_, field_index, true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001156
Mathieu Chartierc7853442015-03-27 14:35:38 -07001157 if (resolved_field == nullptr) {
Calin Juravle48c2b032014-12-09 18:11:36 +00001158 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedField);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001159 return false;
1160 }
1161
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001162 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
1163 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
1164 outer_compilation_unit_->GetClassLinker()->FindDexCache(outer_dex_file)));
Nicolas Geoffray30451742015-06-19 13:32:41 +01001165 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001166
1167 // The index at which the field's class is stored in the DexCache's type array.
1168 uint32_t storage_index;
Nicolas Geoffray30451742015-06-19 13:32:41 +01001169 bool is_outer_class = (outer_class.Get() == resolved_field->GetDeclaringClass());
1170 if (is_outer_class) {
1171 storage_index = outer_class->GetDexTypeIndex();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001172 } else if (outer_dex_cache.Get() != dex_cache.Get()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001173 // The compiler driver cannot currently understand multiple dex caches involved. Just bailout.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001174 return false;
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001175 } else {
1176 std::pair<bool, bool> pair = compiler_driver_->IsFastStaticField(
1177 outer_dex_cache.Get(),
Nicolas Geoffray30451742015-06-19 13:32:41 +01001178 GetCompilingClass(),
Mathieu Chartierc7853442015-03-27 14:35:38 -07001179 resolved_field,
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001180 field_index,
1181 &storage_index);
1182 bool can_easily_access = is_put ? pair.second : pair.first;
1183 if (!can_easily_access) {
1184 return false;
1185 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001186 }
1187
1188 // TODO: find out why this check is needed.
1189 bool is_in_dex_cache = compiler_driver_->CanAssumeTypeIsPresentInDexCache(
Nicolas Geoffray6a816cf2015-03-24 16:17:56 +00001190 *outer_compilation_unit_->GetDexFile(), storage_index);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001191 bool is_initialized = resolved_field->GetDeclaringClass()->IsInitialized() && is_in_dex_cache;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001192
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001193 HLoadClass* constant = new (arena_) HLoadClass(graph_->GetCurrentMethod(),
1194 storage_index,
1195 *dex_compilation_unit_->GetDexFile(),
Nicolas Geoffray30451742015-06-19 13:32:41 +01001196 is_outer_class,
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001197 dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001198 current_block_->AddInstruction(constant);
1199
1200 HInstruction* cls = constant;
Nicolas Geoffray30451742015-06-19 13:32:41 +01001201 if (!is_initialized && !is_outer_class) {
Calin Juravle225ff812014-11-13 16:46:39 +00001202 cls = new (arena_) HClinitCheck(constant, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001203 current_block_->AddInstruction(cls);
1204 }
1205
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001206 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001207 if (is_put) {
1208 // We need to keep the class alive before loading the value.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001209 Temporaries temps(graph_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001210 temps.Add(cls);
1211 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1212 DCHECK_EQ(value->GetType(), field_type);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001213 current_block_->AddInstruction(new (arena_) HStaticFieldSet(cls,
1214 value,
1215 field_type,
1216 resolved_field->GetOffset(),
1217 resolved_field->IsVolatile(),
1218 field_index,
1219 *dex_file_));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001220 } else {
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001221 current_block_->AddInstruction(new (arena_) HStaticFieldGet(cls,
1222 field_type,
1223 resolved_field->GetOffset(),
1224 resolved_field->IsVolatile(),
1225 field_index,
1226 *dex_file_));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001227 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1228 }
1229 return true;
1230}
1231
Calin Juravlebacfec32014-11-14 15:54:36 +00001232void HGraphBuilder::BuildCheckedDivRem(uint16_t out_vreg,
1233 uint16_t first_vreg,
1234 int64_t second_vreg_or_constant,
1235 uint32_t dex_pc,
1236 Primitive::Type type,
1237 bool second_is_constant,
1238 bool isDiv) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001239 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Calin Juravled0d48522014-11-04 16:40:20 +00001240
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001241 HInstruction* first = LoadLocal(first_vreg, type);
1242 HInstruction* second = nullptr;
1243 if (second_is_constant) {
1244 if (type == Primitive::kPrimInt) {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001245 second = graph_->GetIntConstant(second_vreg_or_constant);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001246 } else {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001247 second = graph_->GetLongConstant(second_vreg_or_constant);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001248 }
1249 } else {
1250 second = LoadLocal(second_vreg_or_constant, type);
1251 }
1252
1253 if (!second_is_constant
1254 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0)
1255 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) {
1256 second = new (arena_) HDivZeroCheck(second, dex_pc);
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001257 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +00001258 current_block_->AddInstruction(second);
1259 temps.Add(current_block_->GetLastInstruction());
1260 }
1261
Calin Juravlebacfec32014-11-14 15:54:36 +00001262 if (isDiv) {
1263 current_block_->AddInstruction(new (arena_) HDiv(type, first, second, dex_pc));
1264 } else {
1265 current_block_->AddInstruction(new (arena_) HRem(type, first, second, dex_pc));
1266 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001267 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
Calin Juravled0d48522014-11-04 16:40:20 +00001268}
1269
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001270void HGraphBuilder::BuildArrayAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001271 uint32_t dex_pc,
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001272 bool is_put,
1273 Primitive::Type anticipated_type) {
1274 uint8_t source_or_dest_reg = instruction.VRegA_23x();
1275 uint8_t array_reg = instruction.VRegB_23x();
1276 uint8_t index_reg = instruction.VRegC_23x();
1277
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001278 // We need one temporary for the null check, one for the index, and one for the length.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001279 Temporaries temps(graph_);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001280
1281 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00001282 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001283 current_block_->AddInstruction(object);
1284 temps.Add(object);
1285
1286 HInstruction* length = new (arena_) HArrayLength(object);
1287 current_block_->AddInstruction(length);
1288 temps.Add(length);
1289 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt);
Calin Juravle225ff812014-11-13 16:46:39 +00001290 index = new (arena_) HBoundsCheck(index, length, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001291 current_block_->AddInstruction(index);
1292 temps.Add(index);
1293 if (is_put) {
1294 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
1295 // TODO: Insert a type check node if the type is Object.
Nicolas Geoffray39468442014-09-02 15:17:15 +01001296 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001297 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001298 } else {
1299 current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type));
1300 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1301 }
Mark Mendell1152c922015-04-24 17:06:35 -04001302 graph_->SetHasBoundsChecks(true);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001303}
1304
Calin Juravle225ff812014-11-13 16:46:39 +00001305void HGraphBuilder::BuildFilledNewArray(uint32_t dex_pc,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001306 uint32_t type_index,
1307 uint32_t number_of_vreg_arguments,
1308 bool is_range,
1309 uint32_t* args,
1310 uint32_t register_index) {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001311 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments);
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001312 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
1313 ? kQuickAllocArrayWithAccessCheck
1314 : kQuickAllocArray;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01001315 HInstruction* object = new (arena_) HNewArray(length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01001316 graph_->GetCurrentMethod(),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01001317 dex_pc,
1318 type_index,
1319 *dex_compilation_unit_->GetDexFile(),
1320 entrypoint);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001321 current_block_->AddInstruction(object);
1322
1323 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1324 DCHECK_EQ(descriptor[0], '[') << descriptor;
1325 char primitive = descriptor[1];
1326 DCHECK(primitive == 'I'
1327 || primitive == 'L'
1328 || primitive == '[') << descriptor;
1329 bool is_reference_array = (primitive == 'L') || (primitive == '[');
1330 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
1331
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001332 Temporaries temps(graph_);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001333 temps.Add(object);
1334 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
1335 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
David Brazdil8d5b8b22015-03-24 10:51:52 +00001336 HInstruction* index = graph_->GetIntConstant(i);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001337 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001338 new (arena_) HArraySet(object, index, value, type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001339 }
1340 latest_result_ = object;
1341}
1342
1343template <typename T>
1344void HGraphBuilder::BuildFillArrayData(HInstruction* object,
1345 const T* data,
1346 uint32_t element_count,
1347 Primitive::Type anticipated_type,
Calin Juravle225ff812014-11-13 16:46:39 +00001348 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001349 for (uint32_t i = 0; i < element_count; ++i) {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001350 HInstruction* index = graph_->GetIntConstant(i);
1351 HInstruction* value = graph_->GetIntConstant(data[i]);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001352 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001353 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001354 }
1355}
1356
Calin Juravle225ff812014-11-13 16:46:39 +00001357void HGraphBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001358 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +00001359 HInstruction* array = LoadLocal(instruction.VRegA_31t(), Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00001360 HNullCheck* null_check = new (arena_) HNullCheck(array, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001361 current_block_->AddInstruction(null_check);
1362 temps.Add(null_check);
1363
1364 HInstruction* length = new (arena_) HArrayLength(null_check);
1365 current_block_->AddInstruction(length);
1366
Calin Juravle225ff812014-11-13 16:46:39 +00001367 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
Calin Juravled0d48522014-11-04 16:40:20 +00001368 const Instruction::ArrayDataPayload* payload =
1369 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_start_ + payload_offset);
1370 const uint8_t* data = payload->data;
1371 uint32_t element_count = payload->element_count;
1372
1373 // Implementation of this DEX instruction seems to be that the bounds check is
1374 // done before doing any stores.
David Brazdil8d5b8b22015-03-24 10:51:52 +00001375 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1);
Calin Juravle225ff812014-11-13 16:46:39 +00001376 current_block_->AddInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
Calin Juravled0d48522014-11-04 16:40:20 +00001377
1378 switch (payload->element_width) {
1379 case 1:
1380 BuildFillArrayData(null_check,
1381 reinterpret_cast<const int8_t*>(data),
1382 element_count,
1383 Primitive::kPrimByte,
Calin Juravle225ff812014-11-13 16:46:39 +00001384 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001385 break;
1386 case 2:
1387 BuildFillArrayData(null_check,
1388 reinterpret_cast<const int16_t*>(data),
1389 element_count,
1390 Primitive::kPrimShort,
Calin Juravle225ff812014-11-13 16:46:39 +00001391 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001392 break;
1393 case 4:
1394 BuildFillArrayData(null_check,
1395 reinterpret_cast<const int32_t*>(data),
1396 element_count,
1397 Primitive::kPrimInt,
Calin Juravle225ff812014-11-13 16:46:39 +00001398 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001399 break;
1400 case 8:
1401 BuildFillWideArrayData(null_check,
1402 reinterpret_cast<const int64_t*>(data),
1403 element_count,
Calin Juravle225ff812014-11-13 16:46:39 +00001404 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001405 break;
1406 default:
1407 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1408 }
Mark Mendell1152c922015-04-24 17:06:35 -04001409 graph_->SetHasBoundsChecks(true);
Calin Juravled0d48522014-11-04 16:40:20 +00001410}
1411
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001412void HGraphBuilder::BuildFillWideArrayData(HInstruction* object,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +01001413 const int64_t* data,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001414 uint32_t element_count,
Calin Juravle225ff812014-11-13 16:46:39 +00001415 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001416 for (uint32_t i = 0; i < element_count; ++i) {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001417 HInstruction* index = graph_->GetIntConstant(i);
1418 HInstruction* value = graph_->GetLongConstant(data[i]);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001419 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001420 object, index, value, Primitive::kPrimLong, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001421 }
1422}
1423
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001424bool HGraphBuilder::BuildTypeCheck(const Instruction& instruction,
1425 uint8_t destination,
1426 uint8_t reference,
1427 uint16_t type_index,
Calin Juravle225ff812014-11-13 16:46:39 +00001428 uint32_t dex_pc) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001429 bool type_known_final;
1430 bool type_known_abstract;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001431 // `CanAccessTypeWithoutChecks` will tell whether the method being
1432 // built is trying to access its own class, so that the generated
1433 // code can optimize for this case. However, the optimization does not
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001434 // work for inlining, so we use `IsOutermostCompilingClass` instead.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001435 bool dont_use_is_referrers_class;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001436 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
1437 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001438 &type_known_final, &type_known_abstract, &dont_use_is_referrers_class);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001439 if (!can_access) {
Calin Juravle48c2b032014-12-09 18:11:36 +00001440 MaybeRecordStat(MethodCompilationStat::kNotCompiledCantAccesType);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001441 return false;
1442 }
1443 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot);
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001444 HLoadClass* cls = new (arena_) HLoadClass(
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001445 graph_->GetCurrentMethod(),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01001446 type_index,
1447 *dex_compilation_unit_->GetDexFile(),
1448 IsOutermostCompilingClass(type_index),
1449 dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001450 current_block_->AddInstruction(cls);
1451 // The class needs a temporary before being used by the type check.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001452 Temporaries temps(graph_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001453 temps.Add(cls);
1454 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
1455 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001456 new (arena_) HInstanceOf(object, cls, type_known_final, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001457 UpdateLocal(destination, current_block_->GetLastInstruction());
1458 } else {
1459 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
1460 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001461 new (arena_) HCheckCast(object, cls, type_known_final, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001462 }
1463 return true;
1464}
1465
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001466bool HGraphBuilder::NeedsAccessCheck(uint32_t type_index) const {
1467 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
1468 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index);
1469}
1470
Calin Juravle48c2b032014-12-09 18:11:36 +00001471void HGraphBuilder::BuildPackedSwitch(const Instruction& instruction, uint32_t dex_pc) {
David Brazdil2ef645b2015-06-17 18:20:52 +01001472 // Verifier guarantees that the payload for PackedSwitch contains:
1473 // (a) number of entries (may be zero)
1474 // (b) first and lowest switch case value (entry 0, always present)
1475 // (c) list of target pcs (entries 1 <= i <= N)
Andreas Gamped881df52014-11-24 23:28:39 -08001476 SwitchTable table(instruction, dex_pc, false);
1477
1478 // Value to test against.
1479 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
1480
David Brazdil2ef645b2015-06-17 18:20:52 +01001481 // Retrieve number of entries.
Andreas Gampee4d4d322014-12-04 09:09:57 -08001482 uint16_t num_entries = table.GetNumEntries();
David Brazdil2ef645b2015-06-17 18:20:52 +01001483 if (num_entries == 0) {
1484 return;
1485 }
Andreas Gampee4d4d322014-12-04 09:09:57 -08001486
Andreas Gamped881df52014-11-24 23:28:39 -08001487 // Chained cmp-and-branch, starting from starting_key.
1488 int32_t starting_key = table.GetEntryAt(0);
1489
Andreas Gamped881df52014-11-24 23:28:39 -08001490 for (size_t i = 1; i <= num_entries; i++) {
Andreas Gampee4d4d322014-12-04 09:09:57 -08001491 BuildSwitchCaseHelper(instruction, i, i == num_entries, table, value, starting_key + i - 1,
1492 table.GetEntryAt(i), dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08001493 }
Andreas Gamped881df52014-11-24 23:28:39 -08001494}
1495
Calin Juravle48c2b032014-12-09 18:11:36 +00001496void HGraphBuilder::BuildSparseSwitch(const Instruction& instruction, uint32_t dex_pc) {
David Brazdil2ef645b2015-06-17 18:20:52 +01001497 // Verifier guarantees that the payload for SparseSwitch contains:
1498 // (a) number of entries (may be zero)
1499 // (b) sorted key values (entries 0 <= i < N)
1500 // (c) target pcs corresponding to the switch values (entries N <= i < 2*N)
Andreas Gampee4d4d322014-12-04 09:09:57 -08001501 SwitchTable table(instruction, dex_pc, true);
1502
1503 // Value to test against.
1504 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
1505
1506 uint16_t num_entries = table.GetNumEntries();
Andreas Gampee4d4d322014-12-04 09:09:57 -08001507
1508 for (size_t i = 0; i < num_entries; i++) {
1509 BuildSwitchCaseHelper(instruction, i, i == static_cast<size_t>(num_entries) - 1, table, value,
1510 table.GetEntryAt(i), table.GetEntryAt(i + num_entries), dex_pc);
1511 }
Andreas Gampee4d4d322014-12-04 09:09:57 -08001512}
1513
1514void HGraphBuilder::BuildSwitchCaseHelper(const Instruction& instruction, size_t index,
1515 bool is_last_case, const SwitchTable& table,
1516 HInstruction* value, int32_t case_value_int,
1517 int32_t target_offset, uint32_t dex_pc) {
David Brazdil852eaff2015-02-02 15:23:05 +00001518 HBasicBlock* case_target = FindBlockStartingAt(dex_pc + target_offset);
1519 DCHECK(case_target != nullptr);
1520 PotentiallyAddSuspendCheck(case_target, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001521
1522 // The current case's value.
David Brazdil8d5b8b22015-03-24 10:51:52 +00001523 HInstruction* this_case_value = graph_->GetIntConstant(case_value_int);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001524
1525 // Compare value and this_case_value.
1526 HEqual* comparison = new (arena_) HEqual(value, this_case_value);
1527 current_block_->AddInstruction(comparison);
1528 HInstruction* ifinst = new (arena_) HIf(comparison);
1529 current_block_->AddInstruction(ifinst);
1530
1531 // Case hit: use the target offset to determine where to go.
Andreas Gampee4d4d322014-12-04 09:09:57 -08001532 current_block_->AddSuccessor(case_target);
1533
1534 // Case miss: go to the next case (or default fall-through).
1535 // When there is a next case, we use the block stored with the table offset representing this
1536 // case (that is where we registered them in ComputeBranchTargets).
1537 // When there is no next case, we use the following instruction.
1538 // TODO: Find a good way to peel the last iteration to avoid conditional, but still have re-use.
1539 if (!is_last_case) {
1540 HBasicBlock* next_case_target = FindBlockStartingAt(table.GetDexPcForIndex(index));
1541 DCHECK(next_case_target != nullptr);
1542 current_block_->AddSuccessor(next_case_target);
1543
1544 // Need to manually add the block, as there is no dex-pc transition for the cases.
1545 graph_->AddBlock(next_case_target);
1546
1547 current_block_ = next_case_target;
1548 } else {
1549 HBasicBlock* default_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
1550 DCHECK(default_target != nullptr);
1551 current_block_->AddSuccessor(default_target);
1552 current_block_ = nullptr;
1553 }
1554}
1555
David Brazdil852eaff2015-02-02 15:23:05 +00001556void HGraphBuilder::PotentiallyAddSuspendCheck(HBasicBlock* target, uint32_t dex_pc) {
1557 int32_t target_offset = target->GetDexPc() - dex_pc;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001558 if (target_offset <= 0) {
David Brazdil852eaff2015-02-02 15:23:05 +00001559 // DX generates back edges to the first encountered return. We can save
1560 // time of later passes by not adding redundant suspend checks.
David Brazdil2fd6aa52015-02-02 18:58:27 +00001561 HInstruction* last_in_target = target->GetLastInstruction();
1562 if (last_in_target != nullptr &&
1563 (last_in_target->IsReturn() || last_in_target->IsReturnVoid())) {
1564 return;
David Brazdil852eaff2015-02-02 15:23:05 +00001565 }
1566
1567 // Add a suspend check to backward branches which may potentially loop. We
1568 // can remove them after we recognize loops in the graph.
Calin Juravle225ff812014-11-13 16:46:39 +00001569 current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_pc));
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001570 }
1571}
1572
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001573bool HGraphBuilder::CanDecodeQuickenedInfo() const {
1574 return interpreter_metadata_ != nullptr;
1575}
1576
1577uint16_t HGraphBuilder::LookupQuickenedInfo(uint32_t dex_pc) {
1578 DCHECK(interpreter_metadata_ != nullptr);
1579 uint32_t dex_pc_in_map = DecodeUnsignedLeb128(&interpreter_metadata_);
1580 DCHECK_EQ(dex_pc, dex_pc_in_map);
1581 return DecodeUnsignedLeb128(&interpreter_metadata_);
1582}
1583
Calin Juravle225ff812014-11-13 16:46:39 +00001584bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001585 if (current_block_ == nullptr) {
1586 return true; // Dead code
1587 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001588
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001589 switch (instruction.Opcode()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001590 case Instruction::CONST_4: {
1591 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001592 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001593 UpdateLocal(register_index, constant);
1594 break;
1595 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001596
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001597 case Instruction::CONST_16: {
1598 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001599 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001600 UpdateLocal(register_index, constant);
1601 break;
1602 }
1603
Dave Allison20dfc792014-06-16 20:44:29 -07001604 case Instruction::CONST: {
1605 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001606 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i());
Dave Allison20dfc792014-06-16 20:44:29 -07001607 UpdateLocal(register_index, constant);
1608 break;
1609 }
1610
1611 case Instruction::CONST_HIGH16: {
1612 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001613 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16);
Dave Allison20dfc792014-06-16 20:44:29 -07001614 UpdateLocal(register_index, constant);
1615 break;
1616 }
1617
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001618 case Instruction::CONST_WIDE_16: {
1619 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001620 // Get 16 bits of constant value, sign extended to 64 bits.
1621 int64_t value = instruction.VRegB_21s();
1622 value <<= 48;
1623 value >>= 48;
David Brazdil8d5b8b22015-03-24 10:51:52 +00001624 HLongConstant* constant = graph_->GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001625 UpdateLocal(register_index, constant);
1626 break;
1627 }
1628
1629 case Instruction::CONST_WIDE_32: {
1630 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001631 // Get 32 bits of constant value, sign extended to 64 bits.
1632 int64_t value = instruction.VRegB_31i();
1633 value <<= 32;
1634 value >>= 32;
David Brazdil8d5b8b22015-03-24 10:51:52 +00001635 HLongConstant* constant = graph_->GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001636 UpdateLocal(register_index, constant);
1637 break;
1638 }
1639
1640 case Instruction::CONST_WIDE: {
1641 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001642 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001643 UpdateLocal(register_index, constant);
1644 break;
1645 }
1646
Dave Allison20dfc792014-06-16 20:44:29 -07001647 case Instruction::CONST_WIDE_HIGH16: {
1648 int32_t register_index = instruction.VRegA();
1649 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
David Brazdil8d5b8b22015-03-24 10:51:52 +00001650 HLongConstant* constant = graph_->GetLongConstant(value);
Dave Allison20dfc792014-06-16 20:44:29 -07001651 UpdateLocal(register_index, constant);
1652 break;
1653 }
1654
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001655 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001656 case Instruction::MOVE:
1657 case Instruction::MOVE_FROM16:
1658 case Instruction::MOVE_16: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001659 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001660 UpdateLocal(instruction.VRegA(), value);
1661 break;
1662 }
1663
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001664 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001665 case Instruction::MOVE_WIDE:
1666 case Instruction::MOVE_WIDE_FROM16:
1667 case Instruction::MOVE_WIDE_16: {
1668 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong);
1669 UpdateLocal(instruction.VRegA(), value);
1670 break;
1671 }
1672
1673 case Instruction::MOVE_OBJECT:
1674 case Instruction::MOVE_OBJECT_16:
1675 case Instruction::MOVE_OBJECT_FROM16: {
1676 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot);
1677 UpdateLocal(instruction.VRegA(), value);
1678 break;
1679 }
1680
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001681 case Instruction::RETURN_VOID_NO_BARRIER:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001682 case Instruction::RETURN_VOID: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001683 BuildReturn(instruction, Primitive::kPrimVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001684 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001685 }
1686
Dave Allison20dfc792014-06-16 20:44:29 -07001687#define IF_XX(comparison, cond) \
Calin Juravle225ff812014-11-13 16:46:39 +00001688 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
1689 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001690
Dave Allison20dfc792014-06-16 20:44:29 -07001691 IF_XX(HEqual, EQ);
1692 IF_XX(HNotEqual, NE);
1693 IF_XX(HLessThan, LT);
1694 IF_XX(HLessThanOrEqual, LE);
1695 IF_XX(HGreaterThan, GT);
1696 IF_XX(HGreaterThanOrEqual, GE);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001697
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001698 case Instruction::GOTO:
1699 case Instruction::GOTO_16:
1700 case Instruction::GOTO_32: {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001701 int32_t offset = instruction.GetTargetOffset();
Calin Juravle225ff812014-11-13 16:46:39 +00001702 HBasicBlock* target = FindBlockStartingAt(offset + dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001703 DCHECK(target != nullptr);
David Brazdil852eaff2015-02-02 15:23:05 +00001704 PotentiallyAddSuspendCheck(target, dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001705 current_block_->AddInstruction(new (arena_) HGoto());
1706 current_block_->AddSuccessor(target);
1707 current_block_ = nullptr;
1708 break;
1709 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001710
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001711 case Instruction::RETURN: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001712 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001713 break;
1714 }
1715
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001716 case Instruction::RETURN_OBJECT: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001717 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001718 break;
1719 }
1720
1721 case Instruction::RETURN_WIDE: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001722 BuildReturn(instruction, return_type_);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001723 break;
1724 }
1725
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001726 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001727 case Instruction::INVOKE_INTERFACE:
1728 case Instruction::INVOKE_STATIC:
1729 case Instruction::INVOKE_SUPER:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001730 case Instruction::INVOKE_VIRTUAL:
1731 case Instruction::INVOKE_VIRTUAL_QUICK: {
1732 uint16_t method_idx;
1733 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_QUICK) {
1734 if (!CanDecodeQuickenedInfo()) {
1735 return false;
1736 }
1737 method_idx = LookupQuickenedInfo(dex_pc);
1738 } else {
1739 method_idx = instruction.VRegB_35c();
1740 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001741 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001742 uint32_t args[5];
Ian Rogers29a26482014-05-02 15:27:29 -07001743 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00001744 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001745 number_of_vreg_arguments, false, args, -1)) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001746 return false;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001747 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001748 break;
1749 }
1750
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001751 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001752 case Instruction::INVOKE_INTERFACE_RANGE:
1753 case Instruction::INVOKE_STATIC_RANGE:
1754 case Instruction::INVOKE_SUPER_RANGE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001755 case Instruction::INVOKE_VIRTUAL_RANGE:
1756 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
1757 uint16_t method_idx;
1758 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK) {
1759 if (!CanDecodeQuickenedInfo()) {
1760 return false;
1761 }
1762 method_idx = LookupQuickenedInfo(dex_pc);
1763 } else {
1764 method_idx = instruction.VRegB_3rc();
1765 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001766 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1767 uint32_t register_index = instruction.VRegC();
Calin Juravle225ff812014-11-13 16:46:39 +00001768 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001769 number_of_vreg_arguments, true, nullptr, register_index)) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001770 return false;
1771 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001772 break;
1773 }
1774
Roland Levillain88cb1752014-10-20 16:36:47 +01001775 case Instruction::NEG_INT: {
1776 Unop_12x<HNeg>(instruction, Primitive::kPrimInt);
1777 break;
1778 }
1779
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001780 case Instruction::NEG_LONG: {
1781 Unop_12x<HNeg>(instruction, Primitive::kPrimLong);
1782 break;
1783 }
1784
Roland Levillain3dbcb382014-10-28 17:30:07 +00001785 case Instruction::NEG_FLOAT: {
1786 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat);
1787 break;
1788 }
1789
1790 case Instruction::NEG_DOUBLE: {
1791 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble);
1792 break;
1793 }
1794
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001795 case Instruction::NOT_INT: {
1796 Unop_12x<HNot>(instruction, Primitive::kPrimInt);
1797 break;
1798 }
1799
Roland Levillain70566432014-10-24 16:20:17 +01001800 case Instruction::NOT_LONG: {
1801 Unop_12x<HNot>(instruction, Primitive::kPrimLong);
1802 break;
1803 }
1804
Roland Levillaindff1f282014-11-05 14:15:05 +00001805 case Instruction::INT_TO_LONG: {
Roland Levillain624279f2014-12-04 11:54:28 +00001806 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong, dex_pc);
Roland Levillaindff1f282014-11-05 14:15:05 +00001807 break;
1808 }
1809
Roland Levillaincff13742014-11-17 14:32:17 +00001810 case Instruction::INT_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001811 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00001812 break;
1813 }
1814
1815 case Instruction::INT_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001816 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00001817 break;
1818 }
1819
Roland Levillain946e1432014-11-11 17:35:19 +00001820 case Instruction::LONG_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001821 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt, dex_pc);
Roland Levillain946e1432014-11-11 17:35:19 +00001822 break;
1823 }
1824
Roland Levillain6d0e4832014-11-27 18:31:21 +00001825 case Instruction::LONG_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001826 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimFloat, dex_pc);
Roland Levillain6d0e4832014-11-27 18:31:21 +00001827 break;
1828 }
1829
Roland Levillain647b9ed2014-11-27 12:06:00 +00001830 case Instruction::LONG_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001831 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble, dex_pc);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001832 break;
1833 }
1834
Roland Levillain3f8f9362014-12-02 17:45:01 +00001835 case Instruction::FLOAT_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001836 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimInt, dex_pc);
1837 break;
1838 }
1839
1840 case Instruction::FLOAT_TO_LONG: {
1841 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimLong, dex_pc);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001842 break;
1843 }
1844
Roland Levillain8964e2b2014-12-04 12:10:50 +00001845 case Instruction::FLOAT_TO_DOUBLE: {
1846 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimDouble, dex_pc);
1847 break;
1848 }
1849
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001850 case Instruction::DOUBLE_TO_INT: {
1851 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimInt, dex_pc);
1852 break;
1853 }
1854
1855 case Instruction::DOUBLE_TO_LONG: {
1856 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimLong, dex_pc);
1857 break;
1858 }
1859
Roland Levillain8964e2b2014-12-04 12:10:50 +00001860 case Instruction::DOUBLE_TO_FLOAT: {
1861 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimFloat, dex_pc);
1862 break;
1863 }
1864
Roland Levillain51d3fc42014-11-13 14:11:42 +00001865 case Instruction::INT_TO_BYTE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001866 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte, dex_pc);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001867 break;
1868 }
1869
Roland Levillain01a8d712014-11-14 16:27:39 +00001870 case Instruction::INT_TO_SHORT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001871 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort, dex_pc);
Roland Levillain01a8d712014-11-14 16:27:39 +00001872 break;
1873 }
1874
Roland Levillain981e4542014-11-14 11:47:14 +00001875 case Instruction::INT_TO_CHAR: {
Roland Levillain624279f2014-12-04 11:54:28 +00001876 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar, dex_pc);
Roland Levillain981e4542014-11-14 11:47:14 +00001877 break;
1878 }
1879
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001880 case Instruction::ADD_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001881 Binop_23x<HAdd>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001882 break;
1883 }
1884
1885 case Instruction::ADD_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001886 Binop_23x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001887 break;
1888 }
1889
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001890 case Instruction::ADD_DOUBLE: {
1891 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble);
1892 break;
1893 }
1894
1895 case Instruction::ADD_FLOAT: {
1896 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat);
1897 break;
1898 }
1899
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001900 case Instruction::SUB_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001901 Binop_23x<HSub>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001902 break;
1903 }
1904
1905 case Instruction::SUB_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001906 Binop_23x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001907 break;
1908 }
1909
Calin Juravle096cc022014-10-23 17:01:13 +01001910 case Instruction::SUB_FLOAT: {
1911 Binop_23x<HSub>(instruction, Primitive::kPrimFloat);
1912 break;
1913 }
1914
1915 case Instruction::SUB_DOUBLE: {
1916 Binop_23x<HSub>(instruction, Primitive::kPrimDouble);
1917 break;
1918 }
1919
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001920 case Instruction::ADD_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001921 Binop_12x<HAdd>(instruction, Primitive::kPrimInt);
1922 break;
1923 }
1924
Calin Juravle34bacdf2014-10-07 20:23:36 +01001925 case Instruction::MUL_INT: {
1926 Binop_23x<HMul>(instruction, Primitive::kPrimInt);
1927 break;
1928 }
1929
1930 case Instruction::MUL_LONG: {
1931 Binop_23x<HMul>(instruction, Primitive::kPrimLong);
1932 break;
1933 }
1934
Calin Juravleb5bfa962014-10-21 18:02:24 +01001935 case Instruction::MUL_FLOAT: {
1936 Binop_23x<HMul>(instruction, Primitive::kPrimFloat);
1937 break;
1938 }
1939
1940 case Instruction::MUL_DOUBLE: {
1941 Binop_23x<HMul>(instruction, Primitive::kPrimDouble);
1942 break;
1943 }
1944
Calin Juravled0d48522014-11-04 16:40:20 +00001945 case Instruction::DIV_INT: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001946 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1947 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravled0d48522014-11-04 16:40:20 +00001948 break;
1949 }
1950
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001951 case Instruction::DIV_LONG: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001952 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1953 dex_pc, Primitive::kPrimLong, false, true);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001954 break;
1955 }
1956
Calin Juravle7c4954d2014-10-28 16:57:40 +00001957 case Instruction::DIV_FLOAT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001958 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001959 break;
1960 }
1961
1962 case Instruction::DIV_DOUBLE: {
Calin Juravle225ff812014-11-13 16:46:39 +00001963 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001964 break;
1965 }
1966
Calin Juravlebacfec32014-11-14 15:54:36 +00001967 case Instruction::REM_INT: {
1968 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1969 dex_pc, Primitive::kPrimInt, false, false);
1970 break;
1971 }
1972
1973 case Instruction::REM_LONG: {
1974 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1975 dex_pc, Primitive::kPrimLong, false, false);
1976 break;
1977 }
1978
Calin Juravled2ec87d2014-12-08 14:24:46 +00001979 case Instruction::REM_FLOAT: {
1980 Binop_23x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
1981 break;
1982 }
1983
1984 case Instruction::REM_DOUBLE: {
1985 Binop_23x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
1986 break;
1987 }
1988
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001989 case Instruction::AND_INT: {
1990 Binop_23x<HAnd>(instruction, Primitive::kPrimInt);
1991 break;
1992 }
1993
1994 case Instruction::AND_LONG: {
1995 Binop_23x<HAnd>(instruction, Primitive::kPrimLong);
1996 break;
1997 }
1998
Calin Juravle9aec02f2014-11-18 23:06:35 +00001999 case Instruction::SHL_INT: {
2000 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt);
2001 break;
2002 }
2003
2004 case Instruction::SHL_LONG: {
2005 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong);
2006 break;
2007 }
2008
2009 case Instruction::SHR_INT: {
2010 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt);
2011 break;
2012 }
2013
2014 case Instruction::SHR_LONG: {
2015 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong);
2016 break;
2017 }
2018
2019 case Instruction::USHR_INT: {
2020 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt);
2021 break;
2022 }
2023
2024 case Instruction::USHR_LONG: {
2025 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong);
2026 break;
2027 }
2028
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002029 case Instruction::OR_INT: {
2030 Binop_23x<HOr>(instruction, Primitive::kPrimInt);
2031 break;
2032 }
2033
2034 case Instruction::OR_LONG: {
2035 Binop_23x<HOr>(instruction, Primitive::kPrimLong);
2036 break;
2037 }
2038
2039 case Instruction::XOR_INT: {
2040 Binop_23x<HXor>(instruction, Primitive::kPrimInt);
2041 break;
2042 }
2043
2044 case Instruction::XOR_LONG: {
2045 Binop_23x<HXor>(instruction, Primitive::kPrimLong);
2046 break;
2047 }
2048
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002049 case Instruction::ADD_LONG_2ADDR: {
2050 Binop_12x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002051 break;
2052 }
2053
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002054 case Instruction::ADD_DOUBLE_2ADDR: {
2055 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble);
2056 break;
2057 }
2058
2059 case Instruction::ADD_FLOAT_2ADDR: {
2060 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat);
2061 break;
2062 }
2063
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002064 case Instruction::SUB_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002065 Binop_12x<HSub>(instruction, Primitive::kPrimInt);
2066 break;
2067 }
2068
2069 case Instruction::SUB_LONG_2ADDR: {
2070 Binop_12x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002071 break;
2072 }
2073
Calin Juravle096cc022014-10-23 17:01:13 +01002074 case Instruction::SUB_FLOAT_2ADDR: {
2075 Binop_12x<HSub>(instruction, Primitive::kPrimFloat);
2076 break;
2077 }
2078
2079 case Instruction::SUB_DOUBLE_2ADDR: {
2080 Binop_12x<HSub>(instruction, Primitive::kPrimDouble);
2081 break;
2082 }
2083
Calin Juravle34bacdf2014-10-07 20:23:36 +01002084 case Instruction::MUL_INT_2ADDR: {
2085 Binop_12x<HMul>(instruction, Primitive::kPrimInt);
2086 break;
2087 }
2088
2089 case Instruction::MUL_LONG_2ADDR: {
2090 Binop_12x<HMul>(instruction, Primitive::kPrimLong);
2091 break;
2092 }
2093
Calin Juravleb5bfa962014-10-21 18:02:24 +01002094 case Instruction::MUL_FLOAT_2ADDR: {
2095 Binop_12x<HMul>(instruction, Primitive::kPrimFloat);
2096 break;
2097 }
2098
2099 case Instruction::MUL_DOUBLE_2ADDR: {
2100 Binop_12x<HMul>(instruction, Primitive::kPrimDouble);
2101 break;
2102 }
2103
Calin Juravle865fc882014-11-06 17:09:03 +00002104 case Instruction::DIV_INT_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002105 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2106 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravle865fc882014-11-06 17:09:03 +00002107 break;
2108 }
2109
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002110 case Instruction::DIV_LONG_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002111 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2112 dex_pc, Primitive::kPrimLong, false, true);
2113 break;
2114 }
2115
2116 case Instruction::REM_INT_2ADDR: {
2117 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2118 dex_pc, Primitive::kPrimInt, false, false);
2119 break;
2120 }
2121
2122 case Instruction::REM_LONG_2ADDR: {
2123 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2124 dex_pc, Primitive::kPrimLong, false, false);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002125 break;
2126 }
2127
Calin Juravled2ec87d2014-12-08 14:24:46 +00002128 case Instruction::REM_FLOAT_2ADDR: {
2129 Binop_12x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2130 break;
2131 }
2132
2133 case Instruction::REM_DOUBLE_2ADDR: {
2134 Binop_12x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2135 break;
2136 }
2137
Calin Juravle9aec02f2014-11-18 23:06:35 +00002138 case Instruction::SHL_INT_2ADDR: {
2139 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt);
2140 break;
2141 }
2142
2143 case Instruction::SHL_LONG_2ADDR: {
2144 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong);
2145 break;
2146 }
2147
2148 case Instruction::SHR_INT_2ADDR: {
2149 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt);
2150 break;
2151 }
2152
2153 case Instruction::SHR_LONG_2ADDR: {
2154 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong);
2155 break;
2156 }
2157
2158 case Instruction::USHR_INT_2ADDR: {
2159 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt);
2160 break;
2161 }
2162
2163 case Instruction::USHR_LONG_2ADDR: {
2164 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong);
2165 break;
2166 }
2167
Calin Juravle7c4954d2014-10-28 16:57:40 +00002168 case Instruction::DIV_FLOAT_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00002169 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002170 break;
2171 }
2172
2173 case Instruction::DIV_DOUBLE_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00002174 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002175 break;
2176 }
2177
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002178 case Instruction::AND_INT_2ADDR: {
2179 Binop_12x<HAnd>(instruction, Primitive::kPrimInt);
2180 break;
2181 }
2182
2183 case Instruction::AND_LONG_2ADDR: {
2184 Binop_12x<HAnd>(instruction, Primitive::kPrimLong);
2185 break;
2186 }
2187
2188 case Instruction::OR_INT_2ADDR: {
2189 Binop_12x<HOr>(instruction, Primitive::kPrimInt);
2190 break;
2191 }
2192
2193 case Instruction::OR_LONG_2ADDR: {
2194 Binop_12x<HOr>(instruction, Primitive::kPrimLong);
2195 break;
2196 }
2197
2198 case Instruction::XOR_INT_2ADDR: {
2199 Binop_12x<HXor>(instruction, Primitive::kPrimInt);
2200 break;
2201 }
2202
2203 case Instruction::XOR_LONG_2ADDR: {
2204 Binop_12x<HXor>(instruction, Primitive::kPrimLong);
2205 break;
2206 }
2207
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002208 case Instruction::ADD_INT_LIT16: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002209 Binop_22s<HAdd>(instruction, false);
2210 break;
2211 }
2212
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002213 case Instruction::AND_INT_LIT16: {
2214 Binop_22s<HAnd>(instruction, false);
2215 break;
2216 }
2217
2218 case Instruction::OR_INT_LIT16: {
2219 Binop_22s<HOr>(instruction, false);
2220 break;
2221 }
2222
2223 case Instruction::XOR_INT_LIT16: {
2224 Binop_22s<HXor>(instruction, false);
2225 break;
2226 }
2227
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002228 case Instruction::RSUB_INT: {
2229 Binop_22s<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002230 break;
2231 }
2232
Calin Juravle34bacdf2014-10-07 20:23:36 +01002233 case Instruction::MUL_INT_LIT16: {
2234 Binop_22s<HMul>(instruction, false);
2235 break;
2236 }
2237
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002238 case Instruction::ADD_INT_LIT8: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002239 Binop_22b<HAdd>(instruction, false);
2240 break;
2241 }
2242
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002243 case Instruction::AND_INT_LIT8: {
2244 Binop_22b<HAnd>(instruction, false);
2245 break;
2246 }
2247
2248 case Instruction::OR_INT_LIT8: {
2249 Binop_22b<HOr>(instruction, false);
2250 break;
2251 }
2252
2253 case Instruction::XOR_INT_LIT8: {
2254 Binop_22b<HXor>(instruction, false);
2255 break;
2256 }
2257
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002258 case Instruction::RSUB_INT_LIT8: {
2259 Binop_22b<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002260 break;
2261 }
2262
Calin Juravle34bacdf2014-10-07 20:23:36 +01002263 case Instruction::MUL_INT_LIT8: {
2264 Binop_22b<HMul>(instruction, false);
2265 break;
2266 }
2267
Calin Juravled0d48522014-11-04 16:40:20 +00002268 case Instruction::DIV_INT_LIT16:
2269 case Instruction::DIV_INT_LIT8: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002270 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2271 dex_pc, Primitive::kPrimInt, true, true);
2272 break;
2273 }
2274
2275 case Instruction::REM_INT_LIT16:
2276 case Instruction::REM_INT_LIT8: {
2277 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2278 dex_pc, Primitive::kPrimInt, true, false);
Calin Juravled0d48522014-11-04 16:40:20 +00002279 break;
2280 }
2281
Calin Juravle9aec02f2014-11-18 23:06:35 +00002282 case Instruction::SHL_INT_LIT8: {
2283 Binop_22b<HShl>(instruction, false);
2284 break;
2285 }
2286
2287 case Instruction::SHR_INT_LIT8: {
2288 Binop_22b<HShr>(instruction, false);
2289 break;
2290 }
2291
2292 case Instruction::USHR_INT_LIT8: {
2293 Binop_22b<HUShr>(instruction, false);
2294 break;
2295 }
2296
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002297 case Instruction::NEW_INSTANCE: {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002298 uint16_t type_index = instruction.VRegB_21c();
Jeff Hao848f70a2014-01-15 13:49:50 -08002299 if (compiler_driver_->IsStringTypeIndex(type_index, dex_file_)) {
Jeff Hao848f70a2014-01-15 13:49:50 -08002300 int32_t register_index = instruction.VRegA();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002301 HFakeString* fake_string = new (arena_) HFakeString();
2302 current_block_->AddInstruction(fake_string);
2303 UpdateLocal(register_index, fake_string);
Jeff Hao848f70a2014-01-15 13:49:50 -08002304 } else {
2305 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
2306 ? kQuickAllocObjectWithAccessCheck
2307 : kQuickAllocObject;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002308
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002309 current_block_->AddInstruction(new (arena_) HNewInstance(
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002310 graph_->GetCurrentMethod(),
2311 dex_pc,
2312 type_index,
2313 *dex_compilation_unit_->GetDexFile(),
2314 entrypoint));
Jeff Hao848f70a2014-01-15 13:49:50 -08002315 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
2316 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002317 break;
2318 }
2319
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002320 case Instruction::NEW_ARRAY: {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002321 uint16_t type_index = instruction.VRegC_22c();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002322 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt);
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002323 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
2324 ? kQuickAllocArrayWithAccessCheck
2325 : kQuickAllocArray;
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002326 current_block_->AddInstruction(new (arena_) HNewArray(length,
2327 graph_->GetCurrentMethod(),
2328 dex_pc,
2329 type_index,
2330 *dex_compilation_unit_->GetDexFile(),
2331 entrypoint));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002332 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
2333 break;
2334 }
2335
2336 case Instruction::FILLED_NEW_ARRAY: {
2337 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
2338 uint32_t type_index = instruction.VRegB_35c();
2339 uint32_t args[5];
2340 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00002341 BuildFilledNewArray(dex_pc, type_index, number_of_vreg_arguments, false, args, 0);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002342 break;
2343 }
2344
2345 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2346 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
2347 uint32_t type_index = instruction.VRegB_3rc();
2348 uint32_t register_index = instruction.VRegC_3rc();
2349 BuildFilledNewArray(
Calin Juravle225ff812014-11-13 16:46:39 +00002350 dex_pc, type_index, number_of_vreg_arguments, true, nullptr, register_index);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002351 break;
2352 }
2353
2354 case Instruction::FILL_ARRAY_DATA: {
Calin Juravle225ff812014-11-13 16:46:39 +00002355 BuildFillArrayData(instruction, dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002356 break;
2357 }
2358
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002359 case Instruction::MOVE_RESULT:
Dave Allison20dfc792014-06-16 20:44:29 -07002360 case Instruction::MOVE_RESULT_WIDE:
David Brazdilfc6a86a2015-06-26 10:33:45 +00002361 case Instruction::MOVE_RESULT_OBJECT: {
Nicolas Geoffray1efcc222015-06-24 12:41:20 +01002362 if (latest_result_ == nullptr) {
2363 // Only dead code can lead to this situation, where the verifier
2364 // does not reject the method.
2365 } else {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002366 // An Invoke/FilledNewArray and its MoveResult could have landed in
2367 // different blocks if there was a try/catch block boundary between
2368 // them. For Invoke, we insert a StoreLocal after the instruction. For
2369 // FilledNewArray, the local needs to be updated after the array was
2370 // filled, otherwise we might overwrite an input vreg.
2371 HStoreLocal* update_local =
2372 new (arena_) HStoreLocal(GetLocalAt(instruction.VRegA()), latest_result_);
2373 HBasicBlock* block = latest_result_->GetBlock();
2374 if (block == current_block_) {
2375 // MoveResult and the previous instruction are in the same block.
2376 current_block_->AddInstruction(update_local);
2377 } else {
2378 // The two instructions are in different blocks. Insert the MoveResult
2379 // before the final control-flow instruction of the previous block.
2380 DCHECK(block->EndsWithControlFlowInstruction());
2381 DCHECK(current_block_->GetInstructions().IsEmpty());
2382 block->InsertInstructionBefore(update_local, block->GetLastInstruction());
2383 }
Nicolas Geoffray1efcc222015-06-24 12:41:20 +01002384 latest_result_ = nullptr;
2385 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002386 break;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002387 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002388
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002389 case Instruction::CMP_LONG: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002390 Binop_23x_cmp(instruction, Primitive::kPrimLong, ComparisonBias::kNoBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002391 break;
2392 }
2393
2394 case Instruction::CMPG_FLOAT: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002395 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kGtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002396 break;
2397 }
2398
2399 case Instruction::CMPG_DOUBLE: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002400 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kGtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002401 break;
2402 }
2403
2404 case Instruction::CMPL_FLOAT: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002405 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kLtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002406 break;
2407 }
2408
2409 case Instruction::CMPL_DOUBLE: {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002410 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kLtBias, dex_pc);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002411 break;
2412 }
2413
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002414 case Instruction::NOP:
2415 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002416
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002417 case Instruction::IGET:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002418 case Instruction::IGET_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002419 case Instruction::IGET_WIDE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002420 case Instruction::IGET_WIDE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002421 case Instruction::IGET_OBJECT:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002422 case Instruction::IGET_OBJECT_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002423 case Instruction::IGET_BOOLEAN:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002424 case Instruction::IGET_BOOLEAN_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002425 case Instruction::IGET_BYTE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002426 case Instruction::IGET_BYTE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002427 case Instruction::IGET_CHAR:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002428 case Instruction::IGET_CHAR_QUICK:
2429 case Instruction::IGET_SHORT:
2430 case Instruction::IGET_SHORT_QUICK: {
Calin Juravle225ff812014-11-13 16:46:39 +00002431 if (!BuildInstanceFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002432 return false;
2433 }
2434 break;
2435 }
2436
2437 case Instruction::IPUT:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002438 case Instruction::IPUT_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002439 case Instruction::IPUT_WIDE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002440 case Instruction::IPUT_WIDE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002441 case Instruction::IPUT_OBJECT:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002442 case Instruction::IPUT_OBJECT_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002443 case Instruction::IPUT_BOOLEAN:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002444 case Instruction::IPUT_BOOLEAN_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002445 case Instruction::IPUT_BYTE:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002446 case Instruction::IPUT_BYTE_QUICK:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002447 case Instruction::IPUT_CHAR:
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00002448 case Instruction::IPUT_CHAR_QUICK:
2449 case Instruction::IPUT_SHORT:
2450 case Instruction::IPUT_SHORT_QUICK: {
Calin Juravle225ff812014-11-13 16:46:39 +00002451 if (!BuildInstanceFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002452 return false;
2453 }
2454 break;
2455 }
2456
2457 case Instruction::SGET:
2458 case Instruction::SGET_WIDE:
2459 case Instruction::SGET_OBJECT:
2460 case Instruction::SGET_BOOLEAN:
2461 case Instruction::SGET_BYTE:
2462 case Instruction::SGET_CHAR:
2463 case Instruction::SGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002464 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002465 return false;
2466 }
2467 break;
2468 }
2469
2470 case Instruction::SPUT:
2471 case Instruction::SPUT_WIDE:
2472 case Instruction::SPUT_OBJECT:
2473 case Instruction::SPUT_BOOLEAN:
2474 case Instruction::SPUT_BYTE:
2475 case Instruction::SPUT_CHAR:
2476 case Instruction::SPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002477 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002478 return false;
2479 }
2480 break;
2481 }
2482
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002483#define ARRAY_XX(kind, anticipated_type) \
2484 case Instruction::AGET##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00002485 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002486 break; \
2487 } \
2488 case Instruction::APUT##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00002489 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002490 break; \
2491 }
2492
2493 ARRAY_XX(, Primitive::kPrimInt);
2494 ARRAY_XX(_WIDE, Primitive::kPrimLong);
2495 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
2496 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
2497 ARRAY_XX(_BYTE, Primitive::kPrimByte);
2498 ARRAY_XX(_CHAR, Primitive::kPrimChar);
2499 ARRAY_XX(_SHORT, Primitive::kPrimShort);
2500
Nicolas Geoffray39468442014-09-02 15:17:15 +01002501 case Instruction::ARRAY_LENGTH: {
2502 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002503 // No need for a temporary for the null check, it is the only input of the following
2504 // instruction.
Calin Juravle225ff812014-11-13 16:46:39 +00002505 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002506 current_block_->AddInstruction(object);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002507 current_block_->AddInstruction(new (arena_) HArrayLength(object));
2508 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
2509 break;
2510 }
2511
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002512 case Instruction::CONST_STRING: {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002513 current_block_->AddInstruction(
2514 new (arena_) HLoadString(graph_->GetCurrentMethod(), instruction.VRegB_21c(), dex_pc));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002515 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2516 break;
2517 }
2518
2519 case Instruction::CONST_STRING_JUMBO: {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002520 current_block_->AddInstruction(
2521 new (arena_) HLoadString(graph_->GetCurrentMethod(), instruction.VRegB_31c(), dex_pc));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002522 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
2523 break;
2524 }
2525
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002526 case Instruction::CONST_CLASS: {
2527 uint16_t type_index = instruction.VRegB_21c();
2528 bool type_known_final;
2529 bool type_known_abstract;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002530 bool dont_use_is_referrers_class;
2531 // `CanAccessTypeWithoutChecks` will tell whether the method being
2532 // built is trying to access its own class, so that the generated
2533 // code can optimize for this case. However, the optimization does not
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002534 // work for inlining, so we use `IsOutermostCompilingClass` instead.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002535 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
2536 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002537 &type_known_final, &type_known_abstract, &dont_use_is_referrers_class);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002538 if (!can_access) {
Calin Juravle48c2b032014-12-09 18:11:36 +00002539 MaybeRecordStat(MethodCompilationStat::kNotCompiledCantAccesType);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002540 return false;
2541 }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002542 current_block_->AddInstruction(new (arena_) HLoadClass(
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002543 graph_->GetCurrentMethod(),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002544 type_index,
2545 *dex_compilation_unit_->GetDexFile(),
2546 IsOutermostCompilingClass(type_index),
2547 dex_pc));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002548 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2549 break;
2550 }
2551
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002552 case Instruction::MOVE_EXCEPTION: {
2553 current_block_->AddInstruction(new (arena_) HLoadException());
2554 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
2555 break;
2556 }
2557
2558 case Instruction::THROW: {
2559 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00002560 current_block_->AddInstruction(new (arena_) HThrow(exception, dex_pc));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002561 // A throw instruction must branch to the exit block.
2562 current_block_->AddSuccessor(exit_block_);
2563 // We finished building this block. Set the current block to null to avoid
2564 // adding dead instructions to it.
2565 current_block_ = nullptr;
2566 break;
2567 }
2568
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002569 case Instruction::INSTANCE_OF: {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002570 uint8_t destination = instruction.VRegA_22c();
2571 uint8_t reference = instruction.VRegB_22c();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002572 uint16_t type_index = instruction.VRegC_22c();
Calin Juravle225ff812014-11-13 16:46:39 +00002573 if (!BuildTypeCheck(instruction, destination, reference, type_index, dex_pc)) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002574 return false;
2575 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002576 break;
2577 }
2578
2579 case Instruction::CHECK_CAST: {
2580 uint8_t reference = instruction.VRegA_21c();
2581 uint16_t type_index = instruction.VRegB_21c();
Calin Juravle225ff812014-11-13 16:46:39 +00002582 if (!BuildTypeCheck(instruction, -1, reference, type_index, dex_pc)) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002583 return false;
2584 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002585 break;
2586 }
2587
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002588 case Instruction::MONITOR_ENTER: {
2589 current_block_->AddInstruction(new (arena_) HMonitorOperation(
2590 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2591 HMonitorOperation::kEnter,
Calin Juravle225ff812014-11-13 16:46:39 +00002592 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002593 break;
2594 }
2595
2596 case Instruction::MONITOR_EXIT: {
2597 current_block_->AddInstruction(new (arena_) HMonitorOperation(
2598 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2599 HMonitorOperation::kExit,
Calin Juravle225ff812014-11-13 16:46:39 +00002600 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002601 break;
2602 }
2603
Andreas Gamped881df52014-11-24 23:28:39 -08002604 case Instruction::PACKED_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002605 BuildPackedSwitch(instruction, dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08002606 break;
2607 }
2608
Andreas Gampee4d4d322014-12-04 09:09:57 -08002609 case Instruction::SPARSE_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002610 BuildSparseSwitch(instruction, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08002611 break;
2612 }
2613
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002614 default:
Calin Juravle48c2b032014-12-09 18:11:36 +00002615 VLOG(compiler) << "Did not compile "
2616 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
2617 << " because of unhandled instruction "
2618 << instruction.Name();
2619 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnhandledInstruction);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002620 return false;
2621 }
2622 return true;
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00002623} // NOLINT(readability/fn_size)
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002624
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002625HLocal* HGraphBuilder::GetLocalAt(int register_index) const {
2626 return locals_.Get(register_index);
2627}
2628
2629void HGraphBuilder::UpdateLocal(int register_index, HInstruction* instruction) const {
2630 HLocal* local = GetLocalAt(register_index);
2631 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction));
2632}
2633
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002634HInstruction* HGraphBuilder::LoadLocal(int register_index, Primitive::Type type) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002635 HLocal* local = GetLocalAt(register_index);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002636 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type));
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002637 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002638}
2639
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002640} // namespace art