blob: 31d64122f63b9b374b08da0bc26c9569f8adef12 [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:
807 case Instruction::INVOKE_VIRTUAL_RANGE:
808 invoke_type = kVirtual;
809 break;
810 case Instruction::INVOKE_INTERFACE:
811 case Instruction::INVOKE_INTERFACE_RANGE:
812 invoke_type = kInterface;
813 break;
814 case Instruction::INVOKE_SUPER_RANGE:
815 case Instruction::INVOKE_SUPER:
816 invoke_type = kSuper;
817 break;
818 default:
819 LOG(FATAL) << "Unexpected invoke op: " << opcode;
820 return false;
821 }
822
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100823 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
824 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(method_id.proto_idx_);
825 const char* descriptor = dex_file_->StringDataByIdx(proto_id.shorty_idx_);
826 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100827 bool is_instance_call = invoke_type != kStatic;
Nicolas Geoffray2e335252015-06-18 11:11:27 +0100828 // Remove the return type from the 'proto'.
829 size_t number_of_arguments = strlen(descriptor) - 1;
830 if (is_instance_call) {
831 // One extra argument for 'this'.
832 ++number_of_arguments;
833 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100834
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000835 MethodReference target_method(dex_file_, method_idx);
836 uintptr_t direct_code;
837 uintptr_t direct_method;
838 int table_index;
839 InvokeType optimized_invoke_type = invoke_type;
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000840
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000841 if (!compiler_driver_->ComputeInvokeInfo(dex_compilation_unit_, dex_pc, true, true,
842 &optimized_invoke_type, &target_method, &table_index,
843 &direct_code, &direct_method)) {
Nicolas Geoffray2e335252015-06-18 11:11:27 +0100844 VLOG(compiler) << "Did not compile "
845 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
Calin Juravle48c2b032014-12-09 18:11:36 +0000846 << " because a method call could not be resolved";
847 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedMethod);
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000848 return false;
849 }
850 DCHECK(optimized_invoke_type != kSuper);
851
Roland Levillain4c0eb422015-04-24 16:43:49 +0100852 // By default, consider that the called method implicitly requires
853 // an initialization check of its declaring method.
854 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement =
855 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
856 // Potential class initialization check, in the case of a static method call.
857 HClinitCheck* clinit_check = nullptr;
Jeff Hao848f70a2014-01-15 13:49:50 -0800858 // Replace calls to String.<init> with StringFactory.
859 int32_t string_init_offset = 0;
860 bool is_string_init = compiler_driver_->IsStringInit(method_idx, dex_file_, &string_init_offset);
861 if (is_string_init) {
862 return_type = Primitive::kPrimNot;
863 is_instance_call = false;
864 number_of_arguments--;
865 invoke_type = kStatic;
866 optimized_invoke_type = kStatic;
867 }
Roland Levillain4c0eb422015-04-24 16:43:49 +0100868
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000869 HInvoke* invoke = nullptr;
Roland Levillain4c0eb422015-04-24 16:43:49 +0100870
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000871 if (optimized_invoke_type == kVirtual) {
872 invoke = new (arena_) HInvokeVirtual(
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800873 arena_, number_of_arguments, return_type, dex_pc, method_idx, table_index);
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000874 } else if (optimized_invoke_type == kInterface) {
875 invoke = new (arena_) HInvokeInterface(
876 arena_, number_of_arguments, return_type, dex_pc, method_idx, table_index);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100877 } else {
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000878 DCHECK(optimized_invoke_type == kDirect || optimized_invoke_type == kStatic);
879 // Sharpening to kDirect only works if we compile PIC.
880 DCHECK((optimized_invoke_type == invoke_type) || (optimized_invoke_type != kDirect)
881 || compiler_driver_->GetCompilerOptions().GetCompilePic());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000882 bool is_recursive =
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100883 (target_method.dex_method_index == outer_compilation_unit_->GetDexMethodIndex())
884 && (target_method.dex_file == outer_compilation_unit_->GetDexFile());
Roland Levillain4c0eb422015-04-24 16:43:49 +0100885
Jeff Haocad65422015-06-18 21:16:08 -0700886 if (optimized_invoke_type == kStatic && !is_string_init) {
Roland Levillain4c0eb422015-04-24 16:43:49 +0100887 ScopedObjectAccess soa(Thread::Current());
888 StackHandleScope<4> hs(soa.Self());
889 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
890 dex_compilation_unit_->GetClassLinker()->FindDexCache(
891 *dex_compilation_unit_->GetDexFile())));
892 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
893 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700894 ArtMethod* resolved_method = compiler_driver_->ResolveMethod(
895 soa, dex_cache, class_loader, dex_compilation_unit_, method_idx, optimized_invoke_type);
Roland Levillain4c0eb422015-04-24 16:43:49 +0100896
897 if (resolved_method == nullptr) {
898 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedMethod);
899 return false;
900 }
901
902 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
903 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
904 outer_compilation_unit_->GetClassLinker()->FindDexCache(outer_dex_file)));
Nicolas Geoffrayafd06412015-06-20 22:44:47 +0100905 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Roland Levillain4c0eb422015-04-24 16:43:49 +0100906
907 // The index at which the method's class is stored in the DexCache's type array.
908 uint32_t storage_index = DexFile::kDexNoIndex;
Nicolas Geoffrayafd06412015-06-20 22:44:47 +0100909 bool is_outer_class = (resolved_method->GetDeclaringClass() == outer_class.Get());
910 if (is_outer_class) {
911 storage_index = outer_class->GetDexTypeIndex();
Roland Levillain4c0eb422015-04-24 16:43:49 +0100912 } else if (outer_dex_cache.Get() == dex_cache.Get()) {
913 // Get `storage_index` from IsClassOfStaticMethodAvailableToReferrer.
914 compiler_driver_->IsClassOfStaticMethodAvailableToReferrer(outer_dex_cache.Get(),
Nicolas Geoffrayafd06412015-06-20 22:44:47 +0100915 GetCompilingClass(),
Roland Levillain4c0eb422015-04-24 16:43:49 +0100916 resolved_method,
917 method_idx,
918 &storage_index);
919 }
920
Nicolas Geoffrayb783b402015-06-22 11:06:43 +0100921 if (!outer_class->IsInterface()
922 && outer_class->IsSubClass(resolved_method->GetDeclaringClass())) {
Nicolas Geoffrayafd06412015-06-20 22:44:47 +0100923 // If the outer class is the declaring class or a subclass
Roland Levillain5f02c6c2015-04-24 19:14:22 +0100924 // of the declaring class, no class initialization is needed
925 // before the static method call.
Nicolas Geoffrayafd06412015-06-20 22:44:47 +0100926 // Note that in case of inlining, we do not need to add clinit checks
927 // to calls that satisfy this subclass check with any inlined methods. This
928 // will be detected by the optimization passes.
Roland Levillain4c0eb422015-04-24 16:43:49 +0100929 clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
930 } else if (storage_index != DexFile::kDexNoIndex) {
931 // If the method's class type index is available, check
932 // whether we should add an explicit class initialization
933 // check for its declaring class before the static method call.
934
935 // TODO: find out why this check is needed.
936 bool is_in_dex_cache = compiler_driver_->CanAssumeTypeIsPresentInDexCache(
937 *outer_compilation_unit_->GetDexFile(), storage_index);
938 bool is_initialized =
939 resolved_method->GetDeclaringClass()->IsInitialized() && is_in_dex_cache;
940
941 if (is_initialized) {
942 clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
943 } else {
944 clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +0100945 HLoadClass* load_class = new (arena_) HLoadClass(
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100946 graph_->GetCurrentMethod(),
947 storage_index,
948 *dex_compilation_unit_->GetDexFile(),
Nicolas Geoffrayafd06412015-06-20 22:44:47 +0100949 is_outer_class,
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100950 dex_pc);
Roland Levillain4c0eb422015-04-24 16:43:49 +0100951 current_block_->AddInstruction(load_class);
952 clinit_check = new (arena_) HClinitCheck(load_class, dex_pc);
953 current_block_->AddInstruction(clinit_check);
Roland Levillain4c0eb422015-04-24 16:43:49 +0100954 }
955 }
956 }
957
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100958 invoke = new (arena_) HInvokeStaticOrDirect(arena_,
959 number_of_arguments,
960 return_type,
961 dex_pc,
962 target_method.dex_method_index,
963 is_recursive,
964 string_init_offset,
965 invoke_type,
966 optimized_invoke_type,
967 clinit_check_requirement);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100968 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100969
970 size_t start_index = 0;
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000971 Temporaries temps(graph_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100972 if (is_instance_call) {
973 HInstruction* arg = LoadLocal(is_range ? register_index : args[0], Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000974 HNullCheck* null_check = new (arena_) HNullCheck(arg, dex_pc);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100975 current_block_->AddInstruction(null_check);
976 temps.Add(null_check);
977 invoke->SetArgumentAt(0, null_check);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100978 start_index = 1;
979 }
980
Nicolas Geoffray2e335252015-06-18 11:11:27 +0100981 uint32_t descriptor_index = 1; // Skip the return type.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100982 uint32_t argument_index = start_index;
Jeff Hao848f70a2014-01-15 13:49:50 -0800983 if (is_string_init) {
984 start_index = 1;
985 }
Nicolas Geoffray2e335252015-06-18 11:11:27 +0100986 for (size_t i = start_index;
987 // Make sure we don't go over the expected arguments or over the number of
988 // dex registers given. If the instruction was seen as dead by the verifier,
989 // it hasn't been properly checked.
990 (i < number_of_vreg_arguments) && (argument_index < number_of_arguments);
991 i++, argument_index++) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100992 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100993 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
Nicolas Geoffray2e335252015-06-18 11:11:27 +0100994 if (!is_range
995 && is_wide
996 && ((i + 1 == number_of_vreg_arguments) || (args[i] + 1 != args[i + 1]))) {
997 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
998 // reject any class where this is violated. However, the verifier only does these checks
999 // on non trivially dead instructions, so we just bailout the compilation.
1000 VLOG(compiler) << "Did not compile "
1001 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
1002 << " because of non-sequential dex register pair in wide argument";
1003 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1004 return false;
1005 }
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001006 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
1007 invoke->SetArgumentAt(argument_index, arg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001008 if (is_wide) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001009 i++;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001010 }
1011 }
Nicolas Geoffray2e335252015-06-18 11:11:27 +01001012
1013 if (argument_index != number_of_arguments) {
1014 VLOG(compiler) << "Did not compile "
1015 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
1016 << " because of wrong number of arguments in invoke instruction";
1017 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1018 return false;
1019 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001020
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001021 if (invoke->IsInvokeStaticOrDirect()) {
1022 invoke->SetArgumentAt(argument_index, graph_->GetCurrentMethod());
1023 argument_index++;
1024 }
1025
Roland Levillain4c0eb422015-04-24 16:43:49 +01001026 if (clinit_check_requirement == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit) {
1027 // Add the class initialization check as last input of `invoke`.
1028 DCHECK(clinit_check != nullptr);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001029 DCHECK(!is_string_init);
Roland Levillain3e3d7332015-04-28 11:00:54 +01001030 invoke->SetArgumentAt(argument_index, clinit_check);
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001031 argument_index++;
Roland Levillain4c0eb422015-04-24 16:43:49 +01001032 }
1033
Jeff Hao848f70a2014-01-15 13:49:50 -08001034 // Add move-result for StringFactory method.
1035 if (is_string_init) {
1036 uint32_t orig_this_reg = is_range ? register_index : args[0];
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001037 HInstruction* fake_string = LoadLocal(orig_this_reg, Primitive::kPrimNot);
1038 invoke->SetArgumentAt(argument_index, fake_string);
1039 current_block_->AddInstruction(invoke);
1040 PotentiallySimplifyFakeString(orig_this_reg, dex_pc, invoke);
1041 } else {
1042 current_block_->AddInstruction(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001043 }
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001044 latest_result_ = invoke;
1045
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001046 return true;
1047}
1048
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001049bool HGraphBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001050 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001051 bool is_put) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001052 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1053 uint32_t obj_reg = instruction.VRegB_22c();
1054 uint16_t field_index = instruction.VRegC_22c();
1055
1056 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierc7853442015-03-27 14:35:38 -07001057 ArtField* resolved_field =
1058 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001059
Mathieu Chartierc7853442015-03-27 14:35:38 -07001060 if (resolved_field == nullptr) {
Calin Juravle48c2b032014-12-09 18:11:36 +00001061 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedField);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001062 return false;
1063 }
Calin Juravle52c48962014-12-16 17:02:57 +00001064
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001065 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001066
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001067 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00001068 current_block_->AddInstruction(new (arena_) HNullCheck(object, dex_pc));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001069 if (is_put) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001070 Temporaries temps(graph_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001071 HInstruction* null_check = current_block_->GetLastInstruction();
1072 // We need one temporary for the null check.
1073 temps.Add(null_check);
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001074 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001075 current_block_->AddInstruction(new (arena_) HInstanceFieldSet(
1076 null_check,
1077 value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001078 field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00001079 resolved_field->GetOffset(),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001080 resolved_field->IsVolatile(),
1081 field_index,
1082 *dex_file_));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001083 } else {
1084 current_block_->AddInstruction(new (arena_) HInstanceFieldGet(
1085 current_block_->GetLastInstruction(),
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001086 field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00001087 resolved_field->GetOffset(),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001088 resolved_field->IsVolatile(),
1089 field_index,
1090 *dex_file_));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001091
1092 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1093 }
1094 return true;
1095}
1096
Nicolas Geoffray30451742015-06-19 13:32:41 +01001097static mirror::Class* GetClassFrom(CompilerDriver* driver,
1098 const DexCompilationUnit& compilation_unit) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001099 ScopedObjectAccess soa(Thread::Current());
1100 StackHandleScope<2> hs(soa.Self());
Nicolas Geoffray30451742015-06-19 13:32:41 +01001101 const DexFile& dex_file = *compilation_unit.GetDexFile();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001102 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Nicolas Geoffray30451742015-06-19 13:32:41 +01001103 soa.Decode<mirror::ClassLoader*>(compilation_unit.GetClassLoader())));
1104 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
1105 compilation_unit.GetClassLinker()->FindDexCache(dex_file)));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001106
Nicolas Geoffray30451742015-06-19 13:32:41 +01001107 return driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, &compilation_unit);
1108}
1109
1110mirror::Class* HGraphBuilder::GetOutermostCompilingClass() const {
1111 return GetClassFrom(compiler_driver_, *outer_compilation_unit_);
1112}
1113
1114mirror::Class* HGraphBuilder::GetCompilingClass() const {
1115 return GetClassFrom(compiler_driver_, *dex_compilation_unit_);
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001116}
1117
1118bool HGraphBuilder::IsOutermostCompilingClass(uint16_t type_index) const {
1119 ScopedObjectAccess soa(Thread::Current());
1120 StackHandleScope<4> hs(soa.Self());
1121 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
1122 dex_compilation_unit_->GetClassLinker()->FindDexCache(*dex_compilation_unit_->GetDexFile())));
1123 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1124 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
1125 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
1126 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
Nicolas Geoffrayafd06412015-06-20 22:44:47 +01001127 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001128
Nicolas Geoffrayafd06412015-06-20 22:44:47 +01001129 return outer_class.Get() == cls.Get();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001130}
1131
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001132bool HGraphBuilder::BuildStaticFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001133 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001134 bool is_put) {
1135 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1136 uint16_t field_index = instruction.VRegB_21c();
1137
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001138 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierc7853442015-03-27 14:35:38 -07001139 StackHandleScope<4> hs(soa.Self());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001140 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
1141 dex_compilation_unit_->GetClassLinker()->FindDexCache(*dex_compilation_unit_->GetDexFile())));
1142 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1143 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
Mathieu Chartierc7853442015-03-27 14:35:38 -07001144 ArtField* resolved_field = compiler_driver_->ResolveField(
1145 soa, dex_cache, class_loader, dex_compilation_unit_, field_index, true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001146
Mathieu Chartierc7853442015-03-27 14:35:38 -07001147 if (resolved_field == nullptr) {
Calin Juravle48c2b032014-12-09 18:11:36 +00001148 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedField);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001149 return false;
1150 }
1151
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001152 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
1153 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
1154 outer_compilation_unit_->GetClassLinker()->FindDexCache(outer_dex_file)));
Nicolas Geoffray30451742015-06-19 13:32:41 +01001155 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001156
1157 // The index at which the field's class is stored in the DexCache's type array.
1158 uint32_t storage_index;
Nicolas Geoffray30451742015-06-19 13:32:41 +01001159 bool is_outer_class = (outer_class.Get() == resolved_field->GetDeclaringClass());
1160 if (is_outer_class) {
1161 storage_index = outer_class->GetDexTypeIndex();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001162 } else if (outer_dex_cache.Get() != dex_cache.Get()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001163 // The compiler driver cannot currently understand multiple dex caches involved. Just bailout.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001164 return false;
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001165 } else {
1166 std::pair<bool, bool> pair = compiler_driver_->IsFastStaticField(
1167 outer_dex_cache.Get(),
Nicolas Geoffray30451742015-06-19 13:32:41 +01001168 GetCompilingClass(),
Mathieu Chartierc7853442015-03-27 14:35:38 -07001169 resolved_field,
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001170 field_index,
1171 &storage_index);
1172 bool can_easily_access = is_put ? pair.second : pair.first;
1173 if (!can_easily_access) {
1174 return false;
1175 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001176 }
1177
1178 // TODO: find out why this check is needed.
1179 bool is_in_dex_cache = compiler_driver_->CanAssumeTypeIsPresentInDexCache(
Nicolas Geoffray6a816cf2015-03-24 16:17:56 +00001180 *outer_compilation_unit_->GetDexFile(), storage_index);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001181 bool is_initialized = resolved_field->GetDeclaringClass()->IsInitialized() && is_in_dex_cache;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001182
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001183 HLoadClass* constant = new (arena_) HLoadClass(graph_->GetCurrentMethod(),
1184 storage_index,
1185 *dex_compilation_unit_->GetDexFile(),
Nicolas Geoffray30451742015-06-19 13:32:41 +01001186 is_outer_class,
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001187 dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001188 current_block_->AddInstruction(constant);
1189
1190 HInstruction* cls = constant;
Nicolas Geoffray30451742015-06-19 13:32:41 +01001191 if (!is_initialized && !is_outer_class) {
Calin Juravle225ff812014-11-13 16:46:39 +00001192 cls = new (arena_) HClinitCheck(constant, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001193 current_block_->AddInstruction(cls);
1194 }
1195
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001196 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001197 if (is_put) {
1198 // We need to keep the class alive before loading the value.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001199 Temporaries temps(graph_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001200 temps.Add(cls);
1201 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1202 DCHECK_EQ(value->GetType(), field_type);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001203 current_block_->AddInstruction(new (arena_) HStaticFieldSet(cls,
1204 value,
1205 field_type,
1206 resolved_field->GetOffset(),
1207 resolved_field->IsVolatile(),
1208 field_index,
1209 *dex_file_));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001210 } else {
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001211 current_block_->AddInstruction(new (arena_) HStaticFieldGet(cls,
1212 field_type,
1213 resolved_field->GetOffset(),
1214 resolved_field->IsVolatile(),
1215 field_index,
1216 *dex_file_));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001217 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1218 }
1219 return true;
1220}
1221
Calin Juravlebacfec32014-11-14 15:54:36 +00001222void HGraphBuilder::BuildCheckedDivRem(uint16_t out_vreg,
1223 uint16_t first_vreg,
1224 int64_t second_vreg_or_constant,
1225 uint32_t dex_pc,
1226 Primitive::Type type,
1227 bool second_is_constant,
1228 bool isDiv) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001229 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Calin Juravled0d48522014-11-04 16:40:20 +00001230
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001231 HInstruction* first = LoadLocal(first_vreg, type);
1232 HInstruction* second = nullptr;
1233 if (second_is_constant) {
1234 if (type == Primitive::kPrimInt) {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001235 second = graph_->GetIntConstant(second_vreg_or_constant);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001236 } else {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001237 second = graph_->GetLongConstant(second_vreg_or_constant);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001238 }
1239 } else {
1240 second = LoadLocal(second_vreg_or_constant, type);
1241 }
1242
1243 if (!second_is_constant
1244 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0)
1245 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) {
1246 second = new (arena_) HDivZeroCheck(second, dex_pc);
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001247 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +00001248 current_block_->AddInstruction(second);
1249 temps.Add(current_block_->GetLastInstruction());
1250 }
1251
Calin Juravlebacfec32014-11-14 15:54:36 +00001252 if (isDiv) {
1253 current_block_->AddInstruction(new (arena_) HDiv(type, first, second, dex_pc));
1254 } else {
1255 current_block_->AddInstruction(new (arena_) HRem(type, first, second, dex_pc));
1256 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001257 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
Calin Juravled0d48522014-11-04 16:40:20 +00001258}
1259
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001260void HGraphBuilder::BuildArrayAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001261 uint32_t dex_pc,
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001262 bool is_put,
1263 Primitive::Type anticipated_type) {
1264 uint8_t source_or_dest_reg = instruction.VRegA_23x();
1265 uint8_t array_reg = instruction.VRegB_23x();
1266 uint8_t index_reg = instruction.VRegC_23x();
1267
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001268 // We need one temporary for the null check, one for the index, and one for the length.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001269 Temporaries temps(graph_);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001270
1271 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00001272 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001273 current_block_->AddInstruction(object);
1274 temps.Add(object);
1275
1276 HInstruction* length = new (arena_) HArrayLength(object);
1277 current_block_->AddInstruction(length);
1278 temps.Add(length);
1279 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt);
Calin Juravle225ff812014-11-13 16:46:39 +00001280 index = new (arena_) HBoundsCheck(index, length, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001281 current_block_->AddInstruction(index);
1282 temps.Add(index);
1283 if (is_put) {
1284 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
1285 // TODO: Insert a type check node if the type is Object.
Nicolas Geoffray39468442014-09-02 15:17:15 +01001286 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001287 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001288 } else {
1289 current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type));
1290 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1291 }
Mark Mendell1152c922015-04-24 17:06:35 -04001292 graph_->SetHasBoundsChecks(true);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001293}
1294
Calin Juravle225ff812014-11-13 16:46:39 +00001295void HGraphBuilder::BuildFilledNewArray(uint32_t dex_pc,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001296 uint32_t type_index,
1297 uint32_t number_of_vreg_arguments,
1298 bool is_range,
1299 uint32_t* args,
1300 uint32_t register_index) {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001301 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments);
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001302 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
1303 ? kQuickAllocArrayWithAccessCheck
1304 : kQuickAllocArray;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01001305 HInstruction* object = new (arena_) HNewArray(length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01001306 graph_->GetCurrentMethod(),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01001307 dex_pc,
1308 type_index,
1309 *dex_compilation_unit_->GetDexFile(),
1310 entrypoint);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001311 current_block_->AddInstruction(object);
1312
1313 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1314 DCHECK_EQ(descriptor[0], '[') << descriptor;
1315 char primitive = descriptor[1];
1316 DCHECK(primitive == 'I'
1317 || primitive == 'L'
1318 || primitive == '[') << descriptor;
1319 bool is_reference_array = (primitive == 'L') || (primitive == '[');
1320 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
1321
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001322 Temporaries temps(graph_);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001323 temps.Add(object);
1324 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
1325 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
David Brazdil8d5b8b22015-03-24 10:51:52 +00001326 HInstruction* index = graph_->GetIntConstant(i);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001327 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001328 new (arena_) HArraySet(object, index, value, type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001329 }
1330 latest_result_ = object;
1331}
1332
1333template <typename T>
1334void HGraphBuilder::BuildFillArrayData(HInstruction* object,
1335 const T* data,
1336 uint32_t element_count,
1337 Primitive::Type anticipated_type,
Calin Juravle225ff812014-11-13 16:46:39 +00001338 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001339 for (uint32_t i = 0; i < element_count; ++i) {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001340 HInstruction* index = graph_->GetIntConstant(i);
1341 HInstruction* value = graph_->GetIntConstant(data[i]);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001342 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001343 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001344 }
1345}
1346
Calin Juravle225ff812014-11-13 16:46:39 +00001347void HGraphBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001348 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +00001349 HInstruction* array = LoadLocal(instruction.VRegA_31t(), Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00001350 HNullCheck* null_check = new (arena_) HNullCheck(array, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001351 current_block_->AddInstruction(null_check);
1352 temps.Add(null_check);
1353
1354 HInstruction* length = new (arena_) HArrayLength(null_check);
1355 current_block_->AddInstruction(length);
1356
Calin Juravle225ff812014-11-13 16:46:39 +00001357 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
Calin Juravled0d48522014-11-04 16:40:20 +00001358 const Instruction::ArrayDataPayload* payload =
1359 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_start_ + payload_offset);
1360 const uint8_t* data = payload->data;
1361 uint32_t element_count = payload->element_count;
1362
1363 // Implementation of this DEX instruction seems to be that the bounds check is
1364 // done before doing any stores.
David Brazdil8d5b8b22015-03-24 10:51:52 +00001365 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1);
Calin Juravle225ff812014-11-13 16:46:39 +00001366 current_block_->AddInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
Calin Juravled0d48522014-11-04 16:40:20 +00001367
1368 switch (payload->element_width) {
1369 case 1:
1370 BuildFillArrayData(null_check,
1371 reinterpret_cast<const int8_t*>(data),
1372 element_count,
1373 Primitive::kPrimByte,
Calin Juravle225ff812014-11-13 16:46:39 +00001374 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001375 break;
1376 case 2:
1377 BuildFillArrayData(null_check,
1378 reinterpret_cast<const int16_t*>(data),
1379 element_count,
1380 Primitive::kPrimShort,
Calin Juravle225ff812014-11-13 16:46:39 +00001381 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001382 break;
1383 case 4:
1384 BuildFillArrayData(null_check,
1385 reinterpret_cast<const int32_t*>(data),
1386 element_count,
1387 Primitive::kPrimInt,
Calin Juravle225ff812014-11-13 16:46:39 +00001388 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001389 break;
1390 case 8:
1391 BuildFillWideArrayData(null_check,
1392 reinterpret_cast<const int64_t*>(data),
1393 element_count,
Calin Juravle225ff812014-11-13 16:46:39 +00001394 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001395 break;
1396 default:
1397 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1398 }
Mark Mendell1152c922015-04-24 17:06:35 -04001399 graph_->SetHasBoundsChecks(true);
Calin Juravled0d48522014-11-04 16:40:20 +00001400}
1401
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001402void HGraphBuilder::BuildFillWideArrayData(HInstruction* object,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +01001403 const int64_t* data,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001404 uint32_t element_count,
Calin Juravle225ff812014-11-13 16:46:39 +00001405 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001406 for (uint32_t i = 0; i < element_count; ++i) {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001407 HInstruction* index = graph_->GetIntConstant(i);
1408 HInstruction* value = graph_->GetLongConstant(data[i]);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001409 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001410 object, index, value, Primitive::kPrimLong, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001411 }
1412}
1413
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001414bool HGraphBuilder::BuildTypeCheck(const Instruction& instruction,
1415 uint8_t destination,
1416 uint8_t reference,
1417 uint16_t type_index,
Calin Juravle225ff812014-11-13 16:46:39 +00001418 uint32_t dex_pc) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001419 bool type_known_final;
1420 bool type_known_abstract;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001421 // `CanAccessTypeWithoutChecks` will tell whether the method being
1422 // built is trying to access its own class, so that the generated
1423 // code can optimize for this case. However, the optimization does not
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001424 // work for inlining, so we use `IsOutermostCompilingClass` instead.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001425 bool dont_use_is_referrers_class;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001426 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
1427 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001428 &type_known_final, &type_known_abstract, &dont_use_is_referrers_class);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001429 if (!can_access) {
Calin Juravle48c2b032014-12-09 18:11:36 +00001430 MaybeRecordStat(MethodCompilationStat::kNotCompiledCantAccesType);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001431 return false;
1432 }
1433 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot);
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001434 HLoadClass* cls = new (arena_) HLoadClass(
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001435 graph_->GetCurrentMethod(),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01001436 type_index,
1437 *dex_compilation_unit_->GetDexFile(),
1438 IsOutermostCompilingClass(type_index),
1439 dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001440 current_block_->AddInstruction(cls);
1441 // The class needs a temporary before being used by the type check.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001442 Temporaries temps(graph_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001443 temps.Add(cls);
1444 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
1445 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001446 new (arena_) HInstanceOf(object, cls, type_known_final, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001447 UpdateLocal(destination, current_block_->GetLastInstruction());
1448 } else {
1449 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
1450 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001451 new (arena_) HCheckCast(object, cls, type_known_final, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001452 }
1453 return true;
1454}
1455
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001456bool HGraphBuilder::NeedsAccessCheck(uint32_t type_index) const {
1457 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
1458 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index);
1459}
1460
Calin Juravle48c2b032014-12-09 18:11:36 +00001461void HGraphBuilder::BuildPackedSwitch(const Instruction& instruction, uint32_t dex_pc) {
David Brazdil2ef645b2015-06-17 18:20:52 +01001462 // Verifier guarantees that the payload for PackedSwitch contains:
1463 // (a) number of entries (may be zero)
1464 // (b) first and lowest switch case value (entry 0, always present)
1465 // (c) list of target pcs (entries 1 <= i <= N)
Andreas Gamped881df52014-11-24 23:28:39 -08001466 SwitchTable table(instruction, dex_pc, false);
1467
1468 // Value to test against.
1469 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
1470
David Brazdil2ef645b2015-06-17 18:20:52 +01001471 // Retrieve number of entries.
Andreas Gampee4d4d322014-12-04 09:09:57 -08001472 uint16_t num_entries = table.GetNumEntries();
David Brazdil2ef645b2015-06-17 18:20:52 +01001473 if (num_entries == 0) {
1474 return;
1475 }
Andreas Gampee4d4d322014-12-04 09:09:57 -08001476
Andreas Gamped881df52014-11-24 23:28:39 -08001477 // Chained cmp-and-branch, starting from starting_key.
1478 int32_t starting_key = table.GetEntryAt(0);
1479
Andreas Gamped881df52014-11-24 23:28:39 -08001480 for (size_t i = 1; i <= num_entries; i++) {
Andreas Gampee4d4d322014-12-04 09:09:57 -08001481 BuildSwitchCaseHelper(instruction, i, i == num_entries, table, value, starting_key + i - 1,
1482 table.GetEntryAt(i), dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08001483 }
Andreas Gamped881df52014-11-24 23:28:39 -08001484}
1485
Calin Juravle48c2b032014-12-09 18:11:36 +00001486void HGraphBuilder::BuildSparseSwitch(const Instruction& instruction, uint32_t dex_pc) {
David Brazdil2ef645b2015-06-17 18:20:52 +01001487 // Verifier guarantees that the payload for SparseSwitch contains:
1488 // (a) number of entries (may be zero)
1489 // (b) sorted key values (entries 0 <= i < N)
1490 // (c) target pcs corresponding to the switch values (entries N <= i < 2*N)
Andreas Gampee4d4d322014-12-04 09:09:57 -08001491 SwitchTable table(instruction, dex_pc, true);
1492
1493 // Value to test against.
1494 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
1495
1496 uint16_t num_entries = table.GetNumEntries();
Andreas Gampee4d4d322014-12-04 09:09:57 -08001497
1498 for (size_t i = 0; i < num_entries; i++) {
1499 BuildSwitchCaseHelper(instruction, i, i == static_cast<size_t>(num_entries) - 1, table, value,
1500 table.GetEntryAt(i), table.GetEntryAt(i + num_entries), dex_pc);
1501 }
Andreas Gampee4d4d322014-12-04 09:09:57 -08001502}
1503
1504void HGraphBuilder::BuildSwitchCaseHelper(const Instruction& instruction, size_t index,
1505 bool is_last_case, const SwitchTable& table,
1506 HInstruction* value, int32_t case_value_int,
1507 int32_t target_offset, uint32_t dex_pc) {
David Brazdil852eaff2015-02-02 15:23:05 +00001508 HBasicBlock* case_target = FindBlockStartingAt(dex_pc + target_offset);
1509 DCHECK(case_target != nullptr);
1510 PotentiallyAddSuspendCheck(case_target, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001511
1512 // The current case's value.
David Brazdil8d5b8b22015-03-24 10:51:52 +00001513 HInstruction* this_case_value = graph_->GetIntConstant(case_value_int);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001514
1515 // Compare value and this_case_value.
1516 HEqual* comparison = new (arena_) HEqual(value, this_case_value);
1517 current_block_->AddInstruction(comparison);
1518 HInstruction* ifinst = new (arena_) HIf(comparison);
1519 current_block_->AddInstruction(ifinst);
1520
1521 // Case hit: use the target offset to determine where to go.
Andreas Gampee4d4d322014-12-04 09:09:57 -08001522 current_block_->AddSuccessor(case_target);
1523
1524 // Case miss: go to the next case (or default fall-through).
1525 // When there is a next case, we use the block stored with the table offset representing this
1526 // case (that is where we registered them in ComputeBranchTargets).
1527 // When there is no next case, we use the following instruction.
1528 // TODO: Find a good way to peel the last iteration to avoid conditional, but still have re-use.
1529 if (!is_last_case) {
1530 HBasicBlock* next_case_target = FindBlockStartingAt(table.GetDexPcForIndex(index));
1531 DCHECK(next_case_target != nullptr);
1532 current_block_->AddSuccessor(next_case_target);
1533
1534 // Need to manually add the block, as there is no dex-pc transition for the cases.
1535 graph_->AddBlock(next_case_target);
1536
1537 current_block_ = next_case_target;
1538 } else {
1539 HBasicBlock* default_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
1540 DCHECK(default_target != nullptr);
1541 current_block_->AddSuccessor(default_target);
1542 current_block_ = nullptr;
1543 }
1544}
1545
David Brazdil852eaff2015-02-02 15:23:05 +00001546void HGraphBuilder::PotentiallyAddSuspendCheck(HBasicBlock* target, uint32_t dex_pc) {
1547 int32_t target_offset = target->GetDexPc() - dex_pc;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001548 if (target_offset <= 0) {
David Brazdil852eaff2015-02-02 15:23:05 +00001549 // DX generates back edges to the first encountered return. We can save
1550 // time of later passes by not adding redundant suspend checks.
David Brazdil2fd6aa52015-02-02 18:58:27 +00001551 HInstruction* last_in_target = target->GetLastInstruction();
1552 if (last_in_target != nullptr &&
1553 (last_in_target->IsReturn() || last_in_target->IsReturnVoid())) {
1554 return;
David Brazdil852eaff2015-02-02 15:23:05 +00001555 }
1556
1557 // Add a suspend check to backward branches which may potentially loop. We
1558 // can remove them after we recognize loops in the graph.
Calin Juravle225ff812014-11-13 16:46:39 +00001559 current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_pc));
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001560 }
1561}
1562
Calin Juravle225ff812014-11-13 16:46:39 +00001563bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001564 if (current_block_ == nullptr) {
1565 return true; // Dead code
1566 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001567
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001568 switch (instruction.Opcode()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001569 case Instruction::CONST_4: {
1570 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001571 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001572 UpdateLocal(register_index, constant);
1573 break;
1574 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001575
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001576 case Instruction::CONST_16: {
1577 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001578 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001579 UpdateLocal(register_index, constant);
1580 break;
1581 }
1582
Dave Allison20dfc792014-06-16 20:44:29 -07001583 case Instruction::CONST: {
1584 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001585 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i());
Dave Allison20dfc792014-06-16 20:44:29 -07001586 UpdateLocal(register_index, constant);
1587 break;
1588 }
1589
1590 case Instruction::CONST_HIGH16: {
1591 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001592 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16);
Dave Allison20dfc792014-06-16 20:44:29 -07001593 UpdateLocal(register_index, constant);
1594 break;
1595 }
1596
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001597 case Instruction::CONST_WIDE_16: {
1598 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001599 // Get 16 bits of constant value, sign extended to 64 bits.
1600 int64_t value = instruction.VRegB_21s();
1601 value <<= 48;
1602 value >>= 48;
David Brazdil8d5b8b22015-03-24 10:51:52 +00001603 HLongConstant* constant = graph_->GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001604 UpdateLocal(register_index, constant);
1605 break;
1606 }
1607
1608 case Instruction::CONST_WIDE_32: {
1609 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001610 // Get 32 bits of constant value, sign extended to 64 bits.
1611 int64_t value = instruction.VRegB_31i();
1612 value <<= 32;
1613 value >>= 32;
David Brazdil8d5b8b22015-03-24 10:51:52 +00001614 HLongConstant* constant = graph_->GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001615 UpdateLocal(register_index, constant);
1616 break;
1617 }
1618
1619 case Instruction::CONST_WIDE: {
1620 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001621 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001622 UpdateLocal(register_index, constant);
1623 break;
1624 }
1625
Dave Allison20dfc792014-06-16 20:44:29 -07001626 case Instruction::CONST_WIDE_HIGH16: {
1627 int32_t register_index = instruction.VRegA();
1628 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
David Brazdil8d5b8b22015-03-24 10:51:52 +00001629 HLongConstant* constant = graph_->GetLongConstant(value);
Dave Allison20dfc792014-06-16 20:44:29 -07001630 UpdateLocal(register_index, constant);
1631 break;
1632 }
1633
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001634 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001635 case Instruction::MOVE:
1636 case Instruction::MOVE_FROM16:
1637 case Instruction::MOVE_16: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001638 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001639 UpdateLocal(instruction.VRegA(), value);
1640 break;
1641 }
1642
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001643 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001644 case Instruction::MOVE_WIDE:
1645 case Instruction::MOVE_WIDE_FROM16:
1646 case Instruction::MOVE_WIDE_16: {
1647 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong);
1648 UpdateLocal(instruction.VRegA(), value);
1649 break;
1650 }
1651
1652 case Instruction::MOVE_OBJECT:
1653 case Instruction::MOVE_OBJECT_16:
1654 case Instruction::MOVE_OBJECT_FROM16: {
1655 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot);
1656 UpdateLocal(instruction.VRegA(), value);
1657 break;
1658 }
1659
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001660 case Instruction::RETURN_VOID: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001661 BuildReturn(instruction, Primitive::kPrimVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001662 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001663 }
1664
Dave Allison20dfc792014-06-16 20:44:29 -07001665#define IF_XX(comparison, cond) \
Calin Juravle225ff812014-11-13 16:46:39 +00001666 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
1667 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001668
Dave Allison20dfc792014-06-16 20:44:29 -07001669 IF_XX(HEqual, EQ);
1670 IF_XX(HNotEqual, NE);
1671 IF_XX(HLessThan, LT);
1672 IF_XX(HLessThanOrEqual, LE);
1673 IF_XX(HGreaterThan, GT);
1674 IF_XX(HGreaterThanOrEqual, GE);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001675
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001676 case Instruction::GOTO:
1677 case Instruction::GOTO_16:
1678 case Instruction::GOTO_32: {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001679 int32_t offset = instruction.GetTargetOffset();
Calin Juravle225ff812014-11-13 16:46:39 +00001680 HBasicBlock* target = FindBlockStartingAt(offset + dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001681 DCHECK(target != nullptr);
David Brazdil852eaff2015-02-02 15:23:05 +00001682 PotentiallyAddSuspendCheck(target, dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001683 current_block_->AddInstruction(new (arena_) HGoto());
1684 current_block_->AddSuccessor(target);
1685 current_block_ = nullptr;
1686 break;
1687 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001688
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001689 case Instruction::RETURN: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001690 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001691 break;
1692 }
1693
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001694 case Instruction::RETURN_OBJECT: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001695 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001696 break;
1697 }
1698
1699 case Instruction::RETURN_WIDE: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001700 BuildReturn(instruction, return_type_);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001701 break;
1702 }
1703
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001704 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001705 case Instruction::INVOKE_INTERFACE:
1706 case Instruction::INVOKE_STATIC:
1707 case Instruction::INVOKE_SUPER:
1708 case Instruction::INVOKE_VIRTUAL: {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001709 uint32_t method_idx = instruction.VRegB_35c();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001710 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001711 uint32_t args[5];
Ian Rogers29a26482014-05-02 15:27:29 -07001712 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00001713 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001714 number_of_vreg_arguments, false, args, -1)) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001715 return false;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001716 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001717 break;
1718 }
1719
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001720 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001721 case Instruction::INVOKE_INTERFACE_RANGE:
1722 case Instruction::INVOKE_STATIC_RANGE:
1723 case Instruction::INVOKE_SUPER_RANGE:
1724 case Instruction::INVOKE_VIRTUAL_RANGE: {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001725 uint32_t method_idx = instruction.VRegB_3rc();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001726 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1727 uint32_t register_index = instruction.VRegC();
Calin Juravle225ff812014-11-13 16:46:39 +00001728 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001729 number_of_vreg_arguments, true, nullptr, register_index)) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001730 return false;
1731 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001732 break;
1733 }
1734
Roland Levillain88cb1752014-10-20 16:36:47 +01001735 case Instruction::NEG_INT: {
1736 Unop_12x<HNeg>(instruction, Primitive::kPrimInt);
1737 break;
1738 }
1739
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001740 case Instruction::NEG_LONG: {
1741 Unop_12x<HNeg>(instruction, Primitive::kPrimLong);
1742 break;
1743 }
1744
Roland Levillain3dbcb382014-10-28 17:30:07 +00001745 case Instruction::NEG_FLOAT: {
1746 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat);
1747 break;
1748 }
1749
1750 case Instruction::NEG_DOUBLE: {
1751 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble);
1752 break;
1753 }
1754
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001755 case Instruction::NOT_INT: {
1756 Unop_12x<HNot>(instruction, Primitive::kPrimInt);
1757 break;
1758 }
1759
Roland Levillain70566432014-10-24 16:20:17 +01001760 case Instruction::NOT_LONG: {
1761 Unop_12x<HNot>(instruction, Primitive::kPrimLong);
1762 break;
1763 }
1764
Roland Levillaindff1f282014-11-05 14:15:05 +00001765 case Instruction::INT_TO_LONG: {
Roland Levillain624279f2014-12-04 11:54:28 +00001766 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong, dex_pc);
Roland Levillaindff1f282014-11-05 14:15:05 +00001767 break;
1768 }
1769
Roland Levillaincff13742014-11-17 14:32:17 +00001770 case Instruction::INT_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001771 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00001772 break;
1773 }
1774
1775 case Instruction::INT_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001776 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00001777 break;
1778 }
1779
Roland Levillain946e1432014-11-11 17:35:19 +00001780 case Instruction::LONG_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001781 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt, dex_pc);
Roland Levillain946e1432014-11-11 17:35:19 +00001782 break;
1783 }
1784
Roland Levillain6d0e4832014-11-27 18:31:21 +00001785 case Instruction::LONG_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001786 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimFloat, dex_pc);
Roland Levillain6d0e4832014-11-27 18:31:21 +00001787 break;
1788 }
1789
Roland Levillain647b9ed2014-11-27 12:06:00 +00001790 case Instruction::LONG_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001791 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble, dex_pc);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001792 break;
1793 }
1794
Roland Levillain3f8f9362014-12-02 17:45:01 +00001795 case Instruction::FLOAT_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001796 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimInt, dex_pc);
1797 break;
1798 }
1799
1800 case Instruction::FLOAT_TO_LONG: {
1801 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimLong, dex_pc);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001802 break;
1803 }
1804
Roland Levillain8964e2b2014-12-04 12:10:50 +00001805 case Instruction::FLOAT_TO_DOUBLE: {
1806 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimDouble, dex_pc);
1807 break;
1808 }
1809
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001810 case Instruction::DOUBLE_TO_INT: {
1811 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimInt, dex_pc);
1812 break;
1813 }
1814
1815 case Instruction::DOUBLE_TO_LONG: {
1816 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimLong, dex_pc);
1817 break;
1818 }
1819
Roland Levillain8964e2b2014-12-04 12:10:50 +00001820 case Instruction::DOUBLE_TO_FLOAT: {
1821 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimFloat, dex_pc);
1822 break;
1823 }
1824
Roland Levillain51d3fc42014-11-13 14:11:42 +00001825 case Instruction::INT_TO_BYTE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001826 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte, dex_pc);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001827 break;
1828 }
1829
Roland Levillain01a8d712014-11-14 16:27:39 +00001830 case Instruction::INT_TO_SHORT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001831 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort, dex_pc);
Roland Levillain01a8d712014-11-14 16:27:39 +00001832 break;
1833 }
1834
Roland Levillain981e4542014-11-14 11:47:14 +00001835 case Instruction::INT_TO_CHAR: {
Roland Levillain624279f2014-12-04 11:54:28 +00001836 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar, dex_pc);
Roland Levillain981e4542014-11-14 11:47:14 +00001837 break;
1838 }
1839
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001840 case Instruction::ADD_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001841 Binop_23x<HAdd>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001842 break;
1843 }
1844
1845 case Instruction::ADD_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001846 Binop_23x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001847 break;
1848 }
1849
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001850 case Instruction::ADD_DOUBLE: {
1851 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble);
1852 break;
1853 }
1854
1855 case Instruction::ADD_FLOAT: {
1856 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat);
1857 break;
1858 }
1859
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001860 case Instruction::SUB_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001861 Binop_23x<HSub>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001862 break;
1863 }
1864
1865 case Instruction::SUB_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001866 Binop_23x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001867 break;
1868 }
1869
Calin Juravle096cc022014-10-23 17:01:13 +01001870 case Instruction::SUB_FLOAT: {
1871 Binop_23x<HSub>(instruction, Primitive::kPrimFloat);
1872 break;
1873 }
1874
1875 case Instruction::SUB_DOUBLE: {
1876 Binop_23x<HSub>(instruction, Primitive::kPrimDouble);
1877 break;
1878 }
1879
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001880 case Instruction::ADD_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001881 Binop_12x<HAdd>(instruction, Primitive::kPrimInt);
1882 break;
1883 }
1884
Calin Juravle34bacdf2014-10-07 20:23:36 +01001885 case Instruction::MUL_INT: {
1886 Binop_23x<HMul>(instruction, Primitive::kPrimInt);
1887 break;
1888 }
1889
1890 case Instruction::MUL_LONG: {
1891 Binop_23x<HMul>(instruction, Primitive::kPrimLong);
1892 break;
1893 }
1894
Calin Juravleb5bfa962014-10-21 18:02:24 +01001895 case Instruction::MUL_FLOAT: {
1896 Binop_23x<HMul>(instruction, Primitive::kPrimFloat);
1897 break;
1898 }
1899
1900 case Instruction::MUL_DOUBLE: {
1901 Binop_23x<HMul>(instruction, Primitive::kPrimDouble);
1902 break;
1903 }
1904
Calin Juravled0d48522014-11-04 16:40:20 +00001905 case Instruction::DIV_INT: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001906 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1907 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravled0d48522014-11-04 16:40:20 +00001908 break;
1909 }
1910
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001911 case Instruction::DIV_LONG: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001912 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1913 dex_pc, Primitive::kPrimLong, false, true);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001914 break;
1915 }
1916
Calin Juravle7c4954d2014-10-28 16:57:40 +00001917 case Instruction::DIV_FLOAT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001918 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001919 break;
1920 }
1921
1922 case Instruction::DIV_DOUBLE: {
Calin Juravle225ff812014-11-13 16:46:39 +00001923 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001924 break;
1925 }
1926
Calin Juravlebacfec32014-11-14 15:54:36 +00001927 case Instruction::REM_INT: {
1928 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1929 dex_pc, Primitive::kPrimInt, false, false);
1930 break;
1931 }
1932
1933 case Instruction::REM_LONG: {
1934 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1935 dex_pc, Primitive::kPrimLong, false, false);
1936 break;
1937 }
1938
Calin Juravled2ec87d2014-12-08 14:24:46 +00001939 case Instruction::REM_FLOAT: {
1940 Binop_23x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
1941 break;
1942 }
1943
1944 case Instruction::REM_DOUBLE: {
1945 Binop_23x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
1946 break;
1947 }
1948
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001949 case Instruction::AND_INT: {
1950 Binop_23x<HAnd>(instruction, Primitive::kPrimInt);
1951 break;
1952 }
1953
1954 case Instruction::AND_LONG: {
1955 Binop_23x<HAnd>(instruction, Primitive::kPrimLong);
1956 break;
1957 }
1958
Calin Juravle9aec02f2014-11-18 23:06:35 +00001959 case Instruction::SHL_INT: {
1960 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt);
1961 break;
1962 }
1963
1964 case Instruction::SHL_LONG: {
1965 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong);
1966 break;
1967 }
1968
1969 case Instruction::SHR_INT: {
1970 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt);
1971 break;
1972 }
1973
1974 case Instruction::SHR_LONG: {
1975 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong);
1976 break;
1977 }
1978
1979 case Instruction::USHR_INT: {
1980 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt);
1981 break;
1982 }
1983
1984 case Instruction::USHR_LONG: {
1985 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong);
1986 break;
1987 }
1988
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001989 case Instruction::OR_INT: {
1990 Binop_23x<HOr>(instruction, Primitive::kPrimInt);
1991 break;
1992 }
1993
1994 case Instruction::OR_LONG: {
1995 Binop_23x<HOr>(instruction, Primitive::kPrimLong);
1996 break;
1997 }
1998
1999 case Instruction::XOR_INT: {
2000 Binop_23x<HXor>(instruction, Primitive::kPrimInt);
2001 break;
2002 }
2003
2004 case Instruction::XOR_LONG: {
2005 Binop_23x<HXor>(instruction, Primitive::kPrimLong);
2006 break;
2007 }
2008
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002009 case Instruction::ADD_LONG_2ADDR: {
2010 Binop_12x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002011 break;
2012 }
2013
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002014 case Instruction::ADD_DOUBLE_2ADDR: {
2015 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble);
2016 break;
2017 }
2018
2019 case Instruction::ADD_FLOAT_2ADDR: {
2020 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat);
2021 break;
2022 }
2023
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002024 case Instruction::SUB_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002025 Binop_12x<HSub>(instruction, Primitive::kPrimInt);
2026 break;
2027 }
2028
2029 case Instruction::SUB_LONG_2ADDR: {
2030 Binop_12x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002031 break;
2032 }
2033
Calin Juravle096cc022014-10-23 17:01:13 +01002034 case Instruction::SUB_FLOAT_2ADDR: {
2035 Binop_12x<HSub>(instruction, Primitive::kPrimFloat);
2036 break;
2037 }
2038
2039 case Instruction::SUB_DOUBLE_2ADDR: {
2040 Binop_12x<HSub>(instruction, Primitive::kPrimDouble);
2041 break;
2042 }
2043
Calin Juravle34bacdf2014-10-07 20:23:36 +01002044 case Instruction::MUL_INT_2ADDR: {
2045 Binop_12x<HMul>(instruction, Primitive::kPrimInt);
2046 break;
2047 }
2048
2049 case Instruction::MUL_LONG_2ADDR: {
2050 Binop_12x<HMul>(instruction, Primitive::kPrimLong);
2051 break;
2052 }
2053
Calin Juravleb5bfa962014-10-21 18:02:24 +01002054 case Instruction::MUL_FLOAT_2ADDR: {
2055 Binop_12x<HMul>(instruction, Primitive::kPrimFloat);
2056 break;
2057 }
2058
2059 case Instruction::MUL_DOUBLE_2ADDR: {
2060 Binop_12x<HMul>(instruction, Primitive::kPrimDouble);
2061 break;
2062 }
2063
Calin Juravle865fc882014-11-06 17:09:03 +00002064 case Instruction::DIV_INT_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002065 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2066 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravle865fc882014-11-06 17:09:03 +00002067 break;
2068 }
2069
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002070 case Instruction::DIV_LONG_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002071 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2072 dex_pc, Primitive::kPrimLong, false, true);
2073 break;
2074 }
2075
2076 case Instruction::REM_INT_2ADDR: {
2077 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2078 dex_pc, Primitive::kPrimInt, false, false);
2079 break;
2080 }
2081
2082 case Instruction::REM_LONG_2ADDR: {
2083 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2084 dex_pc, Primitive::kPrimLong, false, false);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002085 break;
2086 }
2087
Calin Juravled2ec87d2014-12-08 14:24:46 +00002088 case Instruction::REM_FLOAT_2ADDR: {
2089 Binop_12x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2090 break;
2091 }
2092
2093 case Instruction::REM_DOUBLE_2ADDR: {
2094 Binop_12x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2095 break;
2096 }
2097
Calin Juravle9aec02f2014-11-18 23:06:35 +00002098 case Instruction::SHL_INT_2ADDR: {
2099 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt);
2100 break;
2101 }
2102
2103 case Instruction::SHL_LONG_2ADDR: {
2104 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong);
2105 break;
2106 }
2107
2108 case Instruction::SHR_INT_2ADDR: {
2109 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt);
2110 break;
2111 }
2112
2113 case Instruction::SHR_LONG_2ADDR: {
2114 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong);
2115 break;
2116 }
2117
2118 case Instruction::USHR_INT_2ADDR: {
2119 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt);
2120 break;
2121 }
2122
2123 case Instruction::USHR_LONG_2ADDR: {
2124 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong);
2125 break;
2126 }
2127
Calin Juravle7c4954d2014-10-28 16:57:40 +00002128 case Instruction::DIV_FLOAT_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00002129 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002130 break;
2131 }
2132
2133 case Instruction::DIV_DOUBLE_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00002134 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002135 break;
2136 }
2137
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002138 case Instruction::AND_INT_2ADDR: {
2139 Binop_12x<HAnd>(instruction, Primitive::kPrimInt);
2140 break;
2141 }
2142
2143 case Instruction::AND_LONG_2ADDR: {
2144 Binop_12x<HAnd>(instruction, Primitive::kPrimLong);
2145 break;
2146 }
2147
2148 case Instruction::OR_INT_2ADDR: {
2149 Binop_12x<HOr>(instruction, Primitive::kPrimInt);
2150 break;
2151 }
2152
2153 case Instruction::OR_LONG_2ADDR: {
2154 Binop_12x<HOr>(instruction, Primitive::kPrimLong);
2155 break;
2156 }
2157
2158 case Instruction::XOR_INT_2ADDR: {
2159 Binop_12x<HXor>(instruction, Primitive::kPrimInt);
2160 break;
2161 }
2162
2163 case Instruction::XOR_LONG_2ADDR: {
2164 Binop_12x<HXor>(instruction, Primitive::kPrimLong);
2165 break;
2166 }
2167
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002168 case Instruction::ADD_INT_LIT16: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002169 Binop_22s<HAdd>(instruction, false);
2170 break;
2171 }
2172
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002173 case Instruction::AND_INT_LIT16: {
2174 Binop_22s<HAnd>(instruction, false);
2175 break;
2176 }
2177
2178 case Instruction::OR_INT_LIT16: {
2179 Binop_22s<HOr>(instruction, false);
2180 break;
2181 }
2182
2183 case Instruction::XOR_INT_LIT16: {
2184 Binop_22s<HXor>(instruction, false);
2185 break;
2186 }
2187
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002188 case Instruction::RSUB_INT: {
2189 Binop_22s<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002190 break;
2191 }
2192
Calin Juravle34bacdf2014-10-07 20:23:36 +01002193 case Instruction::MUL_INT_LIT16: {
2194 Binop_22s<HMul>(instruction, false);
2195 break;
2196 }
2197
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002198 case Instruction::ADD_INT_LIT8: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002199 Binop_22b<HAdd>(instruction, false);
2200 break;
2201 }
2202
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002203 case Instruction::AND_INT_LIT8: {
2204 Binop_22b<HAnd>(instruction, false);
2205 break;
2206 }
2207
2208 case Instruction::OR_INT_LIT8: {
2209 Binop_22b<HOr>(instruction, false);
2210 break;
2211 }
2212
2213 case Instruction::XOR_INT_LIT8: {
2214 Binop_22b<HXor>(instruction, false);
2215 break;
2216 }
2217
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002218 case Instruction::RSUB_INT_LIT8: {
2219 Binop_22b<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002220 break;
2221 }
2222
Calin Juravle34bacdf2014-10-07 20:23:36 +01002223 case Instruction::MUL_INT_LIT8: {
2224 Binop_22b<HMul>(instruction, false);
2225 break;
2226 }
2227
Calin Juravled0d48522014-11-04 16:40:20 +00002228 case Instruction::DIV_INT_LIT16:
2229 case Instruction::DIV_INT_LIT8: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002230 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2231 dex_pc, Primitive::kPrimInt, true, true);
2232 break;
2233 }
2234
2235 case Instruction::REM_INT_LIT16:
2236 case Instruction::REM_INT_LIT8: {
2237 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2238 dex_pc, Primitive::kPrimInt, true, false);
Calin Juravled0d48522014-11-04 16:40:20 +00002239 break;
2240 }
2241
Calin Juravle9aec02f2014-11-18 23:06:35 +00002242 case Instruction::SHL_INT_LIT8: {
2243 Binop_22b<HShl>(instruction, false);
2244 break;
2245 }
2246
2247 case Instruction::SHR_INT_LIT8: {
2248 Binop_22b<HShr>(instruction, false);
2249 break;
2250 }
2251
2252 case Instruction::USHR_INT_LIT8: {
2253 Binop_22b<HUShr>(instruction, false);
2254 break;
2255 }
2256
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002257 case Instruction::NEW_INSTANCE: {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002258 uint16_t type_index = instruction.VRegB_21c();
Jeff Hao848f70a2014-01-15 13:49:50 -08002259 if (compiler_driver_->IsStringTypeIndex(type_index, dex_file_)) {
Jeff Hao848f70a2014-01-15 13:49:50 -08002260 int32_t register_index = instruction.VRegA();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002261 HFakeString* fake_string = new (arena_) HFakeString();
2262 current_block_->AddInstruction(fake_string);
2263 UpdateLocal(register_index, fake_string);
Jeff Hao848f70a2014-01-15 13:49:50 -08002264 } else {
2265 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
2266 ? kQuickAllocObjectWithAccessCheck
2267 : kQuickAllocObject;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002268
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002269 current_block_->AddInstruction(new (arena_) HNewInstance(
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002270 graph_->GetCurrentMethod(),
2271 dex_pc,
2272 type_index,
2273 *dex_compilation_unit_->GetDexFile(),
2274 entrypoint));
Jeff Hao848f70a2014-01-15 13:49:50 -08002275 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
2276 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002277 break;
2278 }
2279
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002280 case Instruction::NEW_ARRAY: {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002281 uint16_t type_index = instruction.VRegC_22c();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002282 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt);
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002283 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
2284 ? kQuickAllocArrayWithAccessCheck
2285 : kQuickAllocArray;
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002286 current_block_->AddInstruction(new (arena_) HNewArray(length,
2287 graph_->GetCurrentMethod(),
2288 dex_pc,
2289 type_index,
2290 *dex_compilation_unit_->GetDexFile(),
2291 entrypoint));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002292 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
2293 break;
2294 }
2295
2296 case Instruction::FILLED_NEW_ARRAY: {
2297 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
2298 uint32_t type_index = instruction.VRegB_35c();
2299 uint32_t args[5];
2300 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00002301 BuildFilledNewArray(dex_pc, type_index, number_of_vreg_arguments, false, args, 0);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002302 break;
2303 }
2304
2305 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2306 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
2307 uint32_t type_index = instruction.VRegB_3rc();
2308 uint32_t register_index = instruction.VRegC_3rc();
2309 BuildFilledNewArray(
Calin Juravle225ff812014-11-13 16:46:39 +00002310 dex_pc, type_index, number_of_vreg_arguments, true, nullptr, register_index);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002311 break;
2312 }
2313
2314 case Instruction::FILL_ARRAY_DATA: {
Calin Juravle225ff812014-11-13 16:46:39 +00002315 BuildFillArrayData(instruction, dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002316 break;
2317 }
2318
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002319 case Instruction::MOVE_RESULT:
Dave Allison20dfc792014-06-16 20:44:29 -07002320 case Instruction::MOVE_RESULT_WIDE:
David Brazdilfc6a86a2015-06-26 10:33:45 +00002321 case Instruction::MOVE_RESULT_OBJECT: {
Nicolas Geoffray1efcc222015-06-24 12:41:20 +01002322 if (latest_result_ == nullptr) {
2323 // Only dead code can lead to this situation, where the verifier
2324 // does not reject the method.
2325 } else {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002326 // An Invoke/FilledNewArray and its MoveResult could have landed in
2327 // different blocks if there was a try/catch block boundary between
2328 // them. For Invoke, we insert a StoreLocal after the instruction. For
2329 // FilledNewArray, the local needs to be updated after the array was
2330 // filled, otherwise we might overwrite an input vreg.
2331 HStoreLocal* update_local =
2332 new (arena_) HStoreLocal(GetLocalAt(instruction.VRegA()), latest_result_);
2333 HBasicBlock* block = latest_result_->GetBlock();
2334 if (block == current_block_) {
2335 // MoveResult and the previous instruction are in the same block.
2336 current_block_->AddInstruction(update_local);
2337 } else {
2338 // The two instructions are in different blocks. Insert the MoveResult
2339 // before the final control-flow instruction of the previous block.
2340 DCHECK(block->EndsWithControlFlowInstruction());
2341 DCHECK(current_block_->GetInstructions().IsEmpty());
2342 block->InsertInstructionBefore(update_local, block->GetLastInstruction());
2343 }
Nicolas Geoffray1efcc222015-06-24 12:41:20 +01002344 latest_result_ = nullptr;
2345 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002346 break;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002347 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002348
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002349 case Instruction::CMP_LONG: {
Mark Mendellc4701932015-04-10 13:18:51 -04002350 Binop_23x_cmp(instruction, Primitive::kPrimLong, kNoBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002351 break;
2352 }
2353
2354 case Instruction::CMPG_FLOAT: {
Mark Mendellc4701932015-04-10 13:18:51 -04002355 Binop_23x_cmp(instruction, Primitive::kPrimFloat, kGtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002356 break;
2357 }
2358
2359 case Instruction::CMPG_DOUBLE: {
Mark Mendellc4701932015-04-10 13:18:51 -04002360 Binop_23x_cmp(instruction, Primitive::kPrimDouble, kGtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002361 break;
2362 }
2363
2364 case Instruction::CMPL_FLOAT: {
Mark Mendellc4701932015-04-10 13:18:51 -04002365 Binop_23x_cmp(instruction, Primitive::kPrimFloat, kLtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002366 break;
2367 }
2368
2369 case Instruction::CMPL_DOUBLE: {
Mark Mendellc4701932015-04-10 13:18:51 -04002370 Binop_23x_cmp(instruction, Primitive::kPrimDouble, kLtBias, dex_pc);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002371 break;
2372 }
2373
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002374 case Instruction::NOP:
2375 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002376
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002377 case Instruction::IGET:
2378 case Instruction::IGET_WIDE:
2379 case Instruction::IGET_OBJECT:
2380 case Instruction::IGET_BOOLEAN:
2381 case Instruction::IGET_BYTE:
2382 case Instruction::IGET_CHAR:
2383 case Instruction::IGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002384 if (!BuildInstanceFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002385 return false;
2386 }
2387 break;
2388 }
2389
2390 case Instruction::IPUT:
2391 case Instruction::IPUT_WIDE:
2392 case Instruction::IPUT_OBJECT:
2393 case Instruction::IPUT_BOOLEAN:
2394 case Instruction::IPUT_BYTE:
2395 case Instruction::IPUT_CHAR:
2396 case Instruction::IPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002397 if (!BuildInstanceFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002398 return false;
2399 }
2400 break;
2401 }
2402
2403 case Instruction::SGET:
2404 case Instruction::SGET_WIDE:
2405 case Instruction::SGET_OBJECT:
2406 case Instruction::SGET_BOOLEAN:
2407 case Instruction::SGET_BYTE:
2408 case Instruction::SGET_CHAR:
2409 case Instruction::SGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002410 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002411 return false;
2412 }
2413 break;
2414 }
2415
2416 case Instruction::SPUT:
2417 case Instruction::SPUT_WIDE:
2418 case Instruction::SPUT_OBJECT:
2419 case Instruction::SPUT_BOOLEAN:
2420 case Instruction::SPUT_BYTE:
2421 case Instruction::SPUT_CHAR:
2422 case Instruction::SPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002423 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002424 return false;
2425 }
2426 break;
2427 }
2428
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002429#define ARRAY_XX(kind, anticipated_type) \
2430 case Instruction::AGET##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00002431 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002432 break; \
2433 } \
2434 case Instruction::APUT##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00002435 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002436 break; \
2437 }
2438
2439 ARRAY_XX(, Primitive::kPrimInt);
2440 ARRAY_XX(_WIDE, Primitive::kPrimLong);
2441 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
2442 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
2443 ARRAY_XX(_BYTE, Primitive::kPrimByte);
2444 ARRAY_XX(_CHAR, Primitive::kPrimChar);
2445 ARRAY_XX(_SHORT, Primitive::kPrimShort);
2446
Nicolas Geoffray39468442014-09-02 15:17:15 +01002447 case Instruction::ARRAY_LENGTH: {
2448 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002449 // No need for a temporary for the null check, it is the only input of the following
2450 // instruction.
Calin Juravle225ff812014-11-13 16:46:39 +00002451 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002452 current_block_->AddInstruction(object);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002453 current_block_->AddInstruction(new (arena_) HArrayLength(object));
2454 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
2455 break;
2456 }
2457
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002458 case Instruction::CONST_STRING: {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002459 current_block_->AddInstruction(
2460 new (arena_) HLoadString(graph_->GetCurrentMethod(), instruction.VRegB_21c(), dex_pc));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002461 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2462 break;
2463 }
2464
2465 case Instruction::CONST_STRING_JUMBO: {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002466 current_block_->AddInstruction(
2467 new (arena_) HLoadString(graph_->GetCurrentMethod(), instruction.VRegB_31c(), dex_pc));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002468 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
2469 break;
2470 }
2471
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002472 case Instruction::CONST_CLASS: {
2473 uint16_t type_index = instruction.VRegB_21c();
2474 bool type_known_final;
2475 bool type_known_abstract;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002476 bool dont_use_is_referrers_class;
2477 // `CanAccessTypeWithoutChecks` will tell whether the method being
2478 // built is trying to access its own class, so that the generated
2479 // code can optimize for this case. However, the optimization does not
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002480 // work for inlining, so we use `IsOutermostCompilingClass` instead.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002481 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
2482 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002483 &type_known_final, &type_known_abstract, &dont_use_is_referrers_class);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002484 if (!can_access) {
Calin Juravle48c2b032014-12-09 18:11:36 +00002485 MaybeRecordStat(MethodCompilationStat::kNotCompiledCantAccesType);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002486 return false;
2487 }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002488 current_block_->AddInstruction(new (arena_) HLoadClass(
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002489 graph_->GetCurrentMethod(),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002490 type_index,
2491 *dex_compilation_unit_->GetDexFile(),
2492 IsOutermostCompilingClass(type_index),
2493 dex_pc));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002494 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2495 break;
2496 }
2497
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002498 case Instruction::MOVE_EXCEPTION: {
2499 current_block_->AddInstruction(new (arena_) HLoadException());
2500 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
2501 break;
2502 }
2503
2504 case Instruction::THROW: {
2505 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00002506 current_block_->AddInstruction(new (arena_) HThrow(exception, dex_pc));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002507 // A throw instruction must branch to the exit block.
2508 current_block_->AddSuccessor(exit_block_);
2509 // We finished building this block. Set the current block to null to avoid
2510 // adding dead instructions to it.
2511 current_block_ = nullptr;
2512 break;
2513 }
2514
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002515 case Instruction::INSTANCE_OF: {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002516 uint8_t destination = instruction.VRegA_22c();
2517 uint8_t reference = instruction.VRegB_22c();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002518 uint16_t type_index = instruction.VRegC_22c();
Calin Juravle225ff812014-11-13 16:46:39 +00002519 if (!BuildTypeCheck(instruction, destination, reference, type_index, dex_pc)) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002520 return false;
2521 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002522 break;
2523 }
2524
2525 case Instruction::CHECK_CAST: {
2526 uint8_t reference = instruction.VRegA_21c();
2527 uint16_t type_index = instruction.VRegB_21c();
Calin Juravle225ff812014-11-13 16:46:39 +00002528 if (!BuildTypeCheck(instruction, -1, reference, type_index, dex_pc)) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002529 return false;
2530 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002531 break;
2532 }
2533
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002534 case Instruction::MONITOR_ENTER: {
2535 current_block_->AddInstruction(new (arena_) HMonitorOperation(
2536 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2537 HMonitorOperation::kEnter,
Calin Juravle225ff812014-11-13 16:46:39 +00002538 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002539 break;
2540 }
2541
2542 case Instruction::MONITOR_EXIT: {
2543 current_block_->AddInstruction(new (arena_) HMonitorOperation(
2544 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2545 HMonitorOperation::kExit,
Calin Juravle225ff812014-11-13 16:46:39 +00002546 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002547 break;
2548 }
2549
Andreas Gamped881df52014-11-24 23:28:39 -08002550 case Instruction::PACKED_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002551 BuildPackedSwitch(instruction, dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08002552 break;
2553 }
2554
Andreas Gampee4d4d322014-12-04 09:09:57 -08002555 case Instruction::SPARSE_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002556 BuildSparseSwitch(instruction, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08002557 break;
2558 }
2559
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002560 default:
Calin Juravle48c2b032014-12-09 18:11:36 +00002561 VLOG(compiler) << "Did not compile "
2562 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
2563 << " because of unhandled instruction "
2564 << instruction.Name();
2565 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnhandledInstruction);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002566 return false;
2567 }
2568 return true;
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00002569} // NOLINT(readability/fn_size)
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002570
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002571HLocal* HGraphBuilder::GetLocalAt(int register_index) const {
2572 return locals_.Get(register_index);
2573}
2574
2575void HGraphBuilder::UpdateLocal(int register_index, HInstruction* instruction) const {
2576 HLocal* local = GetLocalAt(register_index);
2577 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction));
2578}
2579
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002580HInstruction* HGraphBuilder::LoadLocal(int register_index, Primitive::Type type) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002581 HLocal* local = GetLocalAt(register_index);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002582 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type));
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002583 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002584}
2585
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002586} // namespace art