blob: fe52c44306f186e4b2c5595c48d59af7acc39333 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Nicolas Geoffraye5038322014-07-04 09:41:32 +010017#include "builder.h"
18
Mathieu Chartierc7853442015-03-27 14:35:38 -070019#include "art_field-inl.h"
Andreas Gamped881df52014-11-24 23:28:39 -080020#include "base/logging.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010021#include "class_linker.h"
Jeff Hao848f70a2014-01-15 13:49:50 -080022#include "dex/verified_method.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000023#include "dex_file-inl.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000024#include "dex_instruction-inl.h"
Roland Levillain4c0eb422015-04-24 16:43:49 +010025#include "dex/verified_method.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010026#include "driver/compiler_driver-inl.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000027#include "driver/compiler_options.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010028#include "mirror/class_loader.h"
29#include "mirror/dex_cache.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000030#include "nodes.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000031#include "primitive.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010032#include "scoped_thread_state_change.h"
33#include "thread.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000034
35namespace art {
36
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010037/**
38 * Helper class to add HTemporary instructions. This class is used when
39 * converting a DEX instruction to multiple HInstruction, and where those
40 * instructions do not die at the following instruction, but instead spans
41 * multiple instructions.
42 */
43class Temporaries : public ValueObject {
44 public:
Calin Juravlef97f9fb2014-11-11 15:38:19 +000045 explicit Temporaries(HGraph* graph) : graph_(graph), index_(0) {}
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010046
47 void Add(HInstruction* instruction) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +000048 HInstruction* temp = new (graph_->GetArena()) HTemporary(index_);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010049 instruction->GetBlock()->AddInstruction(temp);
Calin Juravlef97f9fb2014-11-11 15:38:19 +000050
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010051 DCHECK(temp->GetPrevious() == instruction);
Calin Juravlef97f9fb2014-11-11 15:38:19 +000052
53 size_t offset;
54 if (instruction->GetType() == Primitive::kPrimLong
55 || instruction->GetType() == Primitive::kPrimDouble) {
56 offset = 2;
57 } else {
58 offset = 1;
59 }
60 index_ += offset;
61
62 graph_->UpdateTemporariesVRegSlots(index_);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010063 }
64
65 private:
66 HGraph* const graph_;
67
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010068 // Current index in the temporary stack, updated by `Add`.
69 size_t index_;
70};
71
Andreas Gamped881df52014-11-24 23:28:39 -080072class SwitchTable : public ValueObject {
73 public:
74 SwitchTable(const Instruction& instruction, uint32_t dex_pc, bool sparse)
75 : instruction_(instruction), dex_pc_(dex_pc), sparse_(sparse) {
76 int32_t table_offset = instruction.VRegB_31t();
77 const uint16_t* table = reinterpret_cast<const uint16_t*>(&instruction) + table_offset;
78 if (sparse) {
79 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
80 } else {
81 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
82 }
83 num_entries_ = table[1];
84 values_ = reinterpret_cast<const int32_t*>(&table[2]);
85 }
86
87 uint16_t GetNumEntries() const {
88 return num_entries_;
89 }
90
Andreas Gampee4d4d322014-12-04 09:09:57 -080091 void CheckIndex(size_t index) const {
92 if (sparse_) {
93 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
94 DCHECK_LT(index, 2 * static_cast<size_t>(num_entries_));
95 } else {
96 // In a packed table, we have the starting key and num_entries_ values.
97 DCHECK_LT(index, 1 + static_cast<size_t>(num_entries_));
98 }
99 }
100
Andreas Gamped881df52014-11-24 23:28:39 -0800101 int32_t GetEntryAt(size_t index) const {
Andreas Gampee4d4d322014-12-04 09:09:57 -0800102 CheckIndex(index);
Andreas Gamped881df52014-11-24 23:28:39 -0800103 return values_[index];
104 }
105
106 uint32_t GetDexPcForIndex(size_t index) const {
Andreas Gampee4d4d322014-12-04 09:09:57 -0800107 CheckIndex(index);
Andreas Gamped881df52014-11-24 23:28:39 -0800108 return dex_pc_ +
109 (reinterpret_cast<const int16_t*>(values_ + index) -
110 reinterpret_cast<const int16_t*>(&instruction_));
111 }
112
Andreas Gampee4d4d322014-12-04 09:09:57 -0800113 // Index of the first value in the table.
114 size_t GetFirstValueIndex() const {
115 if (sparse_) {
116 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
117 return num_entries_;
118 } else {
119 // In a packed table, we have the starting key and num_entries_ values.
120 return 1;
121 }
122 }
123
Andreas Gamped881df52014-11-24 23:28:39 -0800124 private:
125 const Instruction& instruction_;
126 const uint32_t dex_pc_;
127
128 // Whether this is a sparse-switch table (or a packed-switch one).
129 const bool sparse_;
130
131 // This can't be const as it needs to be computed off of the given instruction, and complicated
132 // expressions in the initializer list seemed very ugly.
133 uint16_t num_entries_;
134
135 const int32_t* values_;
136
137 DISALLOW_COPY_AND_ASSIGN(SwitchTable);
138};
139
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100140void HGraphBuilder::InitializeLocals(uint16_t count) {
141 graph_->SetNumberOfVRegs(count);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000142 locals_.SetSize(count);
143 for (int i = 0; i < count; i++) {
144 HLocal* local = new (arena_) HLocal(i);
145 entry_block_->AddInstruction(local);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000146 locals_.Put(i, local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000147 }
148}
149
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000150void HGraphBuilder::InitializeParameters(uint16_t number_of_parameters) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100151 // dex_compilation_unit_ is null only when unit testing.
152 if (dex_compilation_unit_ == nullptr) {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000153 return;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100154 }
155
156 graph_->SetNumberOfInVRegs(number_of_parameters);
157 const char* shorty = dex_compilation_unit_->GetShorty();
158 int locals_index = locals_.Size() - number_of_parameters;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100159 int parameter_index = 0;
160
161 if (!dex_compilation_unit_->IsStatic()) {
162 // Add the implicit 'this' argument, not expressed in the signature.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100163 HParameterValue* parameter =
Calin Juravle10e244f2015-01-26 18:54:32 +0000164 new (arena_) HParameterValue(parameter_index++, Primitive::kPrimNot, true);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100165 entry_block_->AddInstruction(parameter);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100166 HLocal* local = GetLocalAt(locals_index++);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100167 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100168 number_of_parameters--;
169 }
170
171 uint32_t pos = 1;
172 for (int i = 0; i < number_of_parameters; i++) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100173 HParameterValue* parameter =
174 new (arena_) HParameterValue(parameter_index++, Primitive::GetType(shorty[pos++]));
175 entry_block_->AddInstruction(parameter);
176 HLocal* local = GetLocalAt(locals_index++);
177 // Store the parameter value in the local that the dex code will use
178 // to reference that parameter.
179 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter));
180 bool is_wide = (parameter->GetType() == Primitive::kPrimLong)
181 || (parameter->GetType() == Primitive::kPrimDouble);
182 if (is_wide) {
183 i++;
184 locals_index++;
185 parameter_index++;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100186 }
187 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100188}
189
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100190template<typename T>
Calin Juravle225ff812014-11-13 16:46:39 +0000191void HGraphBuilder::If_22t(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000192 int32_t target_offset = instruction.GetTargetOffset();
David Brazdil852eaff2015-02-02 15:23:05 +0000193 HBasicBlock* branch_target = FindBlockStartingAt(dex_pc + target_offset);
194 HBasicBlock* fallthrough_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
195 DCHECK(branch_target != nullptr);
196 DCHECK(fallthrough_target != nullptr);
197 PotentiallyAddSuspendCheck(branch_target, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100198 HInstruction* first = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
199 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Dave Allison20dfc792014-06-16 20:44:29 -0700200 T* comparison = new (arena_) T(first, second);
201 current_block_->AddInstruction(comparison);
202 HInstruction* ifinst = new (arena_) HIf(comparison);
203 current_block_->AddInstruction(ifinst);
David Brazdil852eaff2015-02-02 15:23:05 +0000204 current_block_->AddSuccessor(branch_target);
205 current_block_->AddSuccessor(fallthrough_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700206 current_block_ = nullptr;
207}
208
209template<typename T>
Calin Juravle225ff812014-11-13 16:46:39 +0000210void HGraphBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000211 int32_t target_offset = instruction.GetTargetOffset();
David Brazdil852eaff2015-02-02 15:23:05 +0000212 HBasicBlock* branch_target = FindBlockStartingAt(dex_pc + target_offset);
213 HBasicBlock* fallthrough_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
214 DCHECK(branch_target != nullptr);
215 DCHECK(fallthrough_target != nullptr);
216 PotentiallyAddSuspendCheck(branch_target, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700217 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000218 T* comparison = new (arena_) T(value, graph_->GetIntConstant(0));
Dave Allison20dfc792014-06-16 20:44:29 -0700219 current_block_->AddInstruction(comparison);
220 HInstruction* ifinst = new (arena_) HIf(comparison);
221 current_block_->AddInstruction(ifinst);
David Brazdil852eaff2015-02-02 15:23:05 +0000222 current_block_->AddSuccessor(branch_target);
223 current_block_->AddSuccessor(fallthrough_target);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100224 current_block_ = nullptr;
225}
226
Calin Juravle48c2b032014-12-09 18:11:36 +0000227void HGraphBuilder::MaybeRecordStat(MethodCompilationStat compilation_stat) {
228 if (compilation_stats_ != nullptr) {
229 compilation_stats_->RecordStat(compilation_stat);
230 }
231}
232
David Brazdil1b498722015-03-31 11:37:18 +0100233bool HGraphBuilder::SkipCompilation(const DexFile::CodeItem& code_item,
Calin Juravle48c2b032014-12-09 18:11:36 +0000234 size_t number_of_branches) {
235 const CompilerOptions& compiler_options = compiler_driver_->GetCompilerOptions();
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000236 CompilerOptions::CompilerFilter compiler_filter = compiler_options.GetCompilerFilter();
237 if (compiler_filter == CompilerOptions::kEverything) {
238 return false;
239 }
240
David Brazdil1b498722015-03-31 11:37:18 +0100241 if (compiler_options.IsHugeMethod(code_item.insns_size_in_code_units_)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000242 VLOG(compiler) << "Skip compilation of huge method "
243 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
David Brazdil1b498722015-03-31 11:37:18 +0100244 << ": " << code_item.insns_size_in_code_units_ << " code units";
Calin Juravle48c2b032014-12-09 18:11:36 +0000245 MaybeRecordStat(MethodCompilationStat::kNotCompiledHugeMethod);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000246 return true;
247 }
248
249 // If it's large and contains no branches, it's likely to be machine generated initialization.
David Brazdil1b498722015-03-31 11:37:18 +0100250 if (compiler_options.IsLargeMethod(code_item.insns_size_in_code_units_)
251 && (number_of_branches == 0)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000252 VLOG(compiler) << "Skip compilation of large method with no branch "
253 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
David Brazdil1b498722015-03-31 11:37:18 +0100254 << ": " << code_item.insns_size_in_code_units_ << " code units";
Calin Juravle48c2b032014-12-09 18:11:36 +0000255 MaybeRecordStat(MethodCompilationStat::kNotCompiledLargeMethodNoBranches);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000256 return true;
257 }
258
259 return false;
260}
261
David Brazdilbff75032015-07-08 17:26:51 +0000262static const DexFile::TryItem* GetTryItem(HBasicBlock* block,
263 const DexFile::CodeItem& code_item,
264 const ArenaBitVector& can_block_throw) {
265 DCHECK(!block->IsSingleTryBoundary());
266
267 // Block does not contain throwing instructions. Even if it is covered by
268 // a TryItem, we will consider it not in a try block.
269 if (!can_block_throw.IsBitSet(block->GetBlockId())) {
270 return nullptr;
271 }
272
273 // Instructions in the block may throw. Find a TryItem covering this block.
274 int32_t try_item_idx = DexFile::FindTryItem(code_item, block->GetDexPc());
David Brazdil6cd788f2015-07-08 16:44:00 +0100275 return (try_item_idx == -1) ? nullptr : DexFile::GetTryItems(code_item, try_item_idx);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000276}
277
278void HGraphBuilder::CreateBlocksForTryCatch(const DexFile::CodeItem& code_item) {
279 if (code_item.tries_size_ == 0) {
280 return;
281 }
282
283 // Create branch targets at the start/end of the TryItem range. These are
284 // places where the program might fall through into/out of the a block and
285 // where TryBoundary instructions will be inserted later. Other edges which
286 // enter/exit the try blocks are a result of branches/switches.
287 for (size_t idx = 0; idx < code_item.tries_size_; ++idx) {
288 const DexFile::TryItem* try_item = DexFile::GetTryItems(code_item, idx);
289 uint32_t dex_pc_start = try_item->start_addr_;
290 uint32_t dex_pc_end = dex_pc_start + try_item->insn_count_;
291 FindOrCreateBlockStartingAt(dex_pc_start);
292 if (dex_pc_end < code_item.insns_size_in_code_units_) {
293 // TODO: Do not create block if the last instruction cannot fall through.
294 FindOrCreateBlockStartingAt(dex_pc_end);
295 } else {
296 // The TryItem spans until the very end of the CodeItem (or beyond if
297 // invalid) and therefore cannot have any code afterwards.
298 }
299 }
300
301 // Create branch targets for exception handlers.
302 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(code_item, 0);
303 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
304 for (uint32_t idx = 0; idx < handlers_size; ++idx) {
305 CatchHandlerIterator iterator(handlers_ptr);
306 for (; iterator.HasNext(); iterator.Next()) {
307 uint32_t address = iterator.GetHandlerAddress();
308 HBasicBlock* block = FindOrCreateBlockStartingAt(address);
309 block->SetIsCatchBlock();
310 }
311 handlers_ptr = iterator.EndDataPointer();
312 }
313}
314
David Brazdil56e1acc2015-06-30 15:41:36 +0100315void HGraphBuilder::SplitTryBoundaryEdge(HBasicBlock* predecessor,
316 HBasicBlock* successor,
317 HTryBoundary::BoundaryKind kind,
318 const DexFile::CodeItem& code_item,
319 const DexFile::TryItem& try_item) {
320 // Split the edge with a single TryBoundary instruction.
321 HTryBoundary* try_boundary = new (arena_) HTryBoundary(kind);
322 HBasicBlock* try_entry_block = graph_->SplitEdge(predecessor, successor);
323 try_entry_block->AddInstruction(try_boundary);
324
325 // Link the TryBoundary to the handlers of `try_item`.
326 for (CatchHandlerIterator it(code_item, try_item); it.HasNext(); it.Next()) {
327 try_boundary->AddExceptionHandler(FindBlockStartingAt(it.GetHandlerAddress()));
328 }
329}
330
David Brazdilfc6a86a2015-06-26 10:33:45 +0000331void HGraphBuilder::InsertTryBoundaryBlocks(const DexFile::CodeItem& code_item) {
332 if (code_item.tries_size_ == 0) {
333 return;
334 }
335
David Brazdil72783ff2015-07-09 14:36:05 +0100336 // Bit vector stores information on which blocks contain throwing instructions.
337 // Must be expandable because catch blocks may be split into two.
338 ArenaBitVector can_block_throw(arena_, graph_->GetBlocks().Size(), /* expandable */ true);
David Brazdilbff75032015-07-08 17:26:51 +0000339
340 // Scan blocks and mark those which contain throwing instructions.
David Brazdil72783ff2015-07-09 14:36:05 +0100341 for (size_t block_id = 0, e = graph_->GetBlocks().Size(); block_id < e; ++block_id) {
David Brazdilbff75032015-07-08 17:26:51 +0000342 HBasicBlock* block = graph_->GetBlocks().Get(block_id);
David Brazdil72783ff2015-07-09 14:36:05 +0100343 bool can_throw = false;
David Brazdilbff75032015-07-08 17:26:51 +0000344 for (HInstructionIterator insn(block->GetInstructions()); !insn.Done(); insn.Advance()) {
345 if (insn.Current()->CanThrow()) {
David Brazdil72783ff2015-07-09 14:36:05 +0100346 can_throw = true;
David Brazdilbff75032015-07-08 17:26:51 +0000347 break;
348 }
349 }
David Brazdil72783ff2015-07-09 14:36:05 +0100350
351 if (can_throw) {
352 if (block->IsCatchBlock()) {
353 // Catch blocks are always considered an entry point into the TryItem in
354 // order to avoid splitting exceptional edges. We split the block after
355 // the move-exception (if present) and mark the first part non-throwing.
356 // Later on, a TryBoundary will be inserted between the two blocks.
357 HInstruction* first_insn = block->GetFirstInstruction();
358 if (first_insn->IsLoadException()) {
359 // Catch block starts with a LoadException. Split the block after the
360 // StoreLocal that must come after the load.
361 DCHECK(first_insn->GetNext()->IsStoreLocal());
362 block = block->SplitBefore(first_insn->GetNext()->GetNext());
363 } else {
364 // Catch block does not load the exception. Split at the beginning to
365 // create an empty catch block.
366 block = block->SplitBefore(first_insn);
367 }
368 }
369 can_block_throw.SetBit(block->GetBlockId());
370 }
David Brazdilbff75032015-07-08 17:26:51 +0000371 }
372
David Brazdil281a6322015-07-03 10:34:57 +0100373 // Iterate over all blocks, find those covered by some TryItem and:
374 // (a) split edges which enter/exit the try range,
375 // (b) create TryBoundary instructions in the new blocks,
376 // (c) link the new blocks to corresponding exception handlers.
377 // We cannot iterate only over blocks in `branch_targets_` because switch-case
378 // blocks share the same dex_pc.
David Brazdil72783ff2015-07-09 14:36:05 +0100379 for (size_t block_id = 0, e = graph_->GetBlocks().Size(); block_id < e; ++block_id) {
David Brazdil281a6322015-07-03 10:34:57 +0100380 HBasicBlock* try_block = graph_->GetBlocks().Get(block_id);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000381
David Brazdil281a6322015-07-03 10:34:57 +0100382 // TryBoundary blocks are added at the end of the list and not iterated over.
383 DCHECK(!try_block->IsSingleTryBoundary());
David Brazdilfc6a86a2015-06-26 10:33:45 +0000384
David Brazdil281a6322015-07-03 10:34:57 +0100385 // Find the TryItem for this block.
David Brazdilbff75032015-07-08 17:26:51 +0000386 const DexFile::TryItem* try_item = GetTryItem(try_block, code_item, can_block_throw);
387 if (try_item == nullptr) {
David Brazdil281a6322015-07-03 10:34:57 +0100388 continue;
389 }
David Brazdil281a6322015-07-03 10:34:57 +0100390
David Brazdil72783ff2015-07-09 14:36:05 +0100391 // Catch blocks were split earlier and cannot throw.
392 DCHECK(!try_block->IsCatchBlock());
393
394 // Find predecessors which are not covered by the same TryItem range. Such
395 // edges enter the try block and will have a TryBoundary inserted.
396 for (size_t i = 0; i < try_block->GetPredecessors().Size(); ++i) {
397 HBasicBlock* predecessor = try_block->GetPredecessors().Get(i);
398 if (predecessor->IsSingleTryBoundary()) {
399 // The edge was already split because of an exit from a neighbouring
400 // TryItem. We split it again and insert an entry point.
401 if (kIsDebugBuild) {
402 HTryBoundary* last_insn = predecessor->GetLastInstruction()->AsTryBoundary();
403 const DexFile::TryItem* predecessor_try_item =
404 GetTryItem(predecessor->GetSinglePredecessor(), code_item, can_block_throw);
405 DCHECK(!last_insn->IsEntry());
406 DCHECK_EQ(last_insn->GetNormalFlowSuccessor(), try_block);
407 DCHECK(try_block->IsFirstIndexOfPredecessor(predecessor, i));
408 DCHECK_NE(try_item, predecessor_try_item);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000409 }
David Brazdil72783ff2015-07-09 14:36:05 +0100410 } else if (GetTryItem(predecessor, code_item, can_block_throw) != try_item) {
411 // This is an entry point into the TryItem and the edge has not been
412 // split yet. That means that `predecessor` is not in a TryItem, or
413 // it is in a different TryItem and we happened to iterate over this
414 // block first. We split the edge and insert an entry point.
415 } else {
416 // Not an edge on the boundary of the try block.
417 continue;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000418 }
David Brazdil72783ff2015-07-09 14:36:05 +0100419 SplitTryBoundaryEdge(predecessor, try_block, HTryBoundary::kEntry, code_item, *try_item);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000420 }
David Brazdil281a6322015-07-03 10:34:57 +0100421
422 // Find successors which are not covered by the same TryItem range. Such
423 // edges exit the try block and will have a TryBoundary inserted.
424 for (size_t i = 0; i < try_block->GetSuccessors().Size(); ++i) {
425 HBasicBlock* successor = try_block->GetSuccessors().Get(i);
426 if (successor->IsCatchBlock()) {
427 // A catch block is always considered an entry point into its TryItem.
428 // We therefore assume this is an exit point, regardless of whether
429 // the catch block is in a different TryItem or not.
430 } else if (successor->IsSingleTryBoundary()) {
431 // The edge was already split because of an entry into a neighbouring
432 // TryItem. We split it again and insert an exit.
433 if (kIsDebugBuild) {
434 HTryBoundary* last_insn = successor->GetLastInstruction()->AsTryBoundary();
David Brazdilbff75032015-07-08 17:26:51 +0000435 const DexFile::TryItem* successor_try_item =
436 GetTryItem(last_insn->GetNormalFlowSuccessor(), code_item, can_block_throw);
David Brazdil281a6322015-07-03 10:34:57 +0100437 DCHECK_EQ(try_block, successor->GetSinglePredecessor());
438 DCHECK(last_insn->IsEntry());
David Brazdilbff75032015-07-08 17:26:51 +0000439 DCHECK_NE(try_item, successor_try_item);
David Brazdil281a6322015-07-03 10:34:57 +0100440 }
David Brazdilbff75032015-07-08 17:26:51 +0000441 } else if (GetTryItem(successor, code_item, can_block_throw) != try_item) {
David Brazdil281a6322015-07-03 10:34:57 +0100442 // This is an exit out of the TryItem and the edge has not been split
443 // yet. That means that either `successor` is not in a TryItem, or it
444 // is in a different TryItem and we happened to iterate over this
445 // block first. We split the edge and insert an exit.
446 HInstruction* last_instruction = try_block->GetLastInstruction();
447 if (last_instruction->IsReturn() || last_instruction->IsReturnVoid()) {
448 DCHECK_EQ(successor, exit_block_);
449 // Control flow exits the try block with a Return(Void). Because
450 // splitting the edge would invalidate the invariant that Return
451 // always jumps to Exit, we move the Return outside the try block.
452 successor = try_block->SplitBefore(last_instruction);
453 }
454 } else {
455 // Not an edge on the boundary of the try block.
456 continue;
457 }
David Brazdilbff75032015-07-08 17:26:51 +0000458 SplitTryBoundaryEdge(try_block, successor, HTryBoundary::kExit, code_item, *try_item);
David Brazdil281a6322015-07-03 10:34:57 +0100459 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000460 }
461}
462
David Brazdil5e8b1372015-01-23 14:39:08 +0000463bool HGraphBuilder::BuildGraph(const DexFile::CodeItem& code_item) {
464 DCHECK(graph_->GetBlocks().IsEmpty());
465
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000466 const uint16_t* code_ptr = code_item.insns_;
467 const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100468 code_start_ = code_ptr;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000469
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000470 // Setup the graph with the entry block and exit block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100471 entry_block_ = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000472 graph_->AddBlock(entry_block_);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100473 exit_block_ = new (arena_) HBasicBlock(graph_, kNoDexPc);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000474 graph_->SetEntryBlock(entry_block_);
475 graph_->SetExitBlock(exit_block_);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000476
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000477 InitializeLocals(code_item.registers_size_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000478 graph_->SetMaximumNumberOfOutVRegs(code_item.outs_size_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000479
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000480 // Compute the number of dex instructions, blocks, and branches. We will
481 // check these values against limits given to the compiler.
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000482 size_t number_of_branches = 0;
483
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000484 // To avoid splitting blocks, we compute ahead of time the instructions that
485 // start a new block, and create these blocks.
Calin Juravle702d2602015-04-30 19:28:21 +0100486 if (!ComputeBranchTargets(code_ptr, code_end, &number_of_branches)) {
487 MaybeRecordStat(MethodCompilationStat::kNotCompiledBranchOutsideMethodCode);
488 return false;
489 }
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000490
491 // Note that the compiler driver is null when unit testing.
David Brazdil1b498722015-03-31 11:37:18 +0100492 if ((compiler_driver_ != nullptr) && SkipCompilation(code_item, number_of_branches)) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000493 return false;
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000494 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000495
David Brazdilfc6a86a2015-06-26 10:33:45 +0000496 CreateBlocksForTryCatch(code_item);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000497
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000498 InitializeParameters(code_item.ins_size_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100499
Calin Juravle225ff812014-11-13 16:46:39 +0000500 size_t dex_pc = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000501 while (code_ptr < code_end) {
Calin Juravle225ff812014-11-13 16:46:39 +0000502 // Update the current block if dex_pc starts a new block.
503 MaybeUpdateCurrentBlock(dex_pc);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000504 const Instruction& instruction = *Instruction::At(code_ptr);
Calin Juravle48c2b032014-12-09 18:11:36 +0000505 if (!AnalyzeDexInstruction(instruction, dex_pc)) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000506 return false;
Calin Juravle48c2b032014-12-09 18:11:36 +0000507 }
Calin Juravle225ff812014-11-13 16:46:39 +0000508 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000509 code_ptr += instruction.SizeInCodeUnits();
510 }
511
David Brazdilfc6a86a2015-06-26 10:33:45 +0000512 // Add Exit to the exit block.
David Brazdil3e187382015-06-26 09:59:52 +0000513 exit_block_->AddInstruction(new (arena_) HExit());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000514 // Add the suspend check to the entry block.
515 entry_block_->AddInstruction(new (arena_) HSuspendCheck(0));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000516 entry_block_->AddInstruction(new (arena_) HGoto());
David Brazdilbff75032015-07-08 17:26:51 +0000517 // Add the exit block at the end.
518 graph_->AddBlock(exit_block_);
David Brazdil5e8b1372015-01-23 14:39:08 +0000519
David Brazdilfc6a86a2015-06-26 10:33:45 +0000520 // Iterate over blocks covered by TryItems and insert TryBoundaries at entry
521 // and exit points. This requires all control-flow instructions and
522 // non-exceptional edges to have been created.
523 InsertTryBoundaryBlocks(code_item);
524
David Brazdil5e8b1372015-01-23 14:39:08 +0000525 return true;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000526}
527
David Brazdilfc6a86a2015-06-26 10:33:45 +0000528void HGraphBuilder::MaybeUpdateCurrentBlock(size_t dex_pc) {
529 HBasicBlock* block = FindBlockStartingAt(dex_pc);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000530 if (block == nullptr) {
531 return;
532 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000533
534 if (current_block_ != nullptr) {
535 // Branching instructions clear current_block, so we know
536 // the last instruction of the current block is not a branching
537 // instruction. We add an unconditional goto to the found block.
538 current_block_->AddInstruction(new (arena_) HGoto());
539 current_block_->AddSuccessor(block);
540 }
541 graph_->AddBlock(block);
542 current_block_ = block;
543}
544
Calin Juravle702d2602015-04-30 19:28:21 +0100545bool HGraphBuilder::ComputeBranchTargets(const uint16_t* code_ptr,
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000546 const uint16_t* code_end,
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000547 size_t* number_of_branches) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000548 branch_targets_.SetSize(code_end - code_ptr);
549
550 // Create the first block for the dex instructions, single successor of the entry block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100551 HBasicBlock* block = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000552 branch_targets_.Put(0, block);
553 entry_block_->AddSuccessor(block);
554
555 // Iterate over all instructions and find branching instructions. Create blocks for
556 // the locations these instructions branch to.
Andreas Gamped881df52014-11-24 23:28:39 -0800557 uint32_t dex_pc = 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000558 while (code_ptr < code_end) {
559 const Instruction& instruction = *Instruction::At(code_ptr);
560 if (instruction.IsBranch()) {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000561 (*number_of_branches)++;
Calin Juravle225ff812014-11-13 16:46:39 +0000562 int32_t target = instruction.GetTargetOffset() + dex_pc;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000563 // Create a block for the target instruction.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000564 FindOrCreateBlockStartingAt(target);
565
Calin Juravle225ff812014-11-13 16:46:39 +0000566 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000567 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle702d2602015-04-30 19:28:21 +0100568
David Brazdilfe659462015-06-24 14:23:56 +0100569 if (instruction.CanFlowThrough()) {
570 if (code_ptr >= code_end) {
Calin Juravle702d2602015-04-30 19:28:21 +0100571 // In the normal case we should never hit this but someone can artificially forge a dex
572 // file to fall-through out the method code. In this case we bail out compilation.
573 return false;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000574 } else {
575 FindOrCreateBlockStartingAt(dex_pc);
Calin Juravle702d2602015-04-30 19:28:21 +0100576 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000577 }
Andreas Gampee4d4d322014-12-04 09:09:57 -0800578 } else if (instruction.IsSwitch()) {
579 SwitchTable table(instruction, dex_pc, instruction.Opcode() == Instruction::SPARSE_SWITCH);
Andreas Gamped881df52014-11-24 23:28:39 -0800580
581 uint16_t num_entries = table.GetNumEntries();
582
Andreas Gampee4d4d322014-12-04 09:09:57 -0800583 // In a packed-switch, the entry at index 0 is the starting key. In a sparse-switch, the
584 // entry at index 0 is the first key, and values are after *all* keys.
585 size_t offset = table.GetFirstValueIndex();
586
587 // Use a larger loop counter type to avoid overflow issues.
588 for (size_t i = 0; i < num_entries; ++i) {
Andreas Gamped881df52014-11-24 23:28:39 -0800589 // The target of the case.
Andreas Gampee4d4d322014-12-04 09:09:57 -0800590 uint32_t target = dex_pc + table.GetEntryAt(i + offset);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000591 FindOrCreateBlockStartingAt(target);
Andreas Gamped881df52014-11-24 23:28:39 -0800592
David Brazdil281a6322015-07-03 10:34:57 +0100593 // Create a block for the switch-case logic. The block gets the dex_pc
594 // of the SWITCH instruction because it is part of its semantics.
595 block = new (arena_) HBasicBlock(graph_, dex_pc);
596 branch_targets_.Put(table.GetDexPcForIndex(i), block);
Andreas Gamped881df52014-11-24 23:28:39 -0800597 }
598
599 // Fall-through. Add a block if there is more code afterwards.
600 dex_pc += instruction.SizeInCodeUnits();
601 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle702d2602015-04-30 19:28:21 +0100602 if (code_ptr >= code_end) {
603 // In the normal case we should never hit this but someone can artificially forge a dex
604 // file to fall-through out the method code. In this case we bail out compilation.
605 // (A switch can fall-through so we don't need to check CanFlowThrough().)
606 return false;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000607 } else {
608 FindOrCreateBlockStartingAt(dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -0800609 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000610 } else {
611 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle225ff812014-11-13 16:46:39 +0000612 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000613 }
614 }
Calin Juravle702d2602015-04-30 19:28:21 +0100615 return true;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000616}
617
David Brazdilfc6a86a2015-06-26 10:33:45 +0000618HBasicBlock* HGraphBuilder::FindBlockStartingAt(int32_t dex_pc) const {
619 DCHECK_GE(dex_pc, 0);
620 DCHECK_LT(static_cast<size_t>(dex_pc), branch_targets_.Size());
621 return branch_targets_.Get(dex_pc);
622}
623
624HBasicBlock* HGraphBuilder::FindOrCreateBlockStartingAt(int32_t dex_pc) {
625 HBasicBlock* block = FindBlockStartingAt(dex_pc);
626 if (block == nullptr) {
627 block = new (arena_) HBasicBlock(graph_, dex_pc);
628 branch_targets_.Put(dex_pc, block);
629 }
630 return block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000631}
632
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100633template<typename T>
Roland Levillain88cb1752014-10-20 16:36:47 +0100634void HGraphBuilder::Unop_12x(const Instruction& instruction, Primitive::Type type) {
635 HInstruction* first = LoadLocal(instruction.VRegB(), type);
636 current_block_->AddInstruction(new (arena_) T(type, first));
637 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
638}
639
Roland Levillaindff1f282014-11-05 14:15:05 +0000640void HGraphBuilder::Conversion_12x(const Instruction& instruction,
641 Primitive::Type input_type,
Roland Levillain624279f2014-12-04 11:54:28 +0000642 Primitive::Type result_type,
643 uint32_t dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +0000644 HInstruction* first = LoadLocal(instruction.VRegB(), input_type);
Roland Levillain624279f2014-12-04 11:54:28 +0000645 current_block_->AddInstruction(new (arena_) HTypeConversion(result_type, first, dex_pc));
Roland Levillaindff1f282014-11-05 14:15:05 +0000646 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
647}
648
Roland Levillain88cb1752014-10-20 16:36:47 +0100649template<typename T>
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100650void HGraphBuilder::Binop_23x(const Instruction& instruction, Primitive::Type type) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100651 HInstruction* first = LoadLocal(instruction.VRegB(), type);
652 HInstruction* second = LoadLocal(instruction.VRegC(), type);
653 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100654 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
655}
656
657template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000658void HGraphBuilder::Binop_23x(const Instruction& instruction,
659 Primitive::Type type,
660 uint32_t dex_pc) {
661 HInstruction* first = LoadLocal(instruction.VRegB(), type);
662 HInstruction* second = LoadLocal(instruction.VRegC(), type);
663 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
664 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
665}
666
667template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000668void HGraphBuilder::Binop_23x_shift(const Instruction& instruction,
669 Primitive::Type type) {
670 HInstruction* first = LoadLocal(instruction.VRegB(), type);
671 HInstruction* second = LoadLocal(instruction.VRegC(), Primitive::kPrimInt);
672 current_block_->AddInstruction(new (arena_) T(type, first, second));
673 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
674}
675
Calin Juravleddb7df22014-11-25 20:56:51 +0000676void HGraphBuilder::Binop_23x_cmp(const Instruction& instruction,
677 Primitive::Type type,
Mark Mendellc4701932015-04-10 13:18:51 -0400678 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700679 uint32_t dex_pc) {
Calin Juravleddb7df22014-11-25 20:56:51 +0000680 HInstruction* first = LoadLocal(instruction.VRegB(), type);
681 HInstruction* second = LoadLocal(instruction.VRegC(), type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700682 current_block_->AddInstruction(new (arena_) HCompare(type, first, second, bias, dex_pc));
Calin Juravleddb7df22014-11-25 20:56:51 +0000683 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
684}
685
Calin Juravle9aec02f2014-11-18 23:06:35 +0000686template<typename T>
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100687void HGraphBuilder::Binop_12x(const Instruction& instruction, Primitive::Type type) {
688 HInstruction* first = LoadLocal(instruction.VRegA(), type);
689 HInstruction* second = LoadLocal(instruction.VRegB(), type);
690 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100691 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
692}
693
694template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000695void HGraphBuilder::Binop_12x_shift(const Instruction& instruction, Primitive::Type type) {
696 HInstruction* first = LoadLocal(instruction.VRegA(), type);
697 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
698 current_block_->AddInstruction(new (arena_) T(type, first, second));
699 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
700}
701
702template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000703void HGraphBuilder::Binop_12x(const Instruction& instruction,
704 Primitive::Type type,
705 uint32_t dex_pc) {
706 HInstruction* first = LoadLocal(instruction.VRegA(), type);
707 HInstruction* second = LoadLocal(instruction.VRegB(), type);
708 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
709 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
710}
711
712template<typename T>
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100713void HGraphBuilder::Binop_22s(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100714 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000715 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100716 if (reverse) {
717 std::swap(first, second);
718 }
719 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
720 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
721}
722
723template<typename T>
724void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100725 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000726 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100727 if (reverse) {
728 std::swap(first, second);
729 }
730 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
731 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
732}
733
Calin Juravle0c25d102015-04-20 14:49:09 +0100734static bool RequiresConstructorBarrier(const DexCompilationUnit* cu, const CompilerDriver& driver) {
Calin Juravle27df7582015-04-17 19:12:31 +0100735 Thread* self = Thread::Current();
Calin Juravle0c25d102015-04-20 14:49:09 +0100736 return cu->IsConstructor()
737 && driver.RequiresConstructorBarrier(self, cu->GetDexFile(), cu->GetClassDefIndex());
Calin Juravle27df7582015-04-17 19:12:31 +0100738}
739
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100740void HGraphBuilder::BuildReturn(const Instruction& instruction, Primitive::Type type) {
741 if (type == Primitive::kPrimVoid) {
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100742 if (graph_->ShouldGenerateConstructorBarrier()) {
743 // The compilation unit is null during testing.
744 if (dex_compilation_unit_ != nullptr) {
745 DCHECK(RequiresConstructorBarrier(dex_compilation_unit_, *compiler_driver_))
746 << "Inconsistent use of ShouldGenerateConstructorBarrier. Should not generate a barrier.";
747 }
Calin Juravle27df7582015-04-17 19:12:31 +0100748 current_block_->AddInstruction(new (arena_) HMemoryBarrier(kStoreStore));
749 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100750 current_block_->AddInstruction(new (arena_) HReturnVoid());
751 } else {
752 HInstruction* value = LoadLocal(instruction.VRegA(), type);
753 current_block_->AddInstruction(new (arena_) HReturn(value));
754 }
755 current_block_->AddSuccessor(exit_block_);
756 current_block_ = nullptr;
757}
758
759bool HGraphBuilder::BuildInvoke(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000760 uint32_t dex_pc,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100761 uint32_t method_idx,
762 uint32_t number_of_vreg_arguments,
763 bool is_range,
764 uint32_t* args,
765 uint32_t register_index) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100766 Instruction::Code opcode = instruction.Opcode();
767 InvokeType invoke_type;
768 switch (opcode) {
769 case Instruction::INVOKE_STATIC:
770 case Instruction::INVOKE_STATIC_RANGE:
771 invoke_type = kStatic;
772 break;
773 case Instruction::INVOKE_DIRECT:
774 case Instruction::INVOKE_DIRECT_RANGE:
775 invoke_type = kDirect;
776 break;
777 case Instruction::INVOKE_VIRTUAL:
778 case Instruction::INVOKE_VIRTUAL_RANGE:
779 invoke_type = kVirtual;
780 break;
781 case Instruction::INVOKE_INTERFACE:
782 case Instruction::INVOKE_INTERFACE_RANGE:
783 invoke_type = kInterface;
784 break;
785 case Instruction::INVOKE_SUPER_RANGE:
786 case Instruction::INVOKE_SUPER:
787 invoke_type = kSuper;
788 break;
789 default:
790 LOG(FATAL) << "Unexpected invoke op: " << opcode;
791 return false;
792 }
793
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100794 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
795 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(method_id.proto_idx_);
796 const char* descriptor = dex_file_->StringDataByIdx(proto_id.shorty_idx_);
797 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100798 bool is_instance_call = invoke_type != kStatic;
Nicolas Geoffray2e335252015-06-18 11:11:27 +0100799 // Remove the return type from the 'proto'.
800 size_t number_of_arguments = strlen(descriptor) - 1;
801 if (is_instance_call) {
802 // One extra argument for 'this'.
803 ++number_of_arguments;
804 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100805
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000806 MethodReference target_method(dex_file_, method_idx);
807 uintptr_t direct_code;
808 uintptr_t direct_method;
809 int table_index;
810 InvokeType optimized_invoke_type = invoke_type;
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000811
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000812 if (!compiler_driver_->ComputeInvokeInfo(dex_compilation_unit_, dex_pc, true, true,
813 &optimized_invoke_type, &target_method, &table_index,
814 &direct_code, &direct_method)) {
Nicolas Geoffray2e335252015-06-18 11:11:27 +0100815 VLOG(compiler) << "Did not compile "
816 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
Calin Juravle48c2b032014-12-09 18:11:36 +0000817 << " because a method call could not be resolved";
818 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedMethod);
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000819 return false;
820 }
821 DCHECK(optimized_invoke_type != kSuper);
822
Roland Levillain4c0eb422015-04-24 16:43:49 +0100823 // By default, consider that the called method implicitly requires
824 // an initialization check of its declaring method.
825 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement =
826 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
827 // Potential class initialization check, in the case of a static method call.
828 HClinitCheck* clinit_check = nullptr;
Jeff Hao848f70a2014-01-15 13:49:50 -0800829 // Replace calls to String.<init> with StringFactory.
830 int32_t string_init_offset = 0;
831 bool is_string_init = compiler_driver_->IsStringInit(method_idx, dex_file_, &string_init_offset);
832 if (is_string_init) {
833 return_type = Primitive::kPrimNot;
834 is_instance_call = false;
835 number_of_arguments--;
836 invoke_type = kStatic;
837 optimized_invoke_type = kStatic;
838 }
Roland Levillain4c0eb422015-04-24 16:43:49 +0100839
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000840 HInvoke* invoke = nullptr;
Roland Levillain4c0eb422015-04-24 16:43:49 +0100841
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000842 if (optimized_invoke_type == kVirtual) {
843 invoke = new (arena_) HInvokeVirtual(
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800844 arena_, number_of_arguments, return_type, dex_pc, method_idx, table_index);
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000845 } else if (optimized_invoke_type == kInterface) {
846 invoke = new (arena_) HInvokeInterface(
847 arena_, number_of_arguments, return_type, dex_pc, method_idx, table_index);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100848 } else {
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000849 DCHECK(optimized_invoke_type == kDirect || optimized_invoke_type == kStatic);
850 // Sharpening to kDirect only works if we compile PIC.
851 DCHECK((optimized_invoke_type == invoke_type) || (optimized_invoke_type != kDirect)
852 || compiler_driver_->GetCompilerOptions().GetCompilePic());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000853 bool is_recursive =
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100854 (target_method.dex_method_index == outer_compilation_unit_->GetDexMethodIndex())
855 && (target_method.dex_file == outer_compilation_unit_->GetDexFile());
Roland Levillain4c0eb422015-04-24 16:43:49 +0100856
Jeff Haocad65422015-06-18 21:16:08 -0700857 if (optimized_invoke_type == kStatic && !is_string_init) {
Roland Levillain4c0eb422015-04-24 16:43:49 +0100858 ScopedObjectAccess soa(Thread::Current());
859 StackHandleScope<4> hs(soa.Self());
860 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
861 dex_compilation_unit_->GetClassLinker()->FindDexCache(
862 *dex_compilation_unit_->GetDexFile())));
863 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
864 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700865 ArtMethod* resolved_method = compiler_driver_->ResolveMethod(
866 soa, dex_cache, class_loader, dex_compilation_unit_, method_idx, optimized_invoke_type);
Roland Levillain4c0eb422015-04-24 16:43:49 +0100867
868 if (resolved_method == nullptr) {
869 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedMethod);
870 return false;
871 }
872
873 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
874 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
875 outer_compilation_unit_->GetClassLinker()->FindDexCache(outer_dex_file)));
Nicolas Geoffrayafd06412015-06-20 22:44:47 +0100876 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Roland Levillain4c0eb422015-04-24 16:43:49 +0100877
878 // The index at which the method's class is stored in the DexCache's type array.
879 uint32_t storage_index = DexFile::kDexNoIndex;
Nicolas Geoffrayafd06412015-06-20 22:44:47 +0100880 bool is_outer_class = (resolved_method->GetDeclaringClass() == outer_class.Get());
881 if (is_outer_class) {
882 storage_index = outer_class->GetDexTypeIndex();
Roland Levillain4c0eb422015-04-24 16:43:49 +0100883 } else if (outer_dex_cache.Get() == dex_cache.Get()) {
884 // Get `storage_index` from IsClassOfStaticMethodAvailableToReferrer.
885 compiler_driver_->IsClassOfStaticMethodAvailableToReferrer(outer_dex_cache.Get(),
Nicolas Geoffrayafd06412015-06-20 22:44:47 +0100886 GetCompilingClass(),
Roland Levillain4c0eb422015-04-24 16:43:49 +0100887 resolved_method,
888 method_idx,
889 &storage_index);
890 }
891
Nicolas Geoffrayb783b402015-06-22 11:06:43 +0100892 if (!outer_class->IsInterface()
893 && outer_class->IsSubClass(resolved_method->GetDeclaringClass())) {
Nicolas Geoffrayafd06412015-06-20 22:44:47 +0100894 // If the outer class is the declaring class or a subclass
Roland Levillain5f02c6c2015-04-24 19:14:22 +0100895 // of the declaring class, no class initialization is needed
896 // before the static method call.
Nicolas Geoffrayafd06412015-06-20 22:44:47 +0100897 // Note that in case of inlining, we do not need to add clinit checks
898 // to calls that satisfy this subclass check with any inlined methods. This
899 // will be detected by the optimization passes.
Roland Levillain4c0eb422015-04-24 16:43:49 +0100900 clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
901 } else if (storage_index != DexFile::kDexNoIndex) {
902 // If the method's class type index is available, check
903 // whether we should add an explicit class initialization
904 // check for its declaring class before the static method call.
905
906 // TODO: find out why this check is needed.
907 bool is_in_dex_cache = compiler_driver_->CanAssumeTypeIsPresentInDexCache(
908 *outer_compilation_unit_->GetDexFile(), storage_index);
909 bool is_initialized =
910 resolved_method->GetDeclaringClass()->IsInitialized() && is_in_dex_cache;
911
912 if (is_initialized) {
913 clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
914 } else {
915 clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +0100916 HLoadClass* load_class = new (arena_) HLoadClass(
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100917 graph_->GetCurrentMethod(),
918 storage_index,
919 *dex_compilation_unit_->GetDexFile(),
Nicolas Geoffrayafd06412015-06-20 22:44:47 +0100920 is_outer_class,
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100921 dex_pc);
Roland Levillain4c0eb422015-04-24 16:43:49 +0100922 current_block_->AddInstruction(load_class);
923 clinit_check = new (arena_) HClinitCheck(load_class, dex_pc);
924 current_block_->AddInstruction(clinit_check);
Roland Levillain4c0eb422015-04-24 16:43:49 +0100925 }
926 }
927 }
928
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100929 invoke = new (arena_) HInvokeStaticOrDirect(arena_,
930 number_of_arguments,
931 return_type,
932 dex_pc,
933 target_method.dex_method_index,
934 is_recursive,
935 string_init_offset,
936 invoke_type,
937 optimized_invoke_type,
938 clinit_check_requirement);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100939 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100940
941 size_t start_index = 0;
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000942 Temporaries temps(graph_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100943 if (is_instance_call) {
944 HInstruction* arg = LoadLocal(is_range ? register_index : args[0], Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000945 HNullCheck* null_check = new (arena_) HNullCheck(arg, dex_pc);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100946 current_block_->AddInstruction(null_check);
947 temps.Add(null_check);
948 invoke->SetArgumentAt(0, null_check);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100949 start_index = 1;
950 }
951
Nicolas Geoffray2e335252015-06-18 11:11:27 +0100952 uint32_t descriptor_index = 1; // Skip the return type.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100953 uint32_t argument_index = start_index;
Jeff Hao848f70a2014-01-15 13:49:50 -0800954 if (is_string_init) {
955 start_index = 1;
956 }
Nicolas Geoffray2e335252015-06-18 11:11:27 +0100957 for (size_t i = start_index;
958 // Make sure we don't go over the expected arguments or over the number of
959 // dex registers given. If the instruction was seen as dead by the verifier,
960 // it hasn't been properly checked.
961 (i < number_of_vreg_arguments) && (argument_index < number_of_arguments);
962 i++, argument_index++) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100963 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100964 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
Nicolas Geoffray2e335252015-06-18 11:11:27 +0100965 if (!is_range
966 && is_wide
967 && ((i + 1 == number_of_vreg_arguments) || (args[i] + 1 != args[i + 1]))) {
968 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
969 // reject any class where this is violated. However, the verifier only does these checks
970 // on non trivially dead instructions, so we just bailout the compilation.
971 VLOG(compiler) << "Did not compile "
972 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
973 << " because of non-sequential dex register pair in wide argument";
974 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
975 return false;
976 }
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100977 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
978 invoke->SetArgumentAt(argument_index, arg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100979 if (is_wide) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100980 i++;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100981 }
982 }
Nicolas Geoffray2e335252015-06-18 11:11:27 +0100983
984 if (argument_index != number_of_arguments) {
985 VLOG(compiler) << "Did not compile "
986 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
987 << " because of wrong number of arguments in invoke instruction";
988 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
989 return false;
990 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100991
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100992 if (invoke->IsInvokeStaticOrDirect()) {
993 invoke->SetArgumentAt(argument_index, graph_->GetCurrentMethod());
994 argument_index++;
995 }
996
Roland Levillain4c0eb422015-04-24 16:43:49 +0100997 if (clinit_check_requirement == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit) {
998 // Add the class initialization check as last input of `invoke`.
999 DCHECK(clinit_check != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01001000 invoke->SetArgumentAt(argument_index, clinit_check);
Roland Levillain4c0eb422015-04-24 16:43:49 +01001001 }
1002
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001003 current_block_->AddInstruction(invoke);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001004 latest_result_ = invoke;
Jeff Hao848f70a2014-01-15 13:49:50 -08001005
1006 // Add move-result for StringFactory method.
1007 if (is_string_init) {
1008 uint32_t orig_this_reg = is_range ? register_index : args[0];
Nicolas Geoffrayaa919202015-06-21 18:57:02 +01001009 UpdateLocal(orig_this_reg, invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001010 const VerifiedMethod* verified_method =
1011 compiler_driver_->GetVerifiedMethod(dex_file_, dex_compilation_unit_->GetDexMethodIndex());
1012 if (verified_method == nullptr) {
1013 LOG(WARNING) << "No verified method for method calling String.<init>: "
1014 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_);
1015 return false;
1016 }
1017 const SafeMap<uint32_t, std::set<uint32_t>>& string_init_map =
1018 verified_method->GetStringInitPcRegMap();
1019 auto map_it = string_init_map.find(dex_pc);
1020 if (map_it != string_init_map.end()) {
1021 std::set<uint32_t> reg_set = map_it->second;
1022 for (auto set_it = reg_set.begin(); set_it != reg_set.end(); ++set_it) {
Nicolas Geoffrayaa919202015-06-21 18:57:02 +01001023 HInstruction* load_local = LoadLocal(orig_this_reg, Primitive::kPrimNot);
1024 UpdateLocal(*set_it, load_local);
Jeff Hao848f70a2014-01-15 13:49:50 -08001025 }
1026 }
Jeff Hao848f70a2014-01-15 13:49:50 -08001027 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001028 return true;
1029}
1030
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001031bool HGraphBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001032 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001033 bool is_put) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001034 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1035 uint32_t obj_reg = instruction.VRegB_22c();
1036 uint16_t field_index = instruction.VRegC_22c();
1037
1038 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierc7853442015-03-27 14:35:38 -07001039 ArtField* resolved_field =
1040 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001041
Mathieu Chartierc7853442015-03-27 14:35:38 -07001042 if (resolved_field == nullptr) {
Calin Juravle48c2b032014-12-09 18:11:36 +00001043 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedField);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001044 return false;
1045 }
Calin Juravle52c48962014-12-16 17:02:57 +00001046
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001047 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001048
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001049 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00001050 current_block_->AddInstruction(new (arena_) HNullCheck(object, dex_pc));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001051 if (is_put) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001052 Temporaries temps(graph_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001053 HInstruction* null_check = current_block_->GetLastInstruction();
1054 // We need one temporary for the null check.
1055 temps.Add(null_check);
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001056 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001057 current_block_->AddInstruction(new (arena_) HInstanceFieldSet(
1058 null_check,
1059 value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001060 field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00001061 resolved_field->GetOffset(),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001062 resolved_field->IsVolatile(),
1063 field_index,
1064 *dex_file_));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001065 } else {
1066 current_block_->AddInstruction(new (arena_) HInstanceFieldGet(
1067 current_block_->GetLastInstruction(),
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +01001068 field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00001069 resolved_field->GetOffset(),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001070 resolved_field->IsVolatile(),
1071 field_index,
1072 *dex_file_));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001073
1074 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1075 }
1076 return true;
1077}
1078
Nicolas Geoffray30451742015-06-19 13:32:41 +01001079static mirror::Class* GetClassFrom(CompilerDriver* driver,
1080 const DexCompilationUnit& compilation_unit) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001081 ScopedObjectAccess soa(Thread::Current());
1082 StackHandleScope<2> hs(soa.Self());
Nicolas Geoffray30451742015-06-19 13:32:41 +01001083 const DexFile& dex_file = *compilation_unit.GetDexFile();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001084 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Nicolas Geoffray30451742015-06-19 13:32:41 +01001085 soa.Decode<mirror::ClassLoader*>(compilation_unit.GetClassLoader())));
1086 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
1087 compilation_unit.GetClassLinker()->FindDexCache(dex_file)));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001088
Nicolas Geoffray30451742015-06-19 13:32:41 +01001089 return driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, &compilation_unit);
1090}
1091
1092mirror::Class* HGraphBuilder::GetOutermostCompilingClass() const {
1093 return GetClassFrom(compiler_driver_, *outer_compilation_unit_);
1094}
1095
1096mirror::Class* HGraphBuilder::GetCompilingClass() const {
1097 return GetClassFrom(compiler_driver_, *dex_compilation_unit_);
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001098}
1099
1100bool HGraphBuilder::IsOutermostCompilingClass(uint16_t type_index) const {
1101 ScopedObjectAccess soa(Thread::Current());
1102 StackHandleScope<4> hs(soa.Self());
1103 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
1104 dex_compilation_unit_->GetClassLinker()->FindDexCache(*dex_compilation_unit_->GetDexFile())));
1105 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1106 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
1107 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
1108 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
Nicolas Geoffrayafd06412015-06-20 22:44:47 +01001109 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001110
Nicolas Geoffrayafd06412015-06-20 22:44:47 +01001111 return outer_class.Get() == cls.Get();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001112}
1113
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001114bool HGraphBuilder::BuildStaticFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001115 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001116 bool is_put) {
1117 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1118 uint16_t field_index = instruction.VRegB_21c();
1119
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001120 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierc7853442015-03-27 14:35:38 -07001121 StackHandleScope<4> hs(soa.Self());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001122 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
1123 dex_compilation_unit_->GetClassLinker()->FindDexCache(*dex_compilation_unit_->GetDexFile())));
1124 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1125 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
Mathieu Chartierc7853442015-03-27 14:35:38 -07001126 ArtField* resolved_field = compiler_driver_->ResolveField(
1127 soa, dex_cache, class_loader, dex_compilation_unit_, field_index, true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001128
Mathieu Chartierc7853442015-03-27 14:35:38 -07001129 if (resolved_field == nullptr) {
Calin Juravle48c2b032014-12-09 18:11:36 +00001130 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedField);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001131 return false;
1132 }
1133
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001134 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
1135 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
1136 outer_compilation_unit_->GetClassLinker()->FindDexCache(outer_dex_file)));
Nicolas Geoffray30451742015-06-19 13:32:41 +01001137 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001138
1139 // The index at which the field's class is stored in the DexCache's type array.
1140 uint32_t storage_index;
Nicolas Geoffray30451742015-06-19 13:32:41 +01001141 bool is_outer_class = (outer_class.Get() == resolved_field->GetDeclaringClass());
1142 if (is_outer_class) {
1143 storage_index = outer_class->GetDexTypeIndex();
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001144 } else if (outer_dex_cache.Get() != dex_cache.Get()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001145 // The compiler driver cannot currently understand multiple dex caches involved. Just bailout.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001146 return false;
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001147 } else {
1148 std::pair<bool, bool> pair = compiler_driver_->IsFastStaticField(
1149 outer_dex_cache.Get(),
Nicolas Geoffray30451742015-06-19 13:32:41 +01001150 GetCompilingClass(),
Mathieu Chartierc7853442015-03-27 14:35:38 -07001151 resolved_field,
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001152 field_index,
1153 &storage_index);
1154 bool can_easily_access = is_put ? pair.second : pair.first;
1155 if (!can_easily_access) {
1156 return false;
1157 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001158 }
1159
1160 // TODO: find out why this check is needed.
1161 bool is_in_dex_cache = compiler_driver_->CanAssumeTypeIsPresentInDexCache(
Nicolas Geoffray6a816cf2015-03-24 16:17:56 +00001162 *outer_compilation_unit_->GetDexFile(), storage_index);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001163 bool is_initialized = resolved_field->GetDeclaringClass()->IsInitialized() && is_in_dex_cache;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001164
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001165 HLoadClass* constant = new (arena_) HLoadClass(graph_->GetCurrentMethod(),
1166 storage_index,
1167 *dex_compilation_unit_->GetDexFile(),
Nicolas Geoffray30451742015-06-19 13:32:41 +01001168 is_outer_class,
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001169 dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001170 current_block_->AddInstruction(constant);
1171
1172 HInstruction* cls = constant;
Nicolas Geoffray30451742015-06-19 13:32:41 +01001173 if (!is_initialized && !is_outer_class) {
Calin Juravle225ff812014-11-13 16:46:39 +00001174 cls = new (arena_) HClinitCheck(constant, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001175 current_block_->AddInstruction(cls);
1176 }
1177
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001178 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001179 if (is_put) {
1180 // We need to keep the class alive before loading the value.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001181 Temporaries temps(graph_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001182 temps.Add(cls);
1183 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1184 DCHECK_EQ(value->GetType(), field_type);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001185 current_block_->AddInstruction(new (arena_) HStaticFieldSet(cls,
1186 value,
1187 field_type,
1188 resolved_field->GetOffset(),
1189 resolved_field->IsVolatile(),
1190 field_index,
1191 *dex_file_));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001192 } else {
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01001193 current_block_->AddInstruction(new (arena_) HStaticFieldGet(cls,
1194 field_type,
1195 resolved_field->GetOffset(),
1196 resolved_field->IsVolatile(),
1197 field_index,
1198 *dex_file_));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001199 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1200 }
1201 return true;
1202}
1203
Calin Juravlebacfec32014-11-14 15:54:36 +00001204void HGraphBuilder::BuildCheckedDivRem(uint16_t out_vreg,
1205 uint16_t first_vreg,
1206 int64_t second_vreg_or_constant,
1207 uint32_t dex_pc,
1208 Primitive::Type type,
1209 bool second_is_constant,
1210 bool isDiv) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001211 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Calin Juravled0d48522014-11-04 16:40:20 +00001212
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001213 HInstruction* first = LoadLocal(first_vreg, type);
1214 HInstruction* second = nullptr;
1215 if (second_is_constant) {
1216 if (type == Primitive::kPrimInt) {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001217 second = graph_->GetIntConstant(second_vreg_or_constant);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001218 } else {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001219 second = graph_->GetLongConstant(second_vreg_or_constant);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001220 }
1221 } else {
1222 second = LoadLocal(second_vreg_or_constant, type);
1223 }
1224
1225 if (!second_is_constant
1226 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0)
1227 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) {
1228 second = new (arena_) HDivZeroCheck(second, dex_pc);
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001229 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +00001230 current_block_->AddInstruction(second);
1231 temps.Add(current_block_->GetLastInstruction());
1232 }
1233
Calin Juravlebacfec32014-11-14 15:54:36 +00001234 if (isDiv) {
1235 current_block_->AddInstruction(new (arena_) HDiv(type, first, second, dex_pc));
1236 } else {
1237 current_block_->AddInstruction(new (arena_) HRem(type, first, second, dex_pc));
1238 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001239 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
Calin Juravled0d48522014-11-04 16:40:20 +00001240}
1241
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001242void HGraphBuilder::BuildArrayAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +00001243 uint32_t dex_pc,
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001244 bool is_put,
1245 Primitive::Type anticipated_type) {
1246 uint8_t source_or_dest_reg = instruction.VRegA_23x();
1247 uint8_t array_reg = instruction.VRegB_23x();
1248 uint8_t index_reg = instruction.VRegC_23x();
1249
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001250 // We need one temporary for the null check, one for the index, and one for the length.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001251 Temporaries temps(graph_);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001252
1253 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00001254 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001255 current_block_->AddInstruction(object);
1256 temps.Add(object);
1257
1258 HInstruction* length = new (arena_) HArrayLength(object);
1259 current_block_->AddInstruction(length);
1260 temps.Add(length);
1261 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt);
Calin Juravle225ff812014-11-13 16:46:39 +00001262 index = new (arena_) HBoundsCheck(index, length, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001263 current_block_->AddInstruction(index);
1264 temps.Add(index);
1265 if (is_put) {
1266 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
1267 // TODO: Insert a type check node if the type is Object.
Nicolas Geoffray39468442014-09-02 15:17:15 +01001268 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001269 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001270 } else {
1271 current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type));
1272 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1273 }
Mark Mendell1152c922015-04-24 17:06:35 -04001274 graph_->SetHasBoundsChecks(true);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001275}
1276
Calin Juravle225ff812014-11-13 16:46:39 +00001277void HGraphBuilder::BuildFilledNewArray(uint32_t dex_pc,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001278 uint32_t type_index,
1279 uint32_t number_of_vreg_arguments,
1280 bool is_range,
1281 uint32_t* args,
1282 uint32_t register_index) {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001283 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments);
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001284 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
1285 ? kQuickAllocArrayWithAccessCheck
1286 : kQuickAllocArray;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01001287 HInstruction* object = new (arena_) HNewArray(length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01001288 graph_->GetCurrentMethod(),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01001289 dex_pc,
1290 type_index,
1291 *dex_compilation_unit_->GetDexFile(),
1292 entrypoint);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001293 current_block_->AddInstruction(object);
1294
1295 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1296 DCHECK_EQ(descriptor[0], '[') << descriptor;
1297 char primitive = descriptor[1];
1298 DCHECK(primitive == 'I'
1299 || primitive == 'L'
1300 || primitive == '[') << descriptor;
1301 bool is_reference_array = (primitive == 'L') || (primitive == '[');
1302 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
1303
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001304 Temporaries temps(graph_);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001305 temps.Add(object);
1306 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
1307 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
David Brazdil8d5b8b22015-03-24 10:51:52 +00001308 HInstruction* index = graph_->GetIntConstant(i);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001309 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001310 new (arena_) HArraySet(object, index, value, type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001311 }
1312 latest_result_ = object;
1313}
1314
1315template <typename T>
1316void HGraphBuilder::BuildFillArrayData(HInstruction* object,
1317 const T* data,
1318 uint32_t element_count,
1319 Primitive::Type anticipated_type,
Calin Juravle225ff812014-11-13 16:46:39 +00001320 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001321 for (uint32_t i = 0; i < element_count; ++i) {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001322 HInstruction* index = graph_->GetIntConstant(i);
1323 HInstruction* value = graph_->GetIntConstant(data[i]);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001324 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001325 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001326 }
1327}
1328
Calin Juravle225ff812014-11-13 16:46:39 +00001329void HGraphBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001330 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +00001331 HInstruction* array = LoadLocal(instruction.VRegA_31t(), Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00001332 HNullCheck* null_check = new (arena_) HNullCheck(array, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001333 current_block_->AddInstruction(null_check);
1334 temps.Add(null_check);
1335
1336 HInstruction* length = new (arena_) HArrayLength(null_check);
1337 current_block_->AddInstruction(length);
1338
Calin Juravle225ff812014-11-13 16:46:39 +00001339 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
Calin Juravled0d48522014-11-04 16:40:20 +00001340 const Instruction::ArrayDataPayload* payload =
1341 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_start_ + payload_offset);
1342 const uint8_t* data = payload->data;
1343 uint32_t element_count = payload->element_count;
1344
1345 // Implementation of this DEX instruction seems to be that the bounds check is
1346 // done before doing any stores.
David Brazdil8d5b8b22015-03-24 10:51:52 +00001347 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1);
Calin Juravle225ff812014-11-13 16:46:39 +00001348 current_block_->AddInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
Calin Juravled0d48522014-11-04 16:40:20 +00001349
1350 switch (payload->element_width) {
1351 case 1:
1352 BuildFillArrayData(null_check,
1353 reinterpret_cast<const int8_t*>(data),
1354 element_count,
1355 Primitive::kPrimByte,
Calin Juravle225ff812014-11-13 16:46:39 +00001356 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001357 break;
1358 case 2:
1359 BuildFillArrayData(null_check,
1360 reinterpret_cast<const int16_t*>(data),
1361 element_count,
1362 Primitive::kPrimShort,
Calin Juravle225ff812014-11-13 16:46:39 +00001363 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001364 break;
1365 case 4:
1366 BuildFillArrayData(null_check,
1367 reinterpret_cast<const int32_t*>(data),
1368 element_count,
1369 Primitive::kPrimInt,
Calin Juravle225ff812014-11-13 16:46:39 +00001370 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001371 break;
1372 case 8:
1373 BuildFillWideArrayData(null_check,
1374 reinterpret_cast<const int64_t*>(data),
1375 element_count,
Calin Juravle225ff812014-11-13 16:46:39 +00001376 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001377 break;
1378 default:
1379 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1380 }
Mark Mendell1152c922015-04-24 17:06:35 -04001381 graph_->SetHasBoundsChecks(true);
Calin Juravled0d48522014-11-04 16:40:20 +00001382}
1383
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001384void HGraphBuilder::BuildFillWideArrayData(HInstruction* object,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +01001385 const int64_t* data,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001386 uint32_t element_count,
Calin Juravle225ff812014-11-13 16:46:39 +00001387 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001388 for (uint32_t i = 0; i < element_count; ++i) {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001389 HInstruction* index = graph_->GetIntConstant(i);
1390 HInstruction* value = graph_->GetLongConstant(data[i]);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001391 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001392 object, index, value, Primitive::kPrimLong, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001393 }
1394}
1395
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001396bool HGraphBuilder::BuildTypeCheck(const Instruction& instruction,
1397 uint8_t destination,
1398 uint8_t reference,
1399 uint16_t type_index,
Calin Juravle225ff812014-11-13 16:46:39 +00001400 uint32_t dex_pc) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001401 bool type_known_final;
1402 bool type_known_abstract;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001403 // `CanAccessTypeWithoutChecks` will tell whether the method being
1404 // built is trying to access its own class, so that the generated
1405 // code can optimize for this case. However, the optimization does not
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001406 // work for inlining, so we use `IsOutermostCompilingClass` instead.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001407 bool dont_use_is_referrers_class;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001408 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
1409 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001410 &type_known_final, &type_known_abstract, &dont_use_is_referrers_class);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001411 if (!can_access) {
Calin Juravle48c2b032014-12-09 18:11:36 +00001412 MaybeRecordStat(MethodCompilationStat::kNotCompiledCantAccesType);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001413 return false;
1414 }
1415 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot);
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001416 HLoadClass* cls = new (arena_) HLoadClass(
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001417 graph_->GetCurrentMethod(),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01001418 type_index,
1419 *dex_compilation_unit_->GetDexFile(),
1420 IsOutermostCompilingClass(type_index),
1421 dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001422 current_block_->AddInstruction(cls);
1423 // The class needs a temporary before being used by the type check.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001424 Temporaries temps(graph_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001425 temps.Add(cls);
1426 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
1427 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001428 new (arena_) HInstanceOf(object, cls, type_known_final, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001429 UpdateLocal(destination, current_block_->GetLastInstruction());
1430 } else {
1431 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
1432 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001433 new (arena_) HCheckCast(object, cls, type_known_final, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001434 }
1435 return true;
1436}
1437
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001438bool HGraphBuilder::NeedsAccessCheck(uint32_t type_index) const {
1439 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
1440 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index);
1441}
1442
Calin Juravle48c2b032014-12-09 18:11:36 +00001443void HGraphBuilder::BuildPackedSwitch(const Instruction& instruction, uint32_t dex_pc) {
David Brazdil2ef645b2015-06-17 18:20:52 +01001444 // Verifier guarantees that the payload for PackedSwitch contains:
1445 // (a) number of entries (may be zero)
1446 // (b) first and lowest switch case value (entry 0, always present)
1447 // (c) list of target pcs (entries 1 <= i <= N)
Andreas Gamped881df52014-11-24 23:28:39 -08001448 SwitchTable table(instruction, dex_pc, false);
1449
1450 // Value to test against.
1451 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
1452
David Brazdil2ef645b2015-06-17 18:20:52 +01001453 // Retrieve number of entries.
Andreas Gampee4d4d322014-12-04 09:09:57 -08001454 uint16_t num_entries = table.GetNumEntries();
David Brazdil2ef645b2015-06-17 18:20:52 +01001455 if (num_entries == 0) {
1456 return;
1457 }
Andreas Gampee4d4d322014-12-04 09:09:57 -08001458
Andreas Gamped881df52014-11-24 23:28:39 -08001459 // Chained cmp-and-branch, starting from starting_key.
1460 int32_t starting_key = table.GetEntryAt(0);
1461
Andreas Gamped881df52014-11-24 23:28:39 -08001462 for (size_t i = 1; i <= num_entries; i++) {
Andreas Gampee4d4d322014-12-04 09:09:57 -08001463 BuildSwitchCaseHelper(instruction, i, i == num_entries, table, value, starting_key + i - 1,
1464 table.GetEntryAt(i), dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08001465 }
Andreas Gamped881df52014-11-24 23:28:39 -08001466}
1467
Calin Juravle48c2b032014-12-09 18:11:36 +00001468void HGraphBuilder::BuildSparseSwitch(const Instruction& instruction, uint32_t dex_pc) {
David Brazdil2ef645b2015-06-17 18:20:52 +01001469 // Verifier guarantees that the payload for SparseSwitch contains:
1470 // (a) number of entries (may be zero)
1471 // (b) sorted key values (entries 0 <= i < N)
1472 // (c) target pcs corresponding to the switch values (entries N <= i < 2*N)
Andreas Gampee4d4d322014-12-04 09:09:57 -08001473 SwitchTable table(instruction, dex_pc, true);
1474
1475 // Value to test against.
1476 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
1477
1478 uint16_t num_entries = table.GetNumEntries();
Andreas Gampee4d4d322014-12-04 09:09:57 -08001479
1480 for (size_t i = 0; i < num_entries; i++) {
1481 BuildSwitchCaseHelper(instruction, i, i == static_cast<size_t>(num_entries) - 1, table, value,
1482 table.GetEntryAt(i), table.GetEntryAt(i + num_entries), dex_pc);
1483 }
Andreas Gampee4d4d322014-12-04 09:09:57 -08001484}
1485
1486void HGraphBuilder::BuildSwitchCaseHelper(const Instruction& instruction, size_t index,
1487 bool is_last_case, const SwitchTable& table,
1488 HInstruction* value, int32_t case_value_int,
1489 int32_t target_offset, uint32_t dex_pc) {
David Brazdil852eaff2015-02-02 15:23:05 +00001490 HBasicBlock* case_target = FindBlockStartingAt(dex_pc + target_offset);
1491 DCHECK(case_target != nullptr);
1492 PotentiallyAddSuspendCheck(case_target, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001493
1494 // The current case's value.
David Brazdil8d5b8b22015-03-24 10:51:52 +00001495 HInstruction* this_case_value = graph_->GetIntConstant(case_value_int);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001496
1497 // Compare value and this_case_value.
1498 HEqual* comparison = new (arena_) HEqual(value, this_case_value);
1499 current_block_->AddInstruction(comparison);
1500 HInstruction* ifinst = new (arena_) HIf(comparison);
1501 current_block_->AddInstruction(ifinst);
1502
1503 // Case hit: use the target offset to determine where to go.
Andreas Gampee4d4d322014-12-04 09:09:57 -08001504 current_block_->AddSuccessor(case_target);
1505
1506 // Case miss: go to the next case (or default fall-through).
1507 // When there is a next case, we use the block stored with the table offset representing this
1508 // case (that is where we registered them in ComputeBranchTargets).
1509 // When there is no next case, we use the following instruction.
1510 // TODO: Find a good way to peel the last iteration to avoid conditional, but still have re-use.
1511 if (!is_last_case) {
1512 HBasicBlock* next_case_target = FindBlockStartingAt(table.GetDexPcForIndex(index));
1513 DCHECK(next_case_target != nullptr);
1514 current_block_->AddSuccessor(next_case_target);
1515
1516 // Need to manually add the block, as there is no dex-pc transition for the cases.
1517 graph_->AddBlock(next_case_target);
1518
1519 current_block_ = next_case_target;
1520 } else {
1521 HBasicBlock* default_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
1522 DCHECK(default_target != nullptr);
1523 current_block_->AddSuccessor(default_target);
1524 current_block_ = nullptr;
1525 }
1526}
1527
David Brazdil852eaff2015-02-02 15:23:05 +00001528void HGraphBuilder::PotentiallyAddSuspendCheck(HBasicBlock* target, uint32_t dex_pc) {
1529 int32_t target_offset = target->GetDexPc() - dex_pc;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001530 if (target_offset <= 0) {
David Brazdil852eaff2015-02-02 15:23:05 +00001531 // DX generates back edges to the first encountered return. We can save
1532 // time of later passes by not adding redundant suspend checks.
David Brazdil2fd6aa52015-02-02 18:58:27 +00001533 HInstruction* last_in_target = target->GetLastInstruction();
1534 if (last_in_target != nullptr &&
1535 (last_in_target->IsReturn() || last_in_target->IsReturnVoid())) {
1536 return;
David Brazdil852eaff2015-02-02 15:23:05 +00001537 }
1538
1539 // Add a suspend check to backward branches which may potentially loop. We
1540 // can remove them after we recognize loops in the graph.
Calin Juravle225ff812014-11-13 16:46:39 +00001541 current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_pc));
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001542 }
1543}
1544
Calin Juravle225ff812014-11-13 16:46:39 +00001545bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001546 if (current_block_ == nullptr) {
1547 return true; // Dead code
1548 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001549
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001550 switch (instruction.Opcode()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001551 case Instruction::CONST_4: {
1552 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001553 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001554 UpdateLocal(register_index, constant);
1555 break;
1556 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001557
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001558 case Instruction::CONST_16: {
1559 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001560 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001561 UpdateLocal(register_index, constant);
1562 break;
1563 }
1564
Dave Allison20dfc792014-06-16 20:44:29 -07001565 case Instruction::CONST: {
1566 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001567 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i());
Dave Allison20dfc792014-06-16 20:44:29 -07001568 UpdateLocal(register_index, constant);
1569 break;
1570 }
1571
1572 case Instruction::CONST_HIGH16: {
1573 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001574 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16);
Dave Allison20dfc792014-06-16 20:44:29 -07001575 UpdateLocal(register_index, constant);
1576 break;
1577 }
1578
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001579 case Instruction::CONST_WIDE_16: {
1580 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001581 // Get 16 bits of constant value, sign extended to 64 bits.
1582 int64_t value = instruction.VRegB_21s();
1583 value <<= 48;
1584 value >>= 48;
David Brazdil8d5b8b22015-03-24 10:51:52 +00001585 HLongConstant* constant = graph_->GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001586 UpdateLocal(register_index, constant);
1587 break;
1588 }
1589
1590 case Instruction::CONST_WIDE_32: {
1591 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001592 // Get 32 bits of constant value, sign extended to 64 bits.
1593 int64_t value = instruction.VRegB_31i();
1594 value <<= 32;
1595 value >>= 32;
David Brazdil8d5b8b22015-03-24 10:51:52 +00001596 HLongConstant* constant = graph_->GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001597 UpdateLocal(register_index, constant);
1598 break;
1599 }
1600
1601 case Instruction::CONST_WIDE: {
1602 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001603 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001604 UpdateLocal(register_index, constant);
1605 break;
1606 }
1607
Dave Allison20dfc792014-06-16 20:44:29 -07001608 case Instruction::CONST_WIDE_HIGH16: {
1609 int32_t register_index = instruction.VRegA();
1610 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
David Brazdil8d5b8b22015-03-24 10:51:52 +00001611 HLongConstant* constant = graph_->GetLongConstant(value);
Dave Allison20dfc792014-06-16 20:44:29 -07001612 UpdateLocal(register_index, constant);
1613 break;
1614 }
1615
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001616 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001617 case Instruction::MOVE:
1618 case Instruction::MOVE_FROM16:
1619 case Instruction::MOVE_16: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001620 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001621 UpdateLocal(instruction.VRegA(), value);
1622 break;
1623 }
1624
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001625 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001626 case Instruction::MOVE_WIDE:
1627 case Instruction::MOVE_WIDE_FROM16:
1628 case Instruction::MOVE_WIDE_16: {
1629 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong);
1630 UpdateLocal(instruction.VRegA(), value);
1631 break;
1632 }
1633
1634 case Instruction::MOVE_OBJECT:
1635 case Instruction::MOVE_OBJECT_16:
1636 case Instruction::MOVE_OBJECT_FROM16: {
1637 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot);
1638 UpdateLocal(instruction.VRegA(), value);
1639 break;
1640 }
1641
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001642 case Instruction::RETURN_VOID: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001643 BuildReturn(instruction, Primitive::kPrimVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001644 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001645 }
1646
Dave Allison20dfc792014-06-16 20:44:29 -07001647#define IF_XX(comparison, cond) \
Calin Juravle225ff812014-11-13 16:46:39 +00001648 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
1649 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001650
Dave Allison20dfc792014-06-16 20:44:29 -07001651 IF_XX(HEqual, EQ);
1652 IF_XX(HNotEqual, NE);
1653 IF_XX(HLessThan, LT);
1654 IF_XX(HLessThanOrEqual, LE);
1655 IF_XX(HGreaterThan, GT);
1656 IF_XX(HGreaterThanOrEqual, GE);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001657
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001658 case Instruction::GOTO:
1659 case Instruction::GOTO_16:
1660 case Instruction::GOTO_32: {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001661 int32_t offset = instruction.GetTargetOffset();
Calin Juravle225ff812014-11-13 16:46:39 +00001662 HBasicBlock* target = FindBlockStartingAt(offset + dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001663 DCHECK(target != nullptr);
David Brazdil852eaff2015-02-02 15:23:05 +00001664 PotentiallyAddSuspendCheck(target, dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001665 current_block_->AddInstruction(new (arena_) HGoto());
1666 current_block_->AddSuccessor(target);
1667 current_block_ = nullptr;
1668 break;
1669 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001670
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001671 case Instruction::RETURN: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001672 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001673 break;
1674 }
1675
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001676 case Instruction::RETURN_OBJECT: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001677 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001678 break;
1679 }
1680
1681 case Instruction::RETURN_WIDE: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001682 BuildReturn(instruction, return_type_);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001683 break;
1684 }
1685
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001686 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001687 case Instruction::INVOKE_INTERFACE:
1688 case Instruction::INVOKE_STATIC:
1689 case Instruction::INVOKE_SUPER:
1690 case Instruction::INVOKE_VIRTUAL: {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001691 uint32_t method_idx = instruction.VRegB_35c();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001692 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001693 uint32_t args[5];
Ian Rogers29a26482014-05-02 15:27:29 -07001694 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00001695 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001696 number_of_vreg_arguments, false, args, -1)) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001697 return false;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001698 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001699 break;
1700 }
1701
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001702 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001703 case Instruction::INVOKE_INTERFACE_RANGE:
1704 case Instruction::INVOKE_STATIC_RANGE:
1705 case Instruction::INVOKE_SUPER_RANGE:
1706 case Instruction::INVOKE_VIRTUAL_RANGE: {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001707 uint32_t method_idx = instruction.VRegB_3rc();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001708 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1709 uint32_t register_index = instruction.VRegC();
Calin Juravle225ff812014-11-13 16:46:39 +00001710 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001711 number_of_vreg_arguments, true, nullptr, register_index)) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001712 return false;
1713 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001714 break;
1715 }
1716
Roland Levillain88cb1752014-10-20 16:36:47 +01001717 case Instruction::NEG_INT: {
1718 Unop_12x<HNeg>(instruction, Primitive::kPrimInt);
1719 break;
1720 }
1721
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001722 case Instruction::NEG_LONG: {
1723 Unop_12x<HNeg>(instruction, Primitive::kPrimLong);
1724 break;
1725 }
1726
Roland Levillain3dbcb382014-10-28 17:30:07 +00001727 case Instruction::NEG_FLOAT: {
1728 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat);
1729 break;
1730 }
1731
1732 case Instruction::NEG_DOUBLE: {
1733 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble);
1734 break;
1735 }
1736
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001737 case Instruction::NOT_INT: {
1738 Unop_12x<HNot>(instruction, Primitive::kPrimInt);
1739 break;
1740 }
1741
Roland Levillain70566432014-10-24 16:20:17 +01001742 case Instruction::NOT_LONG: {
1743 Unop_12x<HNot>(instruction, Primitive::kPrimLong);
1744 break;
1745 }
1746
Roland Levillaindff1f282014-11-05 14:15:05 +00001747 case Instruction::INT_TO_LONG: {
Roland Levillain624279f2014-12-04 11:54:28 +00001748 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong, dex_pc);
Roland Levillaindff1f282014-11-05 14:15:05 +00001749 break;
1750 }
1751
Roland Levillaincff13742014-11-17 14:32:17 +00001752 case Instruction::INT_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001753 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00001754 break;
1755 }
1756
1757 case Instruction::INT_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001758 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00001759 break;
1760 }
1761
Roland Levillain946e1432014-11-11 17:35:19 +00001762 case Instruction::LONG_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001763 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt, dex_pc);
Roland Levillain946e1432014-11-11 17:35:19 +00001764 break;
1765 }
1766
Roland Levillain6d0e4832014-11-27 18:31:21 +00001767 case Instruction::LONG_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001768 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimFloat, dex_pc);
Roland Levillain6d0e4832014-11-27 18:31:21 +00001769 break;
1770 }
1771
Roland Levillain647b9ed2014-11-27 12:06:00 +00001772 case Instruction::LONG_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001773 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble, dex_pc);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001774 break;
1775 }
1776
Roland Levillain3f8f9362014-12-02 17:45:01 +00001777 case Instruction::FLOAT_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001778 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimInt, dex_pc);
1779 break;
1780 }
1781
1782 case Instruction::FLOAT_TO_LONG: {
1783 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimLong, dex_pc);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001784 break;
1785 }
1786
Roland Levillain8964e2b2014-12-04 12:10:50 +00001787 case Instruction::FLOAT_TO_DOUBLE: {
1788 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimDouble, dex_pc);
1789 break;
1790 }
1791
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001792 case Instruction::DOUBLE_TO_INT: {
1793 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimInt, dex_pc);
1794 break;
1795 }
1796
1797 case Instruction::DOUBLE_TO_LONG: {
1798 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimLong, dex_pc);
1799 break;
1800 }
1801
Roland Levillain8964e2b2014-12-04 12:10:50 +00001802 case Instruction::DOUBLE_TO_FLOAT: {
1803 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimFloat, dex_pc);
1804 break;
1805 }
1806
Roland Levillain51d3fc42014-11-13 14:11:42 +00001807 case Instruction::INT_TO_BYTE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001808 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte, dex_pc);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001809 break;
1810 }
1811
Roland Levillain01a8d712014-11-14 16:27:39 +00001812 case Instruction::INT_TO_SHORT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001813 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort, dex_pc);
Roland Levillain01a8d712014-11-14 16:27:39 +00001814 break;
1815 }
1816
Roland Levillain981e4542014-11-14 11:47:14 +00001817 case Instruction::INT_TO_CHAR: {
Roland Levillain624279f2014-12-04 11:54:28 +00001818 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar, dex_pc);
Roland Levillain981e4542014-11-14 11:47:14 +00001819 break;
1820 }
1821
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001822 case Instruction::ADD_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001823 Binop_23x<HAdd>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001824 break;
1825 }
1826
1827 case Instruction::ADD_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001828 Binop_23x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001829 break;
1830 }
1831
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001832 case Instruction::ADD_DOUBLE: {
1833 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble);
1834 break;
1835 }
1836
1837 case Instruction::ADD_FLOAT: {
1838 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat);
1839 break;
1840 }
1841
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001842 case Instruction::SUB_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001843 Binop_23x<HSub>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001844 break;
1845 }
1846
1847 case Instruction::SUB_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001848 Binop_23x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001849 break;
1850 }
1851
Calin Juravle096cc022014-10-23 17:01:13 +01001852 case Instruction::SUB_FLOAT: {
1853 Binop_23x<HSub>(instruction, Primitive::kPrimFloat);
1854 break;
1855 }
1856
1857 case Instruction::SUB_DOUBLE: {
1858 Binop_23x<HSub>(instruction, Primitive::kPrimDouble);
1859 break;
1860 }
1861
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001862 case Instruction::ADD_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001863 Binop_12x<HAdd>(instruction, Primitive::kPrimInt);
1864 break;
1865 }
1866
Calin Juravle34bacdf2014-10-07 20:23:36 +01001867 case Instruction::MUL_INT: {
1868 Binop_23x<HMul>(instruction, Primitive::kPrimInt);
1869 break;
1870 }
1871
1872 case Instruction::MUL_LONG: {
1873 Binop_23x<HMul>(instruction, Primitive::kPrimLong);
1874 break;
1875 }
1876
Calin Juravleb5bfa962014-10-21 18:02:24 +01001877 case Instruction::MUL_FLOAT: {
1878 Binop_23x<HMul>(instruction, Primitive::kPrimFloat);
1879 break;
1880 }
1881
1882 case Instruction::MUL_DOUBLE: {
1883 Binop_23x<HMul>(instruction, Primitive::kPrimDouble);
1884 break;
1885 }
1886
Calin Juravled0d48522014-11-04 16:40:20 +00001887 case Instruction::DIV_INT: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001888 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1889 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravled0d48522014-11-04 16:40:20 +00001890 break;
1891 }
1892
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001893 case Instruction::DIV_LONG: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001894 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1895 dex_pc, Primitive::kPrimLong, false, true);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001896 break;
1897 }
1898
Calin Juravle7c4954d2014-10-28 16:57:40 +00001899 case Instruction::DIV_FLOAT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001900 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001901 break;
1902 }
1903
1904 case Instruction::DIV_DOUBLE: {
Calin Juravle225ff812014-11-13 16:46:39 +00001905 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001906 break;
1907 }
1908
Calin Juravlebacfec32014-11-14 15:54:36 +00001909 case Instruction::REM_INT: {
1910 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1911 dex_pc, Primitive::kPrimInt, false, false);
1912 break;
1913 }
1914
1915 case Instruction::REM_LONG: {
1916 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1917 dex_pc, Primitive::kPrimLong, false, false);
1918 break;
1919 }
1920
Calin Juravled2ec87d2014-12-08 14:24:46 +00001921 case Instruction::REM_FLOAT: {
1922 Binop_23x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
1923 break;
1924 }
1925
1926 case Instruction::REM_DOUBLE: {
1927 Binop_23x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
1928 break;
1929 }
1930
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001931 case Instruction::AND_INT: {
1932 Binop_23x<HAnd>(instruction, Primitive::kPrimInt);
1933 break;
1934 }
1935
1936 case Instruction::AND_LONG: {
1937 Binop_23x<HAnd>(instruction, Primitive::kPrimLong);
1938 break;
1939 }
1940
Calin Juravle9aec02f2014-11-18 23:06:35 +00001941 case Instruction::SHL_INT: {
1942 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt);
1943 break;
1944 }
1945
1946 case Instruction::SHL_LONG: {
1947 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong);
1948 break;
1949 }
1950
1951 case Instruction::SHR_INT: {
1952 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt);
1953 break;
1954 }
1955
1956 case Instruction::SHR_LONG: {
1957 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong);
1958 break;
1959 }
1960
1961 case Instruction::USHR_INT: {
1962 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt);
1963 break;
1964 }
1965
1966 case Instruction::USHR_LONG: {
1967 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong);
1968 break;
1969 }
1970
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001971 case Instruction::OR_INT: {
1972 Binop_23x<HOr>(instruction, Primitive::kPrimInt);
1973 break;
1974 }
1975
1976 case Instruction::OR_LONG: {
1977 Binop_23x<HOr>(instruction, Primitive::kPrimLong);
1978 break;
1979 }
1980
1981 case Instruction::XOR_INT: {
1982 Binop_23x<HXor>(instruction, Primitive::kPrimInt);
1983 break;
1984 }
1985
1986 case Instruction::XOR_LONG: {
1987 Binop_23x<HXor>(instruction, Primitive::kPrimLong);
1988 break;
1989 }
1990
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001991 case Instruction::ADD_LONG_2ADDR: {
1992 Binop_12x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001993 break;
1994 }
1995
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001996 case Instruction::ADD_DOUBLE_2ADDR: {
1997 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble);
1998 break;
1999 }
2000
2001 case Instruction::ADD_FLOAT_2ADDR: {
2002 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat);
2003 break;
2004 }
2005
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002006 case Instruction::SUB_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002007 Binop_12x<HSub>(instruction, Primitive::kPrimInt);
2008 break;
2009 }
2010
2011 case Instruction::SUB_LONG_2ADDR: {
2012 Binop_12x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002013 break;
2014 }
2015
Calin Juravle096cc022014-10-23 17:01:13 +01002016 case Instruction::SUB_FLOAT_2ADDR: {
2017 Binop_12x<HSub>(instruction, Primitive::kPrimFloat);
2018 break;
2019 }
2020
2021 case Instruction::SUB_DOUBLE_2ADDR: {
2022 Binop_12x<HSub>(instruction, Primitive::kPrimDouble);
2023 break;
2024 }
2025
Calin Juravle34bacdf2014-10-07 20:23:36 +01002026 case Instruction::MUL_INT_2ADDR: {
2027 Binop_12x<HMul>(instruction, Primitive::kPrimInt);
2028 break;
2029 }
2030
2031 case Instruction::MUL_LONG_2ADDR: {
2032 Binop_12x<HMul>(instruction, Primitive::kPrimLong);
2033 break;
2034 }
2035
Calin Juravleb5bfa962014-10-21 18:02:24 +01002036 case Instruction::MUL_FLOAT_2ADDR: {
2037 Binop_12x<HMul>(instruction, Primitive::kPrimFloat);
2038 break;
2039 }
2040
2041 case Instruction::MUL_DOUBLE_2ADDR: {
2042 Binop_12x<HMul>(instruction, Primitive::kPrimDouble);
2043 break;
2044 }
2045
Calin Juravle865fc882014-11-06 17:09:03 +00002046 case Instruction::DIV_INT_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002047 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2048 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravle865fc882014-11-06 17:09:03 +00002049 break;
2050 }
2051
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002052 case Instruction::DIV_LONG_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002053 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2054 dex_pc, Primitive::kPrimLong, false, true);
2055 break;
2056 }
2057
2058 case Instruction::REM_INT_2ADDR: {
2059 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2060 dex_pc, Primitive::kPrimInt, false, false);
2061 break;
2062 }
2063
2064 case Instruction::REM_LONG_2ADDR: {
2065 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2066 dex_pc, Primitive::kPrimLong, false, false);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002067 break;
2068 }
2069
Calin Juravled2ec87d2014-12-08 14:24:46 +00002070 case Instruction::REM_FLOAT_2ADDR: {
2071 Binop_12x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2072 break;
2073 }
2074
2075 case Instruction::REM_DOUBLE_2ADDR: {
2076 Binop_12x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2077 break;
2078 }
2079
Calin Juravle9aec02f2014-11-18 23:06:35 +00002080 case Instruction::SHL_INT_2ADDR: {
2081 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt);
2082 break;
2083 }
2084
2085 case Instruction::SHL_LONG_2ADDR: {
2086 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong);
2087 break;
2088 }
2089
2090 case Instruction::SHR_INT_2ADDR: {
2091 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt);
2092 break;
2093 }
2094
2095 case Instruction::SHR_LONG_2ADDR: {
2096 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong);
2097 break;
2098 }
2099
2100 case Instruction::USHR_INT_2ADDR: {
2101 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt);
2102 break;
2103 }
2104
2105 case Instruction::USHR_LONG_2ADDR: {
2106 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong);
2107 break;
2108 }
2109
Calin Juravle7c4954d2014-10-28 16:57:40 +00002110 case Instruction::DIV_FLOAT_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00002111 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002112 break;
2113 }
2114
2115 case Instruction::DIV_DOUBLE_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00002116 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002117 break;
2118 }
2119
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002120 case Instruction::AND_INT_2ADDR: {
2121 Binop_12x<HAnd>(instruction, Primitive::kPrimInt);
2122 break;
2123 }
2124
2125 case Instruction::AND_LONG_2ADDR: {
2126 Binop_12x<HAnd>(instruction, Primitive::kPrimLong);
2127 break;
2128 }
2129
2130 case Instruction::OR_INT_2ADDR: {
2131 Binop_12x<HOr>(instruction, Primitive::kPrimInt);
2132 break;
2133 }
2134
2135 case Instruction::OR_LONG_2ADDR: {
2136 Binop_12x<HOr>(instruction, Primitive::kPrimLong);
2137 break;
2138 }
2139
2140 case Instruction::XOR_INT_2ADDR: {
2141 Binop_12x<HXor>(instruction, Primitive::kPrimInt);
2142 break;
2143 }
2144
2145 case Instruction::XOR_LONG_2ADDR: {
2146 Binop_12x<HXor>(instruction, Primitive::kPrimLong);
2147 break;
2148 }
2149
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002150 case Instruction::ADD_INT_LIT16: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002151 Binop_22s<HAdd>(instruction, false);
2152 break;
2153 }
2154
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002155 case Instruction::AND_INT_LIT16: {
2156 Binop_22s<HAnd>(instruction, false);
2157 break;
2158 }
2159
2160 case Instruction::OR_INT_LIT16: {
2161 Binop_22s<HOr>(instruction, false);
2162 break;
2163 }
2164
2165 case Instruction::XOR_INT_LIT16: {
2166 Binop_22s<HXor>(instruction, false);
2167 break;
2168 }
2169
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002170 case Instruction::RSUB_INT: {
2171 Binop_22s<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002172 break;
2173 }
2174
Calin Juravle34bacdf2014-10-07 20:23:36 +01002175 case Instruction::MUL_INT_LIT16: {
2176 Binop_22s<HMul>(instruction, false);
2177 break;
2178 }
2179
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002180 case Instruction::ADD_INT_LIT8: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002181 Binop_22b<HAdd>(instruction, false);
2182 break;
2183 }
2184
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002185 case Instruction::AND_INT_LIT8: {
2186 Binop_22b<HAnd>(instruction, false);
2187 break;
2188 }
2189
2190 case Instruction::OR_INT_LIT8: {
2191 Binop_22b<HOr>(instruction, false);
2192 break;
2193 }
2194
2195 case Instruction::XOR_INT_LIT8: {
2196 Binop_22b<HXor>(instruction, false);
2197 break;
2198 }
2199
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002200 case Instruction::RSUB_INT_LIT8: {
2201 Binop_22b<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002202 break;
2203 }
2204
Calin Juravle34bacdf2014-10-07 20:23:36 +01002205 case Instruction::MUL_INT_LIT8: {
2206 Binop_22b<HMul>(instruction, false);
2207 break;
2208 }
2209
Calin Juravled0d48522014-11-04 16:40:20 +00002210 case Instruction::DIV_INT_LIT16:
2211 case Instruction::DIV_INT_LIT8: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002212 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2213 dex_pc, Primitive::kPrimInt, true, true);
2214 break;
2215 }
2216
2217 case Instruction::REM_INT_LIT16:
2218 case Instruction::REM_INT_LIT8: {
2219 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2220 dex_pc, Primitive::kPrimInt, true, false);
Calin Juravled0d48522014-11-04 16:40:20 +00002221 break;
2222 }
2223
Calin Juravle9aec02f2014-11-18 23:06:35 +00002224 case Instruction::SHL_INT_LIT8: {
2225 Binop_22b<HShl>(instruction, false);
2226 break;
2227 }
2228
2229 case Instruction::SHR_INT_LIT8: {
2230 Binop_22b<HShr>(instruction, false);
2231 break;
2232 }
2233
2234 case Instruction::USHR_INT_LIT8: {
2235 Binop_22b<HUShr>(instruction, false);
2236 break;
2237 }
2238
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002239 case Instruction::NEW_INSTANCE: {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002240 uint16_t type_index = instruction.VRegB_21c();
Jeff Hao848f70a2014-01-15 13:49:50 -08002241 if (compiler_driver_->IsStringTypeIndex(type_index, dex_file_)) {
2242 // Turn new-instance of string into a const 0.
2243 int32_t register_index = instruction.VRegA();
2244 HNullConstant* constant = graph_->GetNullConstant();
2245 UpdateLocal(register_index, constant);
2246 } else {
2247 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
2248 ? kQuickAllocObjectWithAccessCheck
2249 : kQuickAllocObject;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002250
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002251 current_block_->AddInstruction(new (arena_) HNewInstance(
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002252 graph_->GetCurrentMethod(),
2253 dex_pc,
2254 type_index,
2255 *dex_compilation_unit_->GetDexFile(),
2256 entrypoint));
Jeff Hao848f70a2014-01-15 13:49:50 -08002257 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
2258 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002259 break;
2260 }
2261
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002262 case Instruction::NEW_ARRAY: {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002263 uint16_t type_index = instruction.VRegC_22c();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002264 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt);
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002265 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
2266 ? kQuickAllocArrayWithAccessCheck
2267 : kQuickAllocArray;
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002268 current_block_->AddInstruction(new (arena_) HNewArray(length,
2269 graph_->GetCurrentMethod(),
2270 dex_pc,
2271 type_index,
2272 *dex_compilation_unit_->GetDexFile(),
2273 entrypoint));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002274 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
2275 break;
2276 }
2277
2278 case Instruction::FILLED_NEW_ARRAY: {
2279 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
2280 uint32_t type_index = instruction.VRegB_35c();
2281 uint32_t args[5];
2282 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00002283 BuildFilledNewArray(dex_pc, type_index, number_of_vreg_arguments, false, args, 0);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002284 break;
2285 }
2286
2287 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2288 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
2289 uint32_t type_index = instruction.VRegB_3rc();
2290 uint32_t register_index = instruction.VRegC_3rc();
2291 BuildFilledNewArray(
Calin Juravle225ff812014-11-13 16:46:39 +00002292 dex_pc, type_index, number_of_vreg_arguments, true, nullptr, register_index);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002293 break;
2294 }
2295
2296 case Instruction::FILL_ARRAY_DATA: {
Calin Juravle225ff812014-11-13 16:46:39 +00002297 BuildFillArrayData(instruction, dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002298 break;
2299 }
2300
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002301 case Instruction::MOVE_RESULT:
Dave Allison20dfc792014-06-16 20:44:29 -07002302 case Instruction::MOVE_RESULT_WIDE:
David Brazdilfc6a86a2015-06-26 10:33:45 +00002303 case Instruction::MOVE_RESULT_OBJECT: {
Nicolas Geoffray1efcc222015-06-24 12:41:20 +01002304 if (latest_result_ == nullptr) {
2305 // Only dead code can lead to this situation, where the verifier
2306 // does not reject the method.
2307 } else {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002308 // An Invoke/FilledNewArray and its MoveResult could have landed in
2309 // different blocks if there was a try/catch block boundary between
2310 // them. For Invoke, we insert a StoreLocal after the instruction. For
2311 // FilledNewArray, the local needs to be updated after the array was
2312 // filled, otherwise we might overwrite an input vreg.
2313 HStoreLocal* update_local =
2314 new (arena_) HStoreLocal(GetLocalAt(instruction.VRegA()), latest_result_);
2315 HBasicBlock* block = latest_result_->GetBlock();
2316 if (block == current_block_) {
2317 // MoveResult and the previous instruction are in the same block.
2318 current_block_->AddInstruction(update_local);
2319 } else {
2320 // The two instructions are in different blocks. Insert the MoveResult
2321 // before the final control-flow instruction of the previous block.
2322 DCHECK(block->EndsWithControlFlowInstruction());
2323 DCHECK(current_block_->GetInstructions().IsEmpty());
2324 block->InsertInstructionBefore(update_local, block->GetLastInstruction());
2325 }
Nicolas Geoffray1efcc222015-06-24 12:41:20 +01002326 latest_result_ = nullptr;
2327 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002328 break;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002329 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002330
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002331 case Instruction::CMP_LONG: {
Mark Mendellc4701932015-04-10 13:18:51 -04002332 Binop_23x_cmp(instruction, Primitive::kPrimLong, kNoBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002333 break;
2334 }
2335
2336 case Instruction::CMPG_FLOAT: {
Mark Mendellc4701932015-04-10 13:18:51 -04002337 Binop_23x_cmp(instruction, Primitive::kPrimFloat, kGtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002338 break;
2339 }
2340
2341 case Instruction::CMPG_DOUBLE: {
Mark Mendellc4701932015-04-10 13:18:51 -04002342 Binop_23x_cmp(instruction, Primitive::kPrimDouble, kGtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002343 break;
2344 }
2345
2346 case Instruction::CMPL_FLOAT: {
Mark Mendellc4701932015-04-10 13:18:51 -04002347 Binop_23x_cmp(instruction, Primitive::kPrimFloat, kLtBias, dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +00002348 break;
2349 }
2350
2351 case Instruction::CMPL_DOUBLE: {
Mark Mendellc4701932015-04-10 13:18:51 -04002352 Binop_23x_cmp(instruction, Primitive::kPrimDouble, kLtBias, dex_pc);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002353 break;
2354 }
2355
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002356 case Instruction::NOP:
2357 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002358
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002359 case Instruction::IGET:
2360 case Instruction::IGET_WIDE:
2361 case Instruction::IGET_OBJECT:
2362 case Instruction::IGET_BOOLEAN:
2363 case Instruction::IGET_BYTE:
2364 case Instruction::IGET_CHAR:
2365 case Instruction::IGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002366 if (!BuildInstanceFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002367 return false;
2368 }
2369 break;
2370 }
2371
2372 case Instruction::IPUT:
2373 case Instruction::IPUT_WIDE:
2374 case Instruction::IPUT_OBJECT:
2375 case Instruction::IPUT_BOOLEAN:
2376 case Instruction::IPUT_BYTE:
2377 case Instruction::IPUT_CHAR:
2378 case Instruction::IPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002379 if (!BuildInstanceFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002380 return false;
2381 }
2382 break;
2383 }
2384
2385 case Instruction::SGET:
2386 case Instruction::SGET_WIDE:
2387 case Instruction::SGET_OBJECT:
2388 case Instruction::SGET_BOOLEAN:
2389 case Instruction::SGET_BYTE:
2390 case Instruction::SGET_CHAR:
2391 case Instruction::SGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002392 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002393 return false;
2394 }
2395 break;
2396 }
2397
2398 case Instruction::SPUT:
2399 case Instruction::SPUT_WIDE:
2400 case Instruction::SPUT_OBJECT:
2401 case Instruction::SPUT_BOOLEAN:
2402 case Instruction::SPUT_BYTE:
2403 case Instruction::SPUT_CHAR:
2404 case Instruction::SPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002405 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002406 return false;
2407 }
2408 break;
2409 }
2410
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002411#define ARRAY_XX(kind, anticipated_type) \
2412 case Instruction::AGET##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00002413 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002414 break; \
2415 } \
2416 case Instruction::APUT##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00002417 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002418 break; \
2419 }
2420
2421 ARRAY_XX(, Primitive::kPrimInt);
2422 ARRAY_XX(_WIDE, Primitive::kPrimLong);
2423 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
2424 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
2425 ARRAY_XX(_BYTE, Primitive::kPrimByte);
2426 ARRAY_XX(_CHAR, Primitive::kPrimChar);
2427 ARRAY_XX(_SHORT, Primitive::kPrimShort);
2428
Nicolas Geoffray39468442014-09-02 15:17:15 +01002429 case Instruction::ARRAY_LENGTH: {
2430 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002431 // No need for a temporary for the null check, it is the only input of the following
2432 // instruction.
Calin Juravle225ff812014-11-13 16:46:39 +00002433 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002434 current_block_->AddInstruction(object);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002435 current_block_->AddInstruction(new (arena_) HArrayLength(object));
2436 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
2437 break;
2438 }
2439
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002440 case Instruction::CONST_STRING: {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002441 current_block_->AddInstruction(
2442 new (arena_) HLoadString(graph_->GetCurrentMethod(), instruction.VRegB_21c(), dex_pc));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002443 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2444 break;
2445 }
2446
2447 case Instruction::CONST_STRING_JUMBO: {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002448 current_block_->AddInstruction(
2449 new (arena_) HLoadString(graph_->GetCurrentMethod(), instruction.VRegB_31c(), dex_pc));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002450 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
2451 break;
2452 }
2453
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002454 case Instruction::CONST_CLASS: {
2455 uint16_t type_index = instruction.VRegB_21c();
2456 bool type_known_final;
2457 bool type_known_abstract;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002458 bool dont_use_is_referrers_class;
2459 // `CanAccessTypeWithoutChecks` will tell whether the method being
2460 // built is trying to access its own class, so that the generated
2461 // code can optimize for this case. However, the optimization does not
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002462 // work for inlining, so we use `IsOutermostCompilingClass` instead.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002463 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
2464 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002465 &type_known_final, &type_known_abstract, &dont_use_is_referrers_class);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002466 if (!can_access) {
Calin Juravle48c2b032014-12-09 18:11:36 +00002467 MaybeRecordStat(MethodCompilationStat::kNotCompiledCantAccesType);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002468 return false;
2469 }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002470 current_block_->AddInstruction(new (arena_) HLoadClass(
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002471 graph_->GetCurrentMethod(),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002472 type_index,
2473 *dex_compilation_unit_->GetDexFile(),
2474 IsOutermostCompilingClass(type_index),
2475 dex_pc));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002476 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2477 break;
2478 }
2479
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002480 case Instruction::MOVE_EXCEPTION: {
2481 current_block_->AddInstruction(new (arena_) HLoadException());
2482 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
2483 break;
2484 }
2485
2486 case Instruction::THROW: {
2487 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00002488 current_block_->AddInstruction(new (arena_) HThrow(exception, dex_pc));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002489 // A throw instruction must branch to the exit block.
2490 current_block_->AddSuccessor(exit_block_);
2491 // We finished building this block. Set the current block to null to avoid
2492 // adding dead instructions to it.
2493 current_block_ = nullptr;
2494 break;
2495 }
2496
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002497 case Instruction::INSTANCE_OF: {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002498 uint8_t destination = instruction.VRegA_22c();
2499 uint8_t reference = instruction.VRegB_22c();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002500 uint16_t type_index = instruction.VRegC_22c();
Calin Juravle225ff812014-11-13 16:46:39 +00002501 if (!BuildTypeCheck(instruction, destination, reference, type_index, dex_pc)) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002502 return false;
2503 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002504 break;
2505 }
2506
2507 case Instruction::CHECK_CAST: {
2508 uint8_t reference = instruction.VRegA_21c();
2509 uint16_t type_index = instruction.VRegB_21c();
Calin Juravle225ff812014-11-13 16:46:39 +00002510 if (!BuildTypeCheck(instruction, -1, reference, type_index, dex_pc)) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002511 return false;
2512 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002513 break;
2514 }
2515
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002516 case Instruction::MONITOR_ENTER: {
2517 current_block_->AddInstruction(new (arena_) HMonitorOperation(
2518 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2519 HMonitorOperation::kEnter,
Calin Juravle225ff812014-11-13 16:46:39 +00002520 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002521 break;
2522 }
2523
2524 case Instruction::MONITOR_EXIT: {
2525 current_block_->AddInstruction(new (arena_) HMonitorOperation(
2526 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2527 HMonitorOperation::kExit,
Calin Juravle225ff812014-11-13 16:46:39 +00002528 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002529 break;
2530 }
2531
Andreas Gamped881df52014-11-24 23:28:39 -08002532 case Instruction::PACKED_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002533 BuildPackedSwitch(instruction, dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08002534 break;
2535 }
2536
Andreas Gampee4d4d322014-12-04 09:09:57 -08002537 case Instruction::SPARSE_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002538 BuildSparseSwitch(instruction, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08002539 break;
2540 }
2541
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002542 default:
Calin Juravle48c2b032014-12-09 18:11:36 +00002543 VLOG(compiler) << "Did not compile "
2544 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
2545 << " because of unhandled instruction "
2546 << instruction.Name();
2547 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnhandledInstruction);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002548 return false;
2549 }
2550 return true;
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00002551} // NOLINT(readability/fn_size)
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002552
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002553HLocal* HGraphBuilder::GetLocalAt(int register_index) const {
2554 return locals_.Get(register_index);
2555}
2556
2557void HGraphBuilder::UpdateLocal(int register_index, HInstruction* instruction) const {
2558 HLocal* local = GetLocalAt(register_index);
2559 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction));
2560}
2561
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002562HInstruction* HGraphBuilder::LoadLocal(int register_index, Primitive::Type type) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002563 HLocal* local = GetLocalAt(register_index);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002564 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type));
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002565 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002566}
2567
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002568} // namespace art