blob: 2da31768e73b591906084e93f7e900a4f76a4ba3 [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
Andreas Gamped881df52014-11-24 23:28:39 -080019#include "base/logging.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010020#include "class_linker.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000021#include "dex_file.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000022#include "dex_file-inl.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000023#include "dex_instruction.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000024#include "dex_instruction-inl.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/art_field.h"
28#include "mirror/art_field-inl.h"
29#include "mirror/class_loader.h"
30#include "mirror/dex_cache.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000031#include "nodes.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000032#include "primitive.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010033#include "scoped_thread_state_change.h"
34#include "thread.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000035
36namespace art {
37
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010038/**
39 * Helper class to add HTemporary instructions. This class is used when
40 * converting a DEX instruction to multiple HInstruction, and where those
41 * instructions do not die at the following instruction, but instead spans
42 * multiple instructions.
43 */
44class Temporaries : public ValueObject {
45 public:
Calin Juravlef97f9fb2014-11-11 15:38:19 +000046 explicit Temporaries(HGraph* graph) : graph_(graph), index_(0) {}
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010047
48 void Add(HInstruction* instruction) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +000049 HInstruction* temp = new (graph_->GetArena()) HTemporary(index_);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010050 instruction->GetBlock()->AddInstruction(temp);
Calin Juravlef97f9fb2014-11-11 15:38:19 +000051
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010052 DCHECK(temp->GetPrevious() == instruction);
Calin Juravlef97f9fb2014-11-11 15:38:19 +000053
54 size_t offset;
55 if (instruction->GetType() == Primitive::kPrimLong
56 || instruction->GetType() == Primitive::kPrimDouble) {
57 offset = 2;
58 } else {
59 offset = 1;
60 }
61 index_ += offset;
62
63 graph_->UpdateTemporariesVRegSlots(index_);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010064 }
65
66 private:
67 HGraph* const graph_;
68
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010069 // Current index in the temporary stack, updated by `Add`.
70 size_t index_;
71};
72
Andreas Gamped881df52014-11-24 23:28:39 -080073class SwitchTable : public ValueObject {
74 public:
75 SwitchTable(const Instruction& instruction, uint32_t dex_pc, bool sparse)
76 : instruction_(instruction), dex_pc_(dex_pc), sparse_(sparse) {
77 int32_t table_offset = instruction.VRegB_31t();
78 const uint16_t* table = reinterpret_cast<const uint16_t*>(&instruction) + table_offset;
79 if (sparse) {
80 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
81 } else {
82 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
83 }
84 num_entries_ = table[1];
85 values_ = reinterpret_cast<const int32_t*>(&table[2]);
86 }
87
88 uint16_t GetNumEntries() const {
89 return num_entries_;
90 }
91
Andreas Gampee4d4d322014-12-04 09:09:57 -080092 void CheckIndex(size_t index) const {
93 if (sparse_) {
94 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
95 DCHECK_LT(index, 2 * static_cast<size_t>(num_entries_));
96 } else {
97 // In a packed table, we have the starting key and num_entries_ values.
98 DCHECK_LT(index, 1 + static_cast<size_t>(num_entries_));
99 }
100 }
101
Andreas Gamped881df52014-11-24 23:28:39 -0800102 int32_t GetEntryAt(size_t index) const {
Andreas Gampee4d4d322014-12-04 09:09:57 -0800103 CheckIndex(index);
Andreas Gamped881df52014-11-24 23:28:39 -0800104 return values_[index];
105 }
106
107 uint32_t GetDexPcForIndex(size_t index) const {
Andreas Gampee4d4d322014-12-04 09:09:57 -0800108 CheckIndex(index);
Andreas Gamped881df52014-11-24 23:28:39 -0800109 return dex_pc_ +
110 (reinterpret_cast<const int16_t*>(values_ + index) -
111 reinterpret_cast<const int16_t*>(&instruction_));
112 }
113
Andreas Gampee4d4d322014-12-04 09:09:57 -0800114 // Index of the first value in the table.
115 size_t GetFirstValueIndex() const {
116 if (sparse_) {
117 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
118 return num_entries_;
119 } else {
120 // In a packed table, we have the starting key and num_entries_ values.
121 return 1;
122 }
123 }
124
Andreas Gamped881df52014-11-24 23:28:39 -0800125 private:
126 const Instruction& instruction_;
127 const uint32_t dex_pc_;
128
129 // Whether this is a sparse-switch table (or a packed-switch one).
130 const bool sparse_;
131
132 // This can't be const as it needs to be computed off of the given instruction, and complicated
133 // expressions in the initializer list seemed very ugly.
134 uint16_t num_entries_;
135
136 const int32_t* values_;
137
138 DISALLOW_COPY_AND_ASSIGN(SwitchTable);
139};
140
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100141void HGraphBuilder::InitializeLocals(uint16_t count) {
142 graph_->SetNumberOfVRegs(count);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000143 locals_.SetSize(count);
144 for (int i = 0; i < count; i++) {
145 HLocal* local = new (arena_) HLocal(i);
146 entry_block_->AddInstruction(local);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000147 locals_.Put(i, local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000148 }
149}
150
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000151void HGraphBuilder::InitializeParameters(uint16_t number_of_parameters) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100152 // dex_compilation_unit_ is null only when unit testing.
153 if (dex_compilation_unit_ == nullptr) {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000154 return;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100155 }
156
157 graph_->SetNumberOfInVRegs(number_of_parameters);
158 const char* shorty = dex_compilation_unit_->GetShorty();
159 int locals_index = locals_.Size() - number_of_parameters;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100160 int parameter_index = 0;
161
162 if (!dex_compilation_unit_->IsStatic()) {
163 // Add the implicit 'this' argument, not expressed in the signature.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100164 HParameterValue* parameter =
Calin Juravle10e244f2015-01-26 18:54:32 +0000165 new (arena_) HParameterValue(parameter_index++, Primitive::kPrimNot, true);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100166 entry_block_->AddInstruction(parameter);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100167 HLocal* local = GetLocalAt(locals_index++);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100168 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100169 number_of_parameters--;
170 }
171
172 uint32_t pos = 1;
173 for (int i = 0; i < number_of_parameters; i++) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100174 HParameterValue* parameter =
175 new (arena_) HParameterValue(parameter_index++, Primitive::GetType(shorty[pos++]));
176 entry_block_->AddInstruction(parameter);
177 HLocal* local = GetLocalAt(locals_index++);
178 // Store the parameter value in the local that the dex code will use
179 // to reference that parameter.
180 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter));
181 bool is_wide = (parameter->GetType() == Primitive::kPrimLong)
182 || (parameter->GetType() == Primitive::kPrimDouble);
183 if (is_wide) {
184 i++;
185 locals_index++;
186 parameter_index++;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100187 }
188 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100189}
190
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100191template<typename T>
Calin Juravle225ff812014-11-13 16:46:39 +0000192void HGraphBuilder::If_22t(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000193 int32_t target_offset = instruction.GetTargetOffset();
David Brazdil852eaff2015-02-02 15:23:05 +0000194 HBasicBlock* branch_target = FindBlockStartingAt(dex_pc + target_offset);
195 HBasicBlock* fallthrough_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
196 DCHECK(branch_target != nullptr);
197 DCHECK(fallthrough_target != nullptr);
198 PotentiallyAddSuspendCheck(branch_target, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100199 HInstruction* first = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
200 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Dave Allison20dfc792014-06-16 20:44:29 -0700201 T* comparison = new (arena_) T(first, second);
202 current_block_->AddInstruction(comparison);
203 HInstruction* ifinst = new (arena_) HIf(comparison);
204 current_block_->AddInstruction(ifinst);
David Brazdil852eaff2015-02-02 15:23:05 +0000205 current_block_->AddSuccessor(branch_target);
206 current_block_->AddSuccessor(fallthrough_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700207 current_block_ = nullptr;
208}
209
210template<typename T>
Calin Juravle225ff812014-11-13 16:46:39 +0000211void HGraphBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000212 int32_t target_offset = instruction.GetTargetOffset();
David Brazdil852eaff2015-02-02 15:23:05 +0000213 HBasicBlock* branch_target = FindBlockStartingAt(dex_pc + target_offset);
214 HBasicBlock* fallthrough_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
215 DCHECK(branch_target != nullptr);
216 DCHECK(fallthrough_target != nullptr);
217 PotentiallyAddSuspendCheck(branch_target, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700218 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000219 T* comparison = new (arena_) T(value, graph_->GetIntConstant(0));
Dave Allison20dfc792014-06-16 20:44:29 -0700220 current_block_->AddInstruction(comparison);
221 HInstruction* ifinst = new (arena_) HIf(comparison);
222 current_block_->AddInstruction(ifinst);
David Brazdil852eaff2015-02-02 15:23:05 +0000223 current_block_->AddSuccessor(branch_target);
224 current_block_->AddSuccessor(fallthrough_target);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100225 current_block_ = nullptr;
226}
227
Calin Juravle48c2b032014-12-09 18:11:36 +0000228void HGraphBuilder::MaybeRecordStat(MethodCompilationStat compilation_stat) {
229 if (compilation_stats_ != nullptr) {
230 compilation_stats_->RecordStat(compilation_stat);
231 }
232}
233
234bool HGraphBuilder::SkipCompilation(size_t number_of_dex_instructions,
235 size_t number_of_blocks ATTRIBUTE_UNUSED,
236 size_t number_of_branches) {
237 const CompilerOptions& compiler_options = compiler_driver_->GetCompilerOptions();
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000238 CompilerOptions::CompilerFilter compiler_filter = compiler_options.GetCompilerFilter();
239 if (compiler_filter == CompilerOptions::kEverything) {
240 return false;
241 }
242
243 if (compiler_options.IsHugeMethod(number_of_dex_instructions)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000244 VLOG(compiler) << "Skip compilation of huge method "
245 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
246 << ": " << number_of_dex_instructions << " dex instructions";
247 MaybeRecordStat(MethodCompilationStat::kNotCompiledHugeMethod);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000248 return true;
249 }
250
251 // If it's large and contains no branches, it's likely to be machine generated initialization.
252 if (compiler_options.IsLargeMethod(number_of_dex_instructions) && (number_of_branches == 0)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000253 VLOG(compiler) << "Skip compilation of large method with no branch "
254 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
255 << ": " << number_of_dex_instructions << " dex instructions";
256 MaybeRecordStat(MethodCompilationStat::kNotCompiledLargeMethodNoBranches);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000257 return true;
258 }
259
260 return false;
261}
262
David Brazdil5e8b1372015-01-23 14:39:08 +0000263bool HGraphBuilder::BuildGraph(const DexFile::CodeItem& code_item) {
264 DCHECK(graph_->GetBlocks().IsEmpty());
265
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000266 const uint16_t* code_ptr = code_item.insns_;
267 const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100268 code_start_ = code_ptr;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000269
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000270 // Setup the graph with the entry block and exit block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100271 entry_block_ = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000272 graph_->AddBlock(entry_block_);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100273 exit_block_ = new (arena_) HBasicBlock(graph_, kNoDexPc);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000274 graph_->SetEntryBlock(entry_block_);
275 graph_->SetExitBlock(exit_block_);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000276
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000277 InitializeLocals(code_item.registers_size_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000278 graph_->SetMaximumNumberOfOutVRegs(code_item.outs_size_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000279
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000280 // Compute the number of dex instructions, blocks, and branches. We will
281 // check these values against limits given to the compiler.
282 size_t number_of_dex_instructions = 0;
283 size_t number_of_blocks = 0;
284 size_t number_of_branches = 0;
285
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000286 // To avoid splitting blocks, we compute ahead of time the instructions that
287 // start a new block, and create these blocks.
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000288 ComputeBranchTargets(
289 code_ptr, code_end, &number_of_dex_instructions, &number_of_blocks, &number_of_branches);
290
291 // Note that the compiler driver is null when unit testing.
Calin Juravle48c2b032014-12-09 18:11:36 +0000292 if ((compiler_driver_ != nullptr)
293 && SkipCompilation(number_of_dex_instructions, number_of_blocks, number_of_branches)) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000294 return false;
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000295 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000296
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000297 // Also create blocks for catch handlers.
298 if (code_item.tries_size_ != 0) {
299 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(code_item, 0);
300 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
301 for (uint32_t idx = 0; idx < handlers_size; ++idx) {
302 CatchHandlerIterator iterator(handlers_ptr);
303 for (; iterator.HasNext(); iterator.Next()) {
304 uint32_t address = iterator.GetHandlerAddress();
305 HBasicBlock* block = FindBlockStartingAt(address);
306 if (block == nullptr) {
307 block = new (arena_) HBasicBlock(graph_, address);
308 branch_targets_.Put(address, block);
309 }
310 block->SetIsCatchBlock();
311 }
312 handlers_ptr = iterator.EndDataPointer();
313 }
314 }
315
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000316 InitializeParameters(code_item.ins_size_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100317
Calin Juravle225ff812014-11-13 16:46:39 +0000318 size_t dex_pc = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000319 while (code_ptr < code_end) {
Calin Juravle225ff812014-11-13 16:46:39 +0000320 // Update the current block if dex_pc starts a new block.
321 MaybeUpdateCurrentBlock(dex_pc);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000322 const Instruction& instruction = *Instruction::At(code_ptr);
Calin Juravle48c2b032014-12-09 18:11:36 +0000323 if (!AnalyzeDexInstruction(instruction, dex_pc)) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000324 return false;
Calin Juravle48c2b032014-12-09 18:11:36 +0000325 }
Calin Juravle225ff812014-11-13 16:46:39 +0000326 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000327 code_ptr += instruction.SizeInCodeUnits();
328 }
329
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000330 // Add the exit block at the end to give it the highest id.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000331 graph_->AddBlock(exit_block_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000332 exit_block_->AddInstruction(new (arena_) HExit());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000333 // Add the suspend check to the entry block.
334 entry_block_->AddInstruction(new (arena_) HSuspendCheck(0));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000335 entry_block_->AddInstruction(new (arena_) HGoto());
David Brazdil5e8b1372015-01-23 14:39:08 +0000336
337 return true;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000338}
339
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000340void HGraphBuilder::MaybeUpdateCurrentBlock(size_t index) {
341 HBasicBlock* block = FindBlockStartingAt(index);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000342 if (block == nullptr) {
343 return;
344 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000345
346 if (current_block_ != nullptr) {
347 // Branching instructions clear current_block, so we know
348 // the last instruction of the current block is not a branching
349 // instruction. We add an unconditional goto to the found block.
350 current_block_->AddInstruction(new (arena_) HGoto());
351 current_block_->AddSuccessor(block);
352 }
353 graph_->AddBlock(block);
354 current_block_ = block;
355}
356
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000357void HGraphBuilder::ComputeBranchTargets(const uint16_t* code_ptr,
358 const uint16_t* code_end,
359 size_t* number_of_dex_instructions,
360 size_t* number_of_blocks,
361 size_t* number_of_branches) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000362 branch_targets_.SetSize(code_end - code_ptr);
363
364 // Create the first block for the dex instructions, single successor of the entry block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100365 HBasicBlock* block = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000366 branch_targets_.Put(0, block);
367 entry_block_->AddSuccessor(block);
368
369 // Iterate over all instructions and find branching instructions. Create blocks for
370 // the locations these instructions branch to.
Andreas Gamped881df52014-11-24 23:28:39 -0800371 uint32_t dex_pc = 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000372 while (code_ptr < code_end) {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000373 (*number_of_dex_instructions)++;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000374 const Instruction& instruction = *Instruction::At(code_ptr);
375 if (instruction.IsBranch()) {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000376 (*number_of_branches)++;
Calin Juravle225ff812014-11-13 16:46:39 +0000377 int32_t target = instruction.GetTargetOffset() + dex_pc;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000378 // Create a block for the target instruction.
379 if (FindBlockStartingAt(target) == nullptr) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100380 block = new (arena_) HBasicBlock(graph_, target);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000381 branch_targets_.Put(target, block);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000382 (*number_of_blocks)++;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000383 }
Calin Juravle225ff812014-11-13 16:46:39 +0000384 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000385 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle225ff812014-11-13 16:46:39 +0000386 if ((code_ptr < code_end) && (FindBlockStartingAt(dex_pc) == nullptr)) {
387 block = new (arena_) HBasicBlock(graph_, dex_pc);
388 branch_targets_.Put(dex_pc, block);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000389 (*number_of_blocks)++;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000390 }
Andreas Gampee4d4d322014-12-04 09:09:57 -0800391 } else if (instruction.IsSwitch()) {
392 SwitchTable table(instruction, dex_pc, instruction.Opcode() == Instruction::SPARSE_SWITCH);
Andreas Gamped881df52014-11-24 23:28:39 -0800393
394 uint16_t num_entries = table.GetNumEntries();
395
Andreas Gampee4d4d322014-12-04 09:09:57 -0800396 // In a packed-switch, the entry at index 0 is the starting key. In a sparse-switch, the
397 // entry at index 0 is the first key, and values are after *all* keys.
398 size_t offset = table.GetFirstValueIndex();
399
400 // Use a larger loop counter type to avoid overflow issues.
401 for (size_t i = 0; i < num_entries; ++i) {
Andreas Gamped881df52014-11-24 23:28:39 -0800402 // The target of the case.
Andreas Gampee4d4d322014-12-04 09:09:57 -0800403 uint32_t target = dex_pc + table.GetEntryAt(i + offset);
Andreas Gamped881df52014-11-24 23:28:39 -0800404 if (FindBlockStartingAt(target) == nullptr) {
405 block = new (arena_) HBasicBlock(graph_, target);
406 branch_targets_.Put(target, block);
407 (*number_of_blocks)++;
408 }
409
410 // The next case gets its own block.
411 if (i < num_entries) {
412 block = new (arena_) HBasicBlock(graph_, target);
413 branch_targets_.Put(table.GetDexPcForIndex(i), block);
414 (*number_of_blocks)++;
415 }
416 }
417
418 // Fall-through. Add a block if there is more code afterwards.
419 dex_pc += instruction.SizeInCodeUnits();
420 code_ptr += instruction.SizeInCodeUnits();
421 if ((code_ptr < code_end) && (FindBlockStartingAt(dex_pc) == nullptr)) {
422 block = new (arena_) HBasicBlock(graph_, dex_pc);
423 branch_targets_.Put(dex_pc, block);
424 (*number_of_blocks)++;
425 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000426 } else {
427 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle225ff812014-11-13 16:46:39 +0000428 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000429 }
430 }
431}
432
433HBasicBlock* HGraphBuilder::FindBlockStartingAt(int32_t index) const {
434 DCHECK_GE(index, 0);
435 return branch_targets_.Get(index);
436}
437
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100438template<typename T>
Roland Levillain88cb1752014-10-20 16:36:47 +0100439void HGraphBuilder::Unop_12x(const Instruction& instruction, Primitive::Type type) {
440 HInstruction* first = LoadLocal(instruction.VRegB(), type);
441 current_block_->AddInstruction(new (arena_) T(type, first));
442 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
443}
444
Roland Levillaindff1f282014-11-05 14:15:05 +0000445void HGraphBuilder::Conversion_12x(const Instruction& instruction,
446 Primitive::Type input_type,
Roland Levillain624279f2014-12-04 11:54:28 +0000447 Primitive::Type result_type,
448 uint32_t dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +0000449 HInstruction* first = LoadLocal(instruction.VRegB(), input_type);
Roland Levillain624279f2014-12-04 11:54:28 +0000450 current_block_->AddInstruction(new (arena_) HTypeConversion(result_type, first, dex_pc));
Roland Levillaindff1f282014-11-05 14:15:05 +0000451 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
452}
453
Roland Levillain88cb1752014-10-20 16:36:47 +0100454template<typename T>
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100455void HGraphBuilder::Binop_23x(const Instruction& instruction, Primitive::Type type) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100456 HInstruction* first = LoadLocal(instruction.VRegB(), type);
457 HInstruction* second = LoadLocal(instruction.VRegC(), type);
458 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100459 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
460}
461
462template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000463void HGraphBuilder::Binop_23x(const Instruction& instruction,
464 Primitive::Type type,
465 uint32_t dex_pc) {
466 HInstruction* first = LoadLocal(instruction.VRegB(), type);
467 HInstruction* second = LoadLocal(instruction.VRegC(), type);
468 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
469 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
470}
471
472template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000473void HGraphBuilder::Binop_23x_shift(const Instruction& instruction,
474 Primitive::Type type) {
475 HInstruction* first = LoadLocal(instruction.VRegB(), type);
476 HInstruction* second = LoadLocal(instruction.VRegC(), Primitive::kPrimInt);
477 current_block_->AddInstruction(new (arena_) T(type, first, second));
478 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
479}
480
Calin Juravleddb7df22014-11-25 20:56:51 +0000481void HGraphBuilder::Binop_23x_cmp(const Instruction& instruction,
482 Primitive::Type type,
483 HCompare::Bias bias) {
484 HInstruction* first = LoadLocal(instruction.VRegB(), type);
485 HInstruction* second = LoadLocal(instruction.VRegC(), type);
486 current_block_->AddInstruction(new (arena_) HCompare(type, first, second, bias));
487 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
488}
489
Calin Juravle9aec02f2014-11-18 23:06:35 +0000490template<typename T>
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100491void HGraphBuilder::Binop_12x(const Instruction& instruction, Primitive::Type type) {
492 HInstruction* first = LoadLocal(instruction.VRegA(), type);
493 HInstruction* second = LoadLocal(instruction.VRegB(), type);
494 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100495 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
496}
497
498template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000499void HGraphBuilder::Binop_12x_shift(const Instruction& instruction, Primitive::Type type) {
500 HInstruction* first = LoadLocal(instruction.VRegA(), type);
501 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
502 current_block_->AddInstruction(new (arena_) T(type, first, second));
503 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
504}
505
506template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000507void HGraphBuilder::Binop_12x(const Instruction& instruction,
508 Primitive::Type type,
509 uint32_t dex_pc) {
510 HInstruction* first = LoadLocal(instruction.VRegA(), type);
511 HInstruction* second = LoadLocal(instruction.VRegB(), type);
512 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
513 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
514}
515
516template<typename T>
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100517void HGraphBuilder::Binop_22s(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100518 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000519 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100520 if (reverse) {
521 std::swap(first, second);
522 }
523 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
524 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
525}
526
527template<typename T>
528void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100529 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000530 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100531 if (reverse) {
532 std::swap(first, second);
533 }
534 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
535 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
536}
537
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100538void HGraphBuilder::BuildReturn(const Instruction& instruction, Primitive::Type type) {
539 if (type == Primitive::kPrimVoid) {
540 current_block_->AddInstruction(new (arena_) HReturnVoid());
541 } else {
542 HInstruction* value = LoadLocal(instruction.VRegA(), type);
543 current_block_->AddInstruction(new (arena_) HReturn(value));
544 }
545 current_block_->AddSuccessor(exit_block_);
546 current_block_ = nullptr;
547}
548
549bool HGraphBuilder::BuildInvoke(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000550 uint32_t dex_pc,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100551 uint32_t method_idx,
552 uint32_t number_of_vreg_arguments,
553 bool is_range,
554 uint32_t* args,
555 uint32_t register_index) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100556 Instruction::Code opcode = instruction.Opcode();
557 InvokeType invoke_type;
558 switch (opcode) {
559 case Instruction::INVOKE_STATIC:
560 case Instruction::INVOKE_STATIC_RANGE:
561 invoke_type = kStatic;
562 break;
563 case Instruction::INVOKE_DIRECT:
564 case Instruction::INVOKE_DIRECT_RANGE:
565 invoke_type = kDirect;
566 break;
567 case Instruction::INVOKE_VIRTUAL:
568 case Instruction::INVOKE_VIRTUAL_RANGE:
569 invoke_type = kVirtual;
570 break;
571 case Instruction::INVOKE_INTERFACE:
572 case Instruction::INVOKE_INTERFACE_RANGE:
573 invoke_type = kInterface;
574 break;
575 case Instruction::INVOKE_SUPER_RANGE:
576 case Instruction::INVOKE_SUPER:
577 invoke_type = kSuper;
578 break;
579 default:
580 LOG(FATAL) << "Unexpected invoke op: " << opcode;
581 return false;
582 }
583
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100584 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
585 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(method_id.proto_idx_);
586 const char* descriptor = dex_file_->StringDataByIdx(proto_id.shorty_idx_);
587 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100588 bool is_instance_call = invoke_type != kStatic;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100589 const size_t number_of_arguments = strlen(descriptor) - (is_instance_call ? 0 : 1);
590
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000591 MethodReference target_method(dex_file_, method_idx);
592 uintptr_t direct_code;
593 uintptr_t direct_method;
594 int table_index;
595 InvokeType optimized_invoke_type = invoke_type;
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000596
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000597 if (!compiler_driver_->ComputeInvokeInfo(dex_compilation_unit_, dex_pc, true, true,
598 &optimized_invoke_type, &target_method, &table_index,
599 &direct_code, &direct_method)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000600 VLOG(compiler) << "Did not compile " << PrettyMethod(method_idx, *dex_file_)
601 << " because a method call could not be resolved";
602 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedMethod);
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000603 return false;
604 }
605 DCHECK(optimized_invoke_type != kSuper);
606
607 HInvoke* invoke = nullptr;
608 if (optimized_invoke_type == kVirtual) {
609 invoke = new (arena_) HInvokeVirtual(
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800610 arena_, number_of_arguments, return_type, dex_pc, method_idx, table_index);
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000611 } else if (optimized_invoke_type == kInterface) {
612 invoke = new (arena_) HInvokeInterface(
613 arena_, number_of_arguments, return_type, dex_pc, method_idx, table_index);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100614 } else {
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000615 DCHECK(optimized_invoke_type == kDirect || optimized_invoke_type == kStatic);
616 // Sharpening to kDirect only works if we compile PIC.
617 DCHECK((optimized_invoke_type == invoke_type) || (optimized_invoke_type != kDirect)
618 || compiler_driver_->GetCompilerOptions().GetCompilePic());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000619 bool is_recursive =
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000620 (target_method.dex_method_index == dex_compilation_unit_->GetDexMethodIndex());
621 DCHECK(!is_recursive || (target_method.dex_file == dex_compilation_unit_->GetDexFile()));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000622 invoke = new (arena_) HInvokeStaticOrDirect(
623 arena_, number_of_arguments, return_type, dex_pc, target_method.dex_method_index,
Nicolas Geoffray79041292015-03-26 10:05:54 +0000624 is_recursive, invoke_type, optimized_invoke_type);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100625 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100626
627 size_t start_index = 0;
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000628 Temporaries temps(graph_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100629 if (is_instance_call) {
630 HInstruction* arg = LoadLocal(is_range ? register_index : args[0], Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000631 HNullCheck* null_check = new (arena_) HNullCheck(arg, dex_pc);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100632 current_block_->AddInstruction(null_check);
633 temps.Add(null_check);
634 invoke->SetArgumentAt(0, null_check);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100635 start_index = 1;
636 }
637
638 uint32_t descriptor_index = 1;
639 uint32_t argument_index = start_index;
640 for (size_t i = start_index; i < number_of_vreg_arguments; i++, argument_index++) {
641 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100642 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
643 if (!is_range && is_wide && args[i] + 1 != args[i + 1]) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100644 LOG(WARNING) << "Non sequential register pair in " << dex_compilation_unit_->GetSymbol()
Calin Juravle225ff812014-11-13 16:46:39 +0000645 << " at " << dex_pc;
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100646 // We do not implement non sequential register pair.
Calin Juravle48c2b032014-12-09 18:11:36 +0000647 MaybeRecordStat(MethodCompilationStat::kNotCompiledNonSequentialRegPair);
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100648 return false;
649 }
650 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
651 invoke->SetArgumentAt(argument_index, arg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100652 if (is_wide) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100653 i++;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100654 }
655 }
656
657 DCHECK_EQ(argument_index, number_of_arguments);
658 current_block_->AddInstruction(invoke);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100659 latest_result_ = invoke;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100660 return true;
661}
662
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100663bool HGraphBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000664 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100665 bool is_put) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100666 uint32_t source_or_dest_reg = instruction.VRegA_22c();
667 uint32_t obj_reg = instruction.VRegB_22c();
668 uint16_t field_index = instruction.VRegC_22c();
669
670 ScopedObjectAccess soa(Thread::Current());
671 StackHandleScope<1> hs(soa.Self());
672 Handle<mirror::ArtField> resolved_field(hs.NewHandle(
673 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa)));
674
675 if (resolved_field.Get() == nullptr) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000676 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedField);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100677 return false;
678 }
Calin Juravle52c48962014-12-16 17:02:57 +0000679
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100680 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100681
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100682 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000683 current_block_->AddInstruction(new (arena_) HNullCheck(object, dex_pc));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100684 if (is_put) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000685 Temporaries temps(graph_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100686 HInstruction* null_check = current_block_->GetLastInstruction();
687 // We need one temporary for the null check.
688 temps.Add(null_check);
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100689 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100690 current_block_->AddInstruction(new (arena_) HInstanceFieldSet(
691 null_check,
692 value,
Nicolas Geoffray39468442014-09-02 15:17:15 +0100693 field_type,
Calin Juravle52c48962014-12-16 17:02:57 +0000694 resolved_field->GetOffset(),
695 resolved_field->IsVolatile()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100696 } else {
697 current_block_->AddInstruction(new (arena_) HInstanceFieldGet(
698 current_block_->GetLastInstruction(),
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100699 field_type,
Calin Juravle52c48962014-12-16 17:02:57 +0000700 resolved_field->GetOffset(),
701 resolved_field->IsVolatile()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100702
703 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
704 }
705 return true;
706}
707
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000708mirror::Class* HGraphBuilder::GetOutermostCompilingClass() const {
709 ScopedObjectAccess soa(Thread::Current());
710 StackHandleScope<2> hs(soa.Self());
711 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
712 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
713 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
714 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
715 outer_compilation_unit_->GetClassLinker()->FindDexCache(outer_dex_file)));
716
717 return compiler_driver_->ResolveCompilingMethodsClass(
718 soa, outer_dex_cache, class_loader, outer_compilation_unit_);
719}
720
721bool HGraphBuilder::IsOutermostCompilingClass(uint16_t type_index) const {
722 ScopedObjectAccess soa(Thread::Current());
723 StackHandleScope<4> hs(soa.Self());
724 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
725 dex_compilation_unit_->GetClassLinker()->FindDexCache(*dex_compilation_unit_->GetDexFile())));
726 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
727 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
728 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
729 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
730 Handle<mirror::Class> compiling_class(hs.NewHandle(GetOutermostCompilingClass()));
731
732 return compiling_class.Get() == cls.Get();
733}
734
735
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100736bool HGraphBuilder::BuildStaticFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000737 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100738 bool is_put) {
739 uint32_t source_or_dest_reg = instruction.VRegA_21c();
740 uint16_t field_index = instruction.VRegB_21c();
741
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000742 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000743 StackHandleScope<5> hs(soa.Self());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000744 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
745 dex_compilation_unit_->GetClassLinker()->FindDexCache(*dex_compilation_unit_->GetDexFile())));
746 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
747 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
748 Handle<mirror::ArtField> resolved_field(hs.NewHandle(compiler_driver_->ResolveField(
749 soa, dex_cache, class_loader, dex_compilation_unit_, field_index, true)));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100750
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000751 if (resolved_field.Get() == nullptr) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000752 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedField);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100753 return false;
754 }
755
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000756 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
757 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
758 outer_compilation_unit_->GetClassLinker()->FindDexCache(outer_dex_file)));
759 Handle<mirror::Class> referrer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000760
761 // The index at which the field's class is stored in the DexCache's type array.
762 uint32_t storage_index;
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000763 bool is_referrer_class = (referrer_class.Get() == resolved_field->GetDeclaringClass());
764 if (is_referrer_class) {
765 storage_index = referrer_class->GetDexTypeIndex();
766 } else if (outer_dex_cache.Get() != dex_cache.Get()) {
767 // The compiler driver cannot currently understand multple dex caches involved. Just bailout.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000768 return false;
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000769 } else {
770 std::pair<bool, bool> pair = compiler_driver_->IsFastStaticField(
771 outer_dex_cache.Get(),
772 referrer_class.Get(),
773 resolved_field.Get(),
774 field_index,
775 &storage_index);
776 bool can_easily_access = is_put ? pair.second : pair.first;
777 if (!can_easily_access) {
778 return false;
779 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000780 }
781
782 // TODO: find out why this check is needed.
783 bool is_in_dex_cache = compiler_driver_->CanAssumeTypeIsPresentInDexCache(
Nicolas Geoffray6a816cf2015-03-24 16:17:56 +0000784 *outer_compilation_unit_->GetDexFile(), storage_index);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000785 bool is_initialized = resolved_field->GetDeclaringClass()->IsInitialized() && is_in_dex_cache;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000786
787 HLoadClass* constant = new (arena_) HLoadClass(storage_index, is_referrer_class, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100788 current_block_->AddInstruction(constant);
789
790 HInstruction* cls = constant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000791 if (!is_initialized && !is_referrer_class) {
Calin Juravle225ff812014-11-13 16:46:39 +0000792 cls = new (arena_) HClinitCheck(constant, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100793 current_block_->AddInstruction(cls);
794 }
795
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000796 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100797 if (is_put) {
798 // We need to keep the class alive before loading the value.
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000799 Temporaries temps(graph_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100800 temps.Add(cls);
801 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
802 DCHECK_EQ(value->GetType(), field_type);
803 current_block_->AddInstruction(
Calin Juravle52c48962014-12-16 17:02:57 +0000804 new (arena_) HStaticFieldSet(cls, value, field_type, resolved_field->GetOffset(),
805 resolved_field->IsVolatile()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100806 } else {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000807 current_block_->AddInstruction(
Calin Juravle52c48962014-12-16 17:02:57 +0000808 new (arena_) HStaticFieldGet(cls, field_type, resolved_field->GetOffset(),
809 resolved_field->IsVolatile()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100810 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
811 }
812 return true;
813}
814
Calin Juravlebacfec32014-11-14 15:54:36 +0000815void HGraphBuilder::BuildCheckedDivRem(uint16_t out_vreg,
816 uint16_t first_vreg,
817 int64_t second_vreg_or_constant,
818 uint32_t dex_pc,
819 Primitive::Type type,
820 bool second_is_constant,
821 bool isDiv) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000822 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Calin Juravled0d48522014-11-04 16:40:20 +0000823
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000824 HInstruction* first = LoadLocal(first_vreg, type);
825 HInstruction* second = nullptr;
826 if (second_is_constant) {
827 if (type == Primitive::kPrimInt) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000828 second = graph_->GetIntConstant(second_vreg_or_constant);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000829 } else {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000830 second = graph_->GetLongConstant(second_vreg_or_constant);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000831 }
832 } else {
833 second = LoadLocal(second_vreg_or_constant, type);
834 }
835
836 if (!second_is_constant
837 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0)
838 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) {
839 second = new (arena_) HDivZeroCheck(second, dex_pc);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000840 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +0000841 current_block_->AddInstruction(second);
842 temps.Add(current_block_->GetLastInstruction());
843 }
844
Calin Juravlebacfec32014-11-14 15:54:36 +0000845 if (isDiv) {
846 current_block_->AddInstruction(new (arena_) HDiv(type, first, second, dex_pc));
847 } else {
848 current_block_->AddInstruction(new (arena_) HRem(type, first, second, dex_pc));
849 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000850 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
Calin Juravled0d48522014-11-04 16:40:20 +0000851}
852
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100853void HGraphBuilder::BuildArrayAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000854 uint32_t dex_pc,
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100855 bool is_put,
856 Primitive::Type anticipated_type) {
857 uint8_t source_or_dest_reg = instruction.VRegA_23x();
858 uint8_t array_reg = instruction.VRegB_23x();
859 uint8_t index_reg = instruction.VRegC_23x();
860
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100861 // We need one temporary for the null check, one for the index, and one for the length.
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000862 Temporaries temps(graph_);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100863
864 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000865 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100866 current_block_->AddInstruction(object);
867 temps.Add(object);
868
869 HInstruction* length = new (arena_) HArrayLength(object);
870 current_block_->AddInstruction(length);
871 temps.Add(length);
872 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt);
Calin Juravle225ff812014-11-13 16:46:39 +0000873 index = new (arena_) HBoundsCheck(index, length, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100874 current_block_->AddInstruction(index);
875 temps.Add(index);
876 if (is_put) {
877 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
878 // TODO: Insert a type check node if the type is Object.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100879 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +0000880 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100881 } else {
882 current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type));
883 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
884 }
Mingyao Yange4335eb2015-03-02 15:14:13 -0800885 graph_->SetHasArrayAccesses(true);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100886}
887
Calin Juravle225ff812014-11-13 16:46:39 +0000888void HGraphBuilder::BuildFilledNewArray(uint32_t dex_pc,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100889 uint32_t type_index,
890 uint32_t number_of_vreg_arguments,
891 bool is_range,
892 uint32_t* args,
893 uint32_t register_index) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000894 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments);
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +0000895 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
896 ? kQuickAllocArrayWithAccessCheck
897 : kQuickAllocArray;
898 HInstruction* object = new (arena_) HNewArray(length, dex_pc, type_index, entrypoint);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100899 current_block_->AddInstruction(object);
900
901 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
902 DCHECK_EQ(descriptor[0], '[') << descriptor;
903 char primitive = descriptor[1];
904 DCHECK(primitive == 'I'
905 || primitive == 'L'
906 || primitive == '[') << descriptor;
907 bool is_reference_array = (primitive == 'L') || (primitive == '[');
908 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
909
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000910 Temporaries temps(graph_);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100911 temps.Add(object);
912 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
913 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000914 HInstruction* index = graph_->GetIntConstant(i);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100915 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +0000916 new (arena_) HArraySet(object, index, value, type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100917 }
918 latest_result_ = object;
919}
920
921template <typename T>
922void HGraphBuilder::BuildFillArrayData(HInstruction* object,
923 const T* data,
924 uint32_t element_count,
925 Primitive::Type anticipated_type,
Calin Juravle225ff812014-11-13 16:46:39 +0000926 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100927 for (uint32_t i = 0; i < element_count; ++i) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000928 HInstruction* index = graph_->GetIntConstant(i);
929 HInstruction* value = graph_->GetIntConstant(data[i]);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100930 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +0000931 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100932 }
933}
934
Calin Juravle225ff812014-11-13 16:46:39 +0000935void HGraphBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000936 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +0000937 HInstruction* array = LoadLocal(instruction.VRegA_31t(), Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000938 HNullCheck* null_check = new (arena_) HNullCheck(array, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000939 current_block_->AddInstruction(null_check);
940 temps.Add(null_check);
941
942 HInstruction* length = new (arena_) HArrayLength(null_check);
943 current_block_->AddInstruction(length);
944
Calin Juravle225ff812014-11-13 16:46:39 +0000945 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
Calin Juravled0d48522014-11-04 16:40:20 +0000946 const Instruction::ArrayDataPayload* payload =
947 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_start_ + payload_offset);
948 const uint8_t* data = payload->data;
949 uint32_t element_count = payload->element_count;
950
951 // Implementation of this DEX instruction seems to be that the bounds check is
952 // done before doing any stores.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000953 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1);
Calin Juravle225ff812014-11-13 16:46:39 +0000954 current_block_->AddInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
Calin Juravled0d48522014-11-04 16:40:20 +0000955
956 switch (payload->element_width) {
957 case 1:
958 BuildFillArrayData(null_check,
959 reinterpret_cast<const int8_t*>(data),
960 element_count,
961 Primitive::kPrimByte,
Calin Juravle225ff812014-11-13 16:46:39 +0000962 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000963 break;
964 case 2:
965 BuildFillArrayData(null_check,
966 reinterpret_cast<const int16_t*>(data),
967 element_count,
968 Primitive::kPrimShort,
Calin Juravle225ff812014-11-13 16:46:39 +0000969 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000970 break;
971 case 4:
972 BuildFillArrayData(null_check,
973 reinterpret_cast<const int32_t*>(data),
974 element_count,
975 Primitive::kPrimInt,
Calin Juravle225ff812014-11-13 16:46:39 +0000976 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000977 break;
978 case 8:
979 BuildFillWideArrayData(null_check,
980 reinterpret_cast<const int64_t*>(data),
981 element_count,
Calin Juravle225ff812014-11-13 16:46:39 +0000982 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000983 break;
984 default:
985 LOG(FATAL) << "Unknown element width for " << payload->element_width;
986 }
987}
988
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100989void HGraphBuilder::BuildFillWideArrayData(HInstruction* object,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +0100990 const int64_t* data,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100991 uint32_t element_count,
Calin Juravle225ff812014-11-13 16:46:39 +0000992 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100993 for (uint32_t i = 0; i < element_count; ++i) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000994 HInstruction* index = graph_->GetIntConstant(i);
995 HInstruction* value = graph_->GetLongConstant(data[i]);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100996 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +0000997 object, index, value, Primitive::kPrimLong, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100998 }
999}
1000
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001001bool HGraphBuilder::BuildTypeCheck(const Instruction& instruction,
1002 uint8_t destination,
1003 uint8_t reference,
1004 uint16_t type_index,
Calin Juravle225ff812014-11-13 16:46:39 +00001005 uint32_t dex_pc) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001006 bool type_known_final;
1007 bool type_known_abstract;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001008 // `CanAccessTypeWithoutChecks` will tell whether the method being
1009 // built is trying to access its own class, so that the generated
1010 // code can optimize for this case. However, the optimization does not
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001011 // work for inlining, so we use `IsOutermostCompilingClass` instead.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001012 bool dont_use_is_referrers_class;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001013 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
1014 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001015 &type_known_final, &type_known_abstract, &dont_use_is_referrers_class);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001016 if (!can_access) {
Calin Juravle48c2b032014-12-09 18:11:36 +00001017 MaybeRecordStat(MethodCompilationStat::kNotCompiledCantAccesType);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001018 return false;
1019 }
1020 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot);
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001021 HLoadClass* cls = new (arena_) HLoadClass(
1022 type_index, IsOutermostCompilingClass(type_index), dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001023 current_block_->AddInstruction(cls);
1024 // The class needs a temporary before being used by the type check.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001025 Temporaries temps(graph_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001026 temps.Add(cls);
1027 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
1028 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001029 new (arena_) HInstanceOf(object, cls, type_known_final, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001030 UpdateLocal(destination, current_block_->GetLastInstruction());
1031 } else {
1032 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
1033 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001034 new (arena_) HCheckCast(object, cls, type_known_final, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001035 }
1036 return true;
1037}
1038
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001039bool HGraphBuilder::NeedsAccessCheck(uint32_t type_index) const {
1040 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
1041 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index);
1042}
1043
Calin Juravle48c2b032014-12-09 18:11:36 +00001044void HGraphBuilder::BuildPackedSwitch(const Instruction& instruction, uint32_t dex_pc) {
Andreas Gamped881df52014-11-24 23:28:39 -08001045 SwitchTable table(instruction, dex_pc, false);
1046
1047 // Value to test against.
1048 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
1049
Andreas Gampee4d4d322014-12-04 09:09:57 -08001050 uint16_t num_entries = table.GetNumEntries();
1051 // There should be at least one entry here.
1052 DCHECK_GT(num_entries, 0U);
1053
Andreas Gamped881df52014-11-24 23:28:39 -08001054 // Chained cmp-and-branch, starting from starting_key.
1055 int32_t starting_key = table.GetEntryAt(0);
1056
Andreas Gamped881df52014-11-24 23:28:39 -08001057 for (size_t i = 1; i <= num_entries; i++) {
Andreas Gampee4d4d322014-12-04 09:09:57 -08001058 BuildSwitchCaseHelper(instruction, i, i == num_entries, table, value, starting_key + i - 1,
1059 table.GetEntryAt(i), dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08001060 }
Andreas Gamped881df52014-11-24 23:28:39 -08001061}
1062
Calin Juravle48c2b032014-12-09 18:11:36 +00001063void HGraphBuilder::BuildSparseSwitch(const Instruction& instruction, uint32_t dex_pc) {
Andreas Gampee4d4d322014-12-04 09:09:57 -08001064 SwitchTable table(instruction, dex_pc, true);
1065
1066 // Value to test against.
1067 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
1068
1069 uint16_t num_entries = table.GetNumEntries();
Andreas Gampee4d4d322014-12-04 09:09:57 -08001070
1071 for (size_t i = 0; i < num_entries; i++) {
1072 BuildSwitchCaseHelper(instruction, i, i == static_cast<size_t>(num_entries) - 1, table, value,
1073 table.GetEntryAt(i), table.GetEntryAt(i + num_entries), dex_pc);
1074 }
Andreas Gampee4d4d322014-12-04 09:09:57 -08001075}
1076
1077void HGraphBuilder::BuildSwitchCaseHelper(const Instruction& instruction, size_t index,
1078 bool is_last_case, const SwitchTable& table,
1079 HInstruction* value, int32_t case_value_int,
1080 int32_t target_offset, uint32_t dex_pc) {
David Brazdil852eaff2015-02-02 15:23:05 +00001081 HBasicBlock* case_target = FindBlockStartingAt(dex_pc + target_offset);
1082 DCHECK(case_target != nullptr);
1083 PotentiallyAddSuspendCheck(case_target, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001084
1085 // The current case's value.
David Brazdil8d5b8b22015-03-24 10:51:52 +00001086 HInstruction* this_case_value = graph_->GetIntConstant(case_value_int);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001087
1088 // Compare value and this_case_value.
1089 HEqual* comparison = new (arena_) HEqual(value, this_case_value);
1090 current_block_->AddInstruction(comparison);
1091 HInstruction* ifinst = new (arena_) HIf(comparison);
1092 current_block_->AddInstruction(ifinst);
1093
1094 // Case hit: use the target offset to determine where to go.
Andreas Gampee4d4d322014-12-04 09:09:57 -08001095 current_block_->AddSuccessor(case_target);
1096
1097 // Case miss: go to the next case (or default fall-through).
1098 // When there is a next case, we use the block stored with the table offset representing this
1099 // case (that is where we registered them in ComputeBranchTargets).
1100 // When there is no next case, we use the following instruction.
1101 // TODO: Find a good way to peel the last iteration to avoid conditional, but still have re-use.
1102 if (!is_last_case) {
1103 HBasicBlock* next_case_target = FindBlockStartingAt(table.GetDexPcForIndex(index));
1104 DCHECK(next_case_target != nullptr);
1105 current_block_->AddSuccessor(next_case_target);
1106
1107 // Need to manually add the block, as there is no dex-pc transition for the cases.
1108 graph_->AddBlock(next_case_target);
1109
1110 current_block_ = next_case_target;
1111 } else {
1112 HBasicBlock* default_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
1113 DCHECK(default_target != nullptr);
1114 current_block_->AddSuccessor(default_target);
1115 current_block_ = nullptr;
1116 }
1117}
1118
David Brazdil852eaff2015-02-02 15:23:05 +00001119void HGraphBuilder::PotentiallyAddSuspendCheck(HBasicBlock* target, uint32_t dex_pc) {
1120 int32_t target_offset = target->GetDexPc() - dex_pc;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001121 if (target_offset <= 0) {
David Brazdil852eaff2015-02-02 15:23:05 +00001122 // DX generates back edges to the first encountered return. We can save
1123 // time of later passes by not adding redundant suspend checks.
David Brazdil2fd6aa52015-02-02 18:58:27 +00001124 HInstruction* last_in_target = target->GetLastInstruction();
1125 if (last_in_target != nullptr &&
1126 (last_in_target->IsReturn() || last_in_target->IsReturnVoid())) {
1127 return;
David Brazdil852eaff2015-02-02 15:23:05 +00001128 }
1129
1130 // Add a suspend check to backward branches which may potentially loop. We
1131 // can remove them after we recognize loops in the graph.
Calin Juravle225ff812014-11-13 16:46:39 +00001132 current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_pc));
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001133 }
1134}
1135
Calin Juravle225ff812014-11-13 16:46:39 +00001136bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001137 if (current_block_ == nullptr) {
1138 return true; // Dead code
1139 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001140
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001141 switch (instruction.Opcode()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001142 case Instruction::CONST_4: {
1143 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001144 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001145 UpdateLocal(register_index, constant);
1146 break;
1147 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001148
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001149 case Instruction::CONST_16: {
1150 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001151 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001152 UpdateLocal(register_index, constant);
1153 break;
1154 }
1155
Dave Allison20dfc792014-06-16 20:44:29 -07001156 case Instruction::CONST: {
1157 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001158 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i());
Dave Allison20dfc792014-06-16 20:44:29 -07001159 UpdateLocal(register_index, constant);
1160 break;
1161 }
1162
1163 case Instruction::CONST_HIGH16: {
1164 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001165 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16);
Dave Allison20dfc792014-06-16 20:44:29 -07001166 UpdateLocal(register_index, constant);
1167 break;
1168 }
1169
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001170 case Instruction::CONST_WIDE_16: {
1171 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001172 // Get 16 bits of constant value, sign extended to 64 bits.
1173 int64_t value = instruction.VRegB_21s();
1174 value <<= 48;
1175 value >>= 48;
David Brazdil8d5b8b22015-03-24 10:51:52 +00001176 HLongConstant* constant = graph_->GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001177 UpdateLocal(register_index, constant);
1178 break;
1179 }
1180
1181 case Instruction::CONST_WIDE_32: {
1182 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001183 // Get 32 bits of constant value, sign extended to 64 bits.
1184 int64_t value = instruction.VRegB_31i();
1185 value <<= 32;
1186 value >>= 32;
David Brazdil8d5b8b22015-03-24 10:51:52 +00001187 HLongConstant* constant = graph_->GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001188 UpdateLocal(register_index, constant);
1189 break;
1190 }
1191
1192 case Instruction::CONST_WIDE: {
1193 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001194 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001195 UpdateLocal(register_index, constant);
1196 break;
1197 }
1198
Dave Allison20dfc792014-06-16 20:44:29 -07001199 case Instruction::CONST_WIDE_HIGH16: {
1200 int32_t register_index = instruction.VRegA();
1201 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
David Brazdil8d5b8b22015-03-24 10:51:52 +00001202 HLongConstant* constant = graph_->GetLongConstant(value);
Dave Allison20dfc792014-06-16 20:44:29 -07001203 UpdateLocal(register_index, constant);
1204 break;
1205 }
1206
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001207 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001208 case Instruction::MOVE:
1209 case Instruction::MOVE_FROM16:
1210 case Instruction::MOVE_16: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001211 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001212 UpdateLocal(instruction.VRegA(), value);
1213 break;
1214 }
1215
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001216 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001217 case Instruction::MOVE_WIDE:
1218 case Instruction::MOVE_WIDE_FROM16:
1219 case Instruction::MOVE_WIDE_16: {
1220 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong);
1221 UpdateLocal(instruction.VRegA(), value);
1222 break;
1223 }
1224
1225 case Instruction::MOVE_OBJECT:
1226 case Instruction::MOVE_OBJECT_16:
1227 case Instruction::MOVE_OBJECT_FROM16: {
1228 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot);
1229 UpdateLocal(instruction.VRegA(), value);
1230 break;
1231 }
1232
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001233 case Instruction::RETURN_VOID: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001234 BuildReturn(instruction, Primitive::kPrimVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001235 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001236 }
1237
Dave Allison20dfc792014-06-16 20:44:29 -07001238#define IF_XX(comparison, cond) \
Calin Juravle225ff812014-11-13 16:46:39 +00001239 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
1240 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001241
Dave Allison20dfc792014-06-16 20:44:29 -07001242 IF_XX(HEqual, EQ);
1243 IF_XX(HNotEqual, NE);
1244 IF_XX(HLessThan, LT);
1245 IF_XX(HLessThanOrEqual, LE);
1246 IF_XX(HGreaterThan, GT);
1247 IF_XX(HGreaterThanOrEqual, GE);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001248
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001249 case Instruction::GOTO:
1250 case Instruction::GOTO_16:
1251 case Instruction::GOTO_32: {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001252 int32_t offset = instruction.GetTargetOffset();
Calin Juravle225ff812014-11-13 16:46:39 +00001253 HBasicBlock* target = FindBlockStartingAt(offset + dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001254 DCHECK(target != nullptr);
David Brazdil852eaff2015-02-02 15:23:05 +00001255 PotentiallyAddSuspendCheck(target, dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001256 current_block_->AddInstruction(new (arena_) HGoto());
1257 current_block_->AddSuccessor(target);
1258 current_block_ = nullptr;
1259 break;
1260 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001261
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001262 case Instruction::RETURN: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001263 DCHECK_NE(return_type_, Primitive::kPrimNot);
1264 DCHECK_NE(return_type_, Primitive::kPrimLong);
1265 DCHECK_NE(return_type_, Primitive::kPrimDouble);
1266 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001267 break;
1268 }
1269
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001270 case Instruction::RETURN_OBJECT: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001271 DCHECK(return_type_ == Primitive::kPrimNot);
1272 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001273 break;
1274 }
1275
1276 case Instruction::RETURN_WIDE: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001277 DCHECK(return_type_ == Primitive::kPrimDouble || return_type_ == Primitive::kPrimLong);
1278 BuildReturn(instruction, return_type_);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001279 break;
1280 }
1281
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001282 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001283 case Instruction::INVOKE_INTERFACE:
1284 case Instruction::INVOKE_STATIC:
1285 case Instruction::INVOKE_SUPER:
1286 case Instruction::INVOKE_VIRTUAL: {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001287 uint32_t method_idx = instruction.VRegB_35c();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001288 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001289 uint32_t args[5];
Ian Rogers29a26482014-05-02 15:27:29 -07001290 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00001291 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001292 number_of_vreg_arguments, false, args, -1)) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001293 return false;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001294 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001295 break;
1296 }
1297
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001298 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001299 case Instruction::INVOKE_INTERFACE_RANGE:
1300 case Instruction::INVOKE_STATIC_RANGE:
1301 case Instruction::INVOKE_SUPER_RANGE:
1302 case Instruction::INVOKE_VIRTUAL_RANGE: {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001303 uint32_t method_idx = instruction.VRegB_3rc();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001304 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1305 uint32_t register_index = instruction.VRegC();
Calin Juravle225ff812014-11-13 16:46:39 +00001306 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001307 number_of_vreg_arguments, true, nullptr, register_index)) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001308 return false;
1309 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001310 break;
1311 }
1312
Roland Levillain88cb1752014-10-20 16:36:47 +01001313 case Instruction::NEG_INT: {
1314 Unop_12x<HNeg>(instruction, Primitive::kPrimInt);
1315 break;
1316 }
1317
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001318 case Instruction::NEG_LONG: {
1319 Unop_12x<HNeg>(instruction, Primitive::kPrimLong);
1320 break;
1321 }
1322
Roland Levillain3dbcb382014-10-28 17:30:07 +00001323 case Instruction::NEG_FLOAT: {
1324 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat);
1325 break;
1326 }
1327
1328 case Instruction::NEG_DOUBLE: {
1329 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble);
1330 break;
1331 }
1332
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001333 case Instruction::NOT_INT: {
1334 Unop_12x<HNot>(instruction, Primitive::kPrimInt);
1335 break;
1336 }
1337
Roland Levillain70566432014-10-24 16:20:17 +01001338 case Instruction::NOT_LONG: {
1339 Unop_12x<HNot>(instruction, Primitive::kPrimLong);
1340 break;
1341 }
1342
Roland Levillaindff1f282014-11-05 14:15:05 +00001343 case Instruction::INT_TO_LONG: {
Roland Levillain624279f2014-12-04 11:54:28 +00001344 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong, dex_pc);
Roland Levillaindff1f282014-11-05 14:15:05 +00001345 break;
1346 }
1347
Roland Levillaincff13742014-11-17 14:32:17 +00001348 case Instruction::INT_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001349 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00001350 break;
1351 }
1352
1353 case Instruction::INT_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001354 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00001355 break;
1356 }
1357
Roland Levillain946e1432014-11-11 17:35:19 +00001358 case Instruction::LONG_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001359 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt, dex_pc);
Roland Levillain946e1432014-11-11 17:35:19 +00001360 break;
1361 }
1362
Roland Levillain6d0e4832014-11-27 18:31:21 +00001363 case Instruction::LONG_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001364 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimFloat, dex_pc);
Roland Levillain6d0e4832014-11-27 18:31:21 +00001365 break;
1366 }
1367
Roland Levillain647b9ed2014-11-27 12:06:00 +00001368 case Instruction::LONG_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001369 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble, dex_pc);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001370 break;
1371 }
1372
Roland Levillain3f8f9362014-12-02 17:45:01 +00001373 case Instruction::FLOAT_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001374 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimInt, dex_pc);
1375 break;
1376 }
1377
1378 case Instruction::FLOAT_TO_LONG: {
1379 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimLong, dex_pc);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001380 break;
1381 }
1382
Roland Levillain8964e2b2014-12-04 12:10:50 +00001383 case Instruction::FLOAT_TO_DOUBLE: {
1384 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimDouble, dex_pc);
1385 break;
1386 }
1387
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001388 case Instruction::DOUBLE_TO_INT: {
1389 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimInt, dex_pc);
1390 break;
1391 }
1392
1393 case Instruction::DOUBLE_TO_LONG: {
1394 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimLong, dex_pc);
1395 break;
1396 }
1397
Roland Levillain8964e2b2014-12-04 12:10:50 +00001398 case Instruction::DOUBLE_TO_FLOAT: {
1399 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimFloat, dex_pc);
1400 break;
1401 }
1402
Roland Levillain51d3fc42014-11-13 14:11:42 +00001403 case Instruction::INT_TO_BYTE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001404 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte, dex_pc);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001405 break;
1406 }
1407
Roland Levillain01a8d712014-11-14 16:27:39 +00001408 case Instruction::INT_TO_SHORT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001409 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort, dex_pc);
Roland Levillain01a8d712014-11-14 16:27:39 +00001410 break;
1411 }
1412
Roland Levillain981e4542014-11-14 11:47:14 +00001413 case Instruction::INT_TO_CHAR: {
Roland Levillain624279f2014-12-04 11:54:28 +00001414 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar, dex_pc);
Roland Levillain981e4542014-11-14 11:47:14 +00001415 break;
1416 }
1417
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001418 case Instruction::ADD_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001419 Binop_23x<HAdd>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001420 break;
1421 }
1422
1423 case Instruction::ADD_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001424 Binop_23x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001425 break;
1426 }
1427
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001428 case Instruction::ADD_DOUBLE: {
1429 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble);
1430 break;
1431 }
1432
1433 case Instruction::ADD_FLOAT: {
1434 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat);
1435 break;
1436 }
1437
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001438 case Instruction::SUB_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001439 Binop_23x<HSub>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001440 break;
1441 }
1442
1443 case Instruction::SUB_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001444 Binop_23x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001445 break;
1446 }
1447
Calin Juravle096cc022014-10-23 17:01:13 +01001448 case Instruction::SUB_FLOAT: {
1449 Binop_23x<HSub>(instruction, Primitive::kPrimFloat);
1450 break;
1451 }
1452
1453 case Instruction::SUB_DOUBLE: {
1454 Binop_23x<HSub>(instruction, Primitive::kPrimDouble);
1455 break;
1456 }
1457
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001458 case Instruction::ADD_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001459 Binop_12x<HAdd>(instruction, Primitive::kPrimInt);
1460 break;
1461 }
1462
Calin Juravle34bacdf2014-10-07 20:23:36 +01001463 case Instruction::MUL_INT: {
1464 Binop_23x<HMul>(instruction, Primitive::kPrimInt);
1465 break;
1466 }
1467
1468 case Instruction::MUL_LONG: {
1469 Binop_23x<HMul>(instruction, Primitive::kPrimLong);
1470 break;
1471 }
1472
Calin Juravleb5bfa962014-10-21 18:02:24 +01001473 case Instruction::MUL_FLOAT: {
1474 Binop_23x<HMul>(instruction, Primitive::kPrimFloat);
1475 break;
1476 }
1477
1478 case Instruction::MUL_DOUBLE: {
1479 Binop_23x<HMul>(instruction, Primitive::kPrimDouble);
1480 break;
1481 }
1482
Calin Juravled0d48522014-11-04 16:40:20 +00001483 case Instruction::DIV_INT: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001484 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1485 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravled0d48522014-11-04 16:40:20 +00001486 break;
1487 }
1488
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001489 case Instruction::DIV_LONG: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001490 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1491 dex_pc, Primitive::kPrimLong, false, true);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001492 break;
1493 }
1494
Calin Juravle7c4954d2014-10-28 16:57:40 +00001495 case Instruction::DIV_FLOAT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001496 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001497 break;
1498 }
1499
1500 case Instruction::DIV_DOUBLE: {
Calin Juravle225ff812014-11-13 16:46:39 +00001501 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001502 break;
1503 }
1504
Calin Juravlebacfec32014-11-14 15:54:36 +00001505 case Instruction::REM_INT: {
1506 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1507 dex_pc, Primitive::kPrimInt, false, false);
1508 break;
1509 }
1510
1511 case Instruction::REM_LONG: {
1512 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1513 dex_pc, Primitive::kPrimLong, false, false);
1514 break;
1515 }
1516
Calin Juravled2ec87d2014-12-08 14:24:46 +00001517 case Instruction::REM_FLOAT: {
1518 Binop_23x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
1519 break;
1520 }
1521
1522 case Instruction::REM_DOUBLE: {
1523 Binop_23x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
1524 break;
1525 }
1526
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001527 case Instruction::AND_INT: {
1528 Binop_23x<HAnd>(instruction, Primitive::kPrimInt);
1529 break;
1530 }
1531
1532 case Instruction::AND_LONG: {
1533 Binop_23x<HAnd>(instruction, Primitive::kPrimLong);
1534 break;
1535 }
1536
Calin Juravle9aec02f2014-11-18 23:06:35 +00001537 case Instruction::SHL_INT: {
1538 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt);
1539 break;
1540 }
1541
1542 case Instruction::SHL_LONG: {
1543 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong);
1544 break;
1545 }
1546
1547 case Instruction::SHR_INT: {
1548 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt);
1549 break;
1550 }
1551
1552 case Instruction::SHR_LONG: {
1553 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong);
1554 break;
1555 }
1556
1557 case Instruction::USHR_INT: {
1558 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt);
1559 break;
1560 }
1561
1562 case Instruction::USHR_LONG: {
1563 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong);
1564 break;
1565 }
1566
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001567 case Instruction::OR_INT: {
1568 Binop_23x<HOr>(instruction, Primitive::kPrimInt);
1569 break;
1570 }
1571
1572 case Instruction::OR_LONG: {
1573 Binop_23x<HOr>(instruction, Primitive::kPrimLong);
1574 break;
1575 }
1576
1577 case Instruction::XOR_INT: {
1578 Binop_23x<HXor>(instruction, Primitive::kPrimInt);
1579 break;
1580 }
1581
1582 case Instruction::XOR_LONG: {
1583 Binop_23x<HXor>(instruction, Primitive::kPrimLong);
1584 break;
1585 }
1586
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001587 case Instruction::ADD_LONG_2ADDR: {
1588 Binop_12x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001589 break;
1590 }
1591
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001592 case Instruction::ADD_DOUBLE_2ADDR: {
1593 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble);
1594 break;
1595 }
1596
1597 case Instruction::ADD_FLOAT_2ADDR: {
1598 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat);
1599 break;
1600 }
1601
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001602 case Instruction::SUB_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001603 Binop_12x<HSub>(instruction, Primitive::kPrimInt);
1604 break;
1605 }
1606
1607 case Instruction::SUB_LONG_2ADDR: {
1608 Binop_12x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001609 break;
1610 }
1611
Calin Juravle096cc022014-10-23 17:01:13 +01001612 case Instruction::SUB_FLOAT_2ADDR: {
1613 Binop_12x<HSub>(instruction, Primitive::kPrimFloat);
1614 break;
1615 }
1616
1617 case Instruction::SUB_DOUBLE_2ADDR: {
1618 Binop_12x<HSub>(instruction, Primitive::kPrimDouble);
1619 break;
1620 }
1621
Calin Juravle34bacdf2014-10-07 20:23:36 +01001622 case Instruction::MUL_INT_2ADDR: {
1623 Binop_12x<HMul>(instruction, Primitive::kPrimInt);
1624 break;
1625 }
1626
1627 case Instruction::MUL_LONG_2ADDR: {
1628 Binop_12x<HMul>(instruction, Primitive::kPrimLong);
1629 break;
1630 }
1631
Calin Juravleb5bfa962014-10-21 18:02:24 +01001632 case Instruction::MUL_FLOAT_2ADDR: {
1633 Binop_12x<HMul>(instruction, Primitive::kPrimFloat);
1634 break;
1635 }
1636
1637 case Instruction::MUL_DOUBLE_2ADDR: {
1638 Binop_12x<HMul>(instruction, Primitive::kPrimDouble);
1639 break;
1640 }
1641
Calin Juravle865fc882014-11-06 17:09:03 +00001642 case Instruction::DIV_INT_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001643 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1644 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravle865fc882014-11-06 17:09:03 +00001645 break;
1646 }
1647
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001648 case Instruction::DIV_LONG_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001649 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1650 dex_pc, Primitive::kPrimLong, false, true);
1651 break;
1652 }
1653
1654 case Instruction::REM_INT_2ADDR: {
1655 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1656 dex_pc, Primitive::kPrimInt, false, false);
1657 break;
1658 }
1659
1660 case Instruction::REM_LONG_2ADDR: {
1661 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1662 dex_pc, Primitive::kPrimLong, false, false);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001663 break;
1664 }
1665
Calin Juravled2ec87d2014-12-08 14:24:46 +00001666 case Instruction::REM_FLOAT_2ADDR: {
1667 Binop_12x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
1668 break;
1669 }
1670
1671 case Instruction::REM_DOUBLE_2ADDR: {
1672 Binop_12x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
1673 break;
1674 }
1675
Calin Juravle9aec02f2014-11-18 23:06:35 +00001676 case Instruction::SHL_INT_2ADDR: {
1677 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt);
1678 break;
1679 }
1680
1681 case Instruction::SHL_LONG_2ADDR: {
1682 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong);
1683 break;
1684 }
1685
1686 case Instruction::SHR_INT_2ADDR: {
1687 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt);
1688 break;
1689 }
1690
1691 case Instruction::SHR_LONG_2ADDR: {
1692 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong);
1693 break;
1694 }
1695
1696 case Instruction::USHR_INT_2ADDR: {
1697 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt);
1698 break;
1699 }
1700
1701 case Instruction::USHR_LONG_2ADDR: {
1702 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong);
1703 break;
1704 }
1705
Calin Juravle7c4954d2014-10-28 16:57:40 +00001706 case Instruction::DIV_FLOAT_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00001707 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001708 break;
1709 }
1710
1711 case Instruction::DIV_DOUBLE_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00001712 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001713 break;
1714 }
1715
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001716 case Instruction::AND_INT_2ADDR: {
1717 Binop_12x<HAnd>(instruction, Primitive::kPrimInt);
1718 break;
1719 }
1720
1721 case Instruction::AND_LONG_2ADDR: {
1722 Binop_12x<HAnd>(instruction, Primitive::kPrimLong);
1723 break;
1724 }
1725
1726 case Instruction::OR_INT_2ADDR: {
1727 Binop_12x<HOr>(instruction, Primitive::kPrimInt);
1728 break;
1729 }
1730
1731 case Instruction::OR_LONG_2ADDR: {
1732 Binop_12x<HOr>(instruction, Primitive::kPrimLong);
1733 break;
1734 }
1735
1736 case Instruction::XOR_INT_2ADDR: {
1737 Binop_12x<HXor>(instruction, Primitive::kPrimInt);
1738 break;
1739 }
1740
1741 case Instruction::XOR_LONG_2ADDR: {
1742 Binop_12x<HXor>(instruction, Primitive::kPrimLong);
1743 break;
1744 }
1745
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001746 case Instruction::ADD_INT_LIT16: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001747 Binop_22s<HAdd>(instruction, false);
1748 break;
1749 }
1750
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001751 case Instruction::AND_INT_LIT16: {
1752 Binop_22s<HAnd>(instruction, false);
1753 break;
1754 }
1755
1756 case Instruction::OR_INT_LIT16: {
1757 Binop_22s<HOr>(instruction, false);
1758 break;
1759 }
1760
1761 case Instruction::XOR_INT_LIT16: {
1762 Binop_22s<HXor>(instruction, false);
1763 break;
1764 }
1765
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001766 case Instruction::RSUB_INT: {
1767 Binop_22s<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001768 break;
1769 }
1770
Calin Juravle34bacdf2014-10-07 20:23:36 +01001771 case Instruction::MUL_INT_LIT16: {
1772 Binop_22s<HMul>(instruction, false);
1773 break;
1774 }
1775
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001776 case Instruction::ADD_INT_LIT8: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001777 Binop_22b<HAdd>(instruction, false);
1778 break;
1779 }
1780
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001781 case Instruction::AND_INT_LIT8: {
1782 Binop_22b<HAnd>(instruction, false);
1783 break;
1784 }
1785
1786 case Instruction::OR_INT_LIT8: {
1787 Binop_22b<HOr>(instruction, false);
1788 break;
1789 }
1790
1791 case Instruction::XOR_INT_LIT8: {
1792 Binop_22b<HXor>(instruction, false);
1793 break;
1794 }
1795
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001796 case Instruction::RSUB_INT_LIT8: {
1797 Binop_22b<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001798 break;
1799 }
1800
Calin Juravle34bacdf2014-10-07 20:23:36 +01001801 case Instruction::MUL_INT_LIT8: {
1802 Binop_22b<HMul>(instruction, false);
1803 break;
1804 }
1805
Calin Juravled0d48522014-11-04 16:40:20 +00001806 case Instruction::DIV_INT_LIT16:
1807 case Instruction::DIV_INT_LIT8: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001808 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1809 dex_pc, Primitive::kPrimInt, true, true);
1810 break;
1811 }
1812
1813 case Instruction::REM_INT_LIT16:
1814 case Instruction::REM_INT_LIT8: {
1815 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1816 dex_pc, Primitive::kPrimInt, true, false);
Calin Juravled0d48522014-11-04 16:40:20 +00001817 break;
1818 }
1819
Calin Juravle9aec02f2014-11-18 23:06:35 +00001820 case Instruction::SHL_INT_LIT8: {
1821 Binop_22b<HShl>(instruction, false);
1822 break;
1823 }
1824
1825 case Instruction::SHR_INT_LIT8: {
1826 Binop_22b<HShr>(instruction, false);
1827 break;
1828 }
1829
1830 case Instruction::USHR_INT_LIT8: {
1831 Binop_22b<HUShr>(instruction, false);
1832 break;
1833 }
1834
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001835 case Instruction::NEW_INSTANCE: {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001836 uint16_t type_index = instruction.VRegB_21c();
1837 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
1838 ? kQuickAllocObjectWithAccessCheck
1839 : kQuickAllocObject;
1840
1841 current_block_->AddInstruction(new (arena_) HNewInstance(dex_pc, type_index, entrypoint));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001842 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
1843 break;
1844 }
1845
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001846 case Instruction::NEW_ARRAY: {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001847 uint16_t type_index = instruction.VRegC_22c();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001848 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt);
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001849 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
1850 ? kQuickAllocArrayWithAccessCheck
1851 : kQuickAllocArray;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001852 current_block_->AddInstruction(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001853 new (arena_) HNewArray(length, dex_pc, type_index, entrypoint));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001854 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
1855 break;
1856 }
1857
1858 case Instruction::FILLED_NEW_ARRAY: {
1859 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
1860 uint32_t type_index = instruction.VRegB_35c();
1861 uint32_t args[5];
1862 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00001863 BuildFilledNewArray(dex_pc, type_index, number_of_vreg_arguments, false, args, 0);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001864 break;
1865 }
1866
1867 case Instruction::FILLED_NEW_ARRAY_RANGE: {
1868 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1869 uint32_t type_index = instruction.VRegB_3rc();
1870 uint32_t register_index = instruction.VRegC_3rc();
1871 BuildFilledNewArray(
Calin Juravle225ff812014-11-13 16:46:39 +00001872 dex_pc, type_index, number_of_vreg_arguments, true, nullptr, register_index);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001873 break;
1874 }
1875
1876 case Instruction::FILL_ARRAY_DATA: {
Calin Juravle225ff812014-11-13 16:46:39 +00001877 BuildFillArrayData(instruction, dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001878 break;
1879 }
1880
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001881 case Instruction::MOVE_RESULT:
Dave Allison20dfc792014-06-16 20:44:29 -07001882 case Instruction::MOVE_RESULT_WIDE:
1883 case Instruction::MOVE_RESULT_OBJECT:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001884 UpdateLocal(instruction.VRegA(), latest_result_);
1885 latest_result_ = nullptr;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001886 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001887
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001888 case Instruction::CMP_LONG: {
Calin Juravleddb7df22014-11-25 20:56:51 +00001889 Binop_23x_cmp(instruction, Primitive::kPrimLong, HCompare::kNoBias);
1890 break;
1891 }
1892
1893 case Instruction::CMPG_FLOAT: {
1894 Binop_23x_cmp(instruction, Primitive::kPrimFloat, HCompare::kGtBias);
1895 break;
1896 }
1897
1898 case Instruction::CMPG_DOUBLE: {
1899 Binop_23x_cmp(instruction, Primitive::kPrimDouble, HCompare::kGtBias);
1900 break;
1901 }
1902
1903 case Instruction::CMPL_FLOAT: {
1904 Binop_23x_cmp(instruction, Primitive::kPrimFloat, HCompare::kLtBias);
1905 break;
1906 }
1907
1908 case Instruction::CMPL_DOUBLE: {
1909 Binop_23x_cmp(instruction, Primitive::kPrimDouble, HCompare::kLtBias);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001910 break;
1911 }
1912
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001913 case Instruction::NOP:
1914 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001915
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001916 case Instruction::IGET:
1917 case Instruction::IGET_WIDE:
1918 case Instruction::IGET_OBJECT:
1919 case Instruction::IGET_BOOLEAN:
1920 case Instruction::IGET_BYTE:
1921 case Instruction::IGET_CHAR:
1922 case Instruction::IGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001923 if (!BuildInstanceFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001924 return false;
1925 }
1926 break;
1927 }
1928
1929 case Instruction::IPUT:
1930 case Instruction::IPUT_WIDE:
1931 case Instruction::IPUT_OBJECT:
1932 case Instruction::IPUT_BOOLEAN:
1933 case Instruction::IPUT_BYTE:
1934 case Instruction::IPUT_CHAR:
1935 case Instruction::IPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001936 if (!BuildInstanceFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001937 return false;
1938 }
1939 break;
1940 }
1941
1942 case Instruction::SGET:
1943 case Instruction::SGET_WIDE:
1944 case Instruction::SGET_OBJECT:
1945 case Instruction::SGET_BOOLEAN:
1946 case Instruction::SGET_BYTE:
1947 case Instruction::SGET_CHAR:
1948 case Instruction::SGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001949 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001950 return false;
1951 }
1952 break;
1953 }
1954
1955 case Instruction::SPUT:
1956 case Instruction::SPUT_WIDE:
1957 case Instruction::SPUT_OBJECT:
1958 case Instruction::SPUT_BOOLEAN:
1959 case Instruction::SPUT_BYTE:
1960 case Instruction::SPUT_CHAR:
1961 case Instruction::SPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001962 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001963 return false;
1964 }
1965 break;
1966 }
1967
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001968#define ARRAY_XX(kind, anticipated_type) \
1969 case Instruction::AGET##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00001970 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001971 break; \
1972 } \
1973 case Instruction::APUT##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00001974 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001975 break; \
1976 }
1977
1978 ARRAY_XX(, Primitive::kPrimInt);
1979 ARRAY_XX(_WIDE, Primitive::kPrimLong);
1980 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
1981 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
1982 ARRAY_XX(_BYTE, Primitive::kPrimByte);
1983 ARRAY_XX(_CHAR, Primitive::kPrimChar);
1984 ARRAY_XX(_SHORT, Primitive::kPrimShort);
1985
Nicolas Geoffray39468442014-09-02 15:17:15 +01001986 case Instruction::ARRAY_LENGTH: {
1987 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001988 // No need for a temporary for the null check, it is the only input of the following
1989 // instruction.
Calin Juravle225ff812014-11-13 16:46:39 +00001990 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001991 current_block_->AddInstruction(object);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001992 current_block_->AddInstruction(new (arena_) HArrayLength(object));
1993 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
1994 break;
1995 }
1996
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001997 case Instruction::CONST_STRING: {
Calin Juravle225ff812014-11-13 16:46:39 +00001998 current_block_->AddInstruction(new (arena_) HLoadString(instruction.VRegB_21c(), dex_pc));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001999 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2000 break;
2001 }
2002
2003 case Instruction::CONST_STRING_JUMBO: {
Calin Juravle225ff812014-11-13 16:46:39 +00002004 current_block_->AddInstruction(new (arena_) HLoadString(instruction.VRegB_31c(), dex_pc));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002005 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
2006 break;
2007 }
2008
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002009 case Instruction::CONST_CLASS: {
2010 uint16_t type_index = instruction.VRegB_21c();
2011 bool type_known_final;
2012 bool type_known_abstract;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002013 bool dont_use_is_referrers_class;
2014 // `CanAccessTypeWithoutChecks` will tell whether the method being
2015 // built is trying to access its own class, so that the generated
2016 // code can optimize for this case. However, the optimization does not
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002017 // work for inlining, so we use `IsOutermostCompilingClass` instead.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002018 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
2019 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002020 &type_known_final, &type_known_abstract, &dont_use_is_referrers_class);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002021 if (!can_access) {
Calin Juravle48c2b032014-12-09 18:11:36 +00002022 MaybeRecordStat(MethodCompilationStat::kNotCompiledCantAccesType);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002023 return false;
2024 }
2025 current_block_->AddInstruction(
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002026 new (arena_) HLoadClass(type_index, IsOutermostCompilingClass(type_index), dex_pc));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002027 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2028 break;
2029 }
2030
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002031 case Instruction::MOVE_EXCEPTION: {
2032 current_block_->AddInstruction(new (arena_) HLoadException());
2033 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
2034 break;
2035 }
2036
2037 case Instruction::THROW: {
2038 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00002039 current_block_->AddInstruction(new (arena_) HThrow(exception, dex_pc));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002040 // A throw instruction must branch to the exit block.
2041 current_block_->AddSuccessor(exit_block_);
2042 // We finished building this block. Set the current block to null to avoid
2043 // adding dead instructions to it.
2044 current_block_ = nullptr;
2045 break;
2046 }
2047
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002048 case Instruction::INSTANCE_OF: {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002049 uint8_t destination = instruction.VRegA_22c();
2050 uint8_t reference = instruction.VRegB_22c();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002051 uint16_t type_index = instruction.VRegC_22c();
Calin Juravle225ff812014-11-13 16:46:39 +00002052 if (!BuildTypeCheck(instruction, destination, reference, type_index, dex_pc)) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002053 return false;
2054 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002055 break;
2056 }
2057
2058 case Instruction::CHECK_CAST: {
2059 uint8_t reference = instruction.VRegA_21c();
2060 uint16_t type_index = instruction.VRegB_21c();
Calin Juravle225ff812014-11-13 16:46:39 +00002061 if (!BuildTypeCheck(instruction, -1, reference, type_index, dex_pc)) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002062 return false;
2063 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002064 break;
2065 }
2066
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002067 case Instruction::MONITOR_ENTER: {
2068 current_block_->AddInstruction(new (arena_) HMonitorOperation(
2069 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2070 HMonitorOperation::kEnter,
Calin Juravle225ff812014-11-13 16:46:39 +00002071 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002072 break;
2073 }
2074
2075 case Instruction::MONITOR_EXIT: {
2076 current_block_->AddInstruction(new (arena_) HMonitorOperation(
2077 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2078 HMonitorOperation::kExit,
Calin Juravle225ff812014-11-13 16:46:39 +00002079 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002080 break;
2081 }
2082
Andreas Gamped881df52014-11-24 23:28:39 -08002083 case Instruction::PACKED_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002084 BuildPackedSwitch(instruction, dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08002085 break;
2086 }
2087
Andreas Gampee4d4d322014-12-04 09:09:57 -08002088 case Instruction::SPARSE_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002089 BuildSparseSwitch(instruction, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08002090 break;
2091 }
2092
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002093 default:
Calin Juravle48c2b032014-12-09 18:11:36 +00002094 VLOG(compiler) << "Did not compile "
2095 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
2096 << " because of unhandled instruction "
2097 << instruction.Name();
2098 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnhandledInstruction);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002099 return false;
2100 }
2101 return true;
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00002102} // NOLINT(readability/fn_size)
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002103
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002104HLocal* HGraphBuilder::GetLocalAt(int register_index) const {
2105 return locals_.Get(register_index);
2106}
2107
2108void HGraphBuilder::UpdateLocal(int register_index, HInstruction* instruction) const {
2109 HLocal* local = GetLocalAt(register_index);
2110 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction));
2111}
2112
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002113HInstruction* HGraphBuilder::LoadLocal(int register_index, Primitive::Type type) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002114 HLocal* local = GetLocalAt(register_index);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002115 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type));
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002116 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002117}
2118
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002119} // namespace art