blob: 8bf744d0e54321e5ca2ee2f4323585e802fa8a9b [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());
275 if (try_item_idx == -1) {
276 return nullptr;
277 } else {
278 return DexFile::GetTryItems(code_item, try_item_idx);
279 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000280}
281
282void HGraphBuilder::CreateBlocksForTryCatch(const DexFile::CodeItem& code_item) {
283 if (code_item.tries_size_ == 0) {
284 return;
285 }
286
287 // Create branch targets at the start/end of the TryItem range. These are
288 // places where the program might fall through into/out of the a block and
289 // where TryBoundary instructions will be inserted later. Other edges which
290 // enter/exit the try blocks are a result of branches/switches.
291 for (size_t idx = 0; idx < code_item.tries_size_; ++idx) {
292 const DexFile::TryItem* try_item = DexFile::GetTryItems(code_item, idx);
293 uint32_t dex_pc_start = try_item->start_addr_;
294 uint32_t dex_pc_end = dex_pc_start + try_item->insn_count_;
295 FindOrCreateBlockStartingAt(dex_pc_start);
296 if (dex_pc_end < code_item.insns_size_in_code_units_) {
297 // TODO: Do not create block if the last instruction cannot fall through.
298 FindOrCreateBlockStartingAt(dex_pc_end);
299 } else {
300 // The TryItem spans until the very end of the CodeItem (or beyond if
301 // invalid) and therefore cannot have any code afterwards.
302 }
303 }
304
305 // Create branch targets for exception handlers.
306 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(code_item, 0);
307 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
308 for (uint32_t idx = 0; idx < handlers_size; ++idx) {
309 CatchHandlerIterator iterator(handlers_ptr);
310 for (; iterator.HasNext(); iterator.Next()) {
311 uint32_t address = iterator.GetHandlerAddress();
312 HBasicBlock* block = FindOrCreateBlockStartingAt(address);
313 block->SetIsCatchBlock();
314 }
315 handlers_ptr = iterator.EndDataPointer();
316 }
317}
318
David Brazdil56e1acc2015-06-30 15:41:36 +0100319void HGraphBuilder::SplitTryBoundaryEdge(HBasicBlock* predecessor,
320 HBasicBlock* successor,
321 HTryBoundary::BoundaryKind kind,
322 const DexFile::CodeItem& code_item,
323 const DexFile::TryItem& try_item) {
324 // Split the edge with a single TryBoundary instruction.
325 HTryBoundary* try_boundary = new (arena_) HTryBoundary(kind);
326 HBasicBlock* try_entry_block = graph_->SplitEdge(predecessor, successor);
327 try_entry_block->AddInstruction(try_boundary);
328
329 // Link the TryBoundary to the handlers of `try_item`.
330 for (CatchHandlerIterator it(code_item, try_item); it.HasNext(); it.Next()) {
331 try_boundary->AddExceptionHandler(FindBlockStartingAt(it.GetHandlerAddress()));
332 }
333}
334
David Brazdilfc6a86a2015-06-26 10:33:45 +0000335void HGraphBuilder::InsertTryBoundaryBlocks(const DexFile::CodeItem& code_item) {
336 if (code_item.tries_size_ == 0) {
337 return;
338 }
339
David Brazdilbff75032015-07-08 17:26:51 +0000340 const size_t num_blocks = graph_->GetBlocks().Size();
341 ArenaBitVector can_block_throw(arena_, num_blocks, false);
342
343 // Scan blocks and mark those which contain throwing instructions.
344 for (size_t block_id = 0; block_id < num_blocks; ++block_id) {
345 HBasicBlock* block = graph_->GetBlocks().Get(block_id);
346 for (HInstructionIterator insn(block->GetInstructions()); !insn.Done(); insn.Advance()) {
347 if (insn.Current()->CanThrow()) {
348 can_block_throw.SetBit(block_id);
349 break;
350 }
351 }
352 }
353
David Brazdil281a6322015-07-03 10:34:57 +0100354 // Iterate over all blocks, find those covered by some TryItem and:
355 // (a) split edges which enter/exit the try range,
356 // (b) create TryBoundary instructions in the new blocks,
357 // (c) link the new blocks to corresponding exception handlers.
358 // We cannot iterate only over blocks in `branch_targets_` because switch-case
359 // blocks share the same dex_pc.
David Brazdilbff75032015-07-08 17:26:51 +0000360 for (size_t block_id = 1; block_id < num_blocks - 1; ++block_id) {
David Brazdil281a6322015-07-03 10:34:57 +0100361 HBasicBlock* try_block = graph_->GetBlocks().Get(block_id);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000362
David Brazdil281a6322015-07-03 10:34:57 +0100363 // Iteration starts from 1 to skip the entry block.
364 DCHECK_NE(try_block, entry_block_);
David Brazdilbff75032015-07-08 17:26:51 +0000365 // Iteration ends at num_blocks - 1 to skip the exit block.
David Brazdil281a6322015-07-03 10:34:57 +0100366 DCHECK_NE(try_block, exit_block_);
367 // TryBoundary blocks are added at the end of the list and not iterated over.
368 DCHECK(!try_block->IsSingleTryBoundary());
David Brazdilfc6a86a2015-06-26 10:33:45 +0000369
David Brazdil281a6322015-07-03 10:34:57 +0100370 // Find the TryItem for this block.
David Brazdilbff75032015-07-08 17:26:51 +0000371 const DexFile::TryItem* try_item = GetTryItem(try_block, code_item, can_block_throw);
372 if (try_item == nullptr) {
David Brazdil281a6322015-07-03 10:34:57 +0100373 continue;
374 }
David Brazdil281a6322015-07-03 10:34:57 +0100375
376 if (try_block->IsCatchBlock()) {
377 // Catch blocks are always considered an entry point into the TryItem in
378 // order to avoid splitting exceptional edges (they might not have been
379 // created yet). We separate the move-exception (if present) from the
380 // rest of the block and insert a TryBoundary after it, creating a
381 // landing pad for the exceptional edges.
382 HInstruction* first_insn = try_block->GetFirstInstruction();
383 HInstruction* split_position = nullptr;
384 if (first_insn->IsLoadException()) {
385 // Catch block starts with a LoadException. Split the block after the
386 // StoreLocal that must come after the load.
387 DCHECK(first_insn->GetNext()->IsStoreLocal());
388 split_position = first_insn->GetNext()->GetNext();
David Brazdil56e1acc2015-06-30 15:41:36 +0100389 } else {
David Brazdil281a6322015-07-03 10:34:57 +0100390 // Catch block does not obtain the exception. Split at the beginning
391 // to create an empty catch block.
392 split_position = first_insn;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000393 }
David Brazdil281a6322015-07-03 10:34:57 +0100394 DCHECK(split_position != nullptr);
395 HBasicBlock* catch_block = try_block;
396 try_block = catch_block->SplitBefore(split_position);
David Brazdilbff75032015-07-08 17:26:51 +0000397 SplitTryBoundaryEdge(catch_block, try_block, HTryBoundary::kEntry, code_item, *try_item);
David Brazdil281a6322015-07-03 10:34:57 +0100398 } else {
399 // For non-catch blocks, find predecessors which are not covered by the
400 // same TryItem range. Such edges enter the try block and will have
401 // a TryBoundary inserted.
402 for (size_t i = 0; i < try_block->GetPredecessors().Size(); ++i) {
403 HBasicBlock* predecessor = try_block->GetPredecessors().Get(i);
404 if (predecessor->IsSingleTryBoundary()) {
405 // The edge was already split because of an exit from a neighbouring
406 // TryItem. We split it again and insert an entry point.
David Brazdil56e1acc2015-06-30 15:41:36 +0100407 if (kIsDebugBuild) {
David Brazdil281a6322015-07-03 10:34:57 +0100408 HTryBoundary* last_insn = predecessor->GetLastInstruction()->AsTryBoundary();
David Brazdilbff75032015-07-08 17:26:51 +0000409 const DexFile::TryItem* predecessor_try_item =
410 GetTryItem(predecessor->GetSinglePredecessor(), code_item, can_block_throw);
David Brazdil281a6322015-07-03 10:34:57 +0100411 DCHECK(!last_insn->IsEntry());
412 DCHECK_EQ(last_insn->GetNormalFlowSuccessor(), try_block);
413 DCHECK(try_block->IsFirstIndexOfPredecessor(predecessor, i));
David Brazdilbff75032015-07-08 17:26:51 +0000414 DCHECK_NE(try_item, predecessor_try_item);
David Brazdil56e1acc2015-06-30 15:41:36 +0100415 }
David Brazdilbff75032015-07-08 17:26:51 +0000416 } else if (GetTryItem(predecessor, code_item, can_block_throw) != try_item) {
David Brazdil281a6322015-07-03 10:34:57 +0100417 // This is an entry point into the TryItem and the edge has not been
418 // split yet. That means that `predecessor` is not in a TryItem, or
419 // it is in a different TryItem and we happened to iterate over this
420 // block first. We split the edge and insert an entry point.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000421 } else {
422 // Not an edge on the boundary of the try block.
423 continue;
424 }
David Brazdilbff75032015-07-08 17:26:51 +0000425 SplitTryBoundaryEdge(predecessor, try_block, HTryBoundary::kEntry, code_item, *try_item);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000426 }
427 }
David Brazdil281a6322015-07-03 10:34:57 +0100428
429 // Find successors which are not covered by the same TryItem range. Such
430 // edges exit the try block and will have a TryBoundary inserted.
431 for (size_t i = 0; i < try_block->GetSuccessors().Size(); ++i) {
432 HBasicBlock* successor = try_block->GetSuccessors().Get(i);
433 if (successor->IsCatchBlock()) {
434 // A catch block is always considered an entry point into its TryItem.
435 // We therefore assume this is an exit point, regardless of whether
436 // the catch block is in a different TryItem or not.
437 } else if (successor->IsSingleTryBoundary()) {
438 // The edge was already split because of an entry into a neighbouring
439 // TryItem. We split it again and insert an exit.
440 if (kIsDebugBuild) {
441 HTryBoundary* last_insn = successor->GetLastInstruction()->AsTryBoundary();
David Brazdilbff75032015-07-08 17:26:51 +0000442 const DexFile::TryItem* successor_try_item =
443 GetTryItem(last_insn->GetNormalFlowSuccessor(), code_item, can_block_throw);
David Brazdil281a6322015-07-03 10:34:57 +0100444 DCHECK_EQ(try_block, successor->GetSinglePredecessor());
445 DCHECK(last_insn->IsEntry());
David Brazdilbff75032015-07-08 17:26:51 +0000446 DCHECK_NE(try_item, successor_try_item);
David Brazdil281a6322015-07-03 10:34:57 +0100447 }
David Brazdilbff75032015-07-08 17:26:51 +0000448 } else if (GetTryItem(successor, code_item, can_block_throw) != try_item) {
David Brazdil281a6322015-07-03 10:34:57 +0100449 // This is an exit out of the TryItem and the edge has not been split
450 // yet. That means that either `successor` is not in a TryItem, or it
451 // is in a different TryItem and we happened to iterate over this
452 // block first. We split the edge and insert an exit.
453 HInstruction* last_instruction = try_block->GetLastInstruction();
454 if (last_instruction->IsReturn() || last_instruction->IsReturnVoid()) {
455 DCHECK_EQ(successor, exit_block_);
456 // Control flow exits the try block with a Return(Void). Because
457 // splitting the edge would invalidate the invariant that Return
458 // always jumps to Exit, we move the Return outside the try block.
459 successor = try_block->SplitBefore(last_instruction);
460 }
461 } else {
462 // Not an edge on the boundary of the try block.
463 continue;
464 }
David Brazdilbff75032015-07-08 17:26:51 +0000465 SplitTryBoundaryEdge(try_block, successor, HTryBoundary::kExit, code_item, *try_item);
David Brazdil281a6322015-07-03 10:34:57 +0100466 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000467 }
468}
469
David Brazdil5e8b1372015-01-23 14:39:08 +0000470bool HGraphBuilder::BuildGraph(const DexFile::CodeItem& code_item) {
471 DCHECK(graph_->GetBlocks().IsEmpty());
472
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000473 const uint16_t* code_ptr = code_item.insns_;
474 const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100475 code_start_ = code_ptr;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000476
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000477 // Setup the graph with the entry block and exit block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100478 entry_block_ = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000479 graph_->AddBlock(entry_block_);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100480 exit_block_ = new (arena_) HBasicBlock(graph_, kNoDexPc);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000481 graph_->SetEntryBlock(entry_block_);
482 graph_->SetExitBlock(exit_block_);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000483
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000484 InitializeLocals(code_item.registers_size_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000485 graph_->SetMaximumNumberOfOutVRegs(code_item.outs_size_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000486
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000487 // Compute the number of dex instructions, blocks, and branches. We will
488 // check these values against limits given to the compiler.
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000489 size_t number_of_branches = 0;
490
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000491 // To avoid splitting blocks, we compute ahead of time the instructions that
492 // start a new block, and create these blocks.
Calin Juravle702d2602015-04-30 19:28:21 +0100493 if (!ComputeBranchTargets(code_ptr, code_end, &number_of_branches)) {
494 MaybeRecordStat(MethodCompilationStat::kNotCompiledBranchOutsideMethodCode);
495 return false;
496 }
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000497
498 // Note that the compiler driver is null when unit testing.
David Brazdil1b498722015-03-31 11:37:18 +0100499 if ((compiler_driver_ != nullptr) && SkipCompilation(code_item, number_of_branches)) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000500 return false;
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000501 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000502
David Brazdilfc6a86a2015-06-26 10:33:45 +0000503 CreateBlocksForTryCatch(code_item);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000504
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000505 InitializeParameters(code_item.ins_size_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100506
Calin Juravle225ff812014-11-13 16:46:39 +0000507 size_t dex_pc = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000508 while (code_ptr < code_end) {
Calin Juravle225ff812014-11-13 16:46:39 +0000509 // Update the current block if dex_pc starts a new block.
510 MaybeUpdateCurrentBlock(dex_pc);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000511 const Instruction& instruction = *Instruction::At(code_ptr);
Calin Juravle48c2b032014-12-09 18:11:36 +0000512 if (!AnalyzeDexInstruction(instruction, dex_pc)) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000513 return false;
Calin Juravle48c2b032014-12-09 18:11:36 +0000514 }
Calin Juravle225ff812014-11-13 16:46:39 +0000515 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000516 code_ptr += instruction.SizeInCodeUnits();
517 }
518
David Brazdilfc6a86a2015-06-26 10:33:45 +0000519 // Add Exit to the exit block.
David Brazdil3e187382015-06-26 09:59:52 +0000520 exit_block_->AddInstruction(new (arena_) HExit());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000521 // Add the suspend check to the entry block.
522 entry_block_->AddInstruction(new (arena_) HSuspendCheck(0));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000523 entry_block_->AddInstruction(new (arena_) HGoto());
David Brazdilbff75032015-07-08 17:26:51 +0000524 // Add the exit block at the end.
525 graph_->AddBlock(exit_block_);
David Brazdil5e8b1372015-01-23 14:39:08 +0000526
David Brazdilfc6a86a2015-06-26 10:33:45 +0000527 // Iterate over blocks covered by TryItems and insert TryBoundaries at entry
528 // and exit points. This requires all control-flow instructions and
529 // non-exceptional edges to have been created.
530 InsertTryBoundaryBlocks(code_item);
531
David Brazdil5e8b1372015-01-23 14:39:08 +0000532 return true;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000533}
534
David Brazdilfc6a86a2015-06-26 10:33:45 +0000535void HGraphBuilder::MaybeUpdateCurrentBlock(size_t dex_pc) {
536 HBasicBlock* block = FindBlockStartingAt(dex_pc);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000537 if (block == nullptr) {
538 return;
539 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000540
541 if (current_block_ != nullptr) {
542 // Branching instructions clear current_block, so we know
543 // the last instruction of the current block is not a branching
544 // instruction. We add an unconditional goto to the found block.
545 current_block_->AddInstruction(new (arena_) HGoto());
546 current_block_->AddSuccessor(block);
547 }
548 graph_->AddBlock(block);
549 current_block_ = block;
550}
551
Calin Juravle702d2602015-04-30 19:28:21 +0100552bool HGraphBuilder::ComputeBranchTargets(const uint16_t* code_ptr,
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000553 const uint16_t* code_end,
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000554 size_t* number_of_branches) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000555 branch_targets_.SetSize(code_end - code_ptr);
556
557 // Create the first block for the dex instructions, single successor of the entry block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100558 HBasicBlock* block = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000559 branch_targets_.Put(0, block);
560 entry_block_->AddSuccessor(block);
561
562 // Iterate over all instructions and find branching instructions. Create blocks for
563 // the locations these instructions branch to.
Andreas Gamped881df52014-11-24 23:28:39 -0800564 uint32_t dex_pc = 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000565 while (code_ptr < code_end) {
566 const Instruction& instruction = *Instruction::At(code_ptr);
567 if (instruction.IsBranch()) {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000568 (*number_of_branches)++;
Calin Juravle225ff812014-11-13 16:46:39 +0000569 int32_t target = instruction.GetTargetOffset() + dex_pc;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000570 // Create a block for the target instruction.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000571 FindOrCreateBlockStartingAt(target);
572
Calin Juravle225ff812014-11-13 16:46:39 +0000573 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000574 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle702d2602015-04-30 19:28:21 +0100575
David Brazdilfe659462015-06-24 14:23:56 +0100576 if (instruction.CanFlowThrough()) {
577 if (code_ptr >= code_end) {
Calin Juravle702d2602015-04-30 19:28:21 +0100578 // In the normal case we should never hit this but someone can artificially forge a dex
579 // file to fall-through out the method code. In this case we bail out compilation.
580 return false;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000581 } else {
582 FindOrCreateBlockStartingAt(dex_pc);
Calin Juravle702d2602015-04-30 19:28:21 +0100583 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000584 }
Andreas Gampee4d4d322014-12-04 09:09:57 -0800585 } else if (instruction.IsSwitch()) {
586 SwitchTable table(instruction, dex_pc, instruction.Opcode() == Instruction::SPARSE_SWITCH);
Andreas Gamped881df52014-11-24 23:28:39 -0800587
588 uint16_t num_entries = table.GetNumEntries();
589
Andreas Gampee4d4d322014-12-04 09:09:57 -0800590 // In a packed-switch, the entry at index 0 is the starting key. In a sparse-switch, the
591 // entry at index 0 is the first key, and values are after *all* keys.
592 size_t offset = table.GetFirstValueIndex();
593
594 // Use a larger loop counter type to avoid overflow issues.
595 for (size_t i = 0; i < num_entries; ++i) {
Andreas Gamped881df52014-11-24 23:28:39 -0800596 // The target of the case.
Andreas Gampee4d4d322014-12-04 09:09:57 -0800597 uint32_t target = dex_pc + table.GetEntryAt(i + offset);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000598 FindOrCreateBlockStartingAt(target);
Andreas Gamped881df52014-11-24 23:28:39 -0800599
David Brazdil281a6322015-07-03 10:34:57 +0100600 // Create a block for the switch-case logic. The block gets the dex_pc
601 // of the SWITCH instruction because it is part of its semantics.
602 block = new (arena_) HBasicBlock(graph_, dex_pc);
603 branch_targets_.Put(table.GetDexPcForIndex(i), block);
Andreas Gamped881df52014-11-24 23:28:39 -0800604 }
605
606 // Fall-through. Add a block if there is more code afterwards.
607 dex_pc += instruction.SizeInCodeUnits();
608 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle702d2602015-04-30 19:28:21 +0100609 if (code_ptr >= code_end) {
610 // In the normal case we should never hit this but someone can artificially forge a dex
611 // file to fall-through out the method code. In this case we bail out compilation.
612 // (A switch can fall-through so we don't need to check CanFlowThrough().)
613 return false;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000614 } else {
615 FindOrCreateBlockStartingAt(dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -0800616 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000617 } else {
618 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle225ff812014-11-13 16:46:39 +0000619 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000620 }
621 }
Calin Juravle702d2602015-04-30 19:28:21 +0100622 return true;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000623}
624
David Brazdilfc6a86a2015-06-26 10:33:45 +0000625HBasicBlock* HGraphBuilder::FindBlockStartingAt(int32_t dex_pc) const {
626 DCHECK_GE(dex_pc, 0);
627 DCHECK_LT(static_cast<size_t>(dex_pc), branch_targets_.Size());
628 return branch_targets_.Get(dex_pc);
629}
630
631HBasicBlock* HGraphBuilder::FindOrCreateBlockStartingAt(int32_t dex_pc) {
632 HBasicBlock* block = FindBlockStartingAt(dex_pc);
633 if (block == nullptr) {
634 block = new (arena_) HBasicBlock(graph_, dex_pc);
635 branch_targets_.Put(dex_pc, block);
636 }
637 return block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000638}
639
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100640template<typename T>
Roland Levillain88cb1752014-10-20 16:36:47 +0100641void HGraphBuilder::Unop_12x(const Instruction& instruction, Primitive::Type type) {
642 HInstruction* first = LoadLocal(instruction.VRegB(), type);
643 current_block_->AddInstruction(new (arena_) T(type, first));
644 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
645}
646
Roland Levillaindff1f282014-11-05 14:15:05 +0000647void HGraphBuilder::Conversion_12x(const Instruction& instruction,
648 Primitive::Type input_type,
Roland Levillain624279f2014-12-04 11:54:28 +0000649 Primitive::Type result_type,
650 uint32_t dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +0000651 HInstruction* first = LoadLocal(instruction.VRegB(), input_type);
Roland Levillain624279f2014-12-04 11:54:28 +0000652 current_block_->AddInstruction(new (arena_) HTypeConversion(result_type, first, dex_pc));
Roland Levillaindff1f282014-11-05 14:15:05 +0000653 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
654}
655
Roland Levillain88cb1752014-10-20 16:36:47 +0100656template<typename T>
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100657void HGraphBuilder::Binop_23x(const Instruction& instruction, Primitive::Type type) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100658 HInstruction* first = LoadLocal(instruction.VRegB(), type);
659 HInstruction* second = LoadLocal(instruction.VRegC(), type);
660 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100661 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
662}
663
664template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000665void HGraphBuilder::Binop_23x(const Instruction& instruction,
666 Primitive::Type type,
667 uint32_t dex_pc) {
668 HInstruction* first = LoadLocal(instruction.VRegB(), type);
669 HInstruction* second = LoadLocal(instruction.VRegC(), type);
670 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
671 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
672}
673
674template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000675void HGraphBuilder::Binop_23x_shift(const Instruction& instruction,
676 Primitive::Type type) {
677 HInstruction* first = LoadLocal(instruction.VRegB(), type);
678 HInstruction* second = LoadLocal(instruction.VRegC(), Primitive::kPrimInt);
679 current_block_->AddInstruction(new (arena_) T(type, first, second));
680 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
681}
682
Calin Juravleddb7df22014-11-25 20:56:51 +0000683void HGraphBuilder::Binop_23x_cmp(const Instruction& instruction,
684 Primitive::Type type,
Mark Mendellc4701932015-04-10 13:18:51 -0400685 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700686 uint32_t dex_pc) {
Calin Juravleddb7df22014-11-25 20:56:51 +0000687 HInstruction* first = LoadLocal(instruction.VRegB(), type);
688 HInstruction* second = LoadLocal(instruction.VRegC(), type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700689 current_block_->AddInstruction(new (arena_) HCompare(type, first, second, bias, dex_pc));
Calin Juravleddb7df22014-11-25 20:56:51 +0000690 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
691}
692
Calin Juravle9aec02f2014-11-18 23:06:35 +0000693template<typename T>
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100694void HGraphBuilder::Binop_12x(const Instruction& instruction, Primitive::Type type) {
695 HInstruction* first = LoadLocal(instruction.VRegA(), type);
696 HInstruction* second = LoadLocal(instruction.VRegB(), type);
697 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100698 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
699}
700
701template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000702void HGraphBuilder::Binop_12x_shift(const Instruction& instruction, Primitive::Type type) {
703 HInstruction* first = LoadLocal(instruction.VRegA(), type);
704 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
705 current_block_->AddInstruction(new (arena_) T(type, first, second));
706 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
707}
708
709template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000710void HGraphBuilder::Binop_12x(const Instruction& instruction,
711 Primitive::Type type,
712 uint32_t dex_pc) {
713 HInstruction* first = LoadLocal(instruction.VRegA(), type);
714 HInstruction* second = LoadLocal(instruction.VRegB(), type);
715 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
716 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
717}
718
719template<typename T>
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100720void HGraphBuilder::Binop_22s(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100721 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000722 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100723 if (reverse) {
724 std::swap(first, second);
725 }
726 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
727 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
728}
729
730template<typename T>
731void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100732 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000733 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100734 if (reverse) {
735 std::swap(first, second);
736 }
737 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
738 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
739}
740
Calin Juravle0c25d102015-04-20 14:49:09 +0100741static bool RequiresConstructorBarrier(const DexCompilationUnit* cu, const CompilerDriver& driver) {
Calin Juravle27df7582015-04-17 19:12:31 +0100742 Thread* self = Thread::Current();
Calin Juravle0c25d102015-04-20 14:49:09 +0100743 return cu->IsConstructor()
744 && driver.RequiresConstructorBarrier(self, cu->GetDexFile(), cu->GetClassDefIndex());
Calin Juravle27df7582015-04-17 19:12:31 +0100745}
746
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100747void HGraphBuilder::BuildReturn(const Instruction& instruction, Primitive::Type type) {
748 if (type == Primitive::kPrimVoid) {
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100749 if (graph_->ShouldGenerateConstructorBarrier()) {
750 // The compilation unit is null during testing.
751 if (dex_compilation_unit_ != nullptr) {
752 DCHECK(RequiresConstructorBarrier(dex_compilation_unit_, *compiler_driver_))
753 << "Inconsistent use of ShouldGenerateConstructorBarrier. Should not generate a barrier.";
754 }
Calin Juravle27df7582015-04-17 19:12:31 +0100755 current_block_->AddInstruction(new (arena_) HMemoryBarrier(kStoreStore));
756 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100757 current_block_->AddInstruction(new (arena_) HReturnVoid());
758 } else {
759 HInstruction* value = LoadLocal(instruction.VRegA(), type);
760 current_block_->AddInstruction(new (arena_) HReturn(value));
761 }
762 current_block_->AddSuccessor(exit_block_);
763 current_block_ = nullptr;
764}
765
766bool HGraphBuilder::BuildInvoke(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000767 uint32_t dex_pc,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100768 uint32_t method_idx,
769 uint32_t number_of_vreg_arguments,
770 bool is_range,
771 uint32_t* args,
772 uint32_t register_index) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100773 Instruction::Code opcode = instruction.Opcode();
774 InvokeType invoke_type;
775 switch (opcode) {
776 case Instruction::INVOKE_STATIC:
777 case Instruction::INVOKE_STATIC_RANGE:
778 invoke_type = kStatic;
779 break;
780 case Instruction::INVOKE_DIRECT:
781 case Instruction::INVOKE_DIRECT_RANGE:
782 invoke_type = kDirect;
783 break;
784 case Instruction::INVOKE_VIRTUAL:
785 case Instruction::INVOKE_VIRTUAL_RANGE:
786 invoke_type = kVirtual;
787 break;
788 case Instruction::INVOKE_INTERFACE:
789 case Instruction::INVOKE_INTERFACE_RANGE:
790 invoke_type = kInterface;
791 break;
792 case Instruction::INVOKE_SUPER_RANGE:
793 case Instruction::INVOKE_SUPER:
794 invoke_type = kSuper;
795 break;
796 default:
797 LOG(FATAL) << "Unexpected invoke op: " << opcode;
798 return false;
799 }
800
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100801 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
802 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(method_id.proto_idx_);
803 const char* descriptor = dex_file_->StringDataByIdx(proto_id.shorty_idx_);
804 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100805 bool is_instance_call = invoke_type != kStatic;
Nicolas Geoffray2e335252015-06-18 11:11:27 +0100806 // Remove the return type from the 'proto'.
807 size_t number_of_arguments = strlen(descriptor) - 1;
808 if (is_instance_call) {
809 // One extra argument for 'this'.
810 ++number_of_arguments;
811 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100812
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000813 MethodReference target_method(dex_file_, method_idx);
814 uintptr_t direct_code;
815 uintptr_t direct_method;
816 int table_index;
817 InvokeType optimized_invoke_type = invoke_type;
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000818
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000819 if (!compiler_driver_->ComputeInvokeInfo(dex_compilation_unit_, dex_pc, true, true,
820 &optimized_invoke_type, &target_method, &table_index,
821 &direct_code, &direct_method)) {
Nicolas Geoffray2e335252015-06-18 11:11:27 +0100822 VLOG(compiler) << "Did not compile "
823 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
Calin Juravle48c2b032014-12-09 18:11:36 +0000824 << " because a method call could not be resolved";
825 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedMethod);
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000826 return false;
827 }
828 DCHECK(optimized_invoke_type != kSuper);
829
Roland Levillain4c0eb422015-04-24 16:43:49 +0100830 // By default, consider that the called method implicitly requires
831 // an initialization check of its declaring method.
832 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement =
833 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
834 // Potential class initialization check, in the case of a static method call.
835 HClinitCheck* clinit_check = nullptr;
Jeff Hao848f70a2014-01-15 13:49:50 -0800836 // Replace calls to String.<init> with StringFactory.
837 int32_t string_init_offset = 0;
838 bool is_string_init = compiler_driver_->IsStringInit(method_idx, dex_file_, &string_init_offset);
839 if (is_string_init) {
840 return_type = Primitive::kPrimNot;
841 is_instance_call = false;
842 number_of_arguments--;
843 invoke_type = kStatic;
844 optimized_invoke_type = kStatic;
845 }
Roland Levillain4c0eb422015-04-24 16:43:49 +0100846
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000847 HInvoke* invoke = nullptr;
Roland Levillain4c0eb422015-04-24 16:43:49 +0100848
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000849 if (optimized_invoke_type == kVirtual) {
850 invoke = new (arena_) HInvokeVirtual(
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800851 arena_, number_of_arguments, return_type, dex_pc, method_idx, table_index);
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000852 } else if (optimized_invoke_type == kInterface) {
853 invoke = new (arena_) HInvokeInterface(
854 arena_, number_of_arguments, return_type, dex_pc, method_idx, table_index);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100855 } else {
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000856 DCHECK(optimized_invoke_type == kDirect || optimized_invoke_type == kStatic);
857 // Sharpening to kDirect only works if we compile PIC.
858 DCHECK((optimized_invoke_type == invoke_type) || (optimized_invoke_type != kDirect)
859 || compiler_driver_->GetCompilerOptions().GetCompilePic());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000860 bool is_recursive =
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100861 (target_method.dex_method_index == outer_compilation_unit_->GetDexMethodIndex())
862 && (target_method.dex_file == outer_compilation_unit_->GetDexFile());
Roland Levillain4c0eb422015-04-24 16:43:49 +0100863
Jeff Haocad65422015-06-18 21:16:08 -0700864 if (optimized_invoke_type == kStatic && !is_string_init) {
Roland Levillain4c0eb422015-04-24 16:43:49 +0100865 ScopedObjectAccess soa(Thread::Current());
866 StackHandleScope<4> hs(soa.Self());
867 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
868 dex_compilation_unit_->GetClassLinker()->FindDexCache(
869 *dex_compilation_unit_->GetDexFile())));
870 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
871 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700872 ArtMethod* resolved_method = compiler_driver_->ResolveMethod(
873 soa, dex_cache, class_loader, dex_compilation_unit_, method_idx, optimized_invoke_type);
Roland Levillain4c0eb422015-04-24 16:43:49 +0100874
875 if (resolved_method == nullptr) {
876 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedMethod);
877 return false;
878 }
879
880 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
881 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
882 outer_compilation_unit_->GetClassLinker()->FindDexCache(outer_dex_file)));
Nicolas Geoffrayafd06412015-06-20 22:44:47 +0100883 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Roland Levillain4c0eb422015-04-24 16:43:49 +0100884
885 // The index at which the method's class is stored in the DexCache's type array.
886 uint32_t storage_index = DexFile::kDexNoIndex;
Nicolas Geoffrayafd06412015-06-20 22:44:47 +0100887 bool is_outer_class = (resolved_method->GetDeclaringClass() == outer_class.Get());
888 if (is_outer_class) {
889 storage_index = outer_class->GetDexTypeIndex();
Roland Levillain4c0eb422015-04-24 16:43:49 +0100890 } else if (outer_dex_cache.Get() == dex_cache.Get()) {
891 // Get `storage_index` from IsClassOfStaticMethodAvailableToReferrer.
892 compiler_driver_->IsClassOfStaticMethodAvailableToReferrer(outer_dex_cache.Get(),
Nicolas Geoffrayafd06412015-06-20 22:44:47 +0100893 GetCompilingClass(),
Roland Levillain4c0eb422015-04-24 16:43:49 +0100894 resolved_method,
895 method_idx,
896 &storage_index);
897 }
898
Nicolas Geoffrayb783b402015-06-22 11:06:43 +0100899 if (!outer_class->IsInterface()
900 && outer_class->IsSubClass(resolved_method->GetDeclaringClass())) {
Nicolas Geoffrayafd06412015-06-20 22:44:47 +0100901 // If the outer class is the declaring class or a subclass
Roland Levillain5f02c6c2015-04-24 19:14:22 +0100902 // of the declaring class, no class initialization is needed
903 // before the static method call.
Nicolas Geoffrayafd06412015-06-20 22:44:47 +0100904 // Note that in case of inlining, we do not need to add clinit checks
905 // to calls that satisfy this subclass check with any inlined methods. This
906 // will be detected by the optimization passes.
Roland Levillain4c0eb422015-04-24 16:43:49 +0100907 clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
908 } else if (storage_index != DexFile::kDexNoIndex) {
909 // If the method's class type index is available, check
910 // whether we should add an explicit class initialization
911 // check for its declaring class before the static method call.
912
913 // TODO: find out why this check is needed.
914 bool is_in_dex_cache = compiler_driver_->CanAssumeTypeIsPresentInDexCache(
915 *outer_compilation_unit_->GetDexFile(), storage_index);
916 bool is_initialized =
917 resolved_method->GetDeclaringClass()->IsInitialized() && is_in_dex_cache;
918
919 if (is_initialized) {
920 clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
921 } else {
922 clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +0100923 HLoadClass* load_class = new (arena_) HLoadClass(
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100924 graph_->GetCurrentMethod(),
925 storage_index,
926 *dex_compilation_unit_->GetDexFile(),
Nicolas Geoffrayafd06412015-06-20 22:44:47 +0100927 is_outer_class,
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100928 dex_pc);
Roland Levillain4c0eb422015-04-24 16:43:49 +0100929 current_block_->AddInstruction(load_class);
930 clinit_check = new (arena_) HClinitCheck(load_class, dex_pc);
931 current_block_->AddInstruction(clinit_check);
Roland Levillain4c0eb422015-04-24 16:43:49 +0100932 }
933 }
934 }
935
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100936 invoke = new (arena_) HInvokeStaticOrDirect(arena_,
937 number_of_arguments,
938 return_type,
939 dex_pc,
940 target_method.dex_method_index,
941 is_recursive,
942 string_init_offset,
943 invoke_type,
944 optimized_invoke_type,
945 clinit_check_requirement);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100946 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100947
948 size_t start_index = 0;
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000949 Temporaries temps(graph_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100950 if (is_instance_call) {
951 HInstruction* arg = LoadLocal(is_range ? register_index : args[0], Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000952 HNullCheck* null_check = new (arena_) HNullCheck(arg, dex_pc);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100953 current_block_->AddInstruction(null_check);
954 temps.Add(null_check);
955 invoke->SetArgumentAt(0, null_check);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100956 start_index = 1;
957 }
958
Nicolas Geoffray2e335252015-06-18 11:11:27 +0100959 uint32_t descriptor_index = 1; // Skip the return type.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100960 uint32_t argument_index = start_index;
Jeff Hao848f70a2014-01-15 13:49:50 -0800961 if (is_string_init) {
962 start_index = 1;
963 }
Nicolas Geoffray2e335252015-06-18 11:11:27 +0100964 for (size_t i = start_index;
965 // Make sure we don't go over the expected arguments or over the number of
966 // dex registers given. If the instruction was seen as dead by the verifier,
967 // it hasn't been properly checked.
968 (i < number_of_vreg_arguments) && (argument_index < number_of_arguments);
969 i++, argument_index++) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100970 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100971 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
Nicolas Geoffray2e335252015-06-18 11:11:27 +0100972 if (!is_range
973 && is_wide
974 && ((i + 1 == number_of_vreg_arguments) || (args[i] + 1 != args[i + 1]))) {
975 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
976 // reject any class where this is violated. However, the verifier only does these checks
977 // on non trivially dead instructions, so we just bailout the compilation.
978 VLOG(compiler) << "Did not compile "
979 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
980 << " because of non-sequential dex register pair in wide argument";
981 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
982 return false;
983 }
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100984 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
985 invoke->SetArgumentAt(argument_index, arg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100986 if (is_wide) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100987 i++;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100988 }
989 }
Nicolas Geoffray2e335252015-06-18 11:11:27 +0100990
991 if (argument_index != number_of_arguments) {
992 VLOG(compiler) << "Did not compile "
993 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
994 << " because of wrong number of arguments in invoke instruction";
995 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
996 return false;
997 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100998
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100999 if (invoke->IsInvokeStaticOrDirect()) {
1000 invoke->SetArgumentAt(argument_index, graph_->GetCurrentMethod());
1001 argument_index++;
1002 }
1003
Roland Levillain4c0eb422015-04-24 16:43:49 +01001004 if (clinit_check_requirement == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit) {
1005 // Add the class initialization check as last input of `invoke`.
1006 DCHECK(clinit_check != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01001007 invoke->SetArgumentAt(argument_index, clinit_check);
Roland Levillain4c0eb422015-04-24 16:43:49 +01001008 }
1009
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001010 current_block_->AddInstruction(invoke);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001011 latest_result_ = invoke;
Jeff Hao848f70a2014-01-15 13:49:50 -08001012
1013 // Add move-result for StringFactory method.
1014 if (is_string_init) {
1015 uint32_t orig_this_reg = is_range ? register_index : args[0];
Nicolas Geoffrayaa919202015-06-21 18:57:02 +01001016 UpdateLocal(orig_this_reg, invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001017 const VerifiedMethod* verified_method =
1018 compiler_driver_->GetVerifiedMethod(dex_file_, dex_compilation_unit_->GetDexMethodIndex());
1019 if (verified_method == nullptr) {
1020 LOG(WARNING) << "No verified method for method calling String.<init>: "
1021 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_);
1022 return false;
1023 }
1024 const SafeMap<uint32_t, std::set<uint32_t>>& string_init_map =
1025 verified_method->GetStringInitPcRegMap();
1026 auto map_it = string_init_map.find(dex_pc);
1027 if (map_it != string_init_map.end()) {
1028 std::set<uint32_t> reg_set = map_it->second;
1029 for (auto set_it = reg_set.begin(); set_it != reg_set.end(); ++set_it) {
Nicolas Geoffrayaa919202015-06-21 18:57:02 +01001030 HInstruction* load_local = LoadLocal(orig_this_reg, Primitive::kPrimNot);
1031 UpdateLocal(*set_it, load_local);
Jeff Hao848f70a2014-01-15 13:49:50 -08001032 }
1033 }
Jeff Hao848f70a2014-01-15 13:49:50 -08001034 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001035 return true;
1036}
1037
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001038bool HGraphBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001039 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001040 bool is_put) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001041 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1042 uint32_t obj_reg = instruction.VRegB_22c();
1043 uint16_t field_index = instruction.VRegC_22c();
1044
1045 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierc7853442015-03-27 14:35:38 -07001046 ArtField* resolved_field =
1047 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001048
Mathieu Chartierc7853442015-03-27 14:35:38 -07001049 if (resolved_field == nullptr) {
Calin Juravle48c2b032014-12-09 18:11:36 +00001050 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedField);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001051 return false;
1052 }
Calin Juravle52c48962014-12-16 17:02:57 +00001053
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001054 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001055
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001056 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00001057 current_block_->AddInstruction(new (arena_) HNullCheck(object, dex_pc));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001058 if (is_put) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001059 Temporaries temps(graph_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001060 HInstruction* null_check = current_block_->GetLastInstruction();
1061 // We need one temporary for the null check.
1062 temps.Add(null_check);
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001063 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001064 current_block_->AddInstruction(new (arena_) HInstanceFieldSet(
1065 null_check,
1066 value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001067 field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00001068 resolved_field->GetOffset(),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001069 resolved_field->IsVolatile(),
1070 field_index,
1071 *dex_file_));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001072 } else {
1073 current_block_->AddInstruction(new (arena_) HInstanceFieldGet(
1074 current_block_->GetLastInstruction(),
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001075 field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00001076 resolved_field->GetOffset(),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001077 resolved_field->IsVolatile(),
1078 field_index,
1079 *dex_file_));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001080
1081 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1082 }
1083 return true;
1084}
1085
Nicolas Geoffray30451742015-06-19 13:32:41 +01001086static mirror::Class* GetClassFrom(CompilerDriver* driver,
1087 const DexCompilationUnit& compilation_unit) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001088 ScopedObjectAccess soa(Thread::Current());
1089 StackHandleScope<2> hs(soa.Self());
Nicolas Geoffray30451742015-06-19 13:32:41 +01001090 const DexFile& dex_file = *compilation_unit.GetDexFile();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001091 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Nicolas Geoffray30451742015-06-19 13:32:41 +01001092 soa.Decode<mirror::ClassLoader*>(compilation_unit.GetClassLoader())));
1093 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
1094 compilation_unit.GetClassLinker()->FindDexCache(dex_file)));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001095
Nicolas Geoffray30451742015-06-19 13:32:41 +01001096 return driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, &compilation_unit);
1097}
1098
1099mirror::Class* HGraphBuilder::GetOutermostCompilingClass() const {
1100 return GetClassFrom(compiler_driver_, *outer_compilation_unit_);
1101}
1102
1103mirror::Class* HGraphBuilder::GetCompilingClass() const {
1104 return GetClassFrom(compiler_driver_, *dex_compilation_unit_);
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001105}
1106
1107bool HGraphBuilder::IsOutermostCompilingClass(uint16_t type_index) const {
1108 ScopedObjectAccess soa(Thread::Current());
1109 StackHandleScope<4> hs(soa.Self());
1110 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
1111 dex_compilation_unit_->GetClassLinker()->FindDexCache(*dex_compilation_unit_->GetDexFile())));
1112 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1113 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
1114 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
1115 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
Nicolas Geoffrayafd06412015-06-20 22:44:47 +01001116 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001117
Nicolas Geoffrayafd06412015-06-20 22:44:47 +01001118 return outer_class.Get() == cls.Get();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001119}
1120
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001121bool HGraphBuilder::BuildStaticFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001122 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001123 bool is_put) {
1124 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1125 uint16_t field_index = instruction.VRegB_21c();
1126
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001127 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierc7853442015-03-27 14:35:38 -07001128 StackHandleScope<4> hs(soa.Self());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001129 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
1130 dex_compilation_unit_->GetClassLinker()->FindDexCache(*dex_compilation_unit_->GetDexFile())));
1131 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1132 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
Mathieu Chartierc7853442015-03-27 14:35:38 -07001133 ArtField* resolved_field = compiler_driver_->ResolveField(
1134 soa, dex_cache, class_loader, dex_compilation_unit_, field_index, true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001135
Mathieu Chartierc7853442015-03-27 14:35:38 -07001136 if (resolved_field == nullptr) {
Calin Juravle48c2b032014-12-09 18:11:36 +00001137 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedField);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001138 return false;
1139 }
1140
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001141 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
1142 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
1143 outer_compilation_unit_->GetClassLinker()->FindDexCache(outer_dex_file)));
Nicolas Geoffray30451742015-06-19 13:32:41 +01001144 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001145
1146 // The index at which the field's class is stored in the DexCache's type array.
1147 uint32_t storage_index;
Nicolas Geoffray30451742015-06-19 13:32:41 +01001148 bool is_outer_class = (outer_class.Get() == resolved_field->GetDeclaringClass());
1149 if (is_outer_class) {
1150 storage_index = outer_class->GetDexTypeIndex();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001151 } else if (outer_dex_cache.Get() != dex_cache.Get()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001152 // The compiler driver cannot currently understand multiple dex caches involved. Just bailout.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001153 return false;
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001154 } else {
1155 std::pair<bool, bool> pair = compiler_driver_->IsFastStaticField(
1156 outer_dex_cache.Get(),
Nicolas Geoffray30451742015-06-19 13:32:41 +01001157 GetCompilingClass(),
Mathieu Chartierc7853442015-03-27 14:35:38 -07001158 resolved_field,
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001159 field_index,
1160 &storage_index);
1161 bool can_easily_access = is_put ? pair.second : pair.first;
1162 if (!can_easily_access) {
1163 return false;
1164 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001165 }
1166
1167 // TODO: find out why this check is needed.
1168 bool is_in_dex_cache = compiler_driver_->CanAssumeTypeIsPresentInDexCache(
Nicolas Geoffray6a816cf2015-03-24 16:17:56 +00001169 *outer_compilation_unit_->GetDexFile(), storage_index);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001170 bool is_initialized = resolved_field->GetDeclaringClass()->IsInitialized() && is_in_dex_cache;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001171
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001172 HLoadClass* constant = new (arena_) HLoadClass(graph_->GetCurrentMethod(),
1173 storage_index,
1174 *dex_compilation_unit_->GetDexFile(),
Nicolas Geoffray30451742015-06-19 13:32:41 +01001175 is_outer_class,
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001176 dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001177 current_block_->AddInstruction(constant);
1178
1179 HInstruction* cls = constant;
Nicolas Geoffray30451742015-06-19 13:32:41 +01001180 if (!is_initialized && !is_outer_class) {
Calin Juravle225ff812014-11-13 16:46:39 +00001181 cls = new (arena_) HClinitCheck(constant, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001182 current_block_->AddInstruction(cls);
1183 }
1184
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001185 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001186 if (is_put) {
1187 // We need to keep the class alive before loading the value.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001188 Temporaries temps(graph_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001189 temps.Add(cls);
1190 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1191 DCHECK_EQ(value->GetType(), field_type);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001192 current_block_->AddInstruction(new (arena_) HStaticFieldSet(cls,
1193 value,
1194 field_type,
1195 resolved_field->GetOffset(),
1196 resolved_field->IsVolatile(),
1197 field_index,
1198 *dex_file_));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001199 } else {
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001200 current_block_->AddInstruction(new (arena_) HStaticFieldGet(cls,
1201 field_type,
1202 resolved_field->GetOffset(),
1203 resolved_field->IsVolatile(),
1204 field_index,
1205 *dex_file_));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001206 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1207 }
1208 return true;
1209}
1210
Calin Juravlebacfec32014-11-14 15:54:36 +00001211void HGraphBuilder::BuildCheckedDivRem(uint16_t out_vreg,
1212 uint16_t first_vreg,
1213 int64_t second_vreg_or_constant,
1214 uint32_t dex_pc,
1215 Primitive::Type type,
1216 bool second_is_constant,
1217 bool isDiv) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001218 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Calin Juravled0d48522014-11-04 16:40:20 +00001219
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001220 HInstruction* first = LoadLocal(first_vreg, type);
1221 HInstruction* second = nullptr;
1222 if (second_is_constant) {
1223 if (type == Primitive::kPrimInt) {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001224 second = graph_->GetIntConstant(second_vreg_or_constant);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001225 } else {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001226 second = graph_->GetLongConstant(second_vreg_or_constant);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001227 }
1228 } else {
1229 second = LoadLocal(second_vreg_or_constant, type);
1230 }
1231
1232 if (!second_is_constant
1233 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0)
1234 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) {
1235 second = new (arena_) HDivZeroCheck(second, dex_pc);
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001236 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +00001237 current_block_->AddInstruction(second);
1238 temps.Add(current_block_->GetLastInstruction());
1239 }
1240
Calin Juravlebacfec32014-11-14 15:54:36 +00001241 if (isDiv) {
1242 current_block_->AddInstruction(new (arena_) HDiv(type, first, second, dex_pc));
1243 } else {
1244 current_block_->AddInstruction(new (arena_) HRem(type, first, second, dex_pc));
1245 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001246 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
Calin Juravled0d48522014-11-04 16:40:20 +00001247}
1248
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001249void HGraphBuilder::BuildArrayAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001250 uint32_t dex_pc,
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001251 bool is_put,
1252 Primitive::Type anticipated_type) {
1253 uint8_t source_or_dest_reg = instruction.VRegA_23x();
1254 uint8_t array_reg = instruction.VRegB_23x();
1255 uint8_t index_reg = instruction.VRegC_23x();
1256
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001257 // We need one temporary for the null check, one for the index, and one for the length.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001258 Temporaries temps(graph_);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001259
1260 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00001261 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001262 current_block_->AddInstruction(object);
1263 temps.Add(object);
1264
1265 HInstruction* length = new (arena_) HArrayLength(object);
1266 current_block_->AddInstruction(length);
1267 temps.Add(length);
1268 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt);
Calin Juravle225ff812014-11-13 16:46:39 +00001269 index = new (arena_) HBoundsCheck(index, length, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001270 current_block_->AddInstruction(index);
1271 temps.Add(index);
1272 if (is_put) {
1273 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
1274 // TODO: Insert a type check node if the type is Object.
Nicolas Geoffray39468442014-09-02 15:17:15 +01001275 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001276 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001277 } else {
1278 current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type));
1279 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1280 }
Mark Mendell1152c922015-04-24 17:06:35 -04001281 graph_->SetHasBoundsChecks(true);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001282}
1283
Calin Juravle225ff812014-11-13 16:46:39 +00001284void HGraphBuilder::BuildFilledNewArray(uint32_t dex_pc,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001285 uint32_t type_index,
1286 uint32_t number_of_vreg_arguments,
1287 bool is_range,
1288 uint32_t* args,
1289 uint32_t register_index) {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001290 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments);
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001291 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
1292 ? kQuickAllocArrayWithAccessCheck
1293 : kQuickAllocArray;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01001294 HInstruction* object = new (arena_) HNewArray(length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01001295 graph_->GetCurrentMethod(),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01001296 dex_pc,
1297 type_index,
1298 *dex_compilation_unit_->GetDexFile(),
1299 entrypoint);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001300 current_block_->AddInstruction(object);
1301
1302 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1303 DCHECK_EQ(descriptor[0], '[') << descriptor;
1304 char primitive = descriptor[1];
1305 DCHECK(primitive == 'I'
1306 || primitive == 'L'
1307 || primitive == '[') << descriptor;
1308 bool is_reference_array = (primitive == 'L') || (primitive == '[');
1309 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
1310
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001311 Temporaries temps(graph_);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001312 temps.Add(object);
1313 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
1314 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
David Brazdil8d5b8b22015-03-24 10:51:52 +00001315 HInstruction* index = graph_->GetIntConstant(i);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001316 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001317 new (arena_) HArraySet(object, index, value, type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001318 }
1319 latest_result_ = object;
1320}
1321
1322template <typename T>
1323void HGraphBuilder::BuildFillArrayData(HInstruction* object,
1324 const T* data,
1325 uint32_t element_count,
1326 Primitive::Type anticipated_type,
Calin Juravle225ff812014-11-13 16:46:39 +00001327 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001328 for (uint32_t i = 0; i < element_count; ++i) {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001329 HInstruction* index = graph_->GetIntConstant(i);
1330 HInstruction* value = graph_->GetIntConstant(data[i]);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001331 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001332 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001333 }
1334}
1335
Calin Juravle225ff812014-11-13 16:46:39 +00001336void HGraphBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001337 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +00001338 HInstruction* array = LoadLocal(instruction.VRegA_31t(), Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00001339 HNullCheck* null_check = new (arena_) HNullCheck(array, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001340 current_block_->AddInstruction(null_check);
1341 temps.Add(null_check);
1342
1343 HInstruction* length = new (arena_) HArrayLength(null_check);
1344 current_block_->AddInstruction(length);
1345
Calin Juravle225ff812014-11-13 16:46:39 +00001346 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
Calin Juravled0d48522014-11-04 16:40:20 +00001347 const Instruction::ArrayDataPayload* payload =
1348 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_start_ + payload_offset);
1349 const uint8_t* data = payload->data;
1350 uint32_t element_count = payload->element_count;
1351
1352 // Implementation of this DEX instruction seems to be that the bounds check is
1353 // done before doing any stores.
David Brazdil8d5b8b22015-03-24 10:51:52 +00001354 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1);
Calin Juravle225ff812014-11-13 16:46:39 +00001355 current_block_->AddInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
Calin Juravled0d48522014-11-04 16:40:20 +00001356
1357 switch (payload->element_width) {
1358 case 1:
1359 BuildFillArrayData(null_check,
1360 reinterpret_cast<const int8_t*>(data),
1361 element_count,
1362 Primitive::kPrimByte,
Calin Juravle225ff812014-11-13 16:46:39 +00001363 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001364 break;
1365 case 2:
1366 BuildFillArrayData(null_check,
1367 reinterpret_cast<const int16_t*>(data),
1368 element_count,
1369 Primitive::kPrimShort,
Calin Juravle225ff812014-11-13 16:46:39 +00001370 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001371 break;
1372 case 4:
1373 BuildFillArrayData(null_check,
1374 reinterpret_cast<const int32_t*>(data),
1375 element_count,
1376 Primitive::kPrimInt,
Calin Juravle225ff812014-11-13 16:46:39 +00001377 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001378 break;
1379 case 8:
1380 BuildFillWideArrayData(null_check,
1381 reinterpret_cast<const int64_t*>(data),
1382 element_count,
Calin Juravle225ff812014-11-13 16:46:39 +00001383 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001384 break;
1385 default:
1386 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1387 }
Mark Mendell1152c922015-04-24 17:06:35 -04001388 graph_->SetHasBoundsChecks(true);
Calin Juravled0d48522014-11-04 16:40:20 +00001389}
1390
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001391void HGraphBuilder::BuildFillWideArrayData(HInstruction* object,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +01001392 const int64_t* data,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001393 uint32_t element_count,
Calin Juravle225ff812014-11-13 16:46:39 +00001394 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001395 for (uint32_t i = 0; i < element_count; ++i) {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001396 HInstruction* index = graph_->GetIntConstant(i);
1397 HInstruction* value = graph_->GetLongConstant(data[i]);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001398 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001399 object, index, value, Primitive::kPrimLong, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001400 }
1401}
1402
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001403bool HGraphBuilder::BuildTypeCheck(const Instruction& instruction,
1404 uint8_t destination,
1405 uint8_t reference,
1406 uint16_t type_index,
Calin Juravle225ff812014-11-13 16:46:39 +00001407 uint32_t dex_pc) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001408 bool type_known_final;
1409 bool type_known_abstract;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001410 // `CanAccessTypeWithoutChecks` will tell whether the method being
1411 // built is trying to access its own class, so that the generated
1412 // code can optimize for this case. However, the optimization does not
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001413 // work for inlining, so we use `IsOutermostCompilingClass` instead.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001414 bool dont_use_is_referrers_class;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001415 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
1416 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001417 &type_known_final, &type_known_abstract, &dont_use_is_referrers_class);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001418 if (!can_access) {
Calin Juravle48c2b032014-12-09 18:11:36 +00001419 MaybeRecordStat(MethodCompilationStat::kNotCompiledCantAccesType);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001420 return false;
1421 }
1422 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot);
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001423 HLoadClass* cls = new (arena_) HLoadClass(
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001424 graph_->GetCurrentMethod(),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01001425 type_index,
1426 *dex_compilation_unit_->GetDexFile(),
1427 IsOutermostCompilingClass(type_index),
1428 dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001429 current_block_->AddInstruction(cls);
1430 // The class needs a temporary before being used by the type check.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001431 Temporaries temps(graph_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001432 temps.Add(cls);
1433 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
1434 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001435 new (arena_) HInstanceOf(object, cls, type_known_final, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001436 UpdateLocal(destination, current_block_->GetLastInstruction());
1437 } else {
1438 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
1439 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001440 new (arena_) HCheckCast(object, cls, type_known_final, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001441 }
1442 return true;
1443}
1444
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001445bool HGraphBuilder::NeedsAccessCheck(uint32_t type_index) const {
1446 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
1447 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index);
1448}
1449
Calin Juravle48c2b032014-12-09 18:11:36 +00001450void HGraphBuilder::BuildPackedSwitch(const Instruction& instruction, uint32_t dex_pc) {
David Brazdil2ef645b2015-06-17 18:20:52 +01001451 // Verifier guarantees that the payload for PackedSwitch contains:
1452 // (a) number of entries (may be zero)
1453 // (b) first and lowest switch case value (entry 0, always present)
1454 // (c) list of target pcs (entries 1 <= i <= N)
Andreas Gamped881df52014-11-24 23:28:39 -08001455 SwitchTable table(instruction, dex_pc, false);
1456
1457 // Value to test against.
1458 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
1459
David Brazdil2ef645b2015-06-17 18:20:52 +01001460 // Retrieve number of entries.
Andreas Gampee4d4d322014-12-04 09:09:57 -08001461 uint16_t num_entries = table.GetNumEntries();
David Brazdil2ef645b2015-06-17 18:20:52 +01001462 if (num_entries == 0) {
1463 return;
1464 }
Andreas Gampee4d4d322014-12-04 09:09:57 -08001465
Andreas Gamped881df52014-11-24 23:28:39 -08001466 // Chained cmp-and-branch, starting from starting_key.
1467 int32_t starting_key = table.GetEntryAt(0);
1468
Andreas Gamped881df52014-11-24 23:28:39 -08001469 for (size_t i = 1; i <= num_entries; i++) {
Andreas Gampee4d4d322014-12-04 09:09:57 -08001470 BuildSwitchCaseHelper(instruction, i, i == num_entries, table, value, starting_key + i - 1,
1471 table.GetEntryAt(i), dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08001472 }
Andreas Gamped881df52014-11-24 23:28:39 -08001473}
1474
Calin Juravle48c2b032014-12-09 18:11:36 +00001475void HGraphBuilder::BuildSparseSwitch(const Instruction& instruction, uint32_t dex_pc) {
David Brazdil2ef645b2015-06-17 18:20:52 +01001476 // Verifier guarantees that the payload for SparseSwitch contains:
1477 // (a) number of entries (may be zero)
1478 // (b) sorted key values (entries 0 <= i < N)
1479 // (c) target pcs corresponding to the switch values (entries N <= i < 2*N)
Andreas Gampee4d4d322014-12-04 09:09:57 -08001480 SwitchTable table(instruction, dex_pc, true);
1481
1482 // Value to test against.
1483 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
1484
1485 uint16_t num_entries = table.GetNumEntries();
Andreas Gampee4d4d322014-12-04 09:09:57 -08001486
1487 for (size_t i = 0; i < num_entries; i++) {
1488 BuildSwitchCaseHelper(instruction, i, i == static_cast<size_t>(num_entries) - 1, table, value,
1489 table.GetEntryAt(i), table.GetEntryAt(i + num_entries), dex_pc);
1490 }
Andreas Gampee4d4d322014-12-04 09:09:57 -08001491}
1492
1493void HGraphBuilder::BuildSwitchCaseHelper(const Instruction& instruction, size_t index,
1494 bool is_last_case, const SwitchTable& table,
1495 HInstruction* value, int32_t case_value_int,
1496 int32_t target_offset, uint32_t dex_pc) {
David Brazdil852eaff2015-02-02 15:23:05 +00001497 HBasicBlock* case_target = FindBlockStartingAt(dex_pc + target_offset);
1498 DCHECK(case_target != nullptr);
1499 PotentiallyAddSuspendCheck(case_target, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001500
1501 // The current case's value.
David Brazdil8d5b8b22015-03-24 10:51:52 +00001502 HInstruction* this_case_value = graph_->GetIntConstant(case_value_int);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001503
1504 // Compare value and this_case_value.
1505 HEqual* comparison = new (arena_) HEqual(value, this_case_value);
1506 current_block_->AddInstruction(comparison);
1507 HInstruction* ifinst = new (arena_) HIf(comparison);
1508 current_block_->AddInstruction(ifinst);
1509
1510 // Case hit: use the target offset to determine where to go.
Andreas Gampee4d4d322014-12-04 09:09:57 -08001511 current_block_->AddSuccessor(case_target);
1512
1513 // Case miss: go to the next case (or default fall-through).
1514 // When there is a next case, we use the block stored with the table offset representing this
1515 // case (that is where we registered them in ComputeBranchTargets).
1516 // When there is no next case, we use the following instruction.
1517 // TODO: Find a good way to peel the last iteration to avoid conditional, but still have re-use.
1518 if (!is_last_case) {
1519 HBasicBlock* next_case_target = FindBlockStartingAt(table.GetDexPcForIndex(index));
1520 DCHECK(next_case_target != nullptr);
1521 current_block_->AddSuccessor(next_case_target);
1522
1523 // Need to manually add the block, as there is no dex-pc transition for the cases.
1524 graph_->AddBlock(next_case_target);
1525
1526 current_block_ = next_case_target;
1527 } else {
1528 HBasicBlock* default_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
1529 DCHECK(default_target != nullptr);
1530 current_block_->AddSuccessor(default_target);
1531 current_block_ = nullptr;
1532 }
1533}
1534
David Brazdil852eaff2015-02-02 15:23:05 +00001535void HGraphBuilder::PotentiallyAddSuspendCheck(HBasicBlock* target, uint32_t dex_pc) {
1536 int32_t target_offset = target->GetDexPc() - dex_pc;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001537 if (target_offset <= 0) {
David Brazdil852eaff2015-02-02 15:23:05 +00001538 // DX generates back edges to the first encountered return. We can save
1539 // time of later passes by not adding redundant suspend checks.
David Brazdil2fd6aa52015-02-02 18:58:27 +00001540 HInstruction* last_in_target = target->GetLastInstruction();
1541 if (last_in_target != nullptr &&
1542 (last_in_target->IsReturn() || last_in_target->IsReturnVoid())) {
1543 return;
David Brazdil852eaff2015-02-02 15:23:05 +00001544 }
1545
1546 // Add a suspend check to backward branches which may potentially loop. We
1547 // can remove them after we recognize loops in the graph.
Calin Juravle225ff812014-11-13 16:46:39 +00001548 current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_pc));
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001549 }
1550}
1551
Calin Juravle225ff812014-11-13 16:46:39 +00001552bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001553 if (current_block_ == nullptr) {
1554 return true; // Dead code
1555 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001556
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001557 switch (instruction.Opcode()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001558 case Instruction::CONST_4: {
1559 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001560 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001561 UpdateLocal(register_index, constant);
1562 break;
1563 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001564
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001565 case Instruction::CONST_16: {
1566 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001567 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001568 UpdateLocal(register_index, constant);
1569 break;
1570 }
1571
Dave Allison20dfc792014-06-16 20:44:29 -07001572 case Instruction::CONST: {
1573 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001574 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i());
Dave Allison20dfc792014-06-16 20:44:29 -07001575 UpdateLocal(register_index, constant);
1576 break;
1577 }
1578
1579 case Instruction::CONST_HIGH16: {
1580 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001581 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16);
Dave Allison20dfc792014-06-16 20:44:29 -07001582 UpdateLocal(register_index, constant);
1583 break;
1584 }
1585
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001586 case Instruction::CONST_WIDE_16: {
1587 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001588 // Get 16 bits of constant value, sign extended to 64 bits.
1589 int64_t value = instruction.VRegB_21s();
1590 value <<= 48;
1591 value >>= 48;
David Brazdil8d5b8b22015-03-24 10:51:52 +00001592 HLongConstant* constant = graph_->GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001593 UpdateLocal(register_index, constant);
1594 break;
1595 }
1596
1597 case Instruction::CONST_WIDE_32: {
1598 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001599 // Get 32 bits of constant value, sign extended to 64 bits.
1600 int64_t value = instruction.VRegB_31i();
1601 value <<= 32;
1602 value >>= 32;
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: {
1609 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001610 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001611 UpdateLocal(register_index, constant);
1612 break;
1613 }
1614
Dave Allison20dfc792014-06-16 20:44:29 -07001615 case Instruction::CONST_WIDE_HIGH16: {
1616 int32_t register_index = instruction.VRegA();
1617 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
David Brazdil8d5b8b22015-03-24 10:51:52 +00001618 HLongConstant* constant = graph_->GetLongConstant(value);
Dave Allison20dfc792014-06-16 20:44:29 -07001619 UpdateLocal(register_index, constant);
1620 break;
1621 }
1622
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001623 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001624 case Instruction::MOVE:
1625 case Instruction::MOVE_FROM16:
1626 case Instruction::MOVE_16: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001627 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001628 UpdateLocal(instruction.VRegA(), value);
1629 break;
1630 }
1631
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001632 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001633 case Instruction::MOVE_WIDE:
1634 case Instruction::MOVE_WIDE_FROM16:
1635 case Instruction::MOVE_WIDE_16: {
1636 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong);
1637 UpdateLocal(instruction.VRegA(), value);
1638 break;
1639 }
1640
1641 case Instruction::MOVE_OBJECT:
1642 case Instruction::MOVE_OBJECT_16:
1643 case Instruction::MOVE_OBJECT_FROM16: {
1644 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot);
1645 UpdateLocal(instruction.VRegA(), value);
1646 break;
1647 }
1648
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001649 case Instruction::RETURN_VOID: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001650 BuildReturn(instruction, Primitive::kPrimVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001651 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001652 }
1653
Dave Allison20dfc792014-06-16 20:44:29 -07001654#define IF_XX(comparison, cond) \
Calin Juravle225ff812014-11-13 16:46:39 +00001655 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
1656 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001657
Dave Allison20dfc792014-06-16 20:44:29 -07001658 IF_XX(HEqual, EQ);
1659 IF_XX(HNotEqual, NE);
1660 IF_XX(HLessThan, LT);
1661 IF_XX(HLessThanOrEqual, LE);
1662 IF_XX(HGreaterThan, GT);
1663 IF_XX(HGreaterThanOrEqual, GE);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001664
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001665 case Instruction::GOTO:
1666 case Instruction::GOTO_16:
1667 case Instruction::GOTO_32: {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001668 int32_t offset = instruction.GetTargetOffset();
Calin Juravle225ff812014-11-13 16:46:39 +00001669 HBasicBlock* target = FindBlockStartingAt(offset + dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001670 DCHECK(target != nullptr);
David Brazdil852eaff2015-02-02 15:23:05 +00001671 PotentiallyAddSuspendCheck(target, dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001672 current_block_->AddInstruction(new (arena_) HGoto());
1673 current_block_->AddSuccessor(target);
1674 current_block_ = nullptr;
1675 break;
1676 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001677
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001678 case Instruction::RETURN: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001679 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001680 break;
1681 }
1682
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001683 case Instruction::RETURN_OBJECT: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001684 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001685 break;
1686 }
1687
1688 case Instruction::RETURN_WIDE: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001689 BuildReturn(instruction, return_type_);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001690 break;
1691 }
1692
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001693 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001694 case Instruction::INVOKE_INTERFACE:
1695 case Instruction::INVOKE_STATIC:
1696 case Instruction::INVOKE_SUPER:
1697 case Instruction::INVOKE_VIRTUAL: {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001698 uint32_t method_idx = instruction.VRegB_35c();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001699 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001700 uint32_t args[5];
Ian Rogers29a26482014-05-02 15:27:29 -07001701 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00001702 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001703 number_of_vreg_arguments, false, args, -1)) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001704 return false;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001705 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001706 break;
1707 }
1708
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001709 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001710 case Instruction::INVOKE_INTERFACE_RANGE:
1711 case Instruction::INVOKE_STATIC_RANGE:
1712 case Instruction::INVOKE_SUPER_RANGE:
1713 case Instruction::INVOKE_VIRTUAL_RANGE: {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001714 uint32_t method_idx = instruction.VRegB_3rc();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001715 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1716 uint32_t register_index = instruction.VRegC();
Calin Juravle225ff812014-11-13 16:46:39 +00001717 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001718 number_of_vreg_arguments, true, nullptr, register_index)) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001719 return false;
1720 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001721 break;
1722 }
1723
Roland Levillain88cb1752014-10-20 16:36:47 +01001724 case Instruction::NEG_INT: {
1725 Unop_12x<HNeg>(instruction, Primitive::kPrimInt);
1726 break;
1727 }
1728
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001729 case Instruction::NEG_LONG: {
1730 Unop_12x<HNeg>(instruction, Primitive::kPrimLong);
1731 break;
1732 }
1733
Roland Levillain3dbcb382014-10-28 17:30:07 +00001734 case Instruction::NEG_FLOAT: {
1735 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat);
1736 break;
1737 }
1738
1739 case Instruction::NEG_DOUBLE: {
1740 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble);
1741 break;
1742 }
1743
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001744 case Instruction::NOT_INT: {
1745 Unop_12x<HNot>(instruction, Primitive::kPrimInt);
1746 break;
1747 }
1748
Roland Levillain70566432014-10-24 16:20:17 +01001749 case Instruction::NOT_LONG: {
1750 Unop_12x<HNot>(instruction, Primitive::kPrimLong);
1751 break;
1752 }
1753
Roland Levillaindff1f282014-11-05 14:15:05 +00001754 case Instruction::INT_TO_LONG: {
Roland Levillain624279f2014-12-04 11:54:28 +00001755 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong, dex_pc);
Roland Levillaindff1f282014-11-05 14:15:05 +00001756 break;
1757 }
1758
Roland Levillaincff13742014-11-17 14:32:17 +00001759 case Instruction::INT_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001760 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00001761 break;
1762 }
1763
1764 case Instruction::INT_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001765 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00001766 break;
1767 }
1768
Roland Levillain946e1432014-11-11 17:35:19 +00001769 case Instruction::LONG_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001770 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt, dex_pc);
Roland Levillain946e1432014-11-11 17:35:19 +00001771 break;
1772 }
1773
Roland Levillain6d0e4832014-11-27 18:31:21 +00001774 case Instruction::LONG_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001775 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimFloat, dex_pc);
Roland Levillain6d0e4832014-11-27 18:31:21 +00001776 break;
1777 }
1778
Roland Levillain647b9ed2014-11-27 12:06:00 +00001779 case Instruction::LONG_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001780 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble, dex_pc);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001781 break;
1782 }
1783
Roland Levillain3f8f9362014-12-02 17:45:01 +00001784 case Instruction::FLOAT_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001785 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimInt, dex_pc);
1786 break;
1787 }
1788
1789 case Instruction::FLOAT_TO_LONG: {
1790 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimLong, dex_pc);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001791 break;
1792 }
1793
Roland Levillain8964e2b2014-12-04 12:10:50 +00001794 case Instruction::FLOAT_TO_DOUBLE: {
1795 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimDouble, dex_pc);
1796 break;
1797 }
1798
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001799 case Instruction::DOUBLE_TO_INT: {
1800 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimInt, dex_pc);
1801 break;
1802 }
1803
1804 case Instruction::DOUBLE_TO_LONG: {
1805 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimLong, dex_pc);
1806 break;
1807 }
1808
Roland Levillain8964e2b2014-12-04 12:10:50 +00001809 case Instruction::DOUBLE_TO_FLOAT: {
1810 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimFloat, dex_pc);
1811 break;
1812 }
1813
Roland Levillain51d3fc42014-11-13 14:11:42 +00001814 case Instruction::INT_TO_BYTE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001815 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte, dex_pc);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001816 break;
1817 }
1818
Roland Levillain01a8d712014-11-14 16:27:39 +00001819 case Instruction::INT_TO_SHORT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001820 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort, dex_pc);
Roland Levillain01a8d712014-11-14 16:27:39 +00001821 break;
1822 }
1823
Roland Levillain981e4542014-11-14 11:47:14 +00001824 case Instruction::INT_TO_CHAR: {
Roland Levillain624279f2014-12-04 11:54:28 +00001825 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar, dex_pc);
Roland Levillain981e4542014-11-14 11:47:14 +00001826 break;
1827 }
1828
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001829 case Instruction::ADD_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001830 Binop_23x<HAdd>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001831 break;
1832 }
1833
1834 case Instruction::ADD_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001835 Binop_23x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001836 break;
1837 }
1838
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001839 case Instruction::ADD_DOUBLE: {
1840 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble);
1841 break;
1842 }
1843
1844 case Instruction::ADD_FLOAT: {
1845 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat);
1846 break;
1847 }
1848
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001849 case Instruction::SUB_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001850 Binop_23x<HSub>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001851 break;
1852 }
1853
1854 case Instruction::SUB_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001855 Binop_23x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001856 break;
1857 }
1858
Calin Juravle096cc022014-10-23 17:01:13 +01001859 case Instruction::SUB_FLOAT: {
1860 Binop_23x<HSub>(instruction, Primitive::kPrimFloat);
1861 break;
1862 }
1863
1864 case Instruction::SUB_DOUBLE: {
1865 Binop_23x<HSub>(instruction, Primitive::kPrimDouble);
1866 break;
1867 }
1868
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001869 case Instruction::ADD_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001870 Binop_12x<HAdd>(instruction, Primitive::kPrimInt);
1871 break;
1872 }
1873
Calin Juravle34bacdf2014-10-07 20:23:36 +01001874 case Instruction::MUL_INT: {
1875 Binop_23x<HMul>(instruction, Primitive::kPrimInt);
1876 break;
1877 }
1878
1879 case Instruction::MUL_LONG: {
1880 Binop_23x<HMul>(instruction, Primitive::kPrimLong);
1881 break;
1882 }
1883
Calin Juravleb5bfa962014-10-21 18:02:24 +01001884 case Instruction::MUL_FLOAT: {
1885 Binop_23x<HMul>(instruction, Primitive::kPrimFloat);
1886 break;
1887 }
1888
1889 case Instruction::MUL_DOUBLE: {
1890 Binop_23x<HMul>(instruction, Primitive::kPrimDouble);
1891 break;
1892 }
1893
Calin Juravled0d48522014-11-04 16:40:20 +00001894 case Instruction::DIV_INT: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001895 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1896 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravled0d48522014-11-04 16:40:20 +00001897 break;
1898 }
1899
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001900 case Instruction::DIV_LONG: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001901 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1902 dex_pc, Primitive::kPrimLong, false, true);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001903 break;
1904 }
1905
Calin Juravle7c4954d2014-10-28 16:57:40 +00001906 case Instruction::DIV_FLOAT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001907 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001908 break;
1909 }
1910
1911 case Instruction::DIV_DOUBLE: {
Calin Juravle225ff812014-11-13 16:46:39 +00001912 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001913 break;
1914 }
1915
Calin Juravlebacfec32014-11-14 15:54:36 +00001916 case Instruction::REM_INT: {
1917 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1918 dex_pc, Primitive::kPrimInt, false, false);
1919 break;
1920 }
1921
1922 case Instruction::REM_LONG: {
1923 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1924 dex_pc, Primitive::kPrimLong, false, false);
1925 break;
1926 }
1927
Calin Juravled2ec87d2014-12-08 14:24:46 +00001928 case Instruction::REM_FLOAT: {
1929 Binop_23x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
1930 break;
1931 }
1932
1933 case Instruction::REM_DOUBLE: {
1934 Binop_23x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
1935 break;
1936 }
1937
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001938 case Instruction::AND_INT: {
1939 Binop_23x<HAnd>(instruction, Primitive::kPrimInt);
1940 break;
1941 }
1942
1943 case Instruction::AND_LONG: {
1944 Binop_23x<HAnd>(instruction, Primitive::kPrimLong);
1945 break;
1946 }
1947
Calin Juravle9aec02f2014-11-18 23:06:35 +00001948 case Instruction::SHL_INT: {
1949 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt);
1950 break;
1951 }
1952
1953 case Instruction::SHL_LONG: {
1954 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong);
1955 break;
1956 }
1957
1958 case Instruction::SHR_INT: {
1959 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt);
1960 break;
1961 }
1962
1963 case Instruction::SHR_LONG: {
1964 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong);
1965 break;
1966 }
1967
1968 case Instruction::USHR_INT: {
1969 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt);
1970 break;
1971 }
1972
1973 case Instruction::USHR_LONG: {
1974 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong);
1975 break;
1976 }
1977
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001978 case Instruction::OR_INT: {
1979 Binop_23x<HOr>(instruction, Primitive::kPrimInt);
1980 break;
1981 }
1982
1983 case Instruction::OR_LONG: {
1984 Binop_23x<HOr>(instruction, Primitive::kPrimLong);
1985 break;
1986 }
1987
1988 case Instruction::XOR_INT: {
1989 Binop_23x<HXor>(instruction, Primitive::kPrimInt);
1990 break;
1991 }
1992
1993 case Instruction::XOR_LONG: {
1994 Binop_23x<HXor>(instruction, Primitive::kPrimLong);
1995 break;
1996 }
1997
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001998 case Instruction::ADD_LONG_2ADDR: {
1999 Binop_12x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002000 break;
2001 }
2002
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002003 case Instruction::ADD_DOUBLE_2ADDR: {
2004 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble);
2005 break;
2006 }
2007
2008 case Instruction::ADD_FLOAT_2ADDR: {
2009 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat);
2010 break;
2011 }
2012
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002013 case Instruction::SUB_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002014 Binop_12x<HSub>(instruction, Primitive::kPrimInt);
2015 break;
2016 }
2017
2018 case Instruction::SUB_LONG_2ADDR: {
2019 Binop_12x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002020 break;
2021 }
2022
Calin Juravle096cc022014-10-23 17:01:13 +01002023 case Instruction::SUB_FLOAT_2ADDR: {
2024 Binop_12x<HSub>(instruction, Primitive::kPrimFloat);
2025 break;
2026 }
2027
2028 case Instruction::SUB_DOUBLE_2ADDR: {
2029 Binop_12x<HSub>(instruction, Primitive::kPrimDouble);
2030 break;
2031 }
2032
Calin Juravle34bacdf2014-10-07 20:23:36 +01002033 case Instruction::MUL_INT_2ADDR: {
2034 Binop_12x<HMul>(instruction, Primitive::kPrimInt);
2035 break;
2036 }
2037
2038 case Instruction::MUL_LONG_2ADDR: {
2039 Binop_12x<HMul>(instruction, Primitive::kPrimLong);
2040 break;
2041 }
2042
Calin Juravleb5bfa962014-10-21 18:02:24 +01002043 case Instruction::MUL_FLOAT_2ADDR: {
2044 Binop_12x<HMul>(instruction, Primitive::kPrimFloat);
2045 break;
2046 }
2047
2048 case Instruction::MUL_DOUBLE_2ADDR: {
2049 Binop_12x<HMul>(instruction, Primitive::kPrimDouble);
2050 break;
2051 }
2052
Calin Juravle865fc882014-11-06 17:09:03 +00002053 case Instruction::DIV_INT_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002054 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2055 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravle865fc882014-11-06 17:09:03 +00002056 break;
2057 }
2058
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002059 case Instruction::DIV_LONG_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002060 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2061 dex_pc, Primitive::kPrimLong, false, true);
2062 break;
2063 }
2064
2065 case Instruction::REM_INT_2ADDR: {
2066 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2067 dex_pc, Primitive::kPrimInt, false, false);
2068 break;
2069 }
2070
2071 case Instruction::REM_LONG_2ADDR: {
2072 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2073 dex_pc, Primitive::kPrimLong, false, false);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002074 break;
2075 }
2076
Calin Juravled2ec87d2014-12-08 14:24:46 +00002077 case Instruction::REM_FLOAT_2ADDR: {
2078 Binop_12x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2079 break;
2080 }
2081
2082 case Instruction::REM_DOUBLE_2ADDR: {
2083 Binop_12x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2084 break;
2085 }
2086
Calin Juravle9aec02f2014-11-18 23:06:35 +00002087 case Instruction::SHL_INT_2ADDR: {
2088 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt);
2089 break;
2090 }
2091
2092 case Instruction::SHL_LONG_2ADDR: {
2093 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong);
2094 break;
2095 }
2096
2097 case Instruction::SHR_INT_2ADDR: {
2098 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt);
2099 break;
2100 }
2101
2102 case Instruction::SHR_LONG_2ADDR: {
2103 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong);
2104 break;
2105 }
2106
2107 case Instruction::USHR_INT_2ADDR: {
2108 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt);
2109 break;
2110 }
2111
2112 case Instruction::USHR_LONG_2ADDR: {
2113 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong);
2114 break;
2115 }
2116
Calin Juravle7c4954d2014-10-28 16:57:40 +00002117 case Instruction::DIV_FLOAT_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00002118 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002119 break;
2120 }
2121
2122 case Instruction::DIV_DOUBLE_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00002123 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002124 break;
2125 }
2126
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002127 case Instruction::AND_INT_2ADDR: {
2128 Binop_12x<HAnd>(instruction, Primitive::kPrimInt);
2129 break;
2130 }
2131
2132 case Instruction::AND_LONG_2ADDR: {
2133 Binop_12x<HAnd>(instruction, Primitive::kPrimLong);
2134 break;
2135 }
2136
2137 case Instruction::OR_INT_2ADDR: {
2138 Binop_12x<HOr>(instruction, Primitive::kPrimInt);
2139 break;
2140 }
2141
2142 case Instruction::OR_LONG_2ADDR: {
2143 Binop_12x<HOr>(instruction, Primitive::kPrimLong);
2144 break;
2145 }
2146
2147 case Instruction::XOR_INT_2ADDR: {
2148 Binop_12x<HXor>(instruction, Primitive::kPrimInt);
2149 break;
2150 }
2151
2152 case Instruction::XOR_LONG_2ADDR: {
2153 Binop_12x<HXor>(instruction, Primitive::kPrimLong);
2154 break;
2155 }
2156
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002157 case Instruction::ADD_INT_LIT16: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002158 Binop_22s<HAdd>(instruction, false);
2159 break;
2160 }
2161
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002162 case Instruction::AND_INT_LIT16: {
2163 Binop_22s<HAnd>(instruction, false);
2164 break;
2165 }
2166
2167 case Instruction::OR_INT_LIT16: {
2168 Binop_22s<HOr>(instruction, false);
2169 break;
2170 }
2171
2172 case Instruction::XOR_INT_LIT16: {
2173 Binop_22s<HXor>(instruction, false);
2174 break;
2175 }
2176
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002177 case Instruction::RSUB_INT: {
2178 Binop_22s<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002179 break;
2180 }
2181
Calin Juravle34bacdf2014-10-07 20:23:36 +01002182 case Instruction::MUL_INT_LIT16: {
2183 Binop_22s<HMul>(instruction, false);
2184 break;
2185 }
2186
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002187 case Instruction::ADD_INT_LIT8: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002188 Binop_22b<HAdd>(instruction, false);
2189 break;
2190 }
2191
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002192 case Instruction::AND_INT_LIT8: {
2193 Binop_22b<HAnd>(instruction, false);
2194 break;
2195 }
2196
2197 case Instruction::OR_INT_LIT8: {
2198 Binop_22b<HOr>(instruction, false);
2199 break;
2200 }
2201
2202 case Instruction::XOR_INT_LIT8: {
2203 Binop_22b<HXor>(instruction, false);
2204 break;
2205 }
2206
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002207 case Instruction::RSUB_INT_LIT8: {
2208 Binop_22b<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002209 break;
2210 }
2211
Calin Juravle34bacdf2014-10-07 20:23:36 +01002212 case Instruction::MUL_INT_LIT8: {
2213 Binop_22b<HMul>(instruction, false);
2214 break;
2215 }
2216
Calin Juravled0d48522014-11-04 16:40:20 +00002217 case Instruction::DIV_INT_LIT16:
2218 case Instruction::DIV_INT_LIT8: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002219 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2220 dex_pc, Primitive::kPrimInt, true, true);
2221 break;
2222 }
2223
2224 case Instruction::REM_INT_LIT16:
2225 case Instruction::REM_INT_LIT8: {
2226 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2227 dex_pc, Primitive::kPrimInt, true, false);
Calin Juravled0d48522014-11-04 16:40:20 +00002228 break;
2229 }
2230
Calin Juravle9aec02f2014-11-18 23:06:35 +00002231 case Instruction::SHL_INT_LIT8: {
2232 Binop_22b<HShl>(instruction, false);
2233 break;
2234 }
2235
2236 case Instruction::SHR_INT_LIT8: {
2237 Binop_22b<HShr>(instruction, false);
2238 break;
2239 }
2240
2241 case Instruction::USHR_INT_LIT8: {
2242 Binop_22b<HUShr>(instruction, false);
2243 break;
2244 }
2245
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002246 case Instruction::NEW_INSTANCE: {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002247 uint16_t type_index = instruction.VRegB_21c();
Jeff Hao848f70a2014-01-15 13:49:50 -08002248 if (compiler_driver_->IsStringTypeIndex(type_index, dex_file_)) {
2249 // Turn new-instance of string into a const 0.
2250 int32_t register_index = instruction.VRegA();
2251 HNullConstant* constant = graph_->GetNullConstant();
2252 UpdateLocal(register_index, constant);
2253 } else {
2254 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
2255 ? kQuickAllocObjectWithAccessCheck
2256 : kQuickAllocObject;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002257
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002258 current_block_->AddInstruction(new (arena_) HNewInstance(
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002259 graph_->GetCurrentMethod(),
2260 dex_pc,
2261 type_index,
2262 *dex_compilation_unit_->GetDexFile(),
2263 entrypoint));
Jeff Hao848f70a2014-01-15 13:49:50 -08002264 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
2265 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002266 break;
2267 }
2268
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002269 case Instruction::NEW_ARRAY: {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002270 uint16_t type_index = instruction.VRegC_22c();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002271 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt);
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002272 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
2273 ? kQuickAllocArrayWithAccessCheck
2274 : kQuickAllocArray;
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002275 current_block_->AddInstruction(new (arena_) HNewArray(length,
2276 graph_->GetCurrentMethod(),
2277 dex_pc,
2278 type_index,
2279 *dex_compilation_unit_->GetDexFile(),
2280 entrypoint));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002281 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
2282 break;
2283 }
2284
2285 case Instruction::FILLED_NEW_ARRAY: {
2286 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
2287 uint32_t type_index = instruction.VRegB_35c();
2288 uint32_t args[5];
2289 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00002290 BuildFilledNewArray(dex_pc, type_index, number_of_vreg_arguments, false, args, 0);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002291 break;
2292 }
2293
2294 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2295 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
2296 uint32_t type_index = instruction.VRegB_3rc();
2297 uint32_t register_index = instruction.VRegC_3rc();
2298 BuildFilledNewArray(
Calin Juravle225ff812014-11-13 16:46:39 +00002299 dex_pc, type_index, number_of_vreg_arguments, true, nullptr, register_index);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002300 break;
2301 }
2302
2303 case Instruction::FILL_ARRAY_DATA: {
Calin Juravle225ff812014-11-13 16:46:39 +00002304 BuildFillArrayData(instruction, dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002305 break;
2306 }
2307
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002308 case Instruction::MOVE_RESULT:
Dave Allison20dfc792014-06-16 20:44:29 -07002309 case Instruction::MOVE_RESULT_WIDE:
David Brazdilfc6a86a2015-06-26 10:33:45 +00002310 case Instruction::MOVE_RESULT_OBJECT: {
Nicolas Geoffray1efcc222015-06-24 12:41:20 +01002311 if (latest_result_ == nullptr) {
2312 // Only dead code can lead to this situation, where the verifier
2313 // does not reject the method.
2314 } else {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002315 // An Invoke/FilledNewArray and its MoveResult could have landed in
2316 // different blocks if there was a try/catch block boundary between
2317 // them. For Invoke, we insert a StoreLocal after the instruction. For
2318 // FilledNewArray, the local needs to be updated after the array was
2319 // filled, otherwise we might overwrite an input vreg.
2320 HStoreLocal* update_local =
2321 new (arena_) HStoreLocal(GetLocalAt(instruction.VRegA()), latest_result_);
2322 HBasicBlock* block = latest_result_->GetBlock();
2323 if (block == current_block_) {
2324 // MoveResult and the previous instruction are in the same block.
2325 current_block_->AddInstruction(update_local);
2326 } else {
2327 // The two instructions are in different blocks. Insert the MoveResult
2328 // before the final control-flow instruction of the previous block.
2329 DCHECK(block->EndsWithControlFlowInstruction());
2330 DCHECK(current_block_->GetInstructions().IsEmpty());
2331 block->InsertInstructionBefore(update_local, block->GetLastInstruction());
2332 }
Nicolas Geoffray1efcc222015-06-24 12:41:20 +01002333 latest_result_ = nullptr;
2334 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002335 break;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002336 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002337
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002338 case Instruction::CMP_LONG: {
Mark Mendellc4701932015-04-10 13:18:51 -04002339 Binop_23x_cmp(instruction, Primitive::kPrimLong, kNoBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002340 break;
2341 }
2342
2343 case Instruction::CMPG_FLOAT: {
Mark Mendellc4701932015-04-10 13:18:51 -04002344 Binop_23x_cmp(instruction, Primitive::kPrimFloat, kGtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002345 break;
2346 }
2347
2348 case Instruction::CMPG_DOUBLE: {
Mark Mendellc4701932015-04-10 13:18:51 -04002349 Binop_23x_cmp(instruction, Primitive::kPrimDouble, kGtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002350 break;
2351 }
2352
2353 case Instruction::CMPL_FLOAT: {
Mark Mendellc4701932015-04-10 13:18:51 -04002354 Binop_23x_cmp(instruction, Primitive::kPrimFloat, kLtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002355 break;
2356 }
2357
2358 case Instruction::CMPL_DOUBLE: {
Mark Mendellc4701932015-04-10 13:18:51 -04002359 Binop_23x_cmp(instruction, Primitive::kPrimDouble, kLtBias, dex_pc);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002360 break;
2361 }
2362
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002363 case Instruction::NOP:
2364 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002365
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002366 case Instruction::IGET:
2367 case Instruction::IGET_WIDE:
2368 case Instruction::IGET_OBJECT:
2369 case Instruction::IGET_BOOLEAN:
2370 case Instruction::IGET_BYTE:
2371 case Instruction::IGET_CHAR:
2372 case Instruction::IGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002373 if (!BuildInstanceFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002374 return false;
2375 }
2376 break;
2377 }
2378
2379 case Instruction::IPUT:
2380 case Instruction::IPUT_WIDE:
2381 case Instruction::IPUT_OBJECT:
2382 case Instruction::IPUT_BOOLEAN:
2383 case Instruction::IPUT_BYTE:
2384 case Instruction::IPUT_CHAR:
2385 case Instruction::IPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002386 if (!BuildInstanceFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002387 return false;
2388 }
2389 break;
2390 }
2391
2392 case Instruction::SGET:
2393 case Instruction::SGET_WIDE:
2394 case Instruction::SGET_OBJECT:
2395 case Instruction::SGET_BOOLEAN:
2396 case Instruction::SGET_BYTE:
2397 case Instruction::SGET_CHAR:
2398 case Instruction::SGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002399 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002400 return false;
2401 }
2402 break;
2403 }
2404
2405 case Instruction::SPUT:
2406 case Instruction::SPUT_WIDE:
2407 case Instruction::SPUT_OBJECT:
2408 case Instruction::SPUT_BOOLEAN:
2409 case Instruction::SPUT_BYTE:
2410 case Instruction::SPUT_CHAR:
2411 case Instruction::SPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002412 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002413 return false;
2414 }
2415 break;
2416 }
2417
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002418#define ARRAY_XX(kind, anticipated_type) \
2419 case Instruction::AGET##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00002420 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002421 break; \
2422 } \
2423 case Instruction::APUT##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00002424 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002425 break; \
2426 }
2427
2428 ARRAY_XX(, Primitive::kPrimInt);
2429 ARRAY_XX(_WIDE, Primitive::kPrimLong);
2430 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
2431 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
2432 ARRAY_XX(_BYTE, Primitive::kPrimByte);
2433 ARRAY_XX(_CHAR, Primitive::kPrimChar);
2434 ARRAY_XX(_SHORT, Primitive::kPrimShort);
2435
Nicolas Geoffray39468442014-09-02 15:17:15 +01002436 case Instruction::ARRAY_LENGTH: {
2437 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002438 // No need for a temporary for the null check, it is the only input of the following
2439 // instruction.
Calin Juravle225ff812014-11-13 16:46:39 +00002440 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002441 current_block_->AddInstruction(object);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002442 current_block_->AddInstruction(new (arena_) HArrayLength(object));
2443 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
2444 break;
2445 }
2446
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002447 case Instruction::CONST_STRING: {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002448 current_block_->AddInstruction(
2449 new (arena_) HLoadString(graph_->GetCurrentMethod(), instruction.VRegB_21c(), dex_pc));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002450 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2451 break;
2452 }
2453
2454 case Instruction::CONST_STRING_JUMBO: {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002455 current_block_->AddInstruction(
2456 new (arena_) HLoadString(graph_->GetCurrentMethod(), instruction.VRegB_31c(), dex_pc));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002457 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
2458 break;
2459 }
2460
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002461 case Instruction::CONST_CLASS: {
2462 uint16_t type_index = instruction.VRegB_21c();
2463 bool type_known_final;
2464 bool type_known_abstract;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002465 bool dont_use_is_referrers_class;
2466 // `CanAccessTypeWithoutChecks` will tell whether the method being
2467 // built is trying to access its own class, so that the generated
2468 // code can optimize for this case. However, the optimization does not
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002469 // work for inlining, so we use `IsOutermostCompilingClass` instead.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002470 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
2471 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002472 &type_known_final, &type_known_abstract, &dont_use_is_referrers_class);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002473 if (!can_access) {
Calin Juravle48c2b032014-12-09 18:11:36 +00002474 MaybeRecordStat(MethodCompilationStat::kNotCompiledCantAccesType);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002475 return false;
2476 }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002477 current_block_->AddInstruction(new (arena_) HLoadClass(
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002478 graph_->GetCurrentMethod(),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002479 type_index,
2480 *dex_compilation_unit_->GetDexFile(),
2481 IsOutermostCompilingClass(type_index),
2482 dex_pc));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002483 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2484 break;
2485 }
2486
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002487 case Instruction::MOVE_EXCEPTION: {
2488 current_block_->AddInstruction(new (arena_) HLoadException());
2489 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
2490 break;
2491 }
2492
2493 case Instruction::THROW: {
2494 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00002495 current_block_->AddInstruction(new (arena_) HThrow(exception, dex_pc));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002496 // A throw instruction must branch to the exit block.
2497 current_block_->AddSuccessor(exit_block_);
2498 // We finished building this block. Set the current block to null to avoid
2499 // adding dead instructions to it.
2500 current_block_ = nullptr;
2501 break;
2502 }
2503
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002504 case Instruction::INSTANCE_OF: {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002505 uint8_t destination = instruction.VRegA_22c();
2506 uint8_t reference = instruction.VRegB_22c();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002507 uint16_t type_index = instruction.VRegC_22c();
Calin Juravle225ff812014-11-13 16:46:39 +00002508 if (!BuildTypeCheck(instruction, destination, reference, type_index, dex_pc)) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002509 return false;
2510 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002511 break;
2512 }
2513
2514 case Instruction::CHECK_CAST: {
2515 uint8_t reference = instruction.VRegA_21c();
2516 uint16_t type_index = instruction.VRegB_21c();
Calin Juravle225ff812014-11-13 16:46:39 +00002517 if (!BuildTypeCheck(instruction, -1, reference, type_index, dex_pc)) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002518 return false;
2519 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002520 break;
2521 }
2522
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002523 case Instruction::MONITOR_ENTER: {
2524 current_block_->AddInstruction(new (arena_) HMonitorOperation(
2525 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2526 HMonitorOperation::kEnter,
Calin Juravle225ff812014-11-13 16:46:39 +00002527 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002528 break;
2529 }
2530
2531 case Instruction::MONITOR_EXIT: {
2532 current_block_->AddInstruction(new (arena_) HMonitorOperation(
2533 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2534 HMonitorOperation::kExit,
Calin Juravle225ff812014-11-13 16:46:39 +00002535 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002536 break;
2537 }
2538
Andreas Gamped881df52014-11-24 23:28:39 -08002539 case Instruction::PACKED_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002540 BuildPackedSwitch(instruction, dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08002541 break;
2542 }
2543
Andreas Gampee4d4d322014-12-04 09:09:57 -08002544 case Instruction::SPARSE_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002545 BuildSparseSwitch(instruction, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08002546 break;
2547 }
2548
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002549 default:
Calin Juravle48c2b032014-12-09 18:11:36 +00002550 VLOG(compiler) << "Did not compile "
2551 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
2552 << " because of unhandled instruction "
2553 << instruction.Name();
2554 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnhandledInstruction);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002555 return false;
2556 }
2557 return true;
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00002558} // NOLINT(readability/fn_size)
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002559
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002560HLocal* HGraphBuilder::GetLocalAt(int register_index) const {
2561 return locals_.Get(register_index);
2562}
2563
2564void HGraphBuilder::UpdateLocal(int register_index, HInstruction* instruction) const {
2565 HLocal* local = GetLocalAt(register_index);
2566 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction));
2567}
2568
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002569HInstruction* HGraphBuilder::LoadLocal(int register_index, Primitive::Type type) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002570 HLocal* local = GetLocalAt(register_index);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002571 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type));
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002572 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002573}
2574
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002575} // namespace art