blob: bb1b40a1b514e9e5bfeac84a247478919275e224 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Nicolas Geoffraye5038322014-07-04 09:41:32 +010017#include "builder.h"
18
Mathieu Chartierc7853442015-03-27 14:35:38 -070019#include "art_field-inl.h"
Andreas Gamped881df52014-11-24 23:28:39 -080020#include "base/logging.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010021#include "class_linker.h"
Jeff Hao848f70a2014-01-15 13:49:50 -080022#include "dex/verified_method.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000023#include "dex_file-inl.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000024#include "dex_instruction-inl.h"
Roland Levillain4c0eb422015-04-24 16:43:49 +010025#include "dex/verified_method.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010026#include "driver/compiler_driver-inl.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000027#include "driver/compiler_options.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010028#include "mirror/class_loader.h"
29#include "mirror/dex_cache.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000030#include "nodes.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000031#include "primitive.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010032#include "scoped_thread_state_change.h"
33#include "thread.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000034
35namespace art {
36
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010037/**
38 * Helper class to add HTemporary instructions. This class is used when
39 * converting a DEX instruction to multiple HInstruction, and where those
40 * instructions do not die at the following instruction, but instead spans
41 * multiple instructions.
42 */
43class Temporaries : public ValueObject {
44 public:
Calin Juravlef97f9fb2014-11-11 15:38:19 +000045 explicit Temporaries(HGraph* graph) : graph_(graph), index_(0) {}
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010046
47 void Add(HInstruction* instruction) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +000048 HInstruction* temp = new (graph_->GetArena()) HTemporary(index_);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010049 instruction->GetBlock()->AddInstruction(temp);
Calin Juravlef97f9fb2014-11-11 15:38:19 +000050
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010051 DCHECK(temp->GetPrevious() == instruction);
Calin Juravlef97f9fb2014-11-11 15:38:19 +000052
53 size_t offset;
54 if (instruction->GetType() == Primitive::kPrimLong
55 || instruction->GetType() == Primitive::kPrimDouble) {
56 offset = 2;
57 } else {
58 offset = 1;
59 }
60 index_ += offset;
61
62 graph_->UpdateTemporariesVRegSlots(index_);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010063 }
64
65 private:
66 HGraph* const graph_;
67
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010068 // Current index in the temporary stack, updated by `Add`.
69 size_t index_;
70};
71
Andreas Gamped881df52014-11-24 23:28:39 -080072class SwitchTable : public ValueObject {
73 public:
74 SwitchTable(const Instruction& instruction, uint32_t dex_pc, bool sparse)
75 : instruction_(instruction), dex_pc_(dex_pc), sparse_(sparse) {
76 int32_t table_offset = instruction.VRegB_31t();
77 const uint16_t* table = reinterpret_cast<const uint16_t*>(&instruction) + table_offset;
78 if (sparse) {
79 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
80 } else {
81 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
82 }
83 num_entries_ = table[1];
84 values_ = reinterpret_cast<const int32_t*>(&table[2]);
85 }
86
87 uint16_t GetNumEntries() const {
88 return num_entries_;
89 }
90
Andreas Gampee4d4d322014-12-04 09:09:57 -080091 void CheckIndex(size_t index) const {
92 if (sparse_) {
93 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
94 DCHECK_LT(index, 2 * static_cast<size_t>(num_entries_));
95 } else {
96 // In a packed table, we have the starting key and num_entries_ values.
97 DCHECK_LT(index, 1 + static_cast<size_t>(num_entries_));
98 }
99 }
100
Andreas Gamped881df52014-11-24 23:28:39 -0800101 int32_t GetEntryAt(size_t index) const {
Andreas Gampee4d4d322014-12-04 09:09:57 -0800102 CheckIndex(index);
Andreas Gamped881df52014-11-24 23:28:39 -0800103 return values_[index];
104 }
105
106 uint32_t GetDexPcForIndex(size_t index) const {
Andreas Gampee4d4d322014-12-04 09:09:57 -0800107 CheckIndex(index);
Andreas Gamped881df52014-11-24 23:28:39 -0800108 return dex_pc_ +
109 (reinterpret_cast<const int16_t*>(values_ + index) -
110 reinterpret_cast<const int16_t*>(&instruction_));
111 }
112
Andreas Gampee4d4d322014-12-04 09:09:57 -0800113 // Index of the first value in the table.
114 size_t GetFirstValueIndex() const {
115 if (sparse_) {
116 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
117 return num_entries_;
118 } else {
119 // In a packed table, we have the starting key and num_entries_ values.
120 return 1;
121 }
122 }
123
Andreas Gamped881df52014-11-24 23:28:39 -0800124 private:
125 const Instruction& instruction_;
126 const uint32_t dex_pc_;
127
128 // Whether this is a sparse-switch table (or a packed-switch one).
129 const bool sparse_;
130
131 // This can't be const as it needs to be computed off of the given instruction, and complicated
132 // expressions in the initializer list seemed very ugly.
133 uint16_t num_entries_;
134
135 const int32_t* values_;
136
137 DISALLOW_COPY_AND_ASSIGN(SwitchTable);
138};
139
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100140void HGraphBuilder::InitializeLocals(uint16_t count) {
141 graph_->SetNumberOfVRegs(count);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000142 locals_.SetSize(count);
143 for (int i = 0; i < count; i++) {
144 HLocal* local = new (arena_) HLocal(i);
145 entry_block_->AddInstruction(local);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000146 locals_.Put(i, local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000147 }
148}
149
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000150void HGraphBuilder::InitializeParameters(uint16_t number_of_parameters) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100151 // dex_compilation_unit_ is null only when unit testing.
152 if (dex_compilation_unit_ == nullptr) {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000153 return;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100154 }
155
156 graph_->SetNumberOfInVRegs(number_of_parameters);
157 const char* shorty = dex_compilation_unit_->GetShorty();
158 int locals_index = locals_.Size() - number_of_parameters;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100159 int parameter_index = 0;
160
161 if (!dex_compilation_unit_->IsStatic()) {
162 // Add the implicit 'this' argument, not expressed in the signature.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100163 HParameterValue* parameter =
Calin Juravle10e244f2015-01-26 18:54:32 +0000164 new (arena_) HParameterValue(parameter_index++, Primitive::kPrimNot, true);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100165 entry_block_->AddInstruction(parameter);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100166 HLocal* local = GetLocalAt(locals_index++);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100167 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100168 number_of_parameters--;
169 }
170
171 uint32_t pos = 1;
172 for (int i = 0; i < number_of_parameters; i++) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100173 HParameterValue* parameter =
174 new (arena_) HParameterValue(parameter_index++, Primitive::GetType(shorty[pos++]));
175 entry_block_->AddInstruction(parameter);
176 HLocal* local = GetLocalAt(locals_index++);
177 // Store the parameter value in the local that the dex code will use
178 // to reference that parameter.
179 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter));
180 bool is_wide = (parameter->GetType() == Primitive::kPrimLong)
181 || (parameter->GetType() == Primitive::kPrimDouble);
182 if (is_wide) {
183 i++;
184 locals_index++;
185 parameter_index++;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100186 }
187 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100188}
189
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100190template<typename T>
Calin Juravle225ff812014-11-13 16:46:39 +0000191void HGraphBuilder::If_22t(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000192 int32_t target_offset = instruction.GetTargetOffset();
David Brazdil852eaff2015-02-02 15:23:05 +0000193 HBasicBlock* branch_target = FindBlockStartingAt(dex_pc + target_offset);
194 HBasicBlock* fallthrough_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
195 DCHECK(branch_target != nullptr);
196 DCHECK(fallthrough_target != nullptr);
197 PotentiallyAddSuspendCheck(branch_target, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100198 HInstruction* first = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
199 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Dave Allison20dfc792014-06-16 20:44:29 -0700200 T* comparison = new (arena_) T(first, second);
201 current_block_->AddInstruction(comparison);
202 HInstruction* ifinst = new (arena_) HIf(comparison);
203 current_block_->AddInstruction(ifinst);
David Brazdil852eaff2015-02-02 15:23:05 +0000204 current_block_->AddSuccessor(branch_target);
205 current_block_->AddSuccessor(fallthrough_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700206 current_block_ = nullptr;
207}
208
209template<typename T>
Calin Juravle225ff812014-11-13 16:46:39 +0000210void HGraphBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000211 int32_t target_offset = instruction.GetTargetOffset();
David Brazdil852eaff2015-02-02 15:23:05 +0000212 HBasicBlock* branch_target = FindBlockStartingAt(dex_pc + target_offset);
213 HBasicBlock* fallthrough_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
214 DCHECK(branch_target != nullptr);
215 DCHECK(fallthrough_target != nullptr);
216 PotentiallyAddSuspendCheck(branch_target, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700217 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000218 T* comparison = new (arena_) T(value, graph_->GetIntConstant(0));
Dave Allison20dfc792014-06-16 20:44:29 -0700219 current_block_->AddInstruction(comparison);
220 HInstruction* ifinst = new (arena_) HIf(comparison);
221 current_block_->AddInstruction(ifinst);
David Brazdil852eaff2015-02-02 15:23:05 +0000222 current_block_->AddSuccessor(branch_target);
223 current_block_->AddSuccessor(fallthrough_target);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100224 current_block_ = nullptr;
225}
226
Calin Juravle48c2b032014-12-09 18:11:36 +0000227void HGraphBuilder::MaybeRecordStat(MethodCompilationStat compilation_stat) {
228 if (compilation_stats_ != nullptr) {
229 compilation_stats_->RecordStat(compilation_stat);
230 }
231}
232
David Brazdil1b498722015-03-31 11:37:18 +0100233bool HGraphBuilder::SkipCompilation(const DexFile::CodeItem& code_item,
Calin Juravle48c2b032014-12-09 18:11:36 +0000234 size_t number_of_branches) {
235 const CompilerOptions& compiler_options = compiler_driver_->GetCompilerOptions();
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000236 CompilerOptions::CompilerFilter compiler_filter = compiler_options.GetCompilerFilter();
237 if (compiler_filter == CompilerOptions::kEverything) {
238 return false;
239 }
240
David Brazdil1b498722015-03-31 11:37:18 +0100241 if (compiler_options.IsHugeMethod(code_item.insns_size_in_code_units_)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000242 VLOG(compiler) << "Skip compilation of huge method "
243 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
David Brazdil1b498722015-03-31 11:37:18 +0100244 << ": " << code_item.insns_size_in_code_units_ << " code units";
Calin Juravle48c2b032014-12-09 18:11:36 +0000245 MaybeRecordStat(MethodCompilationStat::kNotCompiledHugeMethod);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000246 return true;
247 }
248
249 // If it's large and contains no branches, it's likely to be machine generated initialization.
David Brazdil1b498722015-03-31 11:37:18 +0100250 if (compiler_options.IsLargeMethod(code_item.insns_size_in_code_units_)
251 && (number_of_branches == 0)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000252 VLOG(compiler) << "Skip compilation of large method with no branch "
253 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
David Brazdil1b498722015-03-31 11:37:18 +0100254 << ": " << code_item.insns_size_in_code_units_ << " code units";
Calin Juravle48c2b032014-12-09 18:11:36 +0000255 MaybeRecordStat(MethodCompilationStat::kNotCompiledLargeMethodNoBranches);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000256 return true;
257 }
258
259 return false;
260}
261
David Brazdilbff75032015-07-08 17:26:51 +0000262static const DexFile::TryItem* GetTryItem(HBasicBlock* block,
263 const DexFile::CodeItem& code_item,
264 const ArenaBitVector& can_block_throw) {
265 DCHECK(!block->IsSingleTryBoundary());
266
267 // Block does not contain throwing instructions. Even if it is covered by
268 // a TryItem, we will consider it not in a try block.
269 if (!can_block_throw.IsBitSet(block->GetBlockId())) {
270 return nullptr;
271 }
272
273 // Instructions in the block may throw. Find a TryItem covering this block.
274 int32_t try_item_idx = DexFile::FindTryItem(code_item, block->GetDexPc());
David Brazdil6cd788f2015-07-08 16:44:00 +0100275 return (try_item_idx == -1) ? nullptr : DexFile::GetTryItems(code_item, try_item_idx);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000276}
277
278void HGraphBuilder::CreateBlocksForTryCatch(const DexFile::CodeItem& code_item) {
279 if (code_item.tries_size_ == 0) {
280 return;
281 }
282
283 // Create branch targets at the start/end of the TryItem range. These are
284 // places where the program might fall through into/out of the a block and
285 // where TryBoundary instructions will be inserted later. Other edges which
286 // enter/exit the try blocks are a result of branches/switches.
287 for (size_t idx = 0; idx < code_item.tries_size_; ++idx) {
288 const DexFile::TryItem* try_item = DexFile::GetTryItems(code_item, idx);
289 uint32_t dex_pc_start = try_item->start_addr_;
290 uint32_t dex_pc_end = dex_pc_start + try_item->insn_count_;
291 FindOrCreateBlockStartingAt(dex_pc_start);
292 if (dex_pc_end < code_item.insns_size_in_code_units_) {
293 // TODO: Do not create block if the last instruction cannot fall through.
294 FindOrCreateBlockStartingAt(dex_pc_end);
295 } else {
296 // The TryItem spans until the very end of the CodeItem (or beyond if
297 // invalid) and therefore cannot have any code afterwards.
298 }
299 }
300
301 // Create branch targets for exception handlers.
302 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(code_item, 0);
303 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
304 for (uint32_t idx = 0; idx < handlers_size; ++idx) {
305 CatchHandlerIterator iterator(handlers_ptr);
306 for (; iterator.HasNext(); iterator.Next()) {
307 uint32_t address = iterator.GetHandlerAddress();
308 HBasicBlock* block = FindOrCreateBlockStartingAt(address);
309 block->SetIsCatchBlock();
310 }
311 handlers_ptr = iterator.EndDataPointer();
312 }
313}
314
David Brazdil56e1acc2015-06-30 15:41:36 +0100315void HGraphBuilder::SplitTryBoundaryEdge(HBasicBlock* predecessor,
316 HBasicBlock* successor,
317 HTryBoundary::BoundaryKind kind,
318 const DexFile::CodeItem& code_item,
319 const DexFile::TryItem& try_item) {
320 // Split the edge with a single TryBoundary instruction.
321 HTryBoundary* try_boundary = new (arena_) HTryBoundary(kind);
322 HBasicBlock* try_entry_block = graph_->SplitEdge(predecessor, successor);
323 try_entry_block->AddInstruction(try_boundary);
324
325 // Link the TryBoundary to the handlers of `try_item`.
326 for (CatchHandlerIterator it(code_item, try_item); it.HasNext(); it.Next()) {
327 try_boundary->AddExceptionHandler(FindBlockStartingAt(it.GetHandlerAddress()));
328 }
329}
330
David Brazdilfc6a86a2015-06-26 10:33:45 +0000331void HGraphBuilder::InsertTryBoundaryBlocks(const DexFile::CodeItem& code_item) {
332 if (code_item.tries_size_ == 0) {
333 return;
334 }
335
David Brazdilbff75032015-07-08 17:26:51 +0000336 const size_t num_blocks = graph_->GetBlocks().Size();
337 ArenaBitVector can_block_throw(arena_, num_blocks, false);
338
339 // Scan blocks and mark those which contain throwing instructions.
340 for (size_t block_id = 0; block_id < num_blocks; ++block_id) {
341 HBasicBlock* block = graph_->GetBlocks().Get(block_id);
342 for (HInstructionIterator insn(block->GetInstructions()); !insn.Done(); insn.Advance()) {
343 if (insn.Current()->CanThrow()) {
344 can_block_throw.SetBit(block_id);
345 break;
346 }
347 }
348 }
349
David Brazdil281a6322015-07-03 10:34:57 +0100350 // Iterate over all blocks, find those covered by some TryItem and:
351 // (a) split edges which enter/exit the try range,
352 // (b) create TryBoundary instructions in the new blocks,
353 // (c) link the new blocks to corresponding exception handlers.
354 // We cannot iterate only over blocks in `branch_targets_` because switch-case
355 // blocks share the same dex_pc.
David Brazdil6cd788f2015-07-08 16:44:00 +0100356 for (size_t block_id = 0; block_id < num_blocks; ++block_id) {
David Brazdil281a6322015-07-03 10:34:57 +0100357 HBasicBlock* try_block = graph_->GetBlocks().Get(block_id);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000358
David Brazdil281a6322015-07-03 10:34:57 +0100359 // TryBoundary blocks are added at the end of the list and not iterated over.
360 DCHECK(!try_block->IsSingleTryBoundary());
David Brazdilfc6a86a2015-06-26 10:33:45 +0000361
David Brazdil281a6322015-07-03 10:34:57 +0100362 // Find the TryItem for this block.
David Brazdilbff75032015-07-08 17:26:51 +0000363 const DexFile::TryItem* try_item = GetTryItem(try_block, code_item, can_block_throw);
364 if (try_item == nullptr) {
David Brazdil281a6322015-07-03 10:34:57 +0100365 continue;
366 }
David Brazdil281a6322015-07-03 10:34:57 +0100367
368 if (try_block->IsCatchBlock()) {
369 // Catch blocks are always considered an entry point into the TryItem in
370 // order to avoid splitting exceptional edges (they might not have been
371 // created yet). We separate the move-exception (if present) from the
372 // rest of the block and insert a TryBoundary after it, creating a
373 // landing pad for the exceptional edges.
374 HInstruction* first_insn = try_block->GetFirstInstruction();
375 HInstruction* split_position = nullptr;
376 if (first_insn->IsLoadException()) {
377 // Catch block starts with a LoadException. Split the block after the
378 // StoreLocal that must come after the load.
379 DCHECK(first_insn->GetNext()->IsStoreLocal());
380 split_position = first_insn->GetNext()->GetNext();
David Brazdil56e1acc2015-06-30 15:41:36 +0100381 } else {
David Brazdil281a6322015-07-03 10:34:57 +0100382 // Catch block does not obtain the exception. Split at the beginning
383 // to create an empty catch block.
384 split_position = first_insn;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000385 }
David Brazdil281a6322015-07-03 10:34:57 +0100386 DCHECK(split_position != nullptr);
387 HBasicBlock* catch_block = try_block;
388 try_block = catch_block->SplitBefore(split_position);
David Brazdilbff75032015-07-08 17:26:51 +0000389 SplitTryBoundaryEdge(catch_block, try_block, HTryBoundary::kEntry, code_item, *try_item);
David Brazdil281a6322015-07-03 10:34:57 +0100390 } else {
391 // For non-catch blocks, find predecessors which are not covered by the
392 // same TryItem range. Such edges enter the try block and will have
393 // a TryBoundary inserted.
394 for (size_t i = 0; i < try_block->GetPredecessors().Size(); ++i) {
395 HBasicBlock* predecessor = try_block->GetPredecessors().Get(i);
396 if (predecessor->IsSingleTryBoundary()) {
397 // The edge was already split because of an exit from a neighbouring
398 // TryItem. We split it again and insert an entry point.
David Brazdil56e1acc2015-06-30 15:41:36 +0100399 if (kIsDebugBuild) {
David Brazdil281a6322015-07-03 10:34:57 +0100400 HTryBoundary* last_insn = predecessor->GetLastInstruction()->AsTryBoundary();
David Brazdilbff75032015-07-08 17:26:51 +0000401 const DexFile::TryItem* predecessor_try_item =
402 GetTryItem(predecessor->GetSinglePredecessor(), code_item, can_block_throw);
David Brazdil281a6322015-07-03 10:34:57 +0100403 DCHECK(!last_insn->IsEntry());
404 DCHECK_EQ(last_insn->GetNormalFlowSuccessor(), try_block);
405 DCHECK(try_block->IsFirstIndexOfPredecessor(predecessor, i));
David Brazdilbff75032015-07-08 17:26:51 +0000406 DCHECK_NE(try_item, predecessor_try_item);
David Brazdil56e1acc2015-06-30 15:41:36 +0100407 }
David Brazdilbff75032015-07-08 17:26:51 +0000408 } else if (GetTryItem(predecessor, code_item, can_block_throw) != try_item) {
David Brazdil281a6322015-07-03 10:34:57 +0100409 // This is an entry point into the TryItem and the edge has not been
410 // split yet. That means that `predecessor` is not in a TryItem, or
411 // it is in a different TryItem and we happened to iterate over this
412 // block first. We split the edge and insert an entry point.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000413 } else {
414 // Not an edge on the boundary of the try block.
415 continue;
416 }
David Brazdilbff75032015-07-08 17:26:51 +0000417 SplitTryBoundaryEdge(predecessor, try_block, HTryBoundary::kEntry, code_item, *try_item);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000418 }
419 }
David Brazdil281a6322015-07-03 10:34:57 +0100420
421 // Find successors which are not covered by the same TryItem range. Such
422 // edges exit the try block and will have a TryBoundary inserted.
423 for (size_t i = 0; i < try_block->GetSuccessors().Size(); ++i) {
424 HBasicBlock* successor = try_block->GetSuccessors().Get(i);
425 if (successor->IsCatchBlock()) {
426 // A catch block is always considered an entry point into its TryItem.
427 // We therefore assume this is an exit point, regardless of whether
428 // the catch block is in a different TryItem or not.
429 } else if (successor->IsSingleTryBoundary()) {
430 // The edge was already split because of an entry into a neighbouring
431 // TryItem. We split it again and insert an exit.
432 if (kIsDebugBuild) {
433 HTryBoundary* last_insn = successor->GetLastInstruction()->AsTryBoundary();
David Brazdilbff75032015-07-08 17:26:51 +0000434 const DexFile::TryItem* successor_try_item =
435 GetTryItem(last_insn->GetNormalFlowSuccessor(), code_item, can_block_throw);
David Brazdil281a6322015-07-03 10:34:57 +0100436 DCHECK_EQ(try_block, successor->GetSinglePredecessor());
437 DCHECK(last_insn->IsEntry());
David Brazdilbff75032015-07-08 17:26:51 +0000438 DCHECK_NE(try_item, successor_try_item);
David Brazdil281a6322015-07-03 10:34:57 +0100439 }
David Brazdilbff75032015-07-08 17:26:51 +0000440 } else if (GetTryItem(successor, code_item, can_block_throw) != try_item) {
David Brazdil281a6322015-07-03 10:34:57 +0100441 // This is an exit out of the TryItem and the edge has not been split
442 // yet. That means that either `successor` is not in a TryItem, or it
443 // is in a different TryItem and we happened to iterate over this
444 // block first. We split the edge and insert an exit.
445 HInstruction* last_instruction = try_block->GetLastInstruction();
446 if (last_instruction->IsReturn() || last_instruction->IsReturnVoid()) {
447 DCHECK_EQ(successor, exit_block_);
448 // Control flow exits the try block with a Return(Void). Because
449 // splitting the edge would invalidate the invariant that Return
450 // always jumps to Exit, we move the Return outside the try block.
451 successor = try_block->SplitBefore(last_instruction);
452 }
453 } else {
454 // Not an edge on the boundary of the try block.
455 continue;
456 }
David Brazdilbff75032015-07-08 17:26:51 +0000457 SplitTryBoundaryEdge(try_block, successor, HTryBoundary::kExit, code_item, *try_item);
David Brazdil281a6322015-07-03 10:34:57 +0100458 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000459 }
460}
461
David Brazdil5e8b1372015-01-23 14:39:08 +0000462bool HGraphBuilder::BuildGraph(const DexFile::CodeItem& code_item) {
463 DCHECK(graph_->GetBlocks().IsEmpty());
464
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000465 const uint16_t* code_ptr = code_item.insns_;
466 const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100467 code_start_ = code_ptr;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000468
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000469 // Setup the graph with the entry block and exit block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100470 entry_block_ = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000471 graph_->AddBlock(entry_block_);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100472 exit_block_ = new (arena_) HBasicBlock(graph_, kNoDexPc);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000473 graph_->SetEntryBlock(entry_block_);
474 graph_->SetExitBlock(exit_block_);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000475
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000476 InitializeLocals(code_item.registers_size_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000477 graph_->SetMaximumNumberOfOutVRegs(code_item.outs_size_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000478
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000479 // Compute the number of dex instructions, blocks, and branches. We will
480 // check these values against limits given to the compiler.
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000481 size_t number_of_branches = 0;
482
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000483 // To avoid splitting blocks, we compute ahead of time the instructions that
484 // start a new block, and create these blocks.
Calin Juravle702d2602015-04-30 19:28:21 +0100485 if (!ComputeBranchTargets(code_ptr, code_end, &number_of_branches)) {
486 MaybeRecordStat(MethodCompilationStat::kNotCompiledBranchOutsideMethodCode);
487 return false;
488 }
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000489
490 // Note that the compiler driver is null when unit testing.
David Brazdil1b498722015-03-31 11:37:18 +0100491 if ((compiler_driver_ != nullptr) && SkipCompilation(code_item, number_of_branches)) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000492 return false;
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000493 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000494
David Brazdilfc6a86a2015-06-26 10:33:45 +0000495 CreateBlocksForTryCatch(code_item);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000496
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000497 InitializeParameters(code_item.ins_size_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100498
Calin Juravle225ff812014-11-13 16:46:39 +0000499 size_t dex_pc = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000500 while (code_ptr < code_end) {
Calin Juravle225ff812014-11-13 16:46:39 +0000501 // Update the current block if dex_pc starts a new block.
502 MaybeUpdateCurrentBlock(dex_pc);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000503 const Instruction& instruction = *Instruction::At(code_ptr);
Calin Juravle48c2b032014-12-09 18:11:36 +0000504 if (!AnalyzeDexInstruction(instruction, dex_pc)) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000505 return false;
Calin Juravle48c2b032014-12-09 18:11:36 +0000506 }
Calin Juravle225ff812014-11-13 16:46:39 +0000507 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000508 code_ptr += instruction.SizeInCodeUnits();
509 }
510
David Brazdilfc6a86a2015-06-26 10:33:45 +0000511 // Add Exit to the exit block.
David Brazdil3e187382015-06-26 09:59:52 +0000512 exit_block_->AddInstruction(new (arena_) HExit());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000513 // Add the suspend check to the entry block.
514 entry_block_->AddInstruction(new (arena_) HSuspendCheck(0));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000515 entry_block_->AddInstruction(new (arena_) HGoto());
David Brazdilbff75032015-07-08 17:26:51 +0000516 // Add the exit block at the end.
517 graph_->AddBlock(exit_block_);
David Brazdil5e8b1372015-01-23 14:39:08 +0000518
David Brazdilfc6a86a2015-06-26 10:33:45 +0000519 // Iterate over blocks covered by TryItems and insert TryBoundaries at entry
520 // and exit points. This requires all control-flow instructions and
521 // non-exceptional edges to have been created.
522 InsertTryBoundaryBlocks(code_item);
523
David Brazdil5e8b1372015-01-23 14:39:08 +0000524 return true;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000525}
526
David Brazdilfc6a86a2015-06-26 10:33:45 +0000527void HGraphBuilder::MaybeUpdateCurrentBlock(size_t dex_pc) {
528 HBasicBlock* block = FindBlockStartingAt(dex_pc);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000529 if (block == nullptr) {
530 return;
531 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000532
533 if (current_block_ != nullptr) {
534 // Branching instructions clear current_block, so we know
535 // the last instruction of the current block is not a branching
536 // instruction. We add an unconditional goto to the found block.
537 current_block_->AddInstruction(new (arena_) HGoto());
538 current_block_->AddSuccessor(block);
539 }
540 graph_->AddBlock(block);
541 current_block_ = block;
542}
543
Calin Juravle702d2602015-04-30 19:28:21 +0100544bool HGraphBuilder::ComputeBranchTargets(const uint16_t* code_ptr,
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000545 const uint16_t* code_end,
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000546 size_t* number_of_branches) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000547 branch_targets_.SetSize(code_end - code_ptr);
548
549 // Create the first block for the dex instructions, single successor of the entry block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100550 HBasicBlock* block = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000551 branch_targets_.Put(0, block);
552 entry_block_->AddSuccessor(block);
553
554 // Iterate over all instructions and find branching instructions. Create blocks for
555 // the locations these instructions branch to.
Andreas Gamped881df52014-11-24 23:28:39 -0800556 uint32_t dex_pc = 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000557 while (code_ptr < code_end) {
558 const Instruction& instruction = *Instruction::At(code_ptr);
559 if (instruction.IsBranch()) {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000560 (*number_of_branches)++;
Calin Juravle225ff812014-11-13 16:46:39 +0000561 int32_t target = instruction.GetTargetOffset() + dex_pc;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000562 // Create a block for the target instruction.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000563 FindOrCreateBlockStartingAt(target);
564
Calin Juravle225ff812014-11-13 16:46:39 +0000565 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000566 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle702d2602015-04-30 19:28:21 +0100567
David Brazdilfe659462015-06-24 14:23:56 +0100568 if (instruction.CanFlowThrough()) {
569 if (code_ptr >= code_end) {
Calin Juravle702d2602015-04-30 19:28:21 +0100570 // In the normal case we should never hit this but someone can artificially forge a dex
571 // file to fall-through out the method code. In this case we bail out compilation.
572 return false;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000573 } else {
574 FindOrCreateBlockStartingAt(dex_pc);
Calin Juravle702d2602015-04-30 19:28:21 +0100575 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000576 }
Andreas Gampee4d4d322014-12-04 09:09:57 -0800577 } else if (instruction.IsSwitch()) {
578 SwitchTable table(instruction, dex_pc, instruction.Opcode() == Instruction::SPARSE_SWITCH);
Andreas Gamped881df52014-11-24 23:28:39 -0800579
580 uint16_t num_entries = table.GetNumEntries();
581
Andreas Gampee4d4d322014-12-04 09:09:57 -0800582 // In a packed-switch, the entry at index 0 is the starting key. In a sparse-switch, the
583 // entry at index 0 is the first key, and values are after *all* keys.
584 size_t offset = table.GetFirstValueIndex();
585
586 // Use a larger loop counter type to avoid overflow issues.
587 for (size_t i = 0; i < num_entries; ++i) {
Andreas Gamped881df52014-11-24 23:28:39 -0800588 // The target of the case.
Andreas Gampee4d4d322014-12-04 09:09:57 -0800589 uint32_t target = dex_pc + table.GetEntryAt(i + offset);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000590 FindOrCreateBlockStartingAt(target);
Andreas Gamped881df52014-11-24 23:28:39 -0800591
David Brazdil281a6322015-07-03 10:34:57 +0100592 // Create a block for the switch-case logic. The block gets the dex_pc
593 // of the SWITCH instruction because it is part of its semantics.
594 block = new (arena_) HBasicBlock(graph_, dex_pc);
595 branch_targets_.Put(table.GetDexPcForIndex(i), block);
Andreas Gamped881df52014-11-24 23:28:39 -0800596 }
597
598 // Fall-through. Add a block if there is more code afterwards.
599 dex_pc += instruction.SizeInCodeUnits();
600 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle702d2602015-04-30 19:28:21 +0100601 if (code_ptr >= code_end) {
602 // In the normal case we should never hit this but someone can artificially forge a dex
603 // file to fall-through out the method code. In this case we bail out compilation.
604 // (A switch can fall-through so we don't need to check CanFlowThrough().)
605 return false;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000606 } else {
607 FindOrCreateBlockStartingAt(dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -0800608 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000609 } else {
610 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle225ff812014-11-13 16:46:39 +0000611 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000612 }
613 }
Calin Juravle702d2602015-04-30 19:28:21 +0100614 return true;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000615}
616
David Brazdilfc6a86a2015-06-26 10:33:45 +0000617HBasicBlock* HGraphBuilder::FindBlockStartingAt(int32_t dex_pc) const {
618 DCHECK_GE(dex_pc, 0);
619 DCHECK_LT(static_cast<size_t>(dex_pc), branch_targets_.Size());
620 return branch_targets_.Get(dex_pc);
621}
622
623HBasicBlock* HGraphBuilder::FindOrCreateBlockStartingAt(int32_t dex_pc) {
624 HBasicBlock* block = FindBlockStartingAt(dex_pc);
625 if (block == nullptr) {
626 block = new (arena_) HBasicBlock(graph_, dex_pc);
627 branch_targets_.Put(dex_pc, block);
628 }
629 return block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000630}
631
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100632template<typename T>
Roland Levillain88cb1752014-10-20 16:36:47 +0100633void HGraphBuilder::Unop_12x(const Instruction& instruction, Primitive::Type type) {
634 HInstruction* first = LoadLocal(instruction.VRegB(), type);
635 current_block_->AddInstruction(new (arena_) T(type, first));
636 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
637}
638
Roland Levillaindff1f282014-11-05 14:15:05 +0000639void HGraphBuilder::Conversion_12x(const Instruction& instruction,
640 Primitive::Type input_type,
Roland Levillain624279f2014-12-04 11:54:28 +0000641 Primitive::Type result_type,
642 uint32_t dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +0000643 HInstruction* first = LoadLocal(instruction.VRegB(), input_type);
Roland Levillain624279f2014-12-04 11:54:28 +0000644 current_block_->AddInstruction(new (arena_) HTypeConversion(result_type, first, dex_pc));
Roland Levillaindff1f282014-11-05 14:15:05 +0000645 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
646}
647
Roland Levillain88cb1752014-10-20 16:36:47 +0100648template<typename T>
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100649void HGraphBuilder::Binop_23x(const Instruction& instruction, Primitive::Type type) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100650 HInstruction* first = LoadLocal(instruction.VRegB(), type);
651 HInstruction* second = LoadLocal(instruction.VRegC(), type);
652 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100653 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
654}
655
656template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000657void HGraphBuilder::Binop_23x(const Instruction& instruction,
658 Primitive::Type type,
659 uint32_t dex_pc) {
660 HInstruction* first = LoadLocal(instruction.VRegB(), type);
661 HInstruction* second = LoadLocal(instruction.VRegC(), type);
662 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
663 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
664}
665
666template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000667void HGraphBuilder::Binop_23x_shift(const Instruction& instruction,
668 Primitive::Type type) {
669 HInstruction* first = LoadLocal(instruction.VRegB(), type);
670 HInstruction* second = LoadLocal(instruction.VRegC(), Primitive::kPrimInt);
671 current_block_->AddInstruction(new (arena_) T(type, first, second));
672 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
673}
674
Calin Juravleddb7df22014-11-25 20:56:51 +0000675void HGraphBuilder::Binop_23x_cmp(const Instruction& instruction,
676 Primitive::Type type,
Mark Mendellc4701932015-04-10 13:18:51 -0400677 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700678 uint32_t dex_pc) {
Calin Juravleddb7df22014-11-25 20:56:51 +0000679 HInstruction* first = LoadLocal(instruction.VRegB(), type);
680 HInstruction* second = LoadLocal(instruction.VRegC(), type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700681 current_block_->AddInstruction(new (arena_) HCompare(type, first, second, bias, dex_pc));
Calin Juravleddb7df22014-11-25 20:56:51 +0000682 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
683}
684
Calin Juravle9aec02f2014-11-18 23:06:35 +0000685template<typename T>
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100686void HGraphBuilder::Binop_12x(const Instruction& instruction, Primitive::Type type) {
687 HInstruction* first = LoadLocal(instruction.VRegA(), type);
688 HInstruction* second = LoadLocal(instruction.VRegB(), type);
689 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100690 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
691}
692
693template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000694void HGraphBuilder::Binop_12x_shift(const Instruction& instruction, Primitive::Type type) {
695 HInstruction* first = LoadLocal(instruction.VRegA(), type);
696 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
697 current_block_->AddInstruction(new (arena_) T(type, first, second));
698 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
699}
700
701template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000702void HGraphBuilder::Binop_12x(const Instruction& instruction,
703 Primitive::Type type,
704 uint32_t dex_pc) {
705 HInstruction* first = LoadLocal(instruction.VRegA(), type);
706 HInstruction* second = LoadLocal(instruction.VRegB(), type);
707 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
708 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
709}
710
711template<typename T>
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100712void HGraphBuilder::Binop_22s(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100713 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000714 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100715 if (reverse) {
716 std::swap(first, second);
717 }
718 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
719 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
720}
721
722template<typename T>
723void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100724 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000725 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100726 if (reverse) {
727 std::swap(first, second);
728 }
729 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
730 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
731}
732
Calin Juravle0c25d102015-04-20 14:49:09 +0100733static bool RequiresConstructorBarrier(const DexCompilationUnit* cu, const CompilerDriver& driver) {
Calin Juravle27df7582015-04-17 19:12:31 +0100734 Thread* self = Thread::Current();
Calin Juravle0c25d102015-04-20 14:49:09 +0100735 return cu->IsConstructor()
736 && driver.RequiresConstructorBarrier(self, cu->GetDexFile(), cu->GetClassDefIndex());
Calin Juravle27df7582015-04-17 19:12:31 +0100737}
738
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100739void HGraphBuilder::BuildReturn(const Instruction& instruction, Primitive::Type type) {
740 if (type == Primitive::kPrimVoid) {
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100741 if (graph_->ShouldGenerateConstructorBarrier()) {
742 // The compilation unit is null during testing.
743 if (dex_compilation_unit_ != nullptr) {
744 DCHECK(RequiresConstructorBarrier(dex_compilation_unit_, *compiler_driver_))
745 << "Inconsistent use of ShouldGenerateConstructorBarrier. Should not generate a barrier.";
746 }
Calin Juravle27df7582015-04-17 19:12:31 +0100747 current_block_->AddInstruction(new (arena_) HMemoryBarrier(kStoreStore));
748 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100749 current_block_->AddInstruction(new (arena_) HReturnVoid());
750 } else {
751 HInstruction* value = LoadLocal(instruction.VRegA(), type);
752 current_block_->AddInstruction(new (arena_) HReturn(value));
753 }
754 current_block_->AddSuccessor(exit_block_);
755 current_block_ = nullptr;
756}
757
758bool HGraphBuilder::BuildInvoke(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000759 uint32_t dex_pc,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100760 uint32_t method_idx,
761 uint32_t number_of_vreg_arguments,
762 bool is_range,
763 uint32_t* args,
764 uint32_t register_index) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100765 Instruction::Code opcode = instruction.Opcode();
766 InvokeType invoke_type;
767 switch (opcode) {
768 case Instruction::INVOKE_STATIC:
769 case Instruction::INVOKE_STATIC_RANGE:
770 invoke_type = kStatic;
771 break;
772 case Instruction::INVOKE_DIRECT:
773 case Instruction::INVOKE_DIRECT_RANGE:
774 invoke_type = kDirect;
775 break;
776 case Instruction::INVOKE_VIRTUAL:
777 case Instruction::INVOKE_VIRTUAL_RANGE:
778 invoke_type = kVirtual;
779 break;
780 case Instruction::INVOKE_INTERFACE:
781 case Instruction::INVOKE_INTERFACE_RANGE:
782 invoke_type = kInterface;
783 break;
784 case Instruction::INVOKE_SUPER_RANGE:
785 case Instruction::INVOKE_SUPER:
786 invoke_type = kSuper;
787 break;
788 default:
789 LOG(FATAL) << "Unexpected invoke op: " << opcode;
790 return false;
791 }
792
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100793 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
794 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(method_id.proto_idx_);
795 const char* descriptor = dex_file_->StringDataByIdx(proto_id.shorty_idx_);
796 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100797 bool is_instance_call = invoke_type != kStatic;
Nicolas Geoffray2e335252015-06-18 11:11:27 +0100798 // Remove the return type from the 'proto'.
799 size_t number_of_arguments = strlen(descriptor) - 1;
800 if (is_instance_call) {
801 // One extra argument for 'this'.
802 ++number_of_arguments;
803 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100804
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000805 MethodReference target_method(dex_file_, method_idx);
806 uintptr_t direct_code;
807 uintptr_t direct_method;
808 int table_index;
809 InvokeType optimized_invoke_type = invoke_type;
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000810
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000811 if (!compiler_driver_->ComputeInvokeInfo(dex_compilation_unit_, dex_pc, true, true,
812 &optimized_invoke_type, &target_method, &table_index,
813 &direct_code, &direct_method)) {
Nicolas Geoffray2e335252015-06-18 11:11:27 +0100814 VLOG(compiler) << "Did not compile "
815 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
Calin Juravle48c2b032014-12-09 18:11:36 +0000816 << " because a method call could not be resolved";
817 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedMethod);
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000818 return false;
819 }
820 DCHECK(optimized_invoke_type != kSuper);
821
Roland Levillain4c0eb422015-04-24 16:43:49 +0100822 // By default, consider that the called method implicitly requires
823 // an initialization check of its declaring method.
824 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement =
825 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
826 // Potential class initialization check, in the case of a static method call.
827 HClinitCheck* clinit_check = nullptr;
Jeff Hao848f70a2014-01-15 13:49:50 -0800828 // Replace calls to String.<init> with StringFactory.
829 int32_t string_init_offset = 0;
830 bool is_string_init = compiler_driver_->IsStringInit(method_idx, dex_file_, &string_init_offset);
831 if (is_string_init) {
832 return_type = Primitive::kPrimNot;
833 is_instance_call = false;
834 number_of_arguments--;
835 invoke_type = kStatic;
836 optimized_invoke_type = kStatic;
837 }
Roland Levillain4c0eb422015-04-24 16:43:49 +0100838
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000839 HInvoke* invoke = nullptr;
Roland Levillain4c0eb422015-04-24 16:43:49 +0100840
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000841 if (optimized_invoke_type == kVirtual) {
842 invoke = new (arena_) HInvokeVirtual(
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800843 arena_, number_of_arguments, return_type, dex_pc, method_idx, table_index);
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000844 } else if (optimized_invoke_type == kInterface) {
845 invoke = new (arena_) HInvokeInterface(
846 arena_, number_of_arguments, return_type, dex_pc, method_idx, table_index);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100847 } else {
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000848 DCHECK(optimized_invoke_type == kDirect || optimized_invoke_type == kStatic);
849 // Sharpening to kDirect only works if we compile PIC.
850 DCHECK((optimized_invoke_type == invoke_type) || (optimized_invoke_type != kDirect)
851 || compiler_driver_->GetCompilerOptions().GetCompilePic());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000852 bool is_recursive =
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100853 (target_method.dex_method_index == outer_compilation_unit_->GetDexMethodIndex())
854 && (target_method.dex_file == outer_compilation_unit_->GetDexFile());
Roland Levillain4c0eb422015-04-24 16:43:49 +0100855
Jeff Haocad65422015-06-18 21:16:08 -0700856 if (optimized_invoke_type == kStatic && !is_string_init) {
Roland Levillain4c0eb422015-04-24 16:43:49 +0100857 ScopedObjectAccess soa(Thread::Current());
858 StackHandleScope<4> hs(soa.Self());
859 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
860 dex_compilation_unit_->GetClassLinker()->FindDexCache(
861 *dex_compilation_unit_->GetDexFile())));
862 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
863 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700864 ArtMethod* resolved_method = compiler_driver_->ResolveMethod(
865 soa, dex_cache, class_loader, dex_compilation_unit_, method_idx, optimized_invoke_type);
Roland Levillain4c0eb422015-04-24 16:43:49 +0100866
867 if (resolved_method == nullptr) {
868 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedMethod);
869 return false;
870 }
871
872 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
873 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
874 outer_compilation_unit_->GetClassLinker()->FindDexCache(outer_dex_file)));
Nicolas Geoffrayafd06412015-06-20 22:44:47 +0100875 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Roland Levillain4c0eb422015-04-24 16:43:49 +0100876
877 // The index at which the method's class is stored in the DexCache's type array.
878 uint32_t storage_index = DexFile::kDexNoIndex;
Nicolas Geoffrayafd06412015-06-20 22:44:47 +0100879 bool is_outer_class = (resolved_method->GetDeclaringClass() == outer_class.Get());
880 if (is_outer_class) {
881 storage_index = outer_class->GetDexTypeIndex();
Roland Levillain4c0eb422015-04-24 16:43:49 +0100882 } else if (outer_dex_cache.Get() == dex_cache.Get()) {
883 // Get `storage_index` from IsClassOfStaticMethodAvailableToReferrer.
884 compiler_driver_->IsClassOfStaticMethodAvailableToReferrer(outer_dex_cache.Get(),
Nicolas Geoffrayafd06412015-06-20 22:44:47 +0100885 GetCompilingClass(),
Roland Levillain4c0eb422015-04-24 16:43:49 +0100886 resolved_method,
887 method_idx,
888 &storage_index);
889 }
890
Nicolas Geoffrayb783b402015-06-22 11:06:43 +0100891 if (!outer_class->IsInterface()
892 && outer_class->IsSubClass(resolved_method->GetDeclaringClass())) {
Nicolas Geoffrayafd06412015-06-20 22:44:47 +0100893 // If the outer class is the declaring class or a subclass
Roland Levillain5f02c6c2015-04-24 19:14:22 +0100894 // of the declaring class, no class initialization is needed
895 // before the static method call.
Nicolas Geoffrayafd06412015-06-20 22:44:47 +0100896 // Note that in case of inlining, we do not need to add clinit checks
897 // to calls that satisfy this subclass check with any inlined methods. This
898 // will be detected by the optimization passes.
Roland Levillain4c0eb422015-04-24 16:43:49 +0100899 clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
900 } else if (storage_index != DexFile::kDexNoIndex) {
901 // If the method's class type index is available, check
902 // whether we should add an explicit class initialization
903 // check for its declaring class before the static method call.
904
905 // TODO: find out why this check is needed.
906 bool is_in_dex_cache = compiler_driver_->CanAssumeTypeIsPresentInDexCache(
907 *outer_compilation_unit_->GetDexFile(), storage_index);
908 bool is_initialized =
909 resolved_method->GetDeclaringClass()->IsInitialized() && is_in_dex_cache;
910
911 if (is_initialized) {
912 clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
913 } else {
914 clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +0100915 HLoadClass* load_class = new (arena_) HLoadClass(
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100916 graph_->GetCurrentMethod(),
917 storage_index,
918 *dex_compilation_unit_->GetDexFile(),
Nicolas Geoffrayafd06412015-06-20 22:44:47 +0100919 is_outer_class,
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100920 dex_pc);
Roland Levillain4c0eb422015-04-24 16:43:49 +0100921 current_block_->AddInstruction(load_class);
922 clinit_check = new (arena_) HClinitCheck(load_class, dex_pc);
923 current_block_->AddInstruction(clinit_check);
Roland Levillain4c0eb422015-04-24 16:43:49 +0100924 }
925 }
926 }
927
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100928 invoke = new (arena_) HInvokeStaticOrDirect(arena_,
929 number_of_arguments,
930 return_type,
931 dex_pc,
932 target_method.dex_method_index,
933 is_recursive,
934 string_init_offset,
935 invoke_type,
936 optimized_invoke_type,
937 clinit_check_requirement);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100938 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100939
940 size_t start_index = 0;
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000941 Temporaries temps(graph_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100942 if (is_instance_call) {
943 HInstruction* arg = LoadLocal(is_range ? register_index : args[0], Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000944 HNullCheck* null_check = new (arena_) HNullCheck(arg, dex_pc);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100945 current_block_->AddInstruction(null_check);
946 temps.Add(null_check);
947 invoke->SetArgumentAt(0, null_check);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100948 start_index = 1;
949 }
950
Nicolas Geoffray2e335252015-06-18 11:11:27 +0100951 uint32_t descriptor_index = 1; // Skip the return type.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100952 uint32_t argument_index = start_index;
Jeff Hao848f70a2014-01-15 13:49:50 -0800953 if (is_string_init) {
954 start_index = 1;
955 }
Nicolas Geoffray2e335252015-06-18 11:11:27 +0100956 for (size_t i = start_index;
957 // Make sure we don't go over the expected arguments or over the number of
958 // dex registers given. If the instruction was seen as dead by the verifier,
959 // it hasn't been properly checked.
960 (i < number_of_vreg_arguments) && (argument_index < number_of_arguments);
961 i++, argument_index++) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100962 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100963 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
Nicolas Geoffray2e335252015-06-18 11:11:27 +0100964 if (!is_range
965 && is_wide
966 && ((i + 1 == number_of_vreg_arguments) || (args[i] + 1 != args[i + 1]))) {
967 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
968 // reject any class where this is violated. However, the verifier only does these checks
969 // on non trivially dead instructions, so we just bailout the compilation.
970 VLOG(compiler) << "Did not compile "
971 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
972 << " because of non-sequential dex register pair in wide argument";
973 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
974 return false;
975 }
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100976 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
977 invoke->SetArgumentAt(argument_index, arg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100978 if (is_wide) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100979 i++;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100980 }
981 }
Nicolas Geoffray2e335252015-06-18 11:11:27 +0100982
983 if (argument_index != number_of_arguments) {
984 VLOG(compiler) << "Did not compile "
985 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
986 << " because of wrong number of arguments in invoke instruction";
987 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
988 return false;
989 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100990
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100991 if (invoke->IsInvokeStaticOrDirect()) {
992 invoke->SetArgumentAt(argument_index, graph_->GetCurrentMethod());
993 argument_index++;
994 }
995
Roland Levillain4c0eb422015-04-24 16:43:49 +0100996 if (clinit_check_requirement == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit) {
997 // Add the class initialization check as last input of `invoke`.
998 DCHECK(clinit_check != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +0100999 invoke->SetArgumentAt(argument_index, clinit_check);
Roland Levillain4c0eb422015-04-24 16:43:49 +01001000 }
1001
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001002 current_block_->AddInstruction(invoke);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001003 latest_result_ = invoke;
Jeff Hao848f70a2014-01-15 13:49:50 -08001004
1005 // Add move-result for StringFactory method.
1006 if (is_string_init) {
1007 uint32_t orig_this_reg = is_range ? register_index : args[0];
Nicolas Geoffrayaa919202015-06-21 18:57:02 +01001008 UpdateLocal(orig_this_reg, invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001009 const VerifiedMethod* verified_method =
1010 compiler_driver_->GetVerifiedMethod(dex_file_, dex_compilation_unit_->GetDexMethodIndex());
1011 if (verified_method == nullptr) {
1012 LOG(WARNING) << "No verified method for method calling String.<init>: "
1013 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_);
1014 return false;
1015 }
1016 const SafeMap<uint32_t, std::set<uint32_t>>& string_init_map =
1017 verified_method->GetStringInitPcRegMap();
1018 auto map_it = string_init_map.find(dex_pc);
1019 if (map_it != string_init_map.end()) {
1020 std::set<uint32_t> reg_set = map_it->second;
1021 for (auto set_it = reg_set.begin(); set_it != reg_set.end(); ++set_it) {
Nicolas Geoffrayaa919202015-06-21 18:57:02 +01001022 HInstruction* load_local = LoadLocal(orig_this_reg, Primitive::kPrimNot);
1023 UpdateLocal(*set_it, load_local);
Jeff Hao848f70a2014-01-15 13:49:50 -08001024 }
1025 }
Jeff Hao848f70a2014-01-15 13:49:50 -08001026 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001027 return true;
1028}
1029
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001030bool HGraphBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001031 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001032 bool is_put) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001033 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1034 uint32_t obj_reg = instruction.VRegB_22c();
1035 uint16_t field_index = instruction.VRegC_22c();
1036
1037 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierc7853442015-03-27 14:35:38 -07001038 ArtField* resolved_field =
1039 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001040
Mathieu Chartierc7853442015-03-27 14:35:38 -07001041 if (resolved_field == nullptr) {
Calin Juravle48c2b032014-12-09 18:11:36 +00001042 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedField);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001043 return false;
1044 }
Calin Juravle52c48962014-12-16 17:02:57 +00001045
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001046 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001047
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001048 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00001049 current_block_->AddInstruction(new (arena_) HNullCheck(object, dex_pc));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001050 if (is_put) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001051 Temporaries temps(graph_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001052 HInstruction* null_check = current_block_->GetLastInstruction();
1053 // We need one temporary for the null check.
1054 temps.Add(null_check);
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001055 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001056 current_block_->AddInstruction(new (arena_) HInstanceFieldSet(
1057 null_check,
1058 value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001059 field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00001060 resolved_field->GetOffset(),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001061 resolved_field->IsVolatile(),
1062 field_index,
1063 *dex_file_));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001064 } else {
1065 current_block_->AddInstruction(new (arena_) HInstanceFieldGet(
1066 current_block_->GetLastInstruction(),
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +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
1073 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1074 }
1075 return true;
1076}
1077
Nicolas Geoffray30451742015-06-19 13:32:41 +01001078static mirror::Class* GetClassFrom(CompilerDriver* driver,
1079 const DexCompilationUnit& compilation_unit) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001080 ScopedObjectAccess soa(Thread::Current());
1081 StackHandleScope<2> hs(soa.Self());
Nicolas Geoffray30451742015-06-19 13:32:41 +01001082 const DexFile& dex_file = *compilation_unit.GetDexFile();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001083 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Nicolas Geoffray30451742015-06-19 13:32:41 +01001084 soa.Decode<mirror::ClassLoader*>(compilation_unit.GetClassLoader())));
1085 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
1086 compilation_unit.GetClassLinker()->FindDexCache(dex_file)));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001087
Nicolas Geoffray30451742015-06-19 13:32:41 +01001088 return driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, &compilation_unit);
1089}
1090
1091mirror::Class* HGraphBuilder::GetOutermostCompilingClass() const {
1092 return GetClassFrom(compiler_driver_, *outer_compilation_unit_);
1093}
1094
1095mirror::Class* HGraphBuilder::GetCompilingClass() const {
1096 return GetClassFrom(compiler_driver_, *dex_compilation_unit_);
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001097}
1098
1099bool HGraphBuilder::IsOutermostCompilingClass(uint16_t type_index) const {
1100 ScopedObjectAccess soa(Thread::Current());
1101 StackHandleScope<4> hs(soa.Self());
1102 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
1103 dex_compilation_unit_->GetClassLinker()->FindDexCache(*dex_compilation_unit_->GetDexFile())));
1104 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1105 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
1106 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
1107 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
Nicolas Geoffrayafd06412015-06-20 22:44:47 +01001108 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001109
Nicolas Geoffrayafd06412015-06-20 22:44:47 +01001110 return outer_class.Get() == cls.Get();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001111}
1112
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001113bool HGraphBuilder::BuildStaticFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001114 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001115 bool is_put) {
1116 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1117 uint16_t field_index = instruction.VRegB_21c();
1118
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001119 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierc7853442015-03-27 14:35:38 -07001120 StackHandleScope<4> hs(soa.Self());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001121 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
1122 dex_compilation_unit_->GetClassLinker()->FindDexCache(*dex_compilation_unit_->GetDexFile())));
1123 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1124 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
Mathieu Chartierc7853442015-03-27 14:35:38 -07001125 ArtField* resolved_field = compiler_driver_->ResolveField(
1126 soa, dex_cache, class_loader, dex_compilation_unit_, field_index, true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001127
Mathieu Chartierc7853442015-03-27 14:35:38 -07001128 if (resolved_field == nullptr) {
Calin Juravle48c2b032014-12-09 18:11:36 +00001129 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedField);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001130 return false;
1131 }
1132
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001133 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
1134 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
1135 outer_compilation_unit_->GetClassLinker()->FindDexCache(outer_dex_file)));
Nicolas Geoffray30451742015-06-19 13:32:41 +01001136 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001137
1138 // The index at which the field's class is stored in the DexCache's type array.
1139 uint32_t storage_index;
Nicolas Geoffray30451742015-06-19 13:32:41 +01001140 bool is_outer_class = (outer_class.Get() == resolved_field->GetDeclaringClass());
1141 if (is_outer_class) {
1142 storage_index = outer_class->GetDexTypeIndex();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001143 } else if (outer_dex_cache.Get() != dex_cache.Get()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001144 // The compiler driver cannot currently understand multiple dex caches involved. Just bailout.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001145 return false;
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001146 } else {
1147 std::pair<bool, bool> pair = compiler_driver_->IsFastStaticField(
1148 outer_dex_cache.Get(),
Nicolas Geoffray30451742015-06-19 13:32:41 +01001149 GetCompilingClass(),
Mathieu Chartierc7853442015-03-27 14:35:38 -07001150 resolved_field,
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001151 field_index,
1152 &storage_index);
1153 bool can_easily_access = is_put ? pair.second : pair.first;
1154 if (!can_easily_access) {
1155 return false;
1156 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001157 }
1158
1159 // TODO: find out why this check is needed.
1160 bool is_in_dex_cache = compiler_driver_->CanAssumeTypeIsPresentInDexCache(
Nicolas Geoffray6a816cf2015-03-24 16:17:56 +00001161 *outer_compilation_unit_->GetDexFile(), storage_index);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001162 bool is_initialized = resolved_field->GetDeclaringClass()->IsInitialized() && is_in_dex_cache;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001163
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001164 HLoadClass* constant = new (arena_) HLoadClass(graph_->GetCurrentMethod(),
1165 storage_index,
1166 *dex_compilation_unit_->GetDexFile(),
Nicolas Geoffray30451742015-06-19 13:32:41 +01001167 is_outer_class,
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001168 dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001169 current_block_->AddInstruction(constant);
1170
1171 HInstruction* cls = constant;
Nicolas Geoffray30451742015-06-19 13:32:41 +01001172 if (!is_initialized && !is_outer_class) {
Calin Juravle225ff812014-11-13 16:46:39 +00001173 cls = new (arena_) HClinitCheck(constant, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001174 current_block_->AddInstruction(cls);
1175 }
1176
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001177 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001178 if (is_put) {
1179 // We need to keep the class alive before loading the value.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001180 Temporaries temps(graph_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001181 temps.Add(cls);
1182 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1183 DCHECK_EQ(value->GetType(), field_type);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001184 current_block_->AddInstruction(new (arena_) HStaticFieldSet(cls,
1185 value,
1186 field_type,
1187 resolved_field->GetOffset(),
1188 resolved_field->IsVolatile(),
1189 field_index,
1190 *dex_file_));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001191 } else {
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001192 current_block_->AddInstruction(new (arena_) HStaticFieldGet(cls,
1193 field_type,
1194 resolved_field->GetOffset(),
1195 resolved_field->IsVolatile(),
1196 field_index,
1197 *dex_file_));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001198 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1199 }
1200 return true;
1201}
1202
Calin Juravlebacfec32014-11-14 15:54:36 +00001203void HGraphBuilder::BuildCheckedDivRem(uint16_t out_vreg,
1204 uint16_t first_vreg,
1205 int64_t second_vreg_or_constant,
1206 uint32_t dex_pc,
1207 Primitive::Type type,
1208 bool second_is_constant,
1209 bool isDiv) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001210 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Calin Juravled0d48522014-11-04 16:40:20 +00001211
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001212 HInstruction* first = LoadLocal(first_vreg, type);
1213 HInstruction* second = nullptr;
1214 if (second_is_constant) {
1215 if (type == Primitive::kPrimInt) {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001216 second = graph_->GetIntConstant(second_vreg_or_constant);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001217 } else {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001218 second = graph_->GetLongConstant(second_vreg_or_constant);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001219 }
1220 } else {
1221 second = LoadLocal(second_vreg_or_constant, type);
1222 }
1223
1224 if (!second_is_constant
1225 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0)
1226 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) {
1227 second = new (arena_) HDivZeroCheck(second, dex_pc);
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001228 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +00001229 current_block_->AddInstruction(second);
1230 temps.Add(current_block_->GetLastInstruction());
1231 }
1232
Calin Juravlebacfec32014-11-14 15:54:36 +00001233 if (isDiv) {
1234 current_block_->AddInstruction(new (arena_) HDiv(type, first, second, dex_pc));
1235 } else {
1236 current_block_->AddInstruction(new (arena_) HRem(type, first, second, dex_pc));
1237 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001238 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
Calin Juravled0d48522014-11-04 16:40:20 +00001239}
1240
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001241void HGraphBuilder::BuildArrayAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001242 uint32_t dex_pc,
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001243 bool is_put,
1244 Primitive::Type anticipated_type) {
1245 uint8_t source_or_dest_reg = instruction.VRegA_23x();
1246 uint8_t array_reg = instruction.VRegB_23x();
1247 uint8_t index_reg = instruction.VRegC_23x();
1248
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001249 // We need one temporary for the null check, one for the index, and one for the length.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001250 Temporaries temps(graph_);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001251
1252 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00001253 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001254 current_block_->AddInstruction(object);
1255 temps.Add(object);
1256
1257 HInstruction* length = new (arena_) HArrayLength(object);
1258 current_block_->AddInstruction(length);
1259 temps.Add(length);
1260 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt);
Calin Juravle225ff812014-11-13 16:46:39 +00001261 index = new (arena_) HBoundsCheck(index, length, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001262 current_block_->AddInstruction(index);
1263 temps.Add(index);
1264 if (is_put) {
1265 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
1266 // TODO: Insert a type check node if the type is Object.
Nicolas Geoffray39468442014-09-02 15:17:15 +01001267 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001268 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001269 } else {
1270 current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type));
1271 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1272 }
Mark Mendell1152c922015-04-24 17:06:35 -04001273 graph_->SetHasBoundsChecks(true);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001274}
1275
Calin Juravle225ff812014-11-13 16:46:39 +00001276void HGraphBuilder::BuildFilledNewArray(uint32_t dex_pc,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001277 uint32_t type_index,
1278 uint32_t number_of_vreg_arguments,
1279 bool is_range,
1280 uint32_t* args,
1281 uint32_t register_index) {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001282 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments);
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001283 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
1284 ? kQuickAllocArrayWithAccessCheck
1285 : kQuickAllocArray;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01001286 HInstruction* object = new (arena_) HNewArray(length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01001287 graph_->GetCurrentMethod(),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01001288 dex_pc,
1289 type_index,
1290 *dex_compilation_unit_->GetDexFile(),
1291 entrypoint);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001292 current_block_->AddInstruction(object);
1293
1294 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1295 DCHECK_EQ(descriptor[0], '[') << descriptor;
1296 char primitive = descriptor[1];
1297 DCHECK(primitive == 'I'
1298 || primitive == 'L'
1299 || primitive == '[') << descriptor;
1300 bool is_reference_array = (primitive == 'L') || (primitive == '[');
1301 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
1302
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001303 Temporaries temps(graph_);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001304 temps.Add(object);
1305 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
1306 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
David Brazdil8d5b8b22015-03-24 10:51:52 +00001307 HInstruction* index = graph_->GetIntConstant(i);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001308 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001309 new (arena_) HArraySet(object, index, value, type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001310 }
1311 latest_result_ = object;
1312}
1313
1314template <typename T>
1315void HGraphBuilder::BuildFillArrayData(HInstruction* object,
1316 const T* data,
1317 uint32_t element_count,
1318 Primitive::Type anticipated_type,
Calin Juravle225ff812014-11-13 16:46:39 +00001319 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001320 for (uint32_t i = 0; i < element_count; ++i) {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001321 HInstruction* index = graph_->GetIntConstant(i);
1322 HInstruction* value = graph_->GetIntConstant(data[i]);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001323 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001324 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001325 }
1326}
1327
Calin Juravle225ff812014-11-13 16:46:39 +00001328void HGraphBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001329 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +00001330 HInstruction* array = LoadLocal(instruction.VRegA_31t(), Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00001331 HNullCheck* null_check = new (arena_) HNullCheck(array, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001332 current_block_->AddInstruction(null_check);
1333 temps.Add(null_check);
1334
1335 HInstruction* length = new (arena_) HArrayLength(null_check);
1336 current_block_->AddInstruction(length);
1337
Calin Juravle225ff812014-11-13 16:46:39 +00001338 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
Calin Juravled0d48522014-11-04 16:40:20 +00001339 const Instruction::ArrayDataPayload* payload =
1340 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_start_ + payload_offset);
1341 const uint8_t* data = payload->data;
1342 uint32_t element_count = payload->element_count;
1343
1344 // Implementation of this DEX instruction seems to be that the bounds check is
1345 // done before doing any stores.
David Brazdil8d5b8b22015-03-24 10:51:52 +00001346 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1);
Calin Juravle225ff812014-11-13 16:46:39 +00001347 current_block_->AddInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
Calin Juravled0d48522014-11-04 16:40:20 +00001348
1349 switch (payload->element_width) {
1350 case 1:
1351 BuildFillArrayData(null_check,
1352 reinterpret_cast<const int8_t*>(data),
1353 element_count,
1354 Primitive::kPrimByte,
Calin Juravle225ff812014-11-13 16:46:39 +00001355 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001356 break;
1357 case 2:
1358 BuildFillArrayData(null_check,
1359 reinterpret_cast<const int16_t*>(data),
1360 element_count,
1361 Primitive::kPrimShort,
Calin Juravle225ff812014-11-13 16:46:39 +00001362 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001363 break;
1364 case 4:
1365 BuildFillArrayData(null_check,
1366 reinterpret_cast<const int32_t*>(data),
1367 element_count,
1368 Primitive::kPrimInt,
Calin Juravle225ff812014-11-13 16:46:39 +00001369 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001370 break;
1371 case 8:
1372 BuildFillWideArrayData(null_check,
1373 reinterpret_cast<const int64_t*>(data),
1374 element_count,
Calin Juravle225ff812014-11-13 16:46:39 +00001375 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001376 break;
1377 default:
1378 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1379 }
Mark Mendell1152c922015-04-24 17:06:35 -04001380 graph_->SetHasBoundsChecks(true);
Calin Juravled0d48522014-11-04 16:40:20 +00001381}
1382
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001383void HGraphBuilder::BuildFillWideArrayData(HInstruction* object,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +01001384 const int64_t* data,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001385 uint32_t element_count,
Calin Juravle225ff812014-11-13 16:46:39 +00001386 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001387 for (uint32_t i = 0; i < element_count; ++i) {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001388 HInstruction* index = graph_->GetIntConstant(i);
1389 HInstruction* value = graph_->GetLongConstant(data[i]);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001390 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001391 object, index, value, Primitive::kPrimLong, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001392 }
1393}
1394
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001395bool HGraphBuilder::BuildTypeCheck(const Instruction& instruction,
1396 uint8_t destination,
1397 uint8_t reference,
1398 uint16_t type_index,
Calin Juravle225ff812014-11-13 16:46:39 +00001399 uint32_t dex_pc) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001400 bool type_known_final;
1401 bool type_known_abstract;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001402 // `CanAccessTypeWithoutChecks` will tell whether the method being
1403 // built is trying to access its own class, so that the generated
1404 // code can optimize for this case. However, the optimization does not
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001405 // work for inlining, so we use `IsOutermostCompilingClass` instead.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001406 bool dont_use_is_referrers_class;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001407 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
1408 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001409 &type_known_final, &type_known_abstract, &dont_use_is_referrers_class);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001410 if (!can_access) {
Calin Juravle48c2b032014-12-09 18:11:36 +00001411 MaybeRecordStat(MethodCompilationStat::kNotCompiledCantAccesType);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001412 return false;
1413 }
1414 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot);
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001415 HLoadClass* cls = new (arena_) HLoadClass(
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001416 graph_->GetCurrentMethod(),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01001417 type_index,
1418 *dex_compilation_unit_->GetDexFile(),
1419 IsOutermostCompilingClass(type_index),
1420 dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001421 current_block_->AddInstruction(cls);
1422 // The class needs a temporary before being used by the type check.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001423 Temporaries temps(graph_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001424 temps.Add(cls);
1425 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
1426 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001427 new (arena_) HInstanceOf(object, cls, type_known_final, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001428 UpdateLocal(destination, current_block_->GetLastInstruction());
1429 } else {
1430 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
1431 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001432 new (arena_) HCheckCast(object, cls, type_known_final, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001433 }
1434 return true;
1435}
1436
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001437bool HGraphBuilder::NeedsAccessCheck(uint32_t type_index) const {
1438 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
1439 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index);
1440}
1441
Calin Juravle48c2b032014-12-09 18:11:36 +00001442void HGraphBuilder::BuildPackedSwitch(const Instruction& instruction, uint32_t dex_pc) {
David Brazdil2ef645b2015-06-17 18:20:52 +01001443 // Verifier guarantees that the payload for PackedSwitch contains:
1444 // (a) number of entries (may be zero)
1445 // (b) first and lowest switch case value (entry 0, always present)
1446 // (c) list of target pcs (entries 1 <= i <= N)
Andreas Gamped881df52014-11-24 23:28:39 -08001447 SwitchTable table(instruction, dex_pc, false);
1448
1449 // Value to test against.
1450 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
1451
David Brazdil2ef645b2015-06-17 18:20:52 +01001452 // Retrieve number of entries.
Andreas Gampee4d4d322014-12-04 09:09:57 -08001453 uint16_t num_entries = table.GetNumEntries();
David Brazdil2ef645b2015-06-17 18:20:52 +01001454 if (num_entries == 0) {
1455 return;
1456 }
Andreas Gampee4d4d322014-12-04 09:09:57 -08001457
Andreas Gamped881df52014-11-24 23:28:39 -08001458 // Chained cmp-and-branch, starting from starting_key.
1459 int32_t starting_key = table.GetEntryAt(0);
1460
Andreas Gamped881df52014-11-24 23:28:39 -08001461 for (size_t i = 1; i <= num_entries; i++) {
Andreas Gampee4d4d322014-12-04 09:09:57 -08001462 BuildSwitchCaseHelper(instruction, i, i == num_entries, table, value, starting_key + i - 1,
1463 table.GetEntryAt(i), dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08001464 }
Andreas Gamped881df52014-11-24 23:28:39 -08001465}
1466
Calin Juravle48c2b032014-12-09 18:11:36 +00001467void HGraphBuilder::BuildSparseSwitch(const Instruction& instruction, uint32_t dex_pc) {
David Brazdil2ef645b2015-06-17 18:20:52 +01001468 // Verifier guarantees that the payload for SparseSwitch contains:
1469 // (a) number of entries (may be zero)
1470 // (b) sorted key values (entries 0 <= i < N)
1471 // (c) target pcs corresponding to the switch values (entries N <= i < 2*N)
Andreas Gampee4d4d322014-12-04 09:09:57 -08001472 SwitchTable table(instruction, dex_pc, true);
1473
1474 // Value to test against.
1475 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
1476
1477 uint16_t num_entries = table.GetNumEntries();
Andreas Gampee4d4d322014-12-04 09:09:57 -08001478
1479 for (size_t i = 0; i < num_entries; i++) {
1480 BuildSwitchCaseHelper(instruction, i, i == static_cast<size_t>(num_entries) - 1, table, value,
1481 table.GetEntryAt(i), table.GetEntryAt(i + num_entries), dex_pc);
1482 }
Andreas Gampee4d4d322014-12-04 09:09:57 -08001483}
1484
1485void HGraphBuilder::BuildSwitchCaseHelper(const Instruction& instruction, size_t index,
1486 bool is_last_case, const SwitchTable& table,
1487 HInstruction* value, int32_t case_value_int,
1488 int32_t target_offset, uint32_t dex_pc) {
David Brazdil852eaff2015-02-02 15:23:05 +00001489 HBasicBlock* case_target = FindBlockStartingAt(dex_pc + target_offset);
1490 DCHECK(case_target != nullptr);
1491 PotentiallyAddSuspendCheck(case_target, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001492
1493 // The current case's value.
David Brazdil8d5b8b22015-03-24 10:51:52 +00001494 HInstruction* this_case_value = graph_->GetIntConstant(case_value_int);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001495
1496 // Compare value and this_case_value.
1497 HEqual* comparison = new (arena_) HEqual(value, this_case_value);
1498 current_block_->AddInstruction(comparison);
1499 HInstruction* ifinst = new (arena_) HIf(comparison);
1500 current_block_->AddInstruction(ifinst);
1501
1502 // Case hit: use the target offset to determine where to go.
Andreas Gampee4d4d322014-12-04 09:09:57 -08001503 current_block_->AddSuccessor(case_target);
1504
1505 // Case miss: go to the next case (or default fall-through).
1506 // When there is a next case, we use the block stored with the table offset representing this
1507 // case (that is where we registered them in ComputeBranchTargets).
1508 // When there is no next case, we use the following instruction.
1509 // TODO: Find a good way to peel the last iteration to avoid conditional, but still have re-use.
1510 if (!is_last_case) {
1511 HBasicBlock* next_case_target = FindBlockStartingAt(table.GetDexPcForIndex(index));
1512 DCHECK(next_case_target != nullptr);
1513 current_block_->AddSuccessor(next_case_target);
1514
1515 // Need to manually add the block, as there is no dex-pc transition for the cases.
1516 graph_->AddBlock(next_case_target);
1517
1518 current_block_ = next_case_target;
1519 } else {
1520 HBasicBlock* default_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
1521 DCHECK(default_target != nullptr);
1522 current_block_->AddSuccessor(default_target);
1523 current_block_ = nullptr;
1524 }
1525}
1526
David Brazdil852eaff2015-02-02 15:23:05 +00001527void HGraphBuilder::PotentiallyAddSuspendCheck(HBasicBlock* target, uint32_t dex_pc) {
1528 int32_t target_offset = target->GetDexPc() - dex_pc;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001529 if (target_offset <= 0) {
David Brazdil852eaff2015-02-02 15:23:05 +00001530 // DX generates back edges to the first encountered return. We can save
1531 // time of later passes by not adding redundant suspend checks.
David Brazdil2fd6aa52015-02-02 18:58:27 +00001532 HInstruction* last_in_target = target->GetLastInstruction();
1533 if (last_in_target != nullptr &&
1534 (last_in_target->IsReturn() || last_in_target->IsReturnVoid())) {
1535 return;
David Brazdil852eaff2015-02-02 15:23:05 +00001536 }
1537
1538 // Add a suspend check to backward branches which may potentially loop. We
1539 // can remove them after we recognize loops in the graph.
Calin Juravle225ff812014-11-13 16:46:39 +00001540 current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_pc));
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001541 }
1542}
1543
Calin Juravle225ff812014-11-13 16:46:39 +00001544bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001545 if (current_block_ == nullptr) {
1546 return true; // Dead code
1547 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001548
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001549 switch (instruction.Opcode()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001550 case Instruction::CONST_4: {
1551 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001552 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001553 UpdateLocal(register_index, constant);
1554 break;
1555 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001556
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001557 case Instruction::CONST_16: {
1558 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001559 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001560 UpdateLocal(register_index, constant);
1561 break;
1562 }
1563
Dave Allison20dfc792014-06-16 20:44:29 -07001564 case Instruction::CONST: {
1565 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001566 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i());
Dave Allison20dfc792014-06-16 20:44:29 -07001567 UpdateLocal(register_index, constant);
1568 break;
1569 }
1570
1571 case Instruction::CONST_HIGH16: {
1572 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001573 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16);
Dave Allison20dfc792014-06-16 20:44:29 -07001574 UpdateLocal(register_index, constant);
1575 break;
1576 }
1577
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001578 case Instruction::CONST_WIDE_16: {
1579 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001580 // Get 16 bits of constant value, sign extended to 64 bits.
1581 int64_t value = instruction.VRegB_21s();
1582 value <<= 48;
1583 value >>= 48;
David Brazdil8d5b8b22015-03-24 10:51:52 +00001584 HLongConstant* constant = graph_->GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001585 UpdateLocal(register_index, constant);
1586 break;
1587 }
1588
1589 case Instruction::CONST_WIDE_32: {
1590 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001591 // Get 32 bits of constant value, sign extended to 64 bits.
1592 int64_t value = instruction.VRegB_31i();
1593 value <<= 32;
1594 value >>= 32;
David Brazdil8d5b8b22015-03-24 10:51:52 +00001595 HLongConstant* constant = graph_->GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001596 UpdateLocal(register_index, constant);
1597 break;
1598 }
1599
1600 case Instruction::CONST_WIDE: {
1601 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001602 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001603 UpdateLocal(register_index, constant);
1604 break;
1605 }
1606
Dave Allison20dfc792014-06-16 20:44:29 -07001607 case Instruction::CONST_WIDE_HIGH16: {
1608 int32_t register_index = instruction.VRegA();
1609 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
David Brazdil8d5b8b22015-03-24 10:51:52 +00001610 HLongConstant* constant = graph_->GetLongConstant(value);
Dave Allison20dfc792014-06-16 20:44:29 -07001611 UpdateLocal(register_index, constant);
1612 break;
1613 }
1614
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001615 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001616 case Instruction::MOVE:
1617 case Instruction::MOVE_FROM16:
1618 case Instruction::MOVE_16: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001619 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001620 UpdateLocal(instruction.VRegA(), value);
1621 break;
1622 }
1623
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001624 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001625 case Instruction::MOVE_WIDE:
1626 case Instruction::MOVE_WIDE_FROM16:
1627 case Instruction::MOVE_WIDE_16: {
1628 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong);
1629 UpdateLocal(instruction.VRegA(), value);
1630 break;
1631 }
1632
1633 case Instruction::MOVE_OBJECT:
1634 case Instruction::MOVE_OBJECT_16:
1635 case Instruction::MOVE_OBJECT_FROM16: {
1636 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot);
1637 UpdateLocal(instruction.VRegA(), value);
1638 break;
1639 }
1640
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001641 case Instruction::RETURN_VOID: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001642 BuildReturn(instruction, Primitive::kPrimVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001643 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001644 }
1645
Dave Allison20dfc792014-06-16 20:44:29 -07001646#define IF_XX(comparison, cond) \
Calin Juravle225ff812014-11-13 16:46:39 +00001647 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
1648 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001649
Dave Allison20dfc792014-06-16 20:44:29 -07001650 IF_XX(HEqual, EQ);
1651 IF_XX(HNotEqual, NE);
1652 IF_XX(HLessThan, LT);
1653 IF_XX(HLessThanOrEqual, LE);
1654 IF_XX(HGreaterThan, GT);
1655 IF_XX(HGreaterThanOrEqual, GE);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001656
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001657 case Instruction::GOTO:
1658 case Instruction::GOTO_16:
1659 case Instruction::GOTO_32: {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001660 int32_t offset = instruction.GetTargetOffset();
Calin Juravle225ff812014-11-13 16:46:39 +00001661 HBasicBlock* target = FindBlockStartingAt(offset + dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001662 DCHECK(target != nullptr);
David Brazdil852eaff2015-02-02 15:23:05 +00001663 PotentiallyAddSuspendCheck(target, dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001664 current_block_->AddInstruction(new (arena_) HGoto());
1665 current_block_->AddSuccessor(target);
1666 current_block_ = nullptr;
1667 break;
1668 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001669
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001670 case Instruction::RETURN: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001671 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001672 break;
1673 }
1674
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001675 case Instruction::RETURN_OBJECT: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001676 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001677 break;
1678 }
1679
1680 case Instruction::RETURN_WIDE: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001681 BuildReturn(instruction, return_type_);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001682 break;
1683 }
1684
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001685 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001686 case Instruction::INVOKE_INTERFACE:
1687 case Instruction::INVOKE_STATIC:
1688 case Instruction::INVOKE_SUPER:
1689 case Instruction::INVOKE_VIRTUAL: {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001690 uint32_t method_idx = instruction.VRegB_35c();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001691 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001692 uint32_t args[5];
Ian Rogers29a26482014-05-02 15:27:29 -07001693 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00001694 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001695 number_of_vreg_arguments, false, args, -1)) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001696 return false;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001697 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001698 break;
1699 }
1700
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001701 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001702 case Instruction::INVOKE_INTERFACE_RANGE:
1703 case Instruction::INVOKE_STATIC_RANGE:
1704 case Instruction::INVOKE_SUPER_RANGE:
1705 case Instruction::INVOKE_VIRTUAL_RANGE: {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001706 uint32_t method_idx = instruction.VRegB_3rc();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001707 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1708 uint32_t register_index = instruction.VRegC();
Calin Juravle225ff812014-11-13 16:46:39 +00001709 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001710 number_of_vreg_arguments, true, nullptr, register_index)) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001711 return false;
1712 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001713 break;
1714 }
1715
Roland Levillain88cb1752014-10-20 16:36:47 +01001716 case Instruction::NEG_INT: {
1717 Unop_12x<HNeg>(instruction, Primitive::kPrimInt);
1718 break;
1719 }
1720
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001721 case Instruction::NEG_LONG: {
1722 Unop_12x<HNeg>(instruction, Primitive::kPrimLong);
1723 break;
1724 }
1725
Roland Levillain3dbcb382014-10-28 17:30:07 +00001726 case Instruction::NEG_FLOAT: {
1727 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat);
1728 break;
1729 }
1730
1731 case Instruction::NEG_DOUBLE: {
1732 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble);
1733 break;
1734 }
1735
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001736 case Instruction::NOT_INT: {
1737 Unop_12x<HNot>(instruction, Primitive::kPrimInt);
1738 break;
1739 }
1740
Roland Levillain70566432014-10-24 16:20:17 +01001741 case Instruction::NOT_LONG: {
1742 Unop_12x<HNot>(instruction, Primitive::kPrimLong);
1743 break;
1744 }
1745
Roland Levillaindff1f282014-11-05 14:15:05 +00001746 case Instruction::INT_TO_LONG: {
Roland Levillain624279f2014-12-04 11:54:28 +00001747 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong, dex_pc);
Roland Levillaindff1f282014-11-05 14:15:05 +00001748 break;
1749 }
1750
Roland Levillaincff13742014-11-17 14:32:17 +00001751 case Instruction::INT_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001752 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00001753 break;
1754 }
1755
1756 case Instruction::INT_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001757 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00001758 break;
1759 }
1760
Roland Levillain946e1432014-11-11 17:35:19 +00001761 case Instruction::LONG_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001762 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt, dex_pc);
Roland Levillain946e1432014-11-11 17:35:19 +00001763 break;
1764 }
1765
Roland Levillain6d0e4832014-11-27 18:31:21 +00001766 case Instruction::LONG_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001767 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimFloat, dex_pc);
Roland Levillain6d0e4832014-11-27 18:31:21 +00001768 break;
1769 }
1770
Roland Levillain647b9ed2014-11-27 12:06:00 +00001771 case Instruction::LONG_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001772 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble, dex_pc);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001773 break;
1774 }
1775
Roland Levillain3f8f9362014-12-02 17:45:01 +00001776 case Instruction::FLOAT_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001777 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimInt, dex_pc);
1778 break;
1779 }
1780
1781 case Instruction::FLOAT_TO_LONG: {
1782 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimLong, dex_pc);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001783 break;
1784 }
1785
Roland Levillain8964e2b2014-12-04 12:10:50 +00001786 case Instruction::FLOAT_TO_DOUBLE: {
1787 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimDouble, dex_pc);
1788 break;
1789 }
1790
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001791 case Instruction::DOUBLE_TO_INT: {
1792 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimInt, dex_pc);
1793 break;
1794 }
1795
1796 case Instruction::DOUBLE_TO_LONG: {
1797 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimLong, dex_pc);
1798 break;
1799 }
1800
Roland Levillain8964e2b2014-12-04 12:10:50 +00001801 case Instruction::DOUBLE_TO_FLOAT: {
1802 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimFloat, dex_pc);
1803 break;
1804 }
1805
Roland Levillain51d3fc42014-11-13 14:11:42 +00001806 case Instruction::INT_TO_BYTE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001807 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte, dex_pc);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001808 break;
1809 }
1810
Roland Levillain01a8d712014-11-14 16:27:39 +00001811 case Instruction::INT_TO_SHORT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001812 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort, dex_pc);
Roland Levillain01a8d712014-11-14 16:27:39 +00001813 break;
1814 }
1815
Roland Levillain981e4542014-11-14 11:47:14 +00001816 case Instruction::INT_TO_CHAR: {
Roland Levillain624279f2014-12-04 11:54:28 +00001817 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar, dex_pc);
Roland Levillain981e4542014-11-14 11:47:14 +00001818 break;
1819 }
1820
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001821 case Instruction::ADD_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001822 Binop_23x<HAdd>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001823 break;
1824 }
1825
1826 case Instruction::ADD_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001827 Binop_23x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001828 break;
1829 }
1830
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001831 case Instruction::ADD_DOUBLE: {
1832 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble);
1833 break;
1834 }
1835
1836 case Instruction::ADD_FLOAT: {
1837 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat);
1838 break;
1839 }
1840
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001841 case Instruction::SUB_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001842 Binop_23x<HSub>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001843 break;
1844 }
1845
1846 case Instruction::SUB_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001847 Binop_23x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001848 break;
1849 }
1850
Calin Juravle096cc022014-10-23 17:01:13 +01001851 case Instruction::SUB_FLOAT: {
1852 Binop_23x<HSub>(instruction, Primitive::kPrimFloat);
1853 break;
1854 }
1855
1856 case Instruction::SUB_DOUBLE: {
1857 Binop_23x<HSub>(instruction, Primitive::kPrimDouble);
1858 break;
1859 }
1860
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001861 case Instruction::ADD_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001862 Binop_12x<HAdd>(instruction, Primitive::kPrimInt);
1863 break;
1864 }
1865
Calin Juravle34bacdf2014-10-07 20:23:36 +01001866 case Instruction::MUL_INT: {
1867 Binop_23x<HMul>(instruction, Primitive::kPrimInt);
1868 break;
1869 }
1870
1871 case Instruction::MUL_LONG: {
1872 Binop_23x<HMul>(instruction, Primitive::kPrimLong);
1873 break;
1874 }
1875
Calin Juravleb5bfa962014-10-21 18:02:24 +01001876 case Instruction::MUL_FLOAT: {
1877 Binop_23x<HMul>(instruction, Primitive::kPrimFloat);
1878 break;
1879 }
1880
1881 case Instruction::MUL_DOUBLE: {
1882 Binop_23x<HMul>(instruction, Primitive::kPrimDouble);
1883 break;
1884 }
1885
Calin Juravled0d48522014-11-04 16:40:20 +00001886 case Instruction::DIV_INT: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001887 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1888 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravled0d48522014-11-04 16:40:20 +00001889 break;
1890 }
1891
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001892 case Instruction::DIV_LONG: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001893 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1894 dex_pc, Primitive::kPrimLong, false, true);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001895 break;
1896 }
1897
Calin Juravle7c4954d2014-10-28 16:57:40 +00001898 case Instruction::DIV_FLOAT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001899 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001900 break;
1901 }
1902
1903 case Instruction::DIV_DOUBLE: {
Calin Juravle225ff812014-11-13 16:46:39 +00001904 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001905 break;
1906 }
1907
Calin Juravlebacfec32014-11-14 15:54:36 +00001908 case Instruction::REM_INT: {
1909 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1910 dex_pc, Primitive::kPrimInt, false, false);
1911 break;
1912 }
1913
1914 case Instruction::REM_LONG: {
1915 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1916 dex_pc, Primitive::kPrimLong, false, false);
1917 break;
1918 }
1919
Calin Juravled2ec87d2014-12-08 14:24:46 +00001920 case Instruction::REM_FLOAT: {
1921 Binop_23x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
1922 break;
1923 }
1924
1925 case Instruction::REM_DOUBLE: {
1926 Binop_23x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
1927 break;
1928 }
1929
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001930 case Instruction::AND_INT: {
1931 Binop_23x<HAnd>(instruction, Primitive::kPrimInt);
1932 break;
1933 }
1934
1935 case Instruction::AND_LONG: {
1936 Binop_23x<HAnd>(instruction, Primitive::kPrimLong);
1937 break;
1938 }
1939
Calin Juravle9aec02f2014-11-18 23:06:35 +00001940 case Instruction::SHL_INT: {
1941 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt);
1942 break;
1943 }
1944
1945 case Instruction::SHL_LONG: {
1946 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong);
1947 break;
1948 }
1949
1950 case Instruction::SHR_INT: {
1951 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt);
1952 break;
1953 }
1954
1955 case Instruction::SHR_LONG: {
1956 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong);
1957 break;
1958 }
1959
1960 case Instruction::USHR_INT: {
1961 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt);
1962 break;
1963 }
1964
1965 case Instruction::USHR_LONG: {
1966 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong);
1967 break;
1968 }
1969
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001970 case Instruction::OR_INT: {
1971 Binop_23x<HOr>(instruction, Primitive::kPrimInt);
1972 break;
1973 }
1974
1975 case Instruction::OR_LONG: {
1976 Binop_23x<HOr>(instruction, Primitive::kPrimLong);
1977 break;
1978 }
1979
1980 case Instruction::XOR_INT: {
1981 Binop_23x<HXor>(instruction, Primitive::kPrimInt);
1982 break;
1983 }
1984
1985 case Instruction::XOR_LONG: {
1986 Binop_23x<HXor>(instruction, Primitive::kPrimLong);
1987 break;
1988 }
1989
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001990 case Instruction::ADD_LONG_2ADDR: {
1991 Binop_12x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001992 break;
1993 }
1994
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001995 case Instruction::ADD_DOUBLE_2ADDR: {
1996 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble);
1997 break;
1998 }
1999
2000 case Instruction::ADD_FLOAT_2ADDR: {
2001 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat);
2002 break;
2003 }
2004
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002005 case Instruction::SUB_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002006 Binop_12x<HSub>(instruction, Primitive::kPrimInt);
2007 break;
2008 }
2009
2010 case Instruction::SUB_LONG_2ADDR: {
2011 Binop_12x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002012 break;
2013 }
2014
Calin Juravle096cc022014-10-23 17:01:13 +01002015 case Instruction::SUB_FLOAT_2ADDR: {
2016 Binop_12x<HSub>(instruction, Primitive::kPrimFloat);
2017 break;
2018 }
2019
2020 case Instruction::SUB_DOUBLE_2ADDR: {
2021 Binop_12x<HSub>(instruction, Primitive::kPrimDouble);
2022 break;
2023 }
2024
Calin Juravle34bacdf2014-10-07 20:23:36 +01002025 case Instruction::MUL_INT_2ADDR: {
2026 Binop_12x<HMul>(instruction, Primitive::kPrimInt);
2027 break;
2028 }
2029
2030 case Instruction::MUL_LONG_2ADDR: {
2031 Binop_12x<HMul>(instruction, Primitive::kPrimLong);
2032 break;
2033 }
2034
Calin Juravleb5bfa962014-10-21 18:02:24 +01002035 case Instruction::MUL_FLOAT_2ADDR: {
2036 Binop_12x<HMul>(instruction, Primitive::kPrimFloat);
2037 break;
2038 }
2039
2040 case Instruction::MUL_DOUBLE_2ADDR: {
2041 Binop_12x<HMul>(instruction, Primitive::kPrimDouble);
2042 break;
2043 }
2044
Calin Juravle865fc882014-11-06 17:09:03 +00002045 case Instruction::DIV_INT_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002046 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2047 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravle865fc882014-11-06 17:09:03 +00002048 break;
2049 }
2050
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002051 case Instruction::DIV_LONG_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002052 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2053 dex_pc, Primitive::kPrimLong, false, true);
2054 break;
2055 }
2056
2057 case Instruction::REM_INT_2ADDR: {
2058 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2059 dex_pc, Primitive::kPrimInt, false, false);
2060 break;
2061 }
2062
2063 case Instruction::REM_LONG_2ADDR: {
2064 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2065 dex_pc, Primitive::kPrimLong, false, false);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002066 break;
2067 }
2068
Calin Juravled2ec87d2014-12-08 14:24:46 +00002069 case Instruction::REM_FLOAT_2ADDR: {
2070 Binop_12x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2071 break;
2072 }
2073
2074 case Instruction::REM_DOUBLE_2ADDR: {
2075 Binop_12x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2076 break;
2077 }
2078
Calin Juravle9aec02f2014-11-18 23:06:35 +00002079 case Instruction::SHL_INT_2ADDR: {
2080 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt);
2081 break;
2082 }
2083
2084 case Instruction::SHL_LONG_2ADDR: {
2085 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong);
2086 break;
2087 }
2088
2089 case Instruction::SHR_INT_2ADDR: {
2090 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt);
2091 break;
2092 }
2093
2094 case Instruction::SHR_LONG_2ADDR: {
2095 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong);
2096 break;
2097 }
2098
2099 case Instruction::USHR_INT_2ADDR: {
2100 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt);
2101 break;
2102 }
2103
2104 case Instruction::USHR_LONG_2ADDR: {
2105 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong);
2106 break;
2107 }
2108
Calin Juravle7c4954d2014-10-28 16:57:40 +00002109 case Instruction::DIV_FLOAT_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00002110 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002111 break;
2112 }
2113
2114 case Instruction::DIV_DOUBLE_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00002115 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002116 break;
2117 }
2118
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002119 case Instruction::AND_INT_2ADDR: {
2120 Binop_12x<HAnd>(instruction, Primitive::kPrimInt);
2121 break;
2122 }
2123
2124 case Instruction::AND_LONG_2ADDR: {
2125 Binop_12x<HAnd>(instruction, Primitive::kPrimLong);
2126 break;
2127 }
2128
2129 case Instruction::OR_INT_2ADDR: {
2130 Binop_12x<HOr>(instruction, Primitive::kPrimInt);
2131 break;
2132 }
2133
2134 case Instruction::OR_LONG_2ADDR: {
2135 Binop_12x<HOr>(instruction, Primitive::kPrimLong);
2136 break;
2137 }
2138
2139 case Instruction::XOR_INT_2ADDR: {
2140 Binop_12x<HXor>(instruction, Primitive::kPrimInt);
2141 break;
2142 }
2143
2144 case Instruction::XOR_LONG_2ADDR: {
2145 Binop_12x<HXor>(instruction, Primitive::kPrimLong);
2146 break;
2147 }
2148
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002149 case Instruction::ADD_INT_LIT16: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002150 Binop_22s<HAdd>(instruction, false);
2151 break;
2152 }
2153
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002154 case Instruction::AND_INT_LIT16: {
2155 Binop_22s<HAnd>(instruction, false);
2156 break;
2157 }
2158
2159 case Instruction::OR_INT_LIT16: {
2160 Binop_22s<HOr>(instruction, false);
2161 break;
2162 }
2163
2164 case Instruction::XOR_INT_LIT16: {
2165 Binop_22s<HXor>(instruction, false);
2166 break;
2167 }
2168
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002169 case Instruction::RSUB_INT: {
2170 Binop_22s<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002171 break;
2172 }
2173
Calin Juravle34bacdf2014-10-07 20:23:36 +01002174 case Instruction::MUL_INT_LIT16: {
2175 Binop_22s<HMul>(instruction, false);
2176 break;
2177 }
2178
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002179 case Instruction::ADD_INT_LIT8: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002180 Binop_22b<HAdd>(instruction, false);
2181 break;
2182 }
2183
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002184 case Instruction::AND_INT_LIT8: {
2185 Binop_22b<HAnd>(instruction, false);
2186 break;
2187 }
2188
2189 case Instruction::OR_INT_LIT8: {
2190 Binop_22b<HOr>(instruction, false);
2191 break;
2192 }
2193
2194 case Instruction::XOR_INT_LIT8: {
2195 Binop_22b<HXor>(instruction, false);
2196 break;
2197 }
2198
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002199 case Instruction::RSUB_INT_LIT8: {
2200 Binop_22b<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002201 break;
2202 }
2203
Calin Juravle34bacdf2014-10-07 20:23:36 +01002204 case Instruction::MUL_INT_LIT8: {
2205 Binop_22b<HMul>(instruction, false);
2206 break;
2207 }
2208
Calin Juravled0d48522014-11-04 16:40:20 +00002209 case Instruction::DIV_INT_LIT16:
2210 case Instruction::DIV_INT_LIT8: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002211 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2212 dex_pc, Primitive::kPrimInt, true, true);
2213 break;
2214 }
2215
2216 case Instruction::REM_INT_LIT16:
2217 case Instruction::REM_INT_LIT8: {
2218 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2219 dex_pc, Primitive::kPrimInt, true, false);
Calin Juravled0d48522014-11-04 16:40:20 +00002220 break;
2221 }
2222
Calin Juravle9aec02f2014-11-18 23:06:35 +00002223 case Instruction::SHL_INT_LIT8: {
2224 Binop_22b<HShl>(instruction, false);
2225 break;
2226 }
2227
2228 case Instruction::SHR_INT_LIT8: {
2229 Binop_22b<HShr>(instruction, false);
2230 break;
2231 }
2232
2233 case Instruction::USHR_INT_LIT8: {
2234 Binop_22b<HUShr>(instruction, false);
2235 break;
2236 }
2237
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002238 case Instruction::NEW_INSTANCE: {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002239 uint16_t type_index = instruction.VRegB_21c();
Jeff Hao848f70a2014-01-15 13:49:50 -08002240 if (compiler_driver_->IsStringTypeIndex(type_index, dex_file_)) {
2241 // Turn new-instance of string into a const 0.
2242 int32_t register_index = instruction.VRegA();
2243 HNullConstant* constant = graph_->GetNullConstant();
2244 UpdateLocal(register_index, constant);
2245 } else {
2246 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
2247 ? kQuickAllocObjectWithAccessCheck
2248 : kQuickAllocObject;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002249
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002250 current_block_->AddInstruction(new (arena_) HNewInstance(
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002251 graph_->GetCurrentMethod(),
2252 dex_pc,
2253 type_index,
2254 *dex_compilation_unit_->GetDexFile(),
2255 entrypoint));
Jeff Hao848f70a2014-01-15 13:49:50 -08002256 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
2257 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002258 break;
2259 }
2260
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002261 case Instruction::NEW_ARRAY: {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002262 uint16_t type_index = instruction.VRegC_22c();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002263 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt);
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002264 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
2265 ? kQuickAllocArrayWithAccessCheck
2266 : kQuickAllocArray;
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002267 current_block_->AddInstruction(new (arena_) HNewArray(length,
2268 graph_->GetCurrentMethod(),
2269 dex_pc,
2270 type_index,
2271 *dex_compilation_unit_->GetDexFile(),
2272 entrypoint));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002273 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
2274 break;
2275 }
2276
2277 case Instruction::FILLED_NEW_ARRAY: {
2278 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
2279 uint32_t type_index = instruction.VRegB_35c();
2280 uint32_t args[5];
2281 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00002282 BuildFilledNewArray(dex_pc, type_index, number_of_vreg_arguments, false, args, 0);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002283 break;
2284 }
2285
2286 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2287 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
2288 uint32_t type_index = instruction.VRegB_3rc();
2289 uint32_t register_index = instruction.VRegC_3rc();
2290 BuildFilledNewArray(
Calin Juravle225ff812014-11-13 16:46:39 +00002291 dex_pc, type_index, number_of_vreg_arguments, true, nullptr, register_index);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002292 break;
2293 }
2294
2295 case Instruction::FILL_ARRAY_DATA: {
Calin Juravle225ff812014-11-13 16:46:39 +00002296 BuildFillArrayData(instruction, dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002297 break;
2298 }
2299
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002300 case Instruction::MOVE_RESULT:
Dave Allison20dfc792014-06-16 20:44:29 -07002301 case Instruction::MOVE_RESULT_WIDE:
David Brazdilfc6a86a2015-06-26 10:33:45 +00002302 case Instruction::MOVE_RESULT_OBJECT: {
Nicolas Geoffray1efcc222015-06-24 12:41:20 +01002303 if (latest_result_ == nullptr) {
2304 // Only dead code can lead to this situation, where the verifier
2305 // does not reject the method.
2306 } else {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002307 // An Invoke/FilledNewArray and its MoveResult could have landed in
2308 // different blocks if there was a try/catch block boundary between
2309 // them. For Invoke, we insert a StoreLocal after the instruction. For
2310 // FilledNewArray, the local needs to be updated after the array was
2311 // filled, otherwise we might overwrite an input vreg.
2312 HStoreLocal* update_local =
2313 new (arena_) HStoreLocal(GetLocalAt(instruction.VRegA()), latest_result_);
2314 HBasicBlock* block = latest_result_->GetBlock();
2315 if (block == current_block_) {
2316 // MoveResult and the previous instruction are in the same block.
2317 current_block_->AddInstruction(update_local);
2318 } else {
2319 // The two instructions are in different blocks. Insert the MoveResult
2320 // before the final control-flow instruction of the previous block.
2321 DCHECK(block->EndsWithControlFlowInstruction());
2322 DCHECK(current_block_->GetInstructions().IsEmpty());
2323 block->InsertInstructionBefore(update_local, block->GetLastInstruction());
2324 }
Nicolas Geoffray1efcc222015-06-24 12:41:20 +01002325 latest_result_ = nullptr;
2326 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002327 break;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002328 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002329
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002330 case Instruction::CMP_LONG: {
Mark Mendellc4701932015-04-10 13:18:51 -04002331 Binop_23x_cmp(instruction, Primitive::kPrimLong, kNoBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002332 break;
2333 }
2334
2335 case Instruction::CMPG_FLOAT: {
Mark Mendellc4701932015-04-10 13:18:51 -04002336 Binop_23x_cmp(instruction, Primitive::kPrimFloat, kGtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002337 break;
2338 }
2339
2340 case Instruction::CMPG_DOUBLE: {
Mark Mendellc4701932015-04-10 13:18:51 -04002341 Binop_23x_cmp(instruction, Primitive::kPrimDouble, kGtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002342 break;
2343 }
2344
2345 case Instruction::CMPL_FLOAT: {
Mark Mendellc4701932015-04-10 13:18:51 -04002346 Binop_23x_cmp(instruction, Primitive::kPrimFloat, kLtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002347 break;
2348 }
2349
2350 case Instruction::CMPL_DOUBLE: {
Mark Mendellc4701932015-04-10 13:18:51 -04002351 Binop_23x_cmp(instruction, Primitive::kPrimDouble, kLtBias, dex_pc);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002352 break;
2353 }
2354
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002355 case Instruction::NOP:
2356 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002357
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002358 case Instruction::IGET:
2359 case Instruction::IGET_WIDE:
2360 case Instruction::IGET_OBJECT:
2361 case Instruction::IGET_BOOLEAN:
2362 case Instruction::IGET_BYTE:
2363 case Instruction::IGET_CHAR:
2364 case Instruction::IGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002365 if (!BuildInstanceFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002366 return false;
2367 }
2368 break;
2369 }
2370
2371 case Instruction::IPUT:
2372 case Instruction::IPUT_WIDE:
2373 case Instruction::IPUT_OBJECT:
2374 case Instruction::IPUT_BOOLEAN:
2375 case Instruction::IPUT_BYTE:
2376 case Instruction::IPUT_CHAR:
2377 case Instruction::IPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002378 if (!BuildInstanceFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002379 return false;
2380 }
2381 break;
2382 }
2383
2384 case Instruction::SGET:
2385 case Instruction::SGET_WIDE:
2386 case Instruction::SGET_OBJECT:
2387 case Instruction::SGET_BOOLEAN:
2388 case Instruction::SGET_BYTE:
2389 case Instruction::SGET_CHAR:
2390 case Instruction::SGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002391 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002392 return false;
2393 }
2394 break;
2395 }
2396
2397 case Instruction::SPUT:
2398 case Instruction::SPUT_WIDE:
2399 case Instruction::SPUT_OBJECT:
2400 case Instruction::SPUT_BOOLEAN:
2401 case Instruction::SPUT_BYTE:
2402 case Instruction::SPUT_CHAR:
2403 case Instruction::SPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002404 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002405 return false;
2406 }
2407 break;
2408 }
2409
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002410#define ARRAY_XX(kind, anticipated_type) \
2411 case Instruction::AGET##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00002412 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002413 break; \
2414 } \
2415 case Instruction::APUT##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00002416 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002417 break; \
2418 }
2419
2420 ARRAY_XX(, Primitive::kPrimInt);
2421 ARRAY_XX(_WIDE, Primitive::kPrimLong);
2422 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
2423 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
2424 ARRAY_XX(_BYTE, Primitive::kPrimByte);
2425 ARRAY_XX(_CHAR, Primitive::kPrimChar);
2426 ARRAY_XX(_SHORT, Primitive::kPrimShort);
2427
Nicolas Geoffray39468442014-09-02 15:17:15 +01002428 case Instruction::ARRAY_LENGTH: {
2429 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002430 // No need for a temporary for the null check, it is the only input of the following
2431 // instruction.
Calin Juravle225ff812014-11-13 16:46:39 +00002432 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002433 current_block_->AddInstruction(object);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002434 current_block_->AddInstruction(new (arena_) HArrayLength(object));
2435 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
2436 break;
2437 }
2438
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002439 case Instruction::CONST_STRING: {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002440 current_block_->AddInstruction(
2441 new (arena_) HLoadString(graph_->GetCurrentMethod(), instruction.VRegB_21c(), dex_pc));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002442 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2443 break;
2444 }
2445
2446 case Instruction::CONST_STRING_JUMBO: {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002447 current_block_->AddInstruction(
2448 new (arena_) HLoadString(graph_->GetCurrentMethod(), instruction.VRegB_31c(), dex_pc));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002449 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
2450 break;
2451 }
2452
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002453 case Instruction::CONST_CLASS: {
2454 uint16_t type_index = instruction.VRegB_21c();
2455 bool type_known_final;
2456 bool type_known_abstract;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002457 bool dont_use_is_referrers_class;
2458 // `CanAccessTypeWithoutChecks` will tell whether the method being
2459 // built is trying to access its own class, so that the generated
2460 // code can optimize for this case. However, the optimization does not
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002461 // work for inlining, so we use `IsOutermostCompilingClass` instead.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002462 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
2463 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002464 &type_known_final, &type_known_abstract, &dont_use_is_referrers_class);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002465 if (!can_access) {
Calin Juravle48c2b032014-12-09 18:11:36 +00002466 MaybeRecordStat(MethodCompilationStat::kNotCompiledCantAccesType);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002467 return false;
2468 }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002469 current_block_->AddInstruction(new (arena_) HLoadClass(
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002470 graph_->GetCurrentMethod(),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002471 type_index,
2472 *dex_compilation_unit_->GetDexFile(),
2473 IsOutermostCompilingClass(type_index),
2474 dex_pc));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002475 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2476 break;
2477 }
2478
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002479 case Instruction::MOVE_EXCEPTION: {
2480 current_block_->AddInstruction(new (arena_) HLoadException());
2481 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
2482 break;
2483 }
2484
2485 case Instruction::THROW: {
2486 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00002487 current_block_->AddInstruction(new (arena_) HThrow(exception, dex_pc));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002488 // A throw instruction must branch to the exit block.
2489 current_block_->AddSuccessor(exit_block_);
2490 // We finished building this block. Set the current block to null to avoid
2491 // adding dead instructions to it.
2492 current_block_ = nullptr;
2493 break;
2494 }
2495
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002496 case Instruction::INSTANCE_OF: {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002497 uint8_t destination = instruction.VRegA_22c();
2498 uint8_t reference = instruction.VRegB_22c();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002499 uint16_t type_index = instruction.VRegC_22c();
Calin Juravle225ff812014-11-13 16:46:39 +00002500 if (!BuildTypeCheck(instruction, destination, reference, type_index, dex_pc)) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002501 return false;
2502 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002503 break;
2504 }
2505
2506 case Instruction::CHECK_CAST: {
2507 uint8_t reference = instruction.VRegA_21c();
2508 uint16_t type_index = instruction.VRegB_21c();
Calin Juravle225ff812014-11-13 16:46:39 +00002509 if (!BuildTypeCheck(instruction, -1, reference, type_index, dex_pc)) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002510 return false;
2511 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002512 break;
2513 }
2514
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002515 case Instruction::MONITOR_ENTER: {
2516 current_block_->AddInstruction(new (arena_) HMonitorOperation(
2517 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2518 HMonitorOperation::kEnter,
Calin Juravle225ff812014-11-13 16:46:39 +00002519 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002520 break;
2521 }
2522
2523 case Instruction::MONITOR_EXIT: {
2524 current_block_->AddInstruction(new (arena_) HMonitorOperation(
2525 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2526 HMonitorOperation::kExit,
Calin Juravle225ff812014-11-13 16:46:39 +00002527 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002528 break;
2529 }
2530
Andreas Gamped881df52014-11-24 23:28:39 -08002531 case Instruction::PACKED_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002532 BuildPackedSwitch(instruction, dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08002533 break;
2534 }
2535
Andreas Gampee4d4d322014-12-04 09:09:57 -08002536 case Instruction::SPARSE_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002537 BuildSparseSwitch(instruction, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08002538 break;
2539 }
2540
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002541 default:
Calin Juravle48c2b032014-12-09 18:11:36 +00002542 VLOG(compiler) << "Did not compile "
2543 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
2544 << " because of unhandled instruction "
2545 << instruction.Name();
2546 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnhandledInstruction);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002547 return false;
2548 }
2549 return true;
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00002550} // NOLINT(readability/fn_size)
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002551
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002552HLocal* HGraphBuilder::GetLocalAt(int register_index) const {
2553 return locals_.Get(register_index);
2554}
2555
2556void HGraphBuilder::UpdateLocal(int register_index, HInstruction* instruction) const {
2557 HLocal* local = GetLocalAt(register_index);
2558 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction));
2559}
2560
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002561HInstruction* HGraphBuilder::LoadLocal(int register_index, Primitive::Type type) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002562 HLocal* local = GetLocalAt(register_index);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002563 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type));
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002564 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002565}
2566
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002567} // namespace art