blob: bb2080766d83c91ca9a3de1bbcd2989f322e2993 [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"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000022#include "dex_file-inl.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000023#include "dex_instruction-inl.h"
Roland Levillain4c0eb422015-04-24 16:43:49 +010024#include "dex/verified_method.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010025#include "driver/compiler_driver-inl.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000026#include "driver/compiler_options.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010027#include "mirror/class_loader.h"
28#include "mirror/dex_cache.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000029#include "nodes.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000030#include "primitive.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010031#include "scoped_thread_state_change.h"
32#include "thread.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000033
34namespace art {
35
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010036/**
37 * Helper class to add HTemporary instructions. This class is used when
38 * converting a DEX instruction to multiple HInstruction, and where those
39 * instructions do not die at the following instruction, but instead spans
40 * multiple instructions.
41 */
42class Temporaries : public ValueObject {
43 public:
Calin Juravlef97f9fb2014-11-11 15:38:19 +000044 explicit Temporaries(HGraph* graph) : graph_(graph), index_(0) {}
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010045
46 void Add(HInstruction* instruction) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +000047 HInstruction* temp = new (graph_->GetArena()) HTemporary(index_);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010048 instruction->GetBlock()->AddInstruction(temp);
Calin Juravlef97f9fb2014-11-11 15:38:19 +000049
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010050 DCHECK(temp->GetPrevious() == instruction);
Calin Juravlef97f9fb2014-11-11 15:38:19 +000051
52 size_t offset;
53 if (instruction->GetType() == Primitive::kPrimLong
54 || instruction->GetType() == Primitive::kPrimDouble) {
55 offset = 2;
56 } else {
57 offset = 1;
58 }
59 index_ += offset;
60
61 graph_->UpdateTemporariesVRegSlots(index_);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010062 }
63
64 private:
65 HGraph* const graph_;
66
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010067 // Current index in the temporary stack, updated by `Add`.
68 size_t index_;
69};
70
Andreas Gamped881df52014-11-24 23:28:39 -080071class SwitchTable : public ValueObject {
72 public:
73 SwitchTable(const Instruction& instruction, uint32_t dex_pc, bool sparse)
74 : instruction_(instruction), dex_pc_(dex_pc), sparse_(sparse) {
75 int32_t table_offset = instruction.VRegB_31t();
76 const uint16_t* table = reinterpret_cast<const uint16_t*>(&instruction) + table_offset;
77 if (sparse) {
78 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
79 } else {
80 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
81 }
82 num_entries_ = table[1];
83 values_ = reinterpret_cast<const int32_t*>(&table[2]);
84 }
85
86 uint16_t GetNumEntries() const {
87 return num_entries_;
88 }
89
Andreas Gampee4d4d322014-12-04 09:09:57 -080090 void CheckIndex(size_t index) const {
91 if (sparse_) {
92 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
93 DCHECK_LT(index, 2 * static_cast<size_t>(num_entries_));
94 } else {
95 // In a packed table, we have the starting key and num_entries_ values.
96 DCHECK_LT(index, 1 + static_cast<size_t>(num_entries_));
97 }
98 }
99
Andreas Gamped881df52014-11-24 23:28:39 -0800100 int32_t GetEntryAt(size_t index) const {
Andreas Gampee4d4d322014-12-04 09:09:57 -0800101 CheckIndex(index);
Andreas Gamped881df52014-11-24 23:28:39 -0800102 return values_[index];
103 }
104
105 uint32_t GetDexPcForIndex(size_t index) const {
Andreas Gampee4d4d322014-12-04 09:09:57 -0800106 CheckIndex(index);
Andreas Gamped881df52014-11-24 23:28:39 -0800107 return dex_pc_ +
108 (reinterpret_cast<const int16_t*>(values_ + index) -
109 reinterpret_cast<const int16_t*>(&instruction_));
110 }
111
Andreas Gampee4d4d322014-12-04 09:09:57 -0800112 // Index of the first value in the table.
113 size_t GetFirstValueIndex() const {
114 if (sparse_) {
115 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
116 return num_entries_;
117 } else {
118 // In a packed table, we have the starting key and num_entries_ values.
119 return 1;
120 }
121 }
122
Andreas Gamped881df52014-11-24 23:28:39 -0800123 private:
124 const Instruction& instruction_;
125 const uint32_t dex_pc_;
126
127 // Whether this is a sparse-switch table (or a packed-switch one).
128 const bool sparse_;
129
130 // This can't be const as it needs to be computed off of the given instruction, and complicated
131 // expressions in the initializer list seemed very ugly.
132 uint16_t num_entries_;
133
134 const int32_t* values_;
135
136 DISALLOW_COPY_AND_ASSIGN(SwitchTable);
137};
138
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100139void HGraphBuilder::InitializeLocals(uint16_t count) {
140 graph_->SetNumberOfVRegs(count);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000141 locals_.SetSize(count);
142 for (int i = 0; i < count; i++) {
143 HLocal* local = new (arena_) HLocal(i);
144 entry_block_->AddInstruction(local);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000145 locals_.Put(i, local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000146 }
147}
148
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000149void HGraphBuilder::InitializeParameters(uint16_t number_of_parameters) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100150 // dex_compilation_unit_ is null only when unit testing.
151 if (dex_compilation_unit_ == nullptr) {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000152 return;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100153 }
154
155 graph_->SetNumberOfInVRegs(number_of_parameters);
156 const char* shorty = dex_compilation_unit_->GetShorty();
157 int locals_index = locals_.Size() - number_of_parameters;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100158 int parameter_index = 0;
159
160 if (!dex_compilation_unit_->IsStatic()) {
161 // Add the implicit 'this' argument, not expressed in the signature.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100162 HParameterValue* parameter =
Calin Juravle10e244f2015-01-26 18:54:32 +0000163 new (arena_) HParameterValue(parameter_index++, Primitive::kPrimNot, true);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100164 entry_block_->AddInstruction(parameter);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100165 HLocal* local = GetLocalAt(locals_index++);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100166 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100167 number_of_parameters--;
168 }
169
170 uint32_t pos = 1;
171 for (int i = 0; i < number_of_parameters; i++) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100172 HParameterValue* parameter =
173 new (arena_) HParameterValue(parameter_index++, Primitive::GetType(shorty[pos++]));
174 entry_block_->AddInstruction(parameter);
175 HLocal* local = GetLocalAt(locals_index++);
176 // Store the parameter value in the local that the dex code will use
177 // to reference that parameter.
178 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter));
179 bool is_wide = (parameter->GetType() == Primitive::kPrimLong)
180 || (parameter->GetType() == Primitive::kPrimDouble);
181 if (is_wide) {
182 i++;
183 locals_index++;
184 parameter_index++;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100185 }
186 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100187}
188
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100189template<typename T>
Calin Juravle225ff812014-11-13 16:46:39 +0000190void HGraphBuilder::If_22t(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000191 int32_t target_offset = instruction.GetTargetOffset();
David Brazdil852eaff2015-02-02 15:23:05 +0000192 HBasicBlock* branch_target = FindBlockStartingAt(dex_pc + target_offset);
193 HBasicBlock* fallthrough_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
194 DCHECK(branch_target != nullptr);
195 DCHECK(fallthrough_target != nullptr);
196 PotentiallyAddSuspendCheck(branch_target, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100197 HInstruction* first = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
198 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Dave Allison20dfc792014-06-16 20:44:29 -0700199 T* comparison = new (arena_) T(first, second);
200 current_block_->AddInstruction(comparison);
201 HInstruction* ifinst = new (arena_) HIf(comparison);
202 current_block_->AddInstruction(ifinst);
David Brazdil852eaff2015-02-02 15:23:05 +0000203 current_block_->AddSuccessor(branch_target);
204 current_block_->AddSuccessor(fallthrough_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700205 current_block_ = nullptr;
206}
207
208template<typename T>
Calin Juravle225ff812014-11-13 16:46:39 +0000209void HGraphBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000210 int32_t target_offset = instruction.GetTargetOffset();
David Brazdil852eaff2015-02-02 15:23:05 +0000211 HBasicBlock* branch_target = FindBlockStartingAt(dex_pc + target_offset);
212 HBasicBlock* fallthrough_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
213 DCHECK(branch_target != nullptr);
214 DCHECK(fallthrough_target != nullptr);
215 PotentiallyAddSuspendCheck(branch_target, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700216 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000217 T* comparison = new (arena_) T(value, graph_->GetIntConstant(0));
Dave Allison20dfc792014-06-16 20:44:29 -0700218 current_block_->AddInstruction(comparison);
219 HInstruction* ifinst = new (arena_) HIf(comparison);
220 current_block_->AddInstruction(ifinst);
David Brazdil852eaff2015-02-02 15:23:05 +0000221 current_block_->AddSuccessor(branch_target);
222 current_block_->AddSuccessor(fallthrough_target);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100223 current_block_ = nullptr;
224}
225
Calin Juravle48c2b032014-12-09 18:11:36 +0000226void HGraphBuilder::MaybeRecordStat(MethodCompilationStat compilation_stat) {
227 if (compilation_stats_ != nullptr) {
228 compilation_stats_->RecordStat(compilation_stat);
229 }
230}
231
David Brazdil1b498722015-03-31 11:37:18 +0100232bool HGraphBuilder::SkipCompilation(const DexFile::CodeItem& code_item,
Calin Juravle48c2b032014-12-09 18:11:36 +0000233 size_t number_of_branches) {
234 const CompilerOptions& compiler_options = compiler_driver_->GetCompilerOptions();
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000235 CompilerOptions::CompilerFilter compiler_filter = compiler_options.GetCompilerFilter();
236 if (compiler_filter == CompilerOptions::kEverything) {
237 return false;
238 }
239
David Brazdil1b498722015-03-31 11:37:18 +0100240 if (compiler_options.IsHugeMethod(code_item.insns_size_in_code_units_)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000241 VLOG(compiler) << "Skip compilation of huge method "
242 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
David Brazdil1b498722015-03-31 11:37:18 +0100243 << ": " << code_item.insns_size_in_code_units_ << " code units";
Calin Juravle48c2b032014-12-09 18:11:36 +0000244 MaybeRecordStat(MethodCompilationStat::kNotCompiledHugeMethod);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000245 return true;
246 }
247
248 // If it's large and contains no branches, it's likely to be machine generated initialization.
David Brazdil1b498722015-03-31 11:37:18 +0100249 if (compiler_options.IsLargeMethod(code_item.insns_size_in_code_units_)
250 && (number_of_branches == 0)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000251 VLOG(compiler) << "Skip compilation of large method with no branch "
252 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
David Brazdil1b498722015-03-31 11:37:18 +0100253 << ": " << code_item.insns_size_in_code_units_ << " code units";
Calin Juravle48c2b032014-12-09 18:11:36 +0000254 MaybeRecordStat(MethodCompilationStat::kNotCompiledLargeMethodNoBranches);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000255 return true;
256 }
257
258 return false;
259}
260
David Brazdil5e8b1372015-01-23 14:39:08 +0000261bool HGraphBuilder::BuildGraph(const DexFile::CodeItem& code_item) {
262 DCHECK(graph_->GetBlocks().IsEmpty());
263
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000264 const uint16_t* code_ptr = code_item.insns_;
265 const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100266 code_start_ = code_ptr;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000267
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000268 // Setup the graph with the entry block and exit block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100269 entry_block_ = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000270 graph_->AddBlock(entry_block_);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100271 exit_block_ = new (arena_) HBasicBlock(graph_, kNoDexPc);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000272 graph_->SetEntryBlock(entry_block_);
273 graph_->SetExitBlock(exit_block_);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000274
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000275 InitializeLocals(code_item.registers_size_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000276 graph_->SetMaximumNumberOfOutVRegs(code_item.outs_size_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000277
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000278 // Compute the number of dex instructions, blocks, and branches. We will
279 // check these values against limits given to the compiler.
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000280 size_t number_of_branches = 0;
281
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000282 // To avoid splitting blocks, we compute ahead of time the instructions that
283 // start a new block, and create these blocks.
David Brazdil1b498722015-03-31 11:37:18 +0100284 ComputeBranchTargets(code_ptr, code_end, &number_of_branches);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000285
286 // Note that the compiler driver is null when unit testing.
David Brazdil1b498722015-03-31 11:37:18 +0100287 if ((compiler_driver_ != nullptr) && SkipCompilation(code_item, number_of_branches)) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000288 return false;
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000289 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000290
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000291 // Also create blocks for catch handlers.
292 if (code_item.tries_size_ != 0) {
293 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(code_item, 0);
294 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
295 for (uint32_t idx = 0; idx < handlers_size; ++idx) {
296 CatchHandlerIterator iterator(handlers_ptr);
297 for (; iterator.HasNext(); iterator.Next()) {
298 uint32_t address = iterator.GetHandlerAddress();
299 HBasicBlock* block = FindBlockStartingAt(address);
300 if (block == nullptr) {
301 block = new (arena_) HBasicBlock(graph_, address);
302 branch_targets_.Put(address, block);
303 }
304 block->SetIsCatchBlock();
305 }
306 handlers_ptr = iterator.EndDataPointer();
307 }
308 }
309
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000310 InitializeParameters(code_item.ins_size_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100311
Calin Juravle225ff812014-11-13 16:46:39 +0000312 size_t dex_pc = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000313 while (code_ptr < code_end) {
Calin Juravle225ff812014-11-13 16:46:39 +0000314 // Update the current block if dex_pc starts a new block.
315 MaybeUpdateCurrentBlock(dex_pc);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000316 const Instruction& instruction = *Instruction::At(code_ptr);
Calin Juravle48c2b032014-12-09 18:11:36 +0000317 if (!AnalyzeDexInstruction(instruction, dex_pc)) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000318 return false;
Calin Juravle48c2b032014-12-09 18:11:36 +0000319 }
Calin Juravle225ff812014-11-13 16:46:39 +0000320 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000321 code_ptr += instruction.SizeInCodeUnits();
322 }
323
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000324 // Add the exit block at the end to give it the highest id.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000325 graph_->AddBlock(exit_block_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000326 exit_block_->AddInstruction(new (arena_) HExit());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000327 // Add the suspend check to the entry block.
328 entry_block_->AddInstruction(new (arena_) HSuspendCheck(0));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000329 entry_block_->AddInstruction(new (arena_) HGoto());
David Brazdil5e8b1372015-01-23 14:39:08 +0000330
331 return true;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000332}
333
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000334void HGraphBuilder::MaybeUpdateCurrentBlock(size_t index) {
335 HBasicBlock* block = FindBlockStartingAt(index);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000336 if (block == nullptr) {
337 return;
338 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000339
340 if (current_block_ != nullptr) {
341 // Branching instructions clear current_block, so we know
342 // the last instruction of the current block is not a branching
343 // instruction. We add an unconditional goto to the found block.
344 current_block_->AddInstruction(new (arena_) HGoto());
345 current_block_->AddSuccessor(block);
346 }
347 graph_->AddBlock(block);
348 current_block_ = block;
349}
350
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000351void HGraphBuilder::ComputeBranchTargets(const uint16_t* code_ptr,
352 const uint16_t* code_end,
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000353 size_t* number_of_branches) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000354 branch_targets_.SetSize(code_end - code_ptr);
355
356 // Create the first block for the dex instructions, single successor of the entry block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100357 HBasicBlock* block = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000358 branch_targets_.Put(0, block);
359 entry_block_->AddSuccessor(block);
360
361 // Iterate over all instructions and find branching instructions. Create blocks for
362 // the locations these instructions branch to.
Andreas Gamped881df52014-11-24 23:28:39 -0800363 uint32_t dex_pc = 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000364 while (code_ptr < code_end) {
365 const Instruction& instruction = *Instruction::At(code_ptr);
366 if (instruction.IsBranch()) {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000367 (*number_of_branches)++;
Calin Juravle225ff812014-11-13 16:46:39 +0000368 int32_t target = instruction.GetTargetOffset() + dex_pc;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000369 // Create a block for the target instruction.
370 if (FindBlockStartingAt(target) == nullptr) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100371 block = new (arena_) HBasicBlock(graph_, target);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000372 branch_targets_.Put(target, block);
373 }
Calin Juravle225ff812014-11-13 16:46:39 +0000374 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000375 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle225ff812014-11-13 16:46:39 +0000376 if ((code_ptr < code_end) && (FindBlockStartingAt(dex_pc) == nullptr)) {
377 block = new (arena_) HBasicBlock(graph_, dex_pc);
378 branch_targets_.Put(dex_pc, block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000379 }
Andreas Gampee4d4d322014-12-04 09:09:57 -0800380 } else if (instruction.IsSwitch()) {
381 SwitchTable table(instruction, dex_pc, instruction.Opcode() == Instruction::SPARSE_SWITCH);
Andreas Gamped881df52014-11-24 23:28:39 -0800382
383 uint16_t num_entries = table.GetNumEntries();
384
Andreas Gampee4d4d322014-12-04 09:09:57 -0800385 // In a packed-switch, the entry at index 0 is the starting key. In a sparse-switch, the
386 // entry at index 0 is the first key, and values are after *all* keys.
387 size_t offset = table.GetFirstValueIndex();
388
389 // Use a larger loop counter type to avoid overflow issues.
390 for (size_t i = 0; i < num_entries; ++i) {
Andreas Gamped881df52014-11-24 23:28:39 -0800391 // The target of the case.
Andreas Gampee4d4d322014-12-04 09:09:57 -0800392 uint32_t target = dex_pc + table.GetEntryAt(i + offset);
Andreas Gamped881df52014-11-24 23:28:39 -0800393 if (FindBlockStartingAt(target) == nullptr) {
394 block = new (arena_) HBasicBlock(graph_, target);
395 branch_targets_.Put(target, block);
Andreas Gamped881df52014-11-24 23:28:39 -0800396 }
397
398 // The next case gets its own block.
399 if (i < num_entries) {
400 block = new (arena_) HBasicBlock(graph_, target);
401 branch_targets_.Put(table.GetDexPcForIndex(i), block);
Andreas Gamped881df52014-11-24 23:28:39 -0800402 }
403 }
404
405 // Fall-through. Add a block if there is more code afterwards.
406 dex_pc += instruction.SizeInCodeUnits();
407 code_ptr += instruction.SizeInCodeUnits();
408 if ((code_ptr < code_end) && (FindBlockStartingAt(dex_pc) == nullptr)) {
409 block = new (arena_) HBasicBlock(graph_, dex_pc);
410 branch_targets_.Put(dex_pc, block);
Andreas Gamped881df52014-11-24 23:28:39 -0800411 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000412 } else {
413 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle225ff812014-11-13 16:46:39 +0000414 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000415 }
416 }
417}
418
419HBasicBlock* HGraphBuilder::FindBlockStartingAt(int32_t index) const {
420 DCHECK_GE(index, 0);
421 return branch_targets_.Get(index);
422}
423
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100424template<typename T>
Roland Levillain88cb1752014-10-20 16:36:47 +0100425void HGraphBuilder::Unop_12x(const Instruction& instruction, Primitive::Type type) {
426 HInstruction* first = LoadLocal(instruction.VRegB(), type);
427 current_block_->AddInstruction(new (arena_) T(type, first));
428 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
429}
430
Roland Levillaindff1f282014-11-05 14:15:05 +0000431void HGraphBuilder::Conversion_12x(const Instruction& instruction,
432 Primitive::Type input_type,
Roland Levillain624279f2014-12-04 11:54:28 +0000433 Primitive::Type result_type,
434 uint32_t dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +0000435 HInstruction* first = LoadLocal(instruction.VRegB(), input_type);
Roland Levillain624279f2014-12-04 11:54:28 +0000436 current_block_->AddInstruction(new (arena_) HTypeConversion(result_type, first, dex_pc));
Roland Levillaindff1f282014-11-05 14:15:05 +0000437 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
438}
439
Roland Levillain88cb1752014-10-20 16:36:47 +0100440template<typename T>
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100441void HGraphBuilder::Binop_23x(const Instruction& instruction, Primitive::Type type) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100442 HInstruction* first = LoadLocal(instruction.VRegB(), type);
443 HInstruction* second = LoadLocal(instruction.VRegC(), type);
444 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100445 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
446}
447
448template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000449void HGraphBuilder::Binop_23x(const Instruction& instruction,
450 Primitive::Type type,
451 uint32_t dex_pc) {
452 HInstruction* first = LoadLocal(instruction.VRegB(), type);
453 HInstruction* second = LoadLocal(instruction.VRegC(), type);
454 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
455 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
456}
457
458template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000459void HGraphBuilder::Binop_23x_shift(const Instruction& instruction,
460 Primitive::Type type) {
461 HInstruction* first = LoadLocal(instruction.VRegB(), type);
462 HInstruction* second = LoadLocal(instruction.VRegC(), Primitive::kPrimInt);
463 current_block_->AddInstruction(new (arena_) T(type, first, second));
464 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
465}
466
Calin Juravleddb7df22014-11-25 20:56:51 +0000467void HGraphBuilder::Binop_23x_cmp(const Instruction& instruction,
468 Primitive::Type type,
469 HCompare::Bias bias) {
470 HInstruction* first = LoadLocal(instruction.VRegB(), type);
471 HInstruction* second = LoadLocal(instruction.VRegC(), type);
472 current_block_->AddInstruction(new (arena_) HCompare(type, first, second, bias));
473 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
474}
475
Calin Juravle9aec02f2014-11-18 23:06:35 +0000476template<typename T>
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100477void HGraphBuilder::Binop_12x(const Instruction& instruction, Primitive::Type type) {
478 HInstruction* first = LoadLocal(instruction.VRegA(), type);
479 HInstruction* second = LoadLocal(instruction.VRegB(), type);
480 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100481 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
482}
483
484template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000485void HGraphBuilder::Binop_12x_shift(const Instruction& instruction, Primitive::Type type) {
486 HInstruction* first = LoadLocal(instruction.VRegA(), type);
487 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
488 current_block_->AddInstruction(new (arena_) T(type, first, second));
489 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
490}
491
492template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000493void HGraphBuilder::Binop_12x(const Instruction& instruction,
494 Primitive::Type type,
495 uint32_t dex_pc) {
496 HInstruction* first = LoadLocal(instruction.VRegA(), type);
497 HInstruction* second = LoadLocal(instruction.VRegB(), type);
498 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
499 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
500}
501
502template<typename T>
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100503void HGraphBuilder::Binop_22s(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100504 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000505 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100506 if (reverse) {
507 std::swap(first, second);
508 }
509 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
510 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
511}
512
513template<typename T>
514void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100515 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000516 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100517 if (reverse) {
518 std::swap(first, second);
519 }
520 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
521 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
522}
523
Calin Juravle0c25d102015-04-20 14:49:09 +0100524static bool RequiresConstructorBarrier(const DexCompilationUnit* cu, const CompilerDriver& driver) {
525 // dex compilation unit is null only when unit testing.
526 if (cu == nullptr) {
527 return false;
528 }
529
Calin Juravle27df7582015-04-17 19:12:31 +0100530 Thread* self = Thread::Current();
Calin Juravle0c25d102015-04-20 14:49:09 +0100531 return cu->IsConstructor()
532 && driver.RequiresConstructorBarrier(self, cu->GetDexFile(), cu->GetClassDefIndex());
Calin Juravle27df7582015-04-17 19:12:31 +0100533}
534
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100535void HGraphBuilder::BuildReturn(const Instruction& instruction, Primitive::Type type) {
536 if (type == Primitive::kPrimVoid) {
Calin Juravle27df7582015-04-17 19:12:31 +0100537 // Note that we might insert redundant barriers when inlining `super` calls.
538 // TODO: add a data flow analysis to get rid of duplicate barriers.
Calin Juravle0c25d102015-04-20 14:49:09 +0100539 if (RequiresConstructorBarrier(dex_compilation_unit_, *compiler_driver_)) {
Calin Juravle27df7582015-04-17 19:12:31 +0100540 current_block_->AddInstruction(new (arena_) HMemoryBarrier(kStoreStore));
541 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100542 current_block_->AddInstruction(new (arena_) HReturnVoid());
543 } else {
544 HInstruction* value = LoadLocal(instruction.VRegA(), type);
545 current_block_->AddInstruction(new (arena_) HReturn(value));
546 }
547 current_block_->AddSuccessor(exit_block_);
548 current_block_ = nullptr;
549}
550
551bool HGraphBuilder::BuildInvoke(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000552 uint32_t dex_pc,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100553 uint32_t method_idx,
554 uint32_t number_of_vreg_arguments,
555 bool is_range,
556 uint32_t* args,
557 uint32_t register_index) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100558 Instruction::Code opcode = instruction.Opcode();
559 InvokeType invoke_type;
560 switch (opcode) {
561 case Instruction::INVOKE_STATIC:
562 case Instruction::INVOKE_STATIC_RANGE:
563 invoke_type = kStatic;
564 break;
565 case Instruction::INVOKE_DIRECT:
566 case Instruction::INVOKE_DIRECT_RANGE:
567 invoke_type = kDirect;
568 break;
569 case Instruction::INVOKE_VIRTUAL:
570 case Instruction::INVOKE_VIRTUAL_RANGE:
571 invoke_type = kVirtual;
572 break;
573 case Instruction::INVOKE_INTERFACE:
574 case Instruction::INVOKE_INTERFACE_RANGE:
575 invoke_type = kInterface;
576 break;
577 case Instruction::INVOKE_SUPER_RANGE:
578 case Instruction::INVOKE_SUPER:
579 invoke_type = kSuper;
580 break;
581 default:
582 LOG(FATAL) << "Unexpected invoke op: " << opcode;
583 return false;
584 }
585
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100586 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
587 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(method_id.proto_idx_);
588 const char* descriptor = dex_file_->StringDataByIdx(proto_id.shorty_idx_);
589 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100590 bool is_instance_call = invoke_type != kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +0100591 size_t number_of_arguments = strlen(descriptor) - (is_instance_call ? 0 : 1);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100592
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000593 MethodReference target_method(dex_file_, method_idx);
594 uintptr_t direct_code;
595 uintptr_t direct_method;
596 int table_index;
597 InvokeType optimized_invoke_type = invoke_type;
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000598
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000599 if (!compiler_driver_->ComputeInvokeInfo(dex_compilation_unit_, dex_pc, true, true,
600 &optimized_invoke_type, &target_method, &table_index,
601 &direct_code, &direct_method)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000602 VLOG(compiler) << "Did not compile " << PrettyMethod(method_idx, *dex_file_)
603 << " because a method call could not be resolved";
604 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedMethod);
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000605 return false;
606 }
607 DCHECK(optimized_invoke_type != kSuper);
608
Roland Levillain4c0eb422015-04-24 16:43:49 +0100609 // By default, consider that the called method implicitly requires
610 // an initialization check of its declaring method.
611 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement =
612 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
613 // Potential class initialization check, in the case of a static method call.
614 HClinitCheck* clinit_check = nullptr;
615
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000616 HInvoke* invoke = nullptr;
Roland Levillain4c0eb422015-04-24 16:43:49 +0100617
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000618 if (optimized_invoke_type == kVirtual) {
619 invoke = new (arena_) HInvokeVirtual(
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800620 arena_, number_of_arguments, return_type, dex_pc, method_idx, table_index);
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000621 } else if (optimized_invoke_type == kInterface) {
622 invoke = new (arena_) HInvokeInterface(
623 arena_, number_of_arguments, return_type, dex_pc, method_idx, table_index);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100624 } else {
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000625 DCHECK(optimized_invoke_type == kDirect || optimized_invoke_type == kStatic);
626 // Sharpening to kDirect only works if we compile PIC.
627 DCHECK((optimized_invoke_type == invoke_type) || (optimized_invoke_type != kDirect)
628 || compiler_driver_->GetCompilerOptions().GetCompilePic());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000629 bool is_recursive =
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000630 (target_method.dex_method_index == dex_compilation_unit_->GetDexMethodIndex());
631 DCHECK(!is_recursive || (target_method.dex_file == dex_compilation_unit_->GetDexFile()));
Roland Levillain4c0eb422015-04-24 16:43:49 +0100632
633 if (optimized_invoke_type == kStatic) {
634 ScopedObjectAccess soa(Thread::Current());
635 StackHandleScope<4> hs(soa.Self());
636 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
637 dex_compilation_unit_->GetClassLinker()->FindDexCache(
638 *dex_compilation_unit_->GetDexFile())));
639 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
640 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
641 mirror::ArtMethod* resolved_method = compiler_driver_->ResolveMethod(
642 soa, dex_cache, class_loader, dex_compilation_unit_, method_idx,
643 optimized_invoke_type);
644
645 if (resolved_method == nullptr) {
646 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedMethod);
647 return false;
648 }
649
650 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
651 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
652 outer_compilation_unit_->GetClassLinker()->FindDexCache(outer_dex_file)));
653 Handle<mirror::Class> referrer_class(hs.NewHandle(GetOutermostCompilingClass()));
654
655 // The index at which the method's class is stored in the DexCache's type array.
656 uint32_t storage_index = DexFile::kDexNoIndex;
657 bool is_referrer_class = (resolved_method->GetDeclaringClass() == referrer_class.Get());
658 if (is_referrer_class) {
659 storage_index = referrer_class->GetDexTypeIndex();
660 } else if (outer_dex_cache.Get() == dex_cache.Get()) {
661 // Get `storage_index` from IsClassOfStaticMethodAvailableToReferrer.
662 compiler_driver_->IsClassOfStaticMethodAvailableToReferrer(outer_dex_cache.Get(),
663 referrer_class.Get(),
664 resolved_method,
665 method_idx,
666 &storage_index);
667 }
668
669 if (is_referrer_class) {
670 // If the declaring class is the referrer class, no class
671 // initialization is needed before the static method call.
672 clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
673 } else if (storage_index != DexFile::kDexNoIndex) {
674 // If the method's class type index is available, check
675 // whether we should add an explicit class initialization
676 // check for its declaring class before the static method call.
677
678 // TODO: find out why this check is needed.
679 bool is_in_dex_cache = compiler_driver_->CanAssumeTypeIsPresentInDexCache(
680 *outer_compilation_unit_->GetDexFile(), storage_index);
681 bool is_initialized =
682 resolved_method->GetDeclaringClass()->IsInitialized() && is_in_dex_cache;
683
684 if (is_initialized) {
685 clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
686 } else {
687 clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
688 HLoadClass* load_class =
689 new (arena_) HLoadClass(storage_index, is_referrer_class, dex_pc);
690 current_block_->AddInstruction(load_class);
691 clinit_check = new (arena_) HClinitCheck(load_class, dex_pc);
692 current_block_->AddInstruction(clinit_check);
693 ++number_of_arguments;
694 }
695 }
696 }
697
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000698 invoke = new (arena_) HInvokeStaticOrDirect(
699 arena_, number_of_arguments, return_type, dex_pc, target_method.dex_method_index,
Roland Levillain4c0eb422015-04-24 16:43:49 +0100700 is_recursive, invoke_type, optimized_invoke_type, clinit_check_requirement);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100701 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100702
703 size_t start_index = 0;
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000704 Temporaries temps(graph_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100705 if (is_instance_call) {
706 HInstruction* arg = LoadLocal(is_range ? register_index : args[0], Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000707 HNullCheck* null_check = new (arena_) HNullCheck(arg, dex_pc);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100708 current_block_->AddInstruction(null_check);
709 temps.Add(null_check);
710 invoke->SetArgumentAt(0, null_check);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100711 start_index = 1;
712 }
713
714 uint32_t descriptor_index = 1;
715 uint32_t argument_index = start_index;
716 for (size_t i = start_index; i < number_of_vreg_arguments; i++, argument_index++) {
717 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100718 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
719 if (!is_range && is_wide && args[i] + 1 != args[i + 1]) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100720 LOG(WARNING) << "Non sequential register pair in " << dex_compilation_unit_->GetSymbol()
Calin Juravle225ff812014-11-13 16:46:39 +0000721 << " at " << dex_pc;
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100722 // We do not implement non sequential register pair.
Calin Juravle48c2b032014-12-09 18:11:36 +0000723 MaybeRecordStat(MethodCompilationStat::kNotCompiledNonSequentialRegPair);
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100724 return false;
725 }
726 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
727 invoke->SetArgumentAt(argument_index, arg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100728 if (is_wide) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100729 i++;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100730 }
731 }
732
Roland Levillain4c0eb422015-04-24 16:43:49 +0100733 if (clinit_check_requirement == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit) {
734 // Add the class initialization check as last input of `invoke`.
735 DCHECK(clinit_check != nullptr);
736 invoke->SetArgumentAt(argument_index++, clinit_check);
737 }
738
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100739 DCHECK_EQ(argument_index, number_of_arguments);
740 current_block_->AddInstruction(invoke);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100741 latest_result_ = invoke;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100742 return true;
743}
744
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100745bool HGraphBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000746 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100747 bool is_put) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100748 uint32_t source_or_dest_reg = instruction.VRegA_22c();
749 uint32_t obj_reg = instruction.VRegB_22c();
750 uint16_t field_index = instruction.VRegC_22c();
751
752 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierc7853442015-03-27 14:35:38 -0700753 ArtField* resolved_field =
754 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100755
Mathieu Chartierc7853442015-03-27 14:35:38 -0700756 if (resolved_field == nullptr) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000757 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedField);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100758 return false;
759 }
Calin Juravle52c48962014-12-16 17:02:57 +0000760
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100761 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100762
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100763 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000764 current_block_->AddInstruction(new (arena_) HNullCheck(object, dex_pc));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100765 if (is_put) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000766 Temporaries temps(graph_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100767 HInstruction* null_check = current_block_->GetLastInstruction();
768 // We need one temporary for the null check.
769 temps.Add(null_check);
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100770 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100771 current_block_->AddInstruction(new (arena_) HInstanceFieldSet(
772 null_check,
773 value,
Nicolas Geoffray39468442014-09-02 15:17:15 +0100774 field_type,
Calin Juravle52c48962014-12-16 17:02:57 +0000775 resolved_field->GetOffset(),
776 resolved_field->IsVolatile()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100777 } else {
778 current_block_->AddInstruction(new (arena_) HInstanceFieldGet(
779 current_block_->GetLastInstruction(),
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100780 field_type,
Calin Juravle52c48962014-12-16 17:02:57 +0000781 resolved_field->GetOffset(),
782 resolved_field->IsVolatile()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100783
784 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
785 }
786 return true;
787}
788
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000789mirror::Class* HGraphBuilder::GetOutermostCompilingClass() const {
790 ScopedObjectAccess soa(Thread::Current());
791 StackHandleScope<2> hs(soa.Self());
792 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
793 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
794 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
795 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
796 outer_compilation_unit_->GetClassLinker()->FindDexCache(outer_dex_file)));
797
798 return compiler_driver_->ResolveCompilingMethodsClass(
799 soa, outer_dex_cache, class_loader, outer_compilation_unit_);
800}
801
802bool HGraphBuilder::IsOutermostCompilingClass(uint16_t type_index) const {
803 ScopedObjectAccess soa(Thread::Current());
804 StackHandleScope<4> hs(soa.Self());
805 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
806 dex_compilation_unit_->GetClassLinker()->FindDexCache(*dex_compilation_unit_->GetDexFile())));
807 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
808 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
809 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
810 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
811 Handle<mirror::Class> compiling_class(hs.NewHandle(GetOutermostCompilingClass()));
812
813 return compiling_class.Get() == cls.Get();
814}
815
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100816bool HGraphBuilder::BuildStaticFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000817 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100818 bool is_put) {
819 uint32_t source_or_dest_reg = instruction.VRegA_21c();
820 uint16_t field_index = instruction.VRegB_21c();
821
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000822 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierc7853442015-03-27 14:35:38 -0700823 StackHandleScope<4> hs(soa.Self());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000824 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
825 dex_compilation_unit_->GetClassLinker()->FindDexCache(*dex_compilation_unit_->GetDexFile())));
826 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
827 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
Mathieu Chartierc7853442015-03-27 14:35:38 -0700828 ArtField* resolved_field = compiler_driver_->ResolveField(
829 soa, dex_cache, class_loader, dex_compilation_unit_, field_index, true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100830
Mathieu Chartierc7853442015-03-27 14:35:38 -0700831 if (resolved_field == nullptr) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000832 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedField);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100833 return false;
834 }
835
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000836 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
837 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
838 outer_compilation_unit_->GetClassLinker()->FindDexCache(outer_dex_file)));
839 Handle<mirror::Class> referrer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000840
841 // The index at which the field's class is stored in the DexCache's type array.
842 uint32_t storage_index;
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000843 bool is_referrer_class = (referrer_class.Get() == resolved_field->GetDeclaringClass());
844 if (is_referrer_class) {
845 storage_index = referrer_class->GetDexTypeIndex();
846 } else if (outer_dex_cache.Get() != dex_cache.Get()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +0100847 // The compiler driver cannot currently understand multiple dex caches involved. Just bailout.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000848 return false;
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000849 } else {
850 std::pair<bool, bool> pair = compiler_driver_->IsFastStaticField(
851 outer_dex_cache.Get(),
852 referrer_class.Get(),
Mathieu Chartierc7853442015-03-27 14:35:38 -0700853 resolved_field,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000854 field_index,
855 &storage_index);
856 bool can_easily_access = is_put ? pair.second : pair.first;
857 if (!can_easily_access) {
858 return false;
859 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000860 }
861
862 // TODO: find out why this check is needed.
863 bool is_in_dex_cache = compiler_driver_->CanAssumeTypeIsPresentInDexCache(
Nicolas Geoffray6a816cf2015-03-24 16:17:56 +0000864 *outer_compilation_unit_->GetDexFile(), storage_index);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000865 bool is_initialized = resolved_field->GetDeclaringClass()->IsInitialized() && is_in_dex_cache;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000866
867 HLoadClass* constant = new (arena_) HLoadClass(storage_index, is_referrer_class, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100868 current_block_->AddInstruction(constant);
869
870 HInstruction* cls = constant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000871 if (!is_initialized && !is_referrer_class) {
Calin Juravle225ff812014-11-13 16:46:39 +0000872 cls = new (arena_) HClinitCheck(constant, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100873 current_block_->AddInstruction(cls);
874 }
875
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000876 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100877 if (is_put) {
878 // We need to keep the class alive before loading the value.
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000879 Temporaries temps(graph_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100880 temps.Add(cls);
881 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
882 DCHECK_EQ(value->GetType(), field_type);
883 current_block_->AddInstruction(
Calin Juravle52c48962014-12-16 17:02:57 +0000884 new (arena_) HStaticFieldSet(cls, value, field_type, resolved_field->GetOffset(),
885 resolved_field->IsVolatile()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100886 } else {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000887 current_block_->AddInstruction(
Calin Juravle52c48962014-12-16 17:02:57 +0000888 new (arena_) HStaticFieldGet(cls, field_type, resolved_field->GetOffset(),
889 resolved_field->IsVolatile()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100890 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
891 }
892 return true;
893}
894
Calin Juravlebacfec32014-11-14 15:54:36 +0000895void HGraphBuilder::BuildCheckedDivRem(uint16_t out_vreg,
896 uint16_t first_vreg,
897 int64_t second_vreg_or_constant,
898 uint32_t dex_pc,
899 Primitive::Type type,
900 bool second_is_constant,
901 bool isDiv) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000902 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Calin Juravled0d48522014-11-04 16:40:20 +0000903
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000904 HInstruction* first = LoadLocal(first_vreg, type);
905 HInstruction* second = nullptr;
906 if (second_is_constant) {
907 if (type == Primitive::kPrimInt) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000908 second = graph_->GetIntConstant(second_vreg_or_constant);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000909 } else {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000910 second = graph_->GetLongConstant(second_vreg_or_constant);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000911 }
912 } else {
913 second = LoadLocal(second_vreg_or_constant, type);
914 }
915
916 if (!second_is_constant
917 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0)
918 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) {
919 second = new (arena_) HDivZeroCheck(second, dex_pc);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000920 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +0000921 current_block_->AddInstruction(second);
922 temps.Add(current_block_->GetLastInstruction());
923 }
924
Calin Juravlebacfec32014-11-14 15:54:36 +0000925 if (isDiv) {
926 current_block_->AddInstruction(new (arena_) HDiv(type, first, second, dex_pc));
927 } else {
928 current_block_->AddInstruction(new (arena_) HRem(type, first, second, dex_pc));
929 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000930 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
Calin Juravled0d48522014-11-04 16:40:20 +0000931}
932
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100933void HGraphBuilder::BuildArrayAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000934 uint32_t dex_pc,
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100935 bool is_put,
936 Primitive::Type anticipated_type) {
937 uint8_t source_or_dest_reg = instruction.VRegA_23x();
938 uint8_t array_reg = instruction.VRegB_23x();
939 uint8_t index_reg = instruction.VRegC_23x();
940
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100941 // We need one temporary for the null check, one for the index, and one for the length.
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000942 Temporaries temps(graph_);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100943
944 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000945 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100946 current_block_->AddInstruction(object);
947 temps.Add(object);
948
949 HInstruction* length = new (arena_) HArrayLength(object);
950 current_block_->AddInstruction(length);
951 temps.Add(length);
952 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt);
Calin Juravle225ff812014-11-13 16:46:39 +0000953 index = new (arena_) HBoundsCheck(index, length, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100954 current_block_->AddInstruction(index);
955 temps.Add(index);
956 if (is_put) {
957 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
958 // TODO: Insert a type check node if the type is Object.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100959 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +0000960 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100961 } else {
962 current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type));
963 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
964 }
Mingyao Yange4335eb2015-03-02 15:14:13 -0800965 graph_->SetHasArrayAccesses(true);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100966}
967
Calin Juravle225ff812014-11-13 16:46:39 +0000968void HGraphBuilder::BuildFilledNewArray(uint32_t dex_pc,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100969 uint32_t type_index,
970 uint32_t number_of_vreg_arguments,
971 bool is_range,
972 uint32_t* args,
973 uint32_t register_index) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000974 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments);
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +0000975 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
976 ? kQuickAllocArrayWithAccessCheck
977 : kQuickAllocArray;
978 HInstruction* object = new (arena_) HNewArray(length, dex_pc, type_index, entrypoint);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100979 current_block_->AddInstruction(object);
980
981 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
982 DCHECK_EQ(descriptor[0], '[') << descriptor;
983 char primitive = descriptor[1];
984 DCHECK(primitive == 'I'
985 || primitive == 'L'
986 || primitive == '[') << descriptor;
987 bool is_reference_array = (primitive == 'L') || (primitive == '[');
988 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
989
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000990 Temporaries temps(graph_);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100991 temps.Add(object);
992 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
993 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000994 HInstruction* index = graph_->GetIntConstant(i);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100995 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +0000996 new (arena_) HArraySet(object, index, value, type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100997 }
998 latest_result_ = object;
999}
1000
1001template <typename T>
1002void HGraphBuilder::BuildFillArrayData(HInstruction* object,
1003 const T* data,
1004 uint32_t element_count,
1005 Primitive::Type anticipated_type,
Calin Juravle225ff812014-11-13 16:46:39 +00001006 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001007 for (uint32_t i = 0; i < element_count; ++i) {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001008 HInstruction* index = graph_->GetIntConstant(i);
1009 HInstruction* value = graph_->GetIntConstant(data[i]);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001010 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001011 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001012 }
1013}
1014
Calin Juravle225ff812014-11-13 16:46:39 +00001015void HGraphBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001016 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +00001017 HInstruction* array = LoadLocal(instruction.VRegA_31t(), Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00001018 HNullCheck* null_check = new (arena_) HNullCheck(array, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001019 current_block_->AddInstruction(null_check);
1020 temps.Add(null_check);
1021
1022 HInstruction* length = new (arena_) HArrayLength(null_check);
1023 current_block_->AddInstruction(length);
1024
Calin Juravle225ff812014-11-13 16:46:39 +00001025 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
Calin Juravled0d48522014-11-04 16:40:20 +00001026 const Instruction::ArrayDataPayload* payload =
1027 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_start_ + payload_offset);
1028 const uint8_t* data = payload->data;
1029 uint32_t element_count = payload->element_count;
1030
1031 // Implementation of this DEX instruction seems to be that the bounds check is
1032 // done before doing any stores.
David Brazdil8d5b8b22015-03-24 10:51:52 +00001033 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1);
Calin Juravle225ff812014-11-13 16:46:39 +00001034 current_block_->AddInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
Calin Juravled0d48522014-11-04 16:40:20 +00001035
1036 switch (payload->element_width) {
1037 case 1:
1038 BuildFillArrayData(null_check,
1039 reinterpret_cast<const int8_t*>(data),
1040 element_count,
1041 Primitive::kPrimByte,
Calin Juravle225ff812014-11-13 16:46:39 +00001042 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001043 break;
1044 case 2:
1045 BuildFillArrayData(null_check,
1046 reinterpret_cast<const int16_t*>(data),
1047 element_count,
1048 Primitive::kPrimShort,
Calin Juravle225ff812014-11-13 16:46:39 +00001049 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001050 break;
1051 case 4:
1052 BuildFillArrayData(null_check,
1053 reinterpret_cast<const int32_t*>(data),
1054 element_count,
1055 Primitive::kPrimInt,
Calin Juravle225ff812014-11-13 16:46:39 +00001056 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001057 break;
1058 case 8:
1059 BuildFillWideArrayData(null_check,
1060 reinterpret_cast<const int64_t*>(data),
1061 element_count,
Calin Juravle225ff812014-11-13 16:46:39 +00001062 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +00001063 break;
1064 default:
1065 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1066 }
1067}
1068
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001069void HGraphBuilder::BuildFillWideArrayData(HInstruction* object,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +01001070 const int64_t* data,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001071 uint32_t element_count,
Calin Juravle225ff812014-11-13 16:46:39 +00001072 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001073 for (uint32_t i = 0; i < element_count; ++i) {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001074 HInstruction* index = graph_->GetIntConstant(i);
1075 HInstruction* value = graph_->GetLongConstant(data[i]);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001076 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +00001077 object, index, value, Primitive::kPrimLong, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001078 }
1079}
1080
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001081bool HGraphBuilder::BuildTypeCheck(const Instruction& instruction,
1082 uint8_t destination,
1083 uint8_t reference,
1084 uint16_t type_index,
Calin Juravle225ff812014-11-13 16:46:39 +00001085 uint32_t dex_pc) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001086 bool type_known_final;
1087 bool type_known_abstract;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001088 // `CanAccessTypeWithoutChecks` will tell whether the method being
1089 // built is trying to access its own class, so that the generated
1090 // code can optimize for this case. However, the optimization does not
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001091 // work for inlining, so we use `IsOutermostCompilingClass` instead.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001092 bool dont_use_is_referrers_class;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001093 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
1094 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001095 &type_known_final, &type_known_abstract, &dont_use_is_referrers_class);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001096 if (!can_access) {
Calin Juravle48c2b032014-12-09 18:11:36 +00001097 MaybeRecordStat(MethodCompilationStat::kNotCompiledCantAccesType);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001098 return false;
1099 }
1100 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot);
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001101 HLoadClass* cls = new (arena_) HLoadClass(
1102 type_index, IsOutermostCompilingClass(type_index), dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001103 current_block_->AddInstruction(cls);
1104 // The class needs a temporary before being used by the type check.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001105 Temporaries temps(graph_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001106 temps.Add(cls);
1107 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
1108 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001109 new (arena_) HInstanceOf(object, cls, type_known_final, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001110 UpdateLocal(destination, current_block_->GetLastInstruction());
1111 } else {
1112 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
1113 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001114 new (arena_) HCheckCast(object, cls, type_known_final, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001115 }
1116 return true;
1117}
1118
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001119bool HGraphBuilder::NeedsAccessCheck(uint32_t type_index) const {
1120 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
1121 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index);
1122}
1123
Calin Juravle48c2b032014-12-09 18:11:36 +00001124void HGraphBuilder::BuildPackedSwitch(const Instruction& instruction, uint32_t dex_pc) {
Andreas Gamped881df52014-11-24 23:28:39 -08001125 SwitchTable table(instruction, dex_pc, false);
1126
1127 // Value to test against.
1128 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
1129
Andreas Gampee4d4d322014-12-04 09:09:57 -08001130 uint16_t num_entries = table.GetNumEntries();
1131 // There should be at least one entry here.
1132 DCHECK_GT(num_entries, 0U);
1133
Andreas Gamped881df52014-11-24 23:28:39 -08001134 // Chained cmp-and-branch, starting from starting_key.
1135 int32_t starting_key = table.GetEntryAt(0);
1136
Andreas Gamped881df52014-11-24 23:28:39 -08001137 for (size_t i = 1; i <= num_entries; i++) {
Andreas Gampee4d4d322014-12-04 09:09:57 -08001138 BuildSwitchCaseHelper(instruction, i, i == num_entries, table, value, starting_key + i - 1,
1139 table.GetEntryAt(i), dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08001140 }
Andreas Gamped881df52014-11-24 23:28:39 -08001141}
1142
Calin Juravle48c2b032014-12-09 18:11:36 +00001143void HGraphBuilder::BuildSparseSwitch(const Instruction& instruction, uint32_t dex_pc) {
Andreas Gampee4d4d322014-12-04 09:09:57 -08001144 SwitchTable table(instruction, dex_pc, true);
1145
1146 // Value to test against.
1147 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
1148
1149 uint16_t num_entries = table.GetNumEntries();
Andreas Gampee4d4d322014-12-04 09:09:57 -08001150
1151 for (size_t i = 0; i < num_entries; i++) {
1152 BuildSwitchCaseHelper(instruction, i, i == static_cast<size_t>(num_entries) - 1, table, value,
1153 table.GetEntryAt(i), table.GetEntryAt(i + num_entries), dex_pc);
1154 }
Andreas Gampee4d4d322014-12-04 09:09:57 -08001155}
1156
1157void HGraphBuilder::BuildSwitchCaseHelper(const Instruction& instruction, size_t index,
1158 bool is_last_case, const SwitchTable& table,
1159 HInstruction* value, int32_t case_value_int,
1160 int32_t target_offset, uint32_t dex_pc) {
David Brazdil852eaff2015-02-02 15:23:05 +00001161 HBasicBlock* case_target = FindBlockStartingAt(dex_pc + target_offset);
1162 DCHECK(case_target != nullptr);
1163 PotentiallyAddSuspendCheck(case_target, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001164
1165 // The current case's value.
David Brazdil8d5b8b22015-03-24 10:51:52 +00001166 HInstruction* this_case_value = graph_->GetIntConstant(case_value_int);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001167
1168 // Compare value and this_case_value.
1169 HEqual* comparison = new (arena_) HEqual(value, this_case_value);
1170 current_block_->AddInstruction(comparison);
1171 HInstruction* ifinst = new (arena_) HIf(comparison);
1172 current_block_->AddInstruction(ifinst);
1173
1174 // Case hit: use the target offset to determine where to go.
Andreas Gampee4d4d322014-12-04 09:09:57 -08001175 current_block_->AddSuccessor(case_target);
1176
1177 // Case miss: go to the next case (or default fall-through).
1178 // When there is a next case, we use the block stored with the table offset representing this
1179 // case (that is where we registered them in ComputeBranchTargets).
1180 // When there is no next case, we use the following instruction.
1181 // TODO: Find a good way to peel the last iteration to avoid conditional, but still have re-use.
1182 if (!is_last_case) {
1183 HBasicBlock* next_case_target = FindBlockStartingAt(table.GetDexPcForIndex(index));
1184 DCHECK(next_case_target != nullptr);
1185 current_block_->AddSuccessor(next_case_target);
1186
1187 // Need to manually add the block, as there is no dex-pc transition for the cases.
1188 graph_->AddBlock(next_case_target);
1189
1190 current_block_ = next_case_target;
1191 } else {
1192 HBasicBlock* default_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
1193 DCHECK(default_target != nullptr);
1194 current_block_->AddSuccessor(default_target);
1195 current_block_ = nullptr;
1196 }
1197}
1198
David Brazdil852eaff2015-02-02 15:23:05 +00001199void HGraphBuilder::PotentiallyAddSuspendCheck(HBasicBlock* target, uint32_t dex_pc) {
1200 int32_t target_offset = target->GetDexPc() - dex_pc;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001201 if (target_offset <= 0) {
David Brazdil852eaff2015-02-02 15:23:05 +00001202 // DX generates back edges to the first encountered return. We can save
1203 // time of later passes by not adding redundant suspend checks.
David Brazdil2fd6aa52015-02-02 18:58:27 +00001204 HInstruction* last_in_target = target->GetLastInstruction();
1205 if (last_in_target != nullptr &&
1206 (last_in_target->IsReturn() || last_in_target->IsReturnVoid())) {
1207 return;
David Brazdil852eaff2015-02-02 15:23:05 +00001208 }
1209
1210 // Add a suspend check to backward branches which may potentially loop. We
1211 // can remove them after we recognize loops in the graph.
Calin Juravle225ff812014-11-13 16:46:39 +00001212 current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_pc));
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001213 }
1214}
1215
Calin Juravle225ff812014-11-13 16:46:39 +00001216bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001217 if (current_block_ == nullptr) {
1218 return true; // Dead code
1219 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001220
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001221 switch (instruction.Opcode()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001222 case Instruction::CONST_4: {
1223 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001224 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001225 UpdateLocal(register_index, constant);
1226 break;
1227 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001228
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001229 case Instruction::CONST_16: {
1230 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001231 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001232 UpdateLocal(register_index, constant);
1233 break;
1234 }
1235
Dave Allison20dfc792014-06-16 20:44:29 -07001236 case Instruction::CONST: {
1237 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001238 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i());
Dave Allison20dfc792014-06-16 20:44:29 -07001239 UpdateLocal(register_index, constant);
1240 break;
1241 }
1242
1243 case Instruction::CONST_HIGH16: {
1244 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001245 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16);
Dave Allison20dfc792014-06-16 20:44:29 -07001246 UpdateLocal(register_index, constant);
1247 break;
1248 }
1249
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001250 case Instruction::CONST_WIDE_16: {
1251 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001252 // Get 16 bits of constant value, sign extended to 64 bits.
1253 int64_t value = instruction.VRegB_21s();
1254 value <<= 48;
1255 value >>= 48;
David Brazdil8d5b8b22015-03-24 10:51:52 +00001256 HLongConstant* constant = graph_->GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001257 UpdateLocal(register_index, constant);
1258 break;
1259 }
1260
1261 case Instruction::CONST_WIDE_32: {
1262 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001263 // Get 32 bits of constant value, sign extended to 64 bits.
1264 int64_t value = instruction.VRegB_31i();
1265 value <<= 32;
1266 value >>= 32;
David Brazdil8d5b8b22015-03-24 10:51:52 +00001267 HLongConstant* constant = graph_->GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001268 UpdateLocal(register_index, constant);
1269 break;
1270 }
1271
1272 case Instruction::CONST_WIDE: {
1273 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001274 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001275 UpdateLocal(register_index, constant);
1276 break;
1277 }
1278
Dave Allison20dfc792014-06-16 20:44:29 -07001279 case Instruction::CONST_WIDE_HIGH16: {
1280 int32_t register_index = instruction.VRegA();
1281 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
David Brazdil8d5b8b22015-03-24 10:51:52 +00001282 HLongConstant* constant = graph_->GetLongConstant(value);
Dave Allison20dfc792014-06-16 20:44:29 -07001283 UpdateLocal(register_index, constant);
1284 break;
1285 }
1286
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001287 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001288 case Instruction::MOVE:
1289 case Instruction::MOVE_FROM16:
1290 case Instruction::MOVE_16: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001291 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001292 UpdateLocal(instruction.VRegA(), value);
1293 break;
1294 }
1295
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001296 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001297 case Instruction::MOVE_WIDE:
1298 case Instruction::MOVE_WIDE_FROM16:
1299 case Instruction::MOVE_WIDE_16: {
1300 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong);
1301 UpdateLocal(instruction.VRegA(), value);
1302 break;
1303 }
1304
1305 case Instruction::MOVE_OBJECT:
1306 case Instruction::MOVE_OBJECT_16:
1307 case Instruction::MOVE_OBJECT_FROM16: {
1308 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot);
1309 UpdateLocal(instruction.VRegA(), value);
1310 break;
1311 }
1312
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001313 case Instruction::RETURN_VOID: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001314 BuildReturn(instruction, Primitive::kPrimVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001315 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001316 }
1317
Dave Allison20dfc792014-06-16 20:44:29 -07001318#define IF_XX(comparison, cond) \
Calin Juravle225ff812014-11-13 16:46:39 +00001319 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
1320 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001321
Dave Allison20dfc792014-06-16 20:44:29 -07001322 IF_XX(HEqual, EQ);
1323 IF_XX(HNotEqual, NE);
1324 IF_XX(HLessThan, LT);
1325 IF_XX(HLessThanOrEqual, LE);
1326 IF_XX(HGreaterThan, GT);
1327 IF_XX(HGreaterThanOrEqual, GE);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001328
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001329 case Instruction::GOTO:
1330 case Instruction::GOTO_16:
1331 case Instruction::GOTO_32: {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001332 int32_t offset = instruction.GetTargetOffset();
Calin Juravle225ff812014-11-13 16:46:39 +00001333 HBasicBlock* target = FindBlockStartingAt(offset + dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001334 DCHECK(target != nullptr);
David Brazdil852eaff2015-02-02 15:23:05 +00001335 PotentiallyAddSuspendCheck(target, dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001336 current_block_->AddInstruction(new (arena_) HGoto());
1337 current_block_->AddSuccessor(target);
1338 current_block_ = nullptr;
1339 break;
1340 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001341
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001342 case Instruction::RETURN: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001343 DCHECK_NE(return_type_, Primitive::kPrimNot);
1344 DCHECK_NE(return_type_, Primitive::kPrimLong);
1345 DCHECK_NE(return_type_, Primitive::kPrimDouble);
1346 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001347 break;
1348 }
1349
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001350 case Instruction::RETURN_OBJECT: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001351 DCHECK(return_type_ == Primitive::kPrimNot);
1352 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001353 break;
1354 }
1355
1356 case Instruction::RETURN_WIDE: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001357 DCHECK(return_type_ == Primitive::kPrimDouble || return_type_ == Primitive::kPrimLong);
1358 BuildReturn(instruction, return_type_);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001359 break;
1360 }
1361
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001362 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001363 case Instruction::INVOKE_INTERFACE:
1364 case Instruction::INVOKE_STATIC:
1365 case Instruction::INVOKE_SUPER:
1366 case Instruction::INVOKE_VIRTUAL: {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001367 uint32_t method_idx = instruction.VRegB_35c();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001368 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001369 uint32_t args[5];
Ian Rogers29a26482014-05-02 15:27:29 -07001370 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00001371 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001372 number_of_vreg_arguments, false, args, -1)) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001373 return false;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001374 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001375 break;
1376 }
1377
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001378 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001379 case Instruction::INVOKE_INTERFACE_RANGE:
1380 case Instruction::INVOKE_STATIC_RANGE:
1381 case Instruction::INVOKE_SUPER_RANGE:
1382 case Instruction::INVOKE_VIRTUAL_RANGE: {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001383 uint32_t method_idx = instruction.VRegB_3rc();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001384 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1385 uint32_t register_index = instruction.VRegC();
Calin Juravle225ff812014-11-13 16:46:39 +00001386 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001387 number_of_vreg_arguments, true, nullptr, register_index)) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001388 return false;
1389 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001390 break;
1391 }
1392
Roland Levillain88cb1752014-10-20 16:36:47 +01001393 case Instruction::NEG_INT: {
1394 Unop_12x<HNeg>(instruction, Primitive::kPrimInt);
1395 break;
1396 }
1397
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001398 case Instruction::NEG_LONG: {
1399 Unop_12x<HNeg>(instruction, Primitive::kPrimLong);
1400 break;
1401 }
1402
Roland Levillain3dbcb382014-10-28 17:30:07 +00001403 case Instruction::NEG_FLOAT: {
1404 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat);
1405 break;
1406 }
1407
1408 case Instruction::NEG_DOUBLE: {
1409 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble);
1410 break;
1411 }
1412
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001413 case Instruction::NOT_INT: {
1414 Unop_12x<HNot>(instruction, Primitive::kPrimInt);
1415 break;
1416 }
1417
Roland Levillain70566432014-10-24 16:20:17 +01001418 case Instruction::NOT_LONG: {
1419 Unop_12x<HNot>(instruction, Primitive::kPrimLong);
1420 break;
1421 }
1422
Roland Levillaindff1f282014-11-05 14:15:05 +00001423 case Instruction::INT_TO_LONG: {
Roland Levillain624279f2014-12-04 11:54:28 +00001424 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong, dex_pc);
Roland Levillaindff1f282014-11-05 14:15:05 +00001425 break;
1426 }
1427
Roland Levillaincff13742014-11-17 14:32:17 +00001428 case Instruction::INT_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001429 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00001430 break;
1431 }
1432
1433 case Instruction::INT_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001434 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00001435 break;
1436 }
1437
Roland Levillain946e1432014-11-11 17:35:19 +00001438 case Instruction::LONG_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001439 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt, dex_pc);
Roland Levillain946e1432014-11-11 17:35:19 +00001440 break;
1441 }
1442
Roland Levillain6d0e4832014-11-27 18:31:21 +00001443 case Instruction::LONG_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001444 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimFloat, dex_pc);
Roland Levillain6d0e4832014-11-27 18:31:21 +00001445 break;
1446 }
1447
Roland Levillain647b9ed2014-11-27 12:06:00 +00001448 case Instruction::LONG_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001449 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble, dex_pc);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001450 break;
1451 }
1452
Roland Levillain3f8f9362014-12-02 17:45:01 +00001453 case Instruction::FLOAT_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001454 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimInt, dex_pc);
1455 break;
1456 }
1457
1458 case Instruction::FLOAT_TO_LONG: {
1459 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimLong, dex_pc);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001460 break;
1461 }
1462
Roland Levillain8964e2b2014-12-04 12:10:50 +00001463 case Instruction::FLOAT_TO_DOUBLE: {
1464 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimDouble, dex_pc);
1465 break;
1466 }
1467
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001468 case Instruction::DOUBLE_TO_INT: {
1469 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimInt, dex_pc);
1470 break;
1471 }
1472
1473 case Instruction::DOUBLE_TO_LONG: {
1474 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimLong, dex_pc);
1475 break;
1476 }
1477
Roland Levillain8964e2b2014-12-04 12:10:50 +00001478 case Instruction::DOUBLE_TO_FLOAT: {
1479 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimFloat, dex_pc);
1480 break;
1481 }
1482
Roland Levillain51d3fc42014-11-13 14:11:42 +00001483 case Instruction::INT_TO_BYTE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001484 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte, dex_pc);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001485 break;
1486 }
1487
Roland Levillain01a8d712014-11-14 16:27:39 +00001488 case Instruction::INT_TO_SHORT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001489 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort, dex_pc);
Roland Levillain01a8d712014-11-14 16:27:39 +00001490 break;
1491 }
1492
Roland Levillain981e4542014-11-14 11:47:14 +00001493 case Instruction::INT_TO_CHAR: {
Roland Levillain624279f2014-12-04 11:54:28 +00001494 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar, dex_pc);
Roland Levillain981e4542014-11-14 11:47:14 +00001495 break;
1496 }
1497
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001498 case Instruction::ADD_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001499 Binop_23x<HAdd>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001500 break;
1501 }
1502
1503 case Instruction::ADD_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001504 Binop_23x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001505 break;
1506 }
1507
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001508 case Instruction::ADD_DOUBLE: {
1509 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble);
1510 break;
1511 }
1512
1513 case Instruction::ADD_FLOAT: {
1514 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat);
1515 break;
1516 }
1517
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001518 case Instruction::SUB_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001519 Binop_23x<HSub>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001520 break;
1521 }
1522
1523 case Instruction::SUB_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001524 Binop_23x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001525 break;
1526 }
1527
Calin Juravle096cc022014-10-23 17:01:13 +01001528 case Instruction::SUB_FLOAT: {
1529 Binop_23x<HSub>(instruction, Primitive::kPrimFloat);
1530 break;
1531 }
1532
1533 case Instruction::SUB_DOUBLE: {
1534 Binop_23x<HSub>(instruction, Primitive::kPrimDouble);
1535 break;
1536 }
1537
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001538 case Instruction::ADD_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001539 Binop_12x<HAdd>(instruction, Primitive::kPrimInt);
1540 break;
1541 }
1542
Calin Juravle34bacdf2014-10-07 20:23:36 +01001543 case Instruction::MUL_INT: {
1544 Binop_23x<HMul>(instruction, Primitive::kPrimInt);
1545 break;
1546 }
1547
1548 case Instruction::MUL_LONG: {
1549 Binop_23x<HMul>(instruction, Primitive::kPrimLong);
1550 break;
1551 }
1552
Calin Juravleb5bfa962014-10-21 18:02:24 +01001553 case Instruction::MUL_FLOAT: {
1554 Binop_23x<HMul>(instruction, Primitive::kPrimFloat);
1555 break;
1556 }
1557
1558 case Instruction::MUL_DOUBLE: {
1559 Binop_23x<HMul>(instruction, Primitive::kPrimDouble);
1560 break;
1561 }
1562
Calin Juravled0d48522014-11-04 16:40:20 +00001563 case Instruction::DIV_INT: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001564 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1565 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravled0d48522014-11-04 16:40:20 +00001566 break;
1567 }
1568
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001569 case Instruction::DIV_LONG: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001570 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1571 dex_pc, Primitive::kPrimLong, false, true);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001572 break;
1573 }
1574
Calin Juravle7c4954d2014-10-28 16:57:40 +00001575 case Instruction::DIV_FLOAT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001576 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001577 break;
1578 }
1579
1580 case Instruction::DIV_DOUBLE: {
Calin Juravle225ff812014-11-13 16:46:39 +00001581 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001582 break;
1583 }
1584
Calin Juravlebacfec32014-11-14 15:54:36 +00001585 case Instruction::REM_INT: {
1586 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1587 dex_pc, Primitive::kPrimInt, false, false);
1588 break;
1589 }
1590
1591 case Instruction::REM_LONG: {
1592 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1593 dex_pc, Primitive::kPrimLong, false, false);
1594 break;
1595 }
1596
Calin Juravled2ec87d2014-12-08 14:24:46 +00001597 case Instruction::REM_FLOAT: {
1598 Binop_23x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
1599 break;
1600 }
1601
1602 case Instruction::REM_DOUBLE: {
1603 Binop_23x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
1604 break;
1605 }
1606
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001607 case Instruction::AND_INT: {
1608 Binop_23x<HAnd>(instruction, Primitive::kPrimInt);
1609 break;
1610 }
1611
1612 case Instruction::AND_LONG: {
1613 Binop_23x<HAnd>(instruction, Primitive::kPrimLong);
1614 break;
1615 }
1616
Calin Juravle9aec02f2014-11-18 23:06:35 +00001617 case Instruction::SHL_INT: {
1618 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt);
1619 break;
1620 }
1621
1622 case Instruction::SHL_LONG: {
1623 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong);
1624 break;
1625 }
1626
1627 case Instruction::SHR_INT: {
1628 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt);
1629 break;
1630 }
1631
1632 case Instruction::SHR_LONG: {
1633 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong);
1634 break;
1635 }
1636
1637 case Instruction::USHR_INT: {
1638 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt);
1639 break;
1640 }
1641
1642 case Instruction::USHR_LONG: {
1643 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong);
1644 break;
1645 }
1646
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001647 case Instruction::OR_INT: {
1648 Binop_23x<HOr>(instruction, Primitive::kPrimInt);
1649 break;
1650 }
1651
1652 case Instruction::OR_LONG: {
1653 Binop_23x<HOr>(instruction, Primitive::kPrimLong);
1654 break;
1655 }
1656
1657 case Instruction::XOR_INT: {
1658 Binop_23x<HXor>(instruction, Primitive::kPrimInt);
1659 break;
1660 }
1661
1662 case Instruction::XOR_LONG: {
1663 Binop_23x<HXor>(instruction, Primitive::kPrimLong);
1664 break;
1665 }
1666
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001667 case Instruction::ADD_LONG_2ADDR: {
1668 Binop_12x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001669 break;
1670 }
1671
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001672 case Instruction::ADD_DOUBLE_2ADDR: {
1673 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble);
1674 break;
1675 }
1676
1677 case Instruction::ADD_FLOAT_2ADDR: {
1678 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat);
1679 break;
1680 }
1681
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001682 case Instruction::SUB_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001683 Binop_12x<HSub>(instruction, Primitive::kPrimInt);
1684 break;
1685 }
1686
1687 case Instruction::SUB_LONG_2ADDR: {
1688 Binop_12x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001689 break;
1690 }
1691
Calin Juravle096cc022014-10-23 17:01:13 +01001692 case Instruction::SUB_FLOAT_2ADDR: {
1693 Binop_12x<HSub>(instruction, Primitive::kPrimFloat);
1694 break;
1695 }
1696
1697 case Instruction::SUB_DOUBLE_2ADDR: {
1698 Binop_12x<HSub>(instruction, Primitive::kPrimDouble);
1699 break;
1700 }
1701
Calin Juravle34bacdf2014-10-07 20:23:36 +01001702 case Instruction::MUL_INT_2ADDR: {
1703 Binop_12x<HMul>(instruction, Primitive::kPrimInt);
1704 break;
1705 }
1706
1707 case Instruction::MUL_LONG_2ADDR: {
1708 Binop_12x<HMul>(instruction, Primitive::kPrimLong);
1709 break;
1710 }
1711
Calin Juravleb5bfa962014-10-21 18:02:24 +01001712 case Instruction::MUL_FLOAT_2ADDR: {
1713 Binop_12x<HMul>(instruction, Primitive::kPrimFloat);
1714 break;
1715 }
1716
1717 case Instruction::MUL_DOUBLE_2ADDR: {
1718 Binop_12x<HMul>(instruction, Primitive::kPrimDouble);
1719 break;
1720 }
1721
Calin Juravle865fc882014-11-06 17:09:03 +00001722 case Instruction::DIV_INT_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001723 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1724 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravle865fc882014-11-06 17:09:03 +00001725 break;
1726 }
1727
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001728 case Instruction::DIV_LONG_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001729 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1730 dex_pc, Primitive::kPrimLong, false, true);
1731 break;
1732 }
1733
1734 case Instruction::REM_INT_2ADDR: {
1735 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1736 dex_pc, Primitive::kPrimInt, false, false);
1737 break;
1738 }
1739
1740 case Instruction::REM_LONG_2ADDR: {
1741 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1742 dex_pc, Primitive::kPrimLong, false, false);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001743 break;
1744 }
1745
Calin Juravled2ec87d2014-12-08 14:24:46 +00001746 case Instruction::REM_FLOAT_2ADDR: {
1747 Binop_12x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
1748 break;
1749 }
1750
1751 case Instruction::REM_DOUBLE_2ADDR: {
1752 Binop_12x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
1753 break;
1754 }
1755
Calin Juravle9aec02f2014-11-18 23:06:35 +00001756 case Instruction::SHL_INT_2ADDR: {
1757 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt);
1758 break;
1759 }
1760
1761 case Instruction::SHL_LONG_2ADDR: {
1762 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong);
1763 break;
1764 }
1765
1766 case Instruction::SHR_INT_2ADDR: {
1767 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt);
1768 break;
1769 }
1770
1771 case Instruction::SHR_LONG_2ADDR: {
1772 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong);
1773 break;
1774 }
1775
1776 case Instruction::USHR_INT_2ADDR: {
1777 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt);
1778 break;
1779 }
1780
1781 case Instruction::USHR_LONG_2ADDR: {
1782 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong);
1783 break;
1784 }
1785
Calin Juravle7c4954d2014-10-28 16:57:40 +00001786 case Instruction::DIV_FLOAT_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00001787 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001788 break;
1789 }
1790
1791 case Instruction::DIV_DOUBLE_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00001792 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001793 break;
1794 }
1795
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001796 case Instruction::AND_INT_2ADDR: {
1797 Binop_12x<HAnd>(instruction, Primitive::kPrimInt);
1798 break;
1799 }
1800
1801 case Instruction::AND_LONG_2ADDR: {
1802 Binop_12x<HAnd>(instruction, Primitive::kPrimLong);
1803 break;
1804 }
1805
1806 case Instruction::OR_INT_2ADDR: {
1807 Binop_12x<HOr>(instruction, Primitive::kPrimInt);
1808 break;
1809 }
1810
1811 case Instruction::OR_LONG_2ADDR: {
1812 Binop_12x<HOr>(instruction, Primitive::kPrimLong);
1813 break;
1814 }
1815
1816 case Instruction::XOR_INT_2ADDR: {
1817 Binop_12x<HXor>(instruction, Primitive::kPrimInt);
1818 break;
1819 }
1820
1821 case Instruction::XOR_LONG_2ADDR: {
1822 Binop_12x<HXor>(instruction, Primitive::kPrimLong);
1823 break;
1824 }
1825
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001826 case Instruction::ADD_INT_LIT16: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001827 Binop_22s<HAdd>(instruction, false);
1828 break;
1829 }
1830
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001831 case Instruction::AND_INT_LIT16: {
1832 Binop_22s<HAnd>(instruction, false);
1833 break;
1834 }
1835
1836 case Instruction::OR_INT_LIT16: {
1837 Binop_22s<HOr>(instruction, false);
1838 break;
1839 }
1840
1841 case Instruction::XOR_INT_LIT16: {
1842 Binop_22s<HXor>(instruction, false);
1843 break;
1844 }
1845
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001846 case Instruction::RSUB_INT: {
1847 Binop_22s<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001848 break;
1849 }
1850
Calin Juravle34bacdf2014-10-07 20:23:36 +01001851 case Instruction::MUL_INT_LIT16: {
1852 Binop_22s<HMul>(instruction, false);
1853 break;
1854 }
1855
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001856 case Instruction::ADD_INT_LIT8: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001857 Binop_22b<HAdd>(instruction, false);
1858 break;
1859 }
1860
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001861 case Instruction::AND_INT_LIT8: {
1862 Binop_22b<HAnd>(instruction, false);
1863 break;
1864 }
1865
1866 case Instruction::OR_INT_LIT8: {
1867 Binop_22b<HOr>(instruction, false);
1868 break;
1869 }
1870
1871 case Instruction::XOR_INT_LIT8: {
1872 Binop_22b<HXor>(instruction, false);
1873 break;
1874 }
1875
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001876 case Instruction::RSUB_INT_LIT8: {
1877 Binop_22b<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001878 break;
1879 }
1880
Calin Juravle34bacdf2014-10-07 20:23:36 +01001881 case Instruction::MUL_INT_LIT8: {
1882 Binop_22b<HMul>(instruction, false);
1883 break;
1884 }
1885
Calin Juravled0d48522014-11-04 16:40:20 +00001886 case Instruction::DIV_INT_LIT16:
1887 case Instruction::DIV_INT_LIT8: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001888 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1889 dex_pc, Primitive::kPrimInt, true, true);
1890 break;
1891 }
1892
1893 case Instruction::REM_INT_LIT16:
1894 case Instruction::REM_INT_LIT8: {
1895 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1896 dex_pc, Primitive::kPrimInt, true, false);
Calin Juravled0d48522014-11-04 16:40:20 +00001897 break;
1898 }
1899
Calin Juravle9aec02f2014-11-18 23:06:35 +00001900 case Instruction::SHL_INT_LIT8: {
1901 Binop_22b<HShl>(instruction, false);
1902 break;
1903 }
1904
1905 case Instruction::SHR_INT_LIT8: {
1906 Binop_22b<HShr>(instruction, false);
1907 break;
1908 }
1909
1910 case Instruction::USHR_INT_LIT8: {
1911 Binop_22b<HUShr>(instruction, false);
1912 break;
1913 }
1914
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001915 case Instruction::NEW_INSTANCE: {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001916 uint16_t type_index = instruction.VRegB_21c();
1917 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
1918 ? kQuickAllocObjectWithAccessCheck
1919 : kQuickAllocObject;
1920
1921 current_block_->AddInstruction(new (arena_) HNewInstance(dex_pc, type_index, entrypoint));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001922 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
1923 break;
1924 }
1925
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001926 case Instruction::NEW_ARRAY: {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001927 uint16_t type_index = instruction.VRegC_22c();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001928 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt);
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001929 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
1930 ? kQuickAllocArrayWithAccessCheck
1931 : kQuickAllocArray;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001932 current_block_->AddInstruction(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001933 new (arena_) HNewArray(length, dex_pc, type_index, entrypoint));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001934 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
1935 break;
1936 }
1937
1938 case Instruction::FILLED_NEW_ARRAY: {
1939 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
1940 uint32_t type_index = instruction.VRegB_35c();
1941 uint32_t args[5];
1942 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00001943 BuildFilledNewArray(dex_pc, type_index, number_of_vreg_arguments, false, args, 0);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001944 break;
1945 }
1946
1947 case Instruction::FILLED_NEW_ARRAY_RANGE: {
1948 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1949 uint32_t type_index = instruction.VRegB_3rc();
1950 uint32_t register_index = instruction.VRegC_3rc();
1951 BuildFilledNewArray(
Calin Juravle225ff812014-11-13 16:46:39 +00001952 dex_pc, type_index, number_of_vreg_arguments, true, nullptr, register_index);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001953 break;
1954 }
1955
1956 case Instruction::FILL_ARRAY_DATA: {
Calin Juravle225ff812014-11-13 16:46:39 +00001957 BuildFillArrayData(instruction, dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001958 break;
1959 }
1960
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001961 case Instruction::MOVE_RESULT:
Dave Allison20dfc792014-06-16 20:44:29 -07001962 case Instruction::MOVE_RESULT_WIDE:
1963 case Instruction::MOVE_RESULT_OBJECT:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001964 UpdateLocal(instruction.VRegA(), latest_result_);
1965 latest_result_ = nullptr;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001966 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001967
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001968 case Instruction::CMP_LONG: {
Calin Juravleddb7df22014-11-25 20:56:51 +00001969 Binop_23x_cmp(instruction, Primitive::kPrimLong, HCompare::kNoBias);
1970 break;
1971 }
1972
1973 case Instruction::CMPG_FLOAT: {
1974 Binop_23x_cmp(instruction, Primitive::kPrimFloat, HCompare::kGtBias);
1975 break;
1976 }
1977
1978 case Instruction::CMPG_DOUBLE: {
1979 Binop_23x_cmp(instruction, Primitive::kPrimDouble, HCompare::kGtBias);
1980 break;
1981 }
1982
1983 case Instruction::CMPL_FLOAT: {
1984 Binop_23x_cmp(instruction, Primitive::kPrimFloat, HCompare::kLtBias);
1985 break;
1986 }
1987
1988 case Instruction::CMPL_DOUBLE: {
1989 Binop_23x_cmp(instruction, Primitive::kPrimDouble, HCompare::kLtBias);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001990 break;
1991 }
1992
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001993 case Instruction::NOP:
1994 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001995
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001996 case Instruction::IGET:
1997 case Instruction::IGET_WIDE:
1998 case Instruction::IGET_OBJECT:
1999 case Instruction::IGET_BOOLEAN:
2000 case Instruction::IGET_BYTE:
2001 case Instruction::IGET_CHAR:
2002 case Instruction::IGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002003 if (!BuildInstanceFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002004 return false;
2005 }
2006 break;
2007 }
2008
2009 case Instruction::IPUT:
2010 case Instruction::IPUT_WIDE:
2011 case Instruction::IPUT_OBJECT:
2012 case Instruction::IPUT_BOOLEAN:
2013 case Instruction::IPUT_BYTE:
2014 case Instruction::IPUT_CHAR:
2015 case Instruction::IPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002016 if (!BuildInstanceFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002017 return false;
2018 }
2019 break;
2020 }
2021
2022 case Instruction::SGET:
2023 case Instruction::SGET_WIDE:
2024 case Instruction::SGET_OBJECT:
2025 case Instruction::SGET_BOOLEAN:
2026 case Instruction::SGET_BYTE:
2027 case Instruction::SGET_CHAR:
2028 case Instruction::SGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002029 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002030 return false;
2031 }
2032 break;
2033 }
2034
2035 case Instruction::SPUT:
2036 case Instruction::SPUT_WIDE:
2037 case Instruction::SPUT_OBJECT:
2038 case Instruction::SPUT_BOOLEAN:
2039 case Instruction::SPUT_BYTE:
2040 case Instruction::SPUT_CHAR:
2041 case Instruction::SPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00002042 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002043 return false;
2044 }
2045 break;
2046 }
2047
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002048#define ARRAY_XX(kind, anticipated_type) \
2049 case Instruction::AGET##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00002050 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002051 break; \
2052 } \
2053 case Instruction::APUT##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00002054 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002055 break; \
2056 }
2057
2058 ARRAY_XX(, Primitive::kPrimInt);
2059 ARRAY_XX(_WIDE, Primitive::kPrimLong);
2060 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
2061 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
2062 ARRAY_XX(_BYTE, Primitive::kPrimByte);
2063 ARRAY_XX(_CHAR, Primitive::kPrimChar);
2064 ARRAY_XX(_SHORT, Primitive::kPrimShort);
2065
Nicolas Geoffray39468442014-09-02 15:17:15 +01002066 case Instruction::ARRAY_LENGTH: {
2067 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002068 // No need for a temporary for the null check, it is the only input of the following
2069 // instruction.
Calin Juravle225ff812014-11-13 16:46:39 +00002070 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002071 current_block_->AddInstruction(object);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002072 current_block_->AddInstruction(new (arena_) HArrayLength(object));
2073 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
2074 break;
2075 }
2076
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002077 case Instruction::CONST_STRING: {
Calin Juravle225ff812014-11-13 16:46:39 +00002078 current_block_->AddInstruction(new (arena_) HLoadString(instruction.VRegB_21c(), dex_pc));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002079 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2080 break;
2081 }
2082
2083 case Instruction::CONST_STRING_JUMBO: {
Calin Juravle225ff812014-11-13 16:46:39 +00002084 current_block_->AddInstruction(new (arena_) HLoadString(instruction.VRegB_31c(), dex_pc));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002085 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
2086 break;
2087 }
2088
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002089 case Instruction::CONST_CLASS: {
2090 uint16_t type_index = instruction.VRegB_21c();
2091 bool type_known_final;
2092 bool type_known_abstract;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002093 bool dont_use_is_referrers_class;
2094 // `CanAccessTypeWithoutChecks` will tell whether the method being
2095 // built is trying to access its own class, so that the generated
2096 // code can optimize for this case. However, the optimization does not
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002097 // work for inlining, so we use `IsOutermostCompilingClass` instead.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002098 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
2099 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002100 &type_known_final, &type_known_abstract, &dont_use_is_referrers_class);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002101 if (!can_access) {
Calin Juravle48c2b032014-12-09 18:11:36 +00002102 MaybeRecordStat(MethodCompilationStat::kNotCompiledCantAccesType);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002103 return false;
2104 }
2105 current_block_->AddInstruction(
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002106 new (arena_) HLoadClass(type_index, IsOutermostCompilingClass(type_index), dex_pc));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002107 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2108 break;
2109 }
2110
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002111 case Instruction::MOVE_EXCEPTION: {
2112 current_block_->AddInstruction(new (arena_) HLoadException());
2113 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
2114 break;
2115 }
2116
2117 case Instruction::THROW: {
2118 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00002119 current_block_->AddInstruction(new (arena_) HThrow(exception, dex_pc));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002120 // A throw instruction must branch to the exit block.
2121 current_block_->AddSuccessor(exit_block_);
2122 // We finished building this block. Set the current block to null to avoid
2123 // adding dead instructions to it.
2124 current_block_ = nullptr;
2125 break;
2126 }
2127
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002128 case Instruction::INSTANCE_OF: {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002129 uint8_t destination = instruction.VRegA_22c();
2130 uint8_t reference = instruction.VRegB_22c();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002131 uint16_t type_index = instruction.VRegC_22c();
Calin Juravle225ff812014-11-13 16:46:39 +00002132 if (!BuildTypeCheck(instruction, destination, reference, type_index, dex_pc)) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002133 return false;
2134 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002135 break;
2136 }
2137
2138 case Instruction::CHECK_CAST: {
2139 uint8_t reference = instruction.VRegA_21c();
2140 uint16_t type_index = instruction.VRegB_21c();
Calin Juravle225ff812014-11-13 16:46:39 +00002141 if (!BuildTypeCheck(instruction, -1, reference, type_index, dex_pc)) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002142 return false;
2143 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002144 break;
2145 }
2146
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002147 case Instruction::MONITOR_ENTER: {
2148 current_block_->AddInstruction(new (arena_) HMonitorOperation(
2149 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2150 HMonitorOperation::kEnter,
Calin Juravle225ff812014-11-13 16:46:39 +00002151 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002152 break;
2153 }
2154
2155 case Instruction::MONITOR_EXIT: {
2156 current_block_->AddInstruction(new (arena_) HMonitorOperation(
2157 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2158 HMonitorOperation::kExit,
Calin Juravle225ff812014-11-13 16:46:39 +00002159 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002160 break;
2161 }
2162
Andreas Gamped881df52014-11-24 23:28:39 -08002163 case Instruction::PACKED_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002164 BuildPackedSwitch(instruction, dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08002165 break;
2166 }
2167
Andreas Gampee4d4d322014-12-04 09:09:57 -08002168 case Instruction::SPARSE_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002169 BuildSparseSwitch(instruction, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08002170 break;
2171 }
2172
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002173 default:
Calin Juravle48c2b032014-12-09 18:11:36 +00002174 VLOG(compiler) << "Did not compile "
2175 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
2176 << " because of unhandled instruction "
2177 << instruction.Name();
2178 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnhandledInstruction);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002179 return false;
2180 }
2181 return true;
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00002182} // NOLINT(readability/fn_size)
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002183
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002184HLocal* HGraphBuilder::GetLocalAt(int register_index) const {
2185 return locals_.Get(register_index);
2186}
2187
2188void HGraphBuilder::UpdateLocal(int register_index, HInstruction* instruction) const {
2189 HLocal* local = GetLocalAt(register_index);
2190 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction));
2191}
2192
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002193HInstruction* HGraphBuilder::LoadLocal(int register_index, Primitive::Type type) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002194 HLocal* local = GetLocalAt(register_index);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002195 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type));
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002196 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002197}
2198
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002199} // namespace art