blob: 8a64d814851ed2dd4e347a7edd8cad3bb2e2e9ae [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Nicolas Geoffraye5038322014-07-04 09:41:32 +010017#include "builder.h"
18
Mathieu Chartierc7853442015-03-27 14:35:38 -070019#include "art_field-inl.h"
Andreas Gamped881df52014-11-24 23:28:39 -080020#include "base/logging.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010021#include "class_linker.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000022#include "dex_file-inl.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000023#include "dex_instruction-inl.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010024#include "driver/compiler_driver-inl.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000025#include "driver/compiler_options.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010026#include "mirror/class_loader.h"
27#include "mirror/dex_cache.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000028#include "nodes.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000029#include "primitive.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010030#include "scoped_thread_state_change.h"
31#include "thread.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000032
33namespace art {
34
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010035/**
36 * Helper class to add HTemporary instructions. This class is used when
37 * converting a DEX instruction to multiple HInstruction, and where those
38 * instructions do not die at the following instruction, but instead spans
39 * multiple instructions.
40 */
41class Temporaries : public ValueObject {
42 public:
Calin Juravlef97f9fb2014-11-11 15:38:19 +000043 explicit Temporaries(HGraph* graph) : graph_(graph), index_(0) {}
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010044
45 void Add(HInstruction* instruction) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +000046 HInstruction* temp = new (graph_->GetArena()) HTemporary(index_);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010047 instruction->GetBlock()->AddInstruction(temp);
Calin Juravlef97f9fb2014-11-11 15:38:19 +000048
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010049 DCHECK(temp->GetPrevious() == instruction);
Calin Juravlef97f9fb2014-11-11 15:38:19 +000050
51 size_t offset;
52 if (instruction->GetType() == Primitive::kPrimLong
53 || instruction->GetType() == Primitive::kPrimDouble) {
54 offset = 2;
55 } else {
56 offset = 1;
57 }
58 index_ += offset;
59
60 graph_->UpdateTemporariesVRegSlots(index_);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010061 }
62
63 private:
64 HGraph* const graph_;
65
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010066 // Current index in the temporary stack, updated by `Add`.
67 size_t index_;
68};
69
Andreas Gamped881df52014-11-24 23:28:39 -080070class SwitchTable : public ValueObject {
71 public:
72 SwitchTable(const Instruction& instruction, uint32_t dex_pc, bool sparse)
73 : instruction_(instruction), dex_pc_(dex_pc), sparse_(sparse) {
74 int32_t table_offset = instruction.VRegB_31t();
75 const uint16_t* table = reinterpret_cast<const uint16_t*>(&instruction) + table_offset;
76 if (sparse) {
77 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
78 } else {
79 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
80 }
81 num_entries_ = table[1];
82 values_ = reinterpret_cast<const int32_t*>(&table[2]);
83 }
84
85 uint16_t GetNumEntries() const {
86 return num_entries_;
87 }
88
Andreas Gampee4d4d322014-12-04 09:09:57 -080089 void CheckIndex(size_t index) const {
90 if (sparse_) {
91 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
92 DCHECK_LT(index, 2 * static_cast<size_t>(num_entries_));
93 } else {
94 // In a packed table, we have the starting key and num_entries_ values.
95 DCHECK_LT(index, 1 + static_cast<size_t>(num_entries_));
96 }
97 }
98
Andreas Gamped881df52014-11-24 23:28:39 -080099 int32_t GetEntryAt(size_t index) const {
Andreas Gampee4d4d322014-12-04 09:09:57 -0800100 CheckIndex(index);
Andreas Gamped881df52014-11-24 23:28:39 -0800101 return values_[index];
102 }
103
104 uint32_t GetDexPcForIndex(size_t index) const {
Andreas Gampee4d4d322014-12-04 09:09:57 -0800105 CheckIndex(index);
Andreas Gamped881df52014-11-24 23:28:39 -0800106 return dex_pc_ +
107 (reinterpret_cast<const int16_t*>(values_ + index) -
108 reinterpret_cast<const int16_t*>(&instruction_));
109 }
110
Andreas Gampee4d4d322014-12-04 09:09:57 -0800111 // Index of the first value in the table.
112 size_t GetFirstValueIndex() const {
113 if (sparse_) {
114 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
115 return num_entries_;
116 } else {
117 // In a packed table, we have the starting key and num_entries_ values.
118 return 1;
119 }
120 }
121
Andreas Gamped881df52014-11-24 23:28:39 -0800122 private:
123 const Instruction& instruction_;
124 const uint32_t dex_pc_;
125
126 // Whether this is a sparse-switch table (or a packed-switch one).
127 const bool sparse_;
128
129 // This can't be const as it needs to be computed off of the given instruction, and complicated
130 // expressions in the initializer list seemed very ugly.
131 uint16_t num_entries_;
132
133 const int32_t* values_;
134
135 DISALLOW_COPY_AND_ASSIGN(SwitchTable);
136};
137
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100138void HGraphBuilder::InitializeLocals(uint16_t count) {
139 graph_->SetNumberOfVRegs(count);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000140 locals_.SetSize(count);
141 for (int i = 0; i < count; i++) {
142 HLocal* local = new (arena_) HLocal(i);
143 entry_block_->AddInstruction(local);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000144 locals_.Put(i, local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000145 }
146}
147
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000148void HGraphBuilder::InitializeParameters(uint16_t number_of_parameters) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100149 // dex_compilation_unit_ is null only when unit testing.
150 if (dex_compilation_unit_ == nullptr) {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000151 return;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100152 }
153
154 graph_->SetNumberOfInVRegs(number_of_parameters);
155 const char* shorty = dex_compilation_unit_->GetShorty();
156 int locals_index = locals_.Size() - number_of_parameters;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100157 int parameter_index = 0;
158
159 if (!dex_compilation_unit_->IsStatic()) {
160 // Add the implicit 'this' argument, not expressed in the signature.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100161 HParameterValue* parameter =
Calin Juravle10e244f2015-01-26 18:54:32 +0000162 new (arena_) HParameterValue(parameter_index++, Primitive::kPrimNot, true);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100163 entry_block_->AddInstruction(parameter);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100164 HLocal* local = GetLocalAt(locals_index++);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100165 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100166 number_of_parameters--;
167 }
168
169 uint32_t pos = 1;
170 for (int i = 0; i < number_of_parameters; i++) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100171 HParameterValue* parameter =
172 new (arena_) HParameterValue(parameter_index++, Primitive::GetType(shorty[pos++]));
173 entry_block_->AddInstruction(parameter);
174 HLocal* local = GetLocalAt(locals_index++);
175 // Store the parameter value in the local that the dex code will use
176 // to reference that parameter.
177 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter));
178 bool is_wide = (parameter->GetType() == Primitive::kPrimLong)
179 || (parameter->GetType() == Primitive::kPrimDouble);
180 if (is_wide) {
181 i++;
182 locals_index++;
183 parameter_index++;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100184 }
185 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100186}
187
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100188template<typename T>
Calin Juravle225ff812014-11-13 16:46:39 +0000189void HGraphBuilder::If_22t(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000190 int32_t target_offset = instruction.GetTargetOffset();
David Brazdil852eaff2015-02-02 15:23:05 +0000191 HBasicBlock* branch_target = FindBlockStartingAt(dex_pc + target_offset);
192 HBasicBlock* fallthrough_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
193 DCHECK(branch_target != nullptr);
194 DCHECK(fallthrough_target != nullptr);
195 PotentiallyAddSuspendCheck(branch_target, dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100196 HInstruction* first = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
197 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Dave Allison20dfc792014-06-16 20:44:29 -0700198 T* comparison = new (arena_) T(first, second);
199 current_block_->AddInstruction(comparison);
200 HInstruction* ifinst = new (arena_) HIf(comparison);
201 current_block_->AddInstruction(ifinst);
David Brazdil852eaff2015-02-02 15:23:05 +0000202 current_block_->AddSuccessor(branch_target);
203 current_block_->AddSuccessor(fallthrough_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700204 current_block_ = nullptr;
205}
206
207template<typename T>
Calin Juravle225ff812014-11-13 16:46:39 +0000208void HGraphBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000209 int32_t target_offset = instruction.GetTargetOffset();
David Brazdil852eaff2015-02-02 15:23:05 +0000210 HBasicBlock* branch_target = FindBlockStartingAt(dex_pc + target_offset);
211 HBasicBlock* fallthrough_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
212 DCHECK(branch_target != nullptr);
213 DCHECK(fallthrough_target != nullptr);
214 PotentiallyAddSuspendCheck(branch_target, dex_pc);
Dave Allison20dfc792014-06-16 20:44:29 -0700215 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000216 T* comparison = new (arena_) T(value, graph_->GetIntConstant(0));
Dave Allison20dfc792014-06-16 20:44:29 -0700217 current_block_->AddInstruction(comparison);
218 HInstruction* ifinst = new (arena_) HIf(comparison);
219 current_block_->AddInstruction(ifinst);
David Brazdil852eaff2015-02-02 15:23:05 +0000220 current_block_->AddSuccessor(branch_target);
221 current_block_->AddSuccessor(fallthrough_target);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100222 current_block_ = nullptr;
223}
224
Calin Juravle48c2b032014-12-09 18:11:36 +0000225void HGraphBuilder::MaybeRecordStat(MethodCompilationStat compilation_stat) {
226 if (compilation_stats_ != nullptr) {
227 compilation_stats_->RecordStat(compilation_stat);
228 }
229}
230
David Brazdil1b498722015-03-31 11:37:18 +0100231bool HGraphBuilder::SkipCompilation(const DexFile::CodeItem& code_item,
Calin Juravle48c2b032014-12-09 18:11:36 +0000232 size_t number_of_branches) {
233 const CompilerOptions& compiler_options = compiler_driver_->GetCompilerOptions();
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000234 CompilerOptions::CompilerFilter compiler_filter = compiler_options.GetCompilerFilter();
235 if (compiler_filter == CompilerOptions::kEverything) {
236 return false;
237 }
238
David Brazdil1b498722015-03-31 11:37:18 +0100239 if (compiler_options.IsHugeMethod(code_item.insns_size_in_code_units_)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000240 VLOG(compiler) << "Skip compilation of huge method "
241 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
David Brazdil1b498722015-03-31 11:37:18 +0100242 << ": " << code_item.insns_size_in_code_units_ << " code units";
Calin Juravle48c2b032014-12-09 18:11:36 +0000243 MaybeRecordStat(MethodCompilationStat::kNotCompiledHugeMethod);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000244 return true;
245 }
246
247 // If it's large and contains no branches, it's likely to be machine generated initialization.
David Brazdil1b498722015-03-31 11:37:18 +0100248 if (compiler_options.IsLargeMethod(code_item.insns_size_in_code_units_)
249 && (number_of_branches == 0)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000250 VLOG(compiler) << "Skip compilation of large method with no branch "
251 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
David Brazdil1b498722015-03-31 11:37:18 +0100252 << ": " << code_item.insns_size_in_code_units_ << " code units";
Calin Juravle48c2b032014-12-09 18:11:36 +0000253 MaybeRecordStat(MethodCompilationStat::kNotCompiledLargeMethodNoBranches);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000254 return true;
255 }
256
257 return false;
258}
259
David Brazdil5e8b1372015-01-23 14:39:08 +0000260bool HGraphBuilder::BuildGraph(const DexFile::CodeItem& code_item) {
261 DCHECK(graph_->GetBlocks().IsEmpty());
262
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000263 const uint16_t* code_ptr = code_item.insns_;
264 const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100265 code_start_ = code_ptr;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000266
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000267 // Setup the graph with the entry block and exit block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100268 entry_block_ = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000269 graph_->AddBlock(entry_block_);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100270 exit_block_ = new (arena_) HBasicBlock(graph_, kNoDexPc);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000271 graph_->SetEntryBlock(entry_block_);
272 graph_->SetExitBlock(exit_block_);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000273
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000274 InitializeLocals(code_item.registers_size_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000275 graph_->SetMaximumNumberOfOutVRegs(code_item.outs_size_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000276
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000277 // Compute the number of dex instructions, blocks, and branches. We will
278 // check these values against limits given to the compiler.
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000279 size_t number_of_branches = 0;
280
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000281 // To avoid splitting blocks, we compute ahead of time the instructions that
282 // start a new block, and create these blocks.
David Brazdil1b498722015-03-31 11:37:18 +0100283 ComputeBranchTargets(code_ptr, code_end, &number_of_branches);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000284
285 // Note that the compiler driver is null when unit testing.
David Brazdil1b498722015-03-31 11:37:18 +0100286 if ((compiler_driver_ != nullptr) && SkipCompilation(code_item, number_of_branches)) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000287 return false;
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000288 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000289
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000290 // Also create blocks for catch handlers.
291 if (code_item.tries_size_ != 0) {
292 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(code_item, 0);
293 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
294 for (uint32_t idx = 0; idx < handlers_size; ++idx) {
295 CatchHandlerIterator iterator(handlers_ptr);
296 for (; iterator.HasNext(); iterator.Next()) {
297 uint32_t address = iterator.GetHandlerAddress();
298 HBasicBlock* block = FindBlockStartingAt(address);
299 if (block == nullptr) {
300 block = new (arena_) HBasicBlock(graph_, address);
301 branch_targets_.Put(address, block);
302 }
303 block->SetIsCatchBlock();
304 }
305 handlers_ptr = iterator.EndDataPointer();
306 }
307 }
308
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000309 InitializeParameters(code_item.ins_size_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100310
Calin Juravle225ff812014-11-13 16:46:39 +0000311 size_t dex_pc = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000312 while (code_ptr < code_end) {
Calin Juravle225ff812014-11-13 16:46:39 +0000313 // Update the current block if dex_pc starts a new block.
314 MaybeUpdateCurrentBlock(dex_pc);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000315 const Instruction& instruction = *Instruction::At(code_ptr);
Calin Juravle48c2b032014-12-09 18:11:36 +0000316 if (!AnalyzeDexInstruction(instruction, dex_pc)) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000317 return false;
Calin Juravle48c2b032014-12-09 18:11:36 +0000318 }
Calin Juravle225ff812014-11-13 16:46:39 +0000319 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000320 code_ptr += instruction.SizeInCodeUnits();
321 }
322
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000323 // Add the exit block at the end to give it the highest id.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000324 graph_->AddBlock(exit_block_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000325 exit_block_->AddInstruction(new (arena_) HExit());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000326 // Add the suspend check to the entry block.
327 entry_block_->AddInstruction(new (arena_) HSuspendCheck(0));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000328 entry_block_->AddInstruction(new (arena_) HGoto());
David Brazdil5e8b1372015-01-23 14:39:08 +0000329
330 return true;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000331}
332
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000333void HGraphBuilder::MaybeUpdateCurrentBlock(size_t index) {
334 HBasicBlock* block = FindBlockStartingAt(index);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000335 if (block == nullptr) {
336 return;
337 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000338
339 if (current_block_ != nullptr) {
340 // Branching instructions clear current_block, so we know
341 // the last instruction of the current block is not a branching
342 // instruction. We add an unconditional goto to the found block.
343 current_block_->AddInstruction(new (arena_) HGoto());
344 current_block_->AddSuccessor(block);
345 }
346 graph_->AddBlock(block);
347 current_block_ = block;
348}
349
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000350void HGraphBuilder::ComputeBranchTargets(const uint16_t* code_ptr,
351 const uint16_t* code_end,
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000352 size_t* number_of_branches) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000353 branch_targets_.SetSize(code_end - code_ptr);
354
355 // Create the first block for the dex instructions, single successor of the entry block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100356 HBasicBlock* block = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000357 branch_targets_.Put(0, block);
358 entry_block_->AddSuccessor(block);
359
360 // Iterate over all instructions and find branching instructions. Create blocks for
361 // the locations these instructions branch to.
Andreas Gamped881df52014-11-24 23:28:39 -0800362 uint32_t dex_pc = 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000363 while (code_ptr < code_end) {
364 const Instruction& instruction = *Instruction::At(code_ptr);
365 if (instruction.IsBranch()) {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000366 (*number_of_branches)++;
Calin Juravle225ff812014-11-13 16:46:39 +0000367 int32_t target = instruction.GetTargetOffset() + dex_pc;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000368 // Create a block for the target instruction.
369 if (FindBlockStartingAt(target) == nullptr) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100370 block = new (arena_) HBasicBlock(graph_, target);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000371 branch_targets_.Put(target, block);
372 }
Calin Juravle225ff812014-11-13 16:46:39 +0000373 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000374 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle225ff812014-11-13 16:46:39 +0000375 if ((code_ptr < code_end) && (FindBlockStartingAt(dex_pc) == nullptr)) {
376 block = new (arena_) HBasicBlock(graph_, dex_pc);
377 branch_targets_.Put(dex_pc, block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000378 }
Andreas Gampee4d4d322014-12-04 09:09:57 -0800379 } else if (instruction.IsSwitch()) {
380 SwitchTable table(instruction, dex_pc, instruction.Opcode() == Instruction::SPARSE_SWITCH);
Andreas Gamped881df52014-11-24 23:28:39 -0800381
382 uint16_t num_entries = table.GetNumEntries();
383
Andreas Gampee4d4d322014-12-04 09:09:57 -0800384 // In a packed-switch, the entry at index 0 is the starting key. In a sparse-switch, the
385 // entry at index 0 is the first key, and values are after *all* keys.
386 size_t offset = table.GetFirstValueIndex();
387
388 // Use a larger loop counter type to avoid overflow issues.
389 for (size_t i = 0; i < num_entries; ++i) {
Andreas Gamped881df52014-11-24 23:28:39 -0800390 // The target of the case.
Andreas Gampee4d4d322014-12-04 09:09:57 -0800391 uint32_t target = dex_pc + table.GetEntryAt(i + offset);
Andreas Gamped881df52014-11-24 23:28:39 -0800392 if (FindBlockStartingAt(target) == nullptr) {
393 block = new (arena_) HBasicBlock(graph_, target);
394 branch_targets_.Put(target, block);
Andreas Gamped881df52014-11-24 23:28:39 -0800395 }
396
397 // The next case gets its own block.
398 if (i < num_entries) {
399 block = new (arena_) HBasicBlock(graph_, target);
400 branch_targets_.Put(table.GetDexPcForIndex(i), block);
Andreas Gamped881df52014-11-24 23:28:39 -0800401 }
402 }
403
404 // Fall-through. Add a block if there is more code afterwards.
405 dex_pc += instruction.SizeInCodeUnits();
406 code_ptr += instruction.SizeInCodeUnits();
407 if ((code_ptr < code_end) && (FindBlockStartingAt(dex_pc) == nullptr)) {
408 block = new (arena_) HBasicBlock(graph_, dex_pc);
409 branch_targets_.Put(dex_pc, block);
Andreas Gamped881df52014-11-24 23:28:39 -0800410 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000411 } else {
412 code_ptr += instruction.SizeInCodeUnits();
Calin Juravle225ff812014-11-13 16:46:39 +0000413 dex_pc += instruction.SizeInCodeUnits();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000414 }
415 }
416}
417
418HBasicBlock* HGraphBuilder::FindBlockStartingAt(int32_t index) const {
419 DCHECK_GE(index, 0);
420 return branch_targets_.Get(index);
421}
422
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100423template<typename T>
Roland Levillain88cb1752014-10-20 16:36:47 +0100424void HGraphBuilder::Unop_12x(const Instruction& instruction, Primitive::Type type) {
425 HInstruction* first = LoadLocal(instruction.VRegB(), type);
426 current_block_->AddInstruction(new (arena_) T(type, first));
427 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
428}
429
Roland Levillaindff1f282014-11-05 14:15:05 +0000430void HGraphBuilder::Conversion_12x(const Instruction& instruction,
431 Primitive::Type input_type,
Roland Levillain624279f2014-12-04 11:54:28 +0000432 Primitive::Type result_type,
433 uint32_t dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +0000434 HInstruction* first = LoadLocal(instruction.VRegB(), input_type);
Roland Levillain624279f2014-12-04 11:54:28 +0000435 current_block_->AddInstruction(new (arena_) HTypeConversion(result_type, first, dex_pc));
Roland Levillaindff1f282014-11-05 14:15:05 +0000436 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
437}
438
Roland Levillain88cb1752014-10-20 16:36:47 +0100439template<typename T>
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100440void HGraphBuilder::Binop_23x(const Instruction& instruction, Primitive::Type type) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100441 HInstruction* first = LoadLocal(instruction.VRegB(), type);
442 HInstruction* second = LoadLocal(instruction.VRegC(), type);
443 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100444 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
445}
446
447template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000448void HGraphBuilder::Binop_23x(const Instruction& instruction,
449 Primitive::Type type,
450 uint32_t dex_pc) {
451 HInstruction* first = LoadLocal(instruction.VRegB(), type);
452 HInstruction* second = LoadLocal(instruction.VRegC(), type);
453 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
454 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
455}
456
457template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000458void HGraphBuilder::Binop_23x_shift(const Instruction& instruction,
459 Primitive::Type type) {
460 HInstruction* first = LoadLocal(instruction.VRegB(), type);
461 HInstruction* second = LoadLocal(instruction.VRegC(), Primitive::kPrimInt);
462 current_block_->AddInstruction(new (arena_) T(type, first, second));
463 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
464}
465
Calin Juravleddb7df22014-11-25 20:56:51 +0000466void HGraphBuilder::Binop_23x_cmp(const Instruction& instruction,
467 Primitive::Type type,
468 HCompare::Bias bias) {
469 HInstruction* first = LoadLocal(instruction.VRegB(), type);
470 HInstruction* second = LoadLocal(instruction.VRegC(), type);
471 current_block_->AddInstruction(new (arena_) HCompare(type, first, second, bias));
472 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
473}
474
Calin Juravle9aec02f2014-11-18 23:06:35 +0000475template<typename T>
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100476void HGraphBuilder::Binop_12x(const Instruction& instruction, Primitive::Type type) {
477 HInstruction* first = LoadLocal(instruction.VRegA(), type);
478 HInstruction* second = LoadLocal(instruction.VRegB(), type);
479 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100480 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
481}
482
483template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000484void HGraphBuilder::Binop_12x_shift(const Instruction& instruction, Primitive::Type type) {
485 HInstruction* first = LoadLocal(instruction.VRegA(), type);
486 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
487 current_block_->AddInstruction(new (arena_) T(type, first, second));
488 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
489}
490
491template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000492void HGraphBuilder::Binop_12x(const Instruction& instruction,
493 Primitive::Type type,
494 uint32_t dex_pc) {
495 HInstruction* first = LoadLocal(instruction.VRegA(), type);
496 HInstruction* second = LoadLocal(instruction.VRegB(), type);
497 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc));
498 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
499}
500
501template<typename T>
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100502void HGraphBuilder::Binop_22s(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100503 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000504 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100505 if (reverse) {
506 std::swap(first, second);
507 }
508 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
509 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
510}
511
512template<typename T>
513void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100514 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000515 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100516 if (reverse) {
517 std::swap(first, second);
518 }
519 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
520 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
521}
522
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100523void HGraphBuilder::BuildReturn(const Instruction& instruction, Primitive::Type type) {
524 if (type == Primitive::kPrimVoid) {
525 current_block_->AddInstruction(new (arena_) HReturnVoid());
526 } else {
527 HInstruction* value = LoadLocal(instruction.VRegA(), type);
528 current_block_->AddInstruction(new (arena_) HReturn(value));
529 }
530 current_block_->AddSuccessor(exit_block_);
531 current_block_ = nullptr;
532}
533
534bool HGraphBuilder::BuildInvoke(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000535 uint32_t dex_pc,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100536 uint32_t method_idx,
537 uint32_t number_of_vreg_arguments,
538 bool is_range,
539 uint32_t* args,
540 uint32_t register_index) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100541 Instruction::Code opcode = instruction.Opcode();
542 InvokeType invoke_type;
543 switch (opcode) {
544 case Instruction::INVOKE_STATIC:
545 case Instruction::INVOKE_STATIC_RANGE:
546 invoke_type = kStatic;
547 break;
548 case Instruction::INVOKE_DIRECT:
549 case Instruction::INVOKE_DIRECT_RANGE:
550 invoke_type = kDirect;
551 break;
552 case Instruction::INVOKE_VIRTUAL:
553 case Instruction::INVOKE_VIRTUAL_RANGE:
554 invoke_type = kVirtual;
555 break;
556 case Instruction::INVOKE_INTERFACE:
557 case Instruction::INVOKE_INTERFACE_RANGE:
558 invoke_type = kInterface;
559 break;
560 case Instruction::INVOKE_SUPER_RANGE:
561 case Instruction::INVOKE_SUPER:
562 invoke_type = kSuper;
563 break;
564 default:
565 LOG(FATAL) << "Unexpected invoke op: " << opcode;
566 return false;
567 }
568
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100569 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
570 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(method_id.proto_idx_);
571 const char* descriptor = dex_file_->StringDataByIdx(proto_id.shorty_idx_);
572 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100573 bool is_instance_call = invoke_type != kStatic;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100574 const size_t number_of_arguments = strlen(descriptor) - (is_instance_call ? 0 : 1);
575
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000576 MethodReference target_method(dex_file_, method_idx);
577 uintptr_t direct_code;
578 uintptr_t direct_method;
579 int table_index;
580 InvokeType optimized_invoke_type = invoke_type;
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000581
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000582 if (!compiler_driver_->ComputeInvokeInfo(dex_compilation_unit_, dex_pc, true, true,
583 &optimized_invoke_type, &target_method, &table_index,
584 &direct_code, &direct_method)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000585 VLOG(compiler) << "Did not compile " << PrettyMethod(method_idx, *dex_file_)
586 << " because a method call could not be resolved";
587 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedMethod);
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000588 return false;
589 }
590 DCHECK(optimized_invoke_type != kSuper);
591
592 HInvoke* invoke = nullptr;
593 if (optimized_invoke_type == kVirtual) {
594 invoke = new (arena_) HInvokeVirtual(
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800595 arena_, number_of_arguments, return_type, dex_pc, method_idx, table_index);
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000596 } else if (optimized_invoke_type == kInterface) {
597 invoke = new (arena_) HInvokeInterface(
598 arena_, number_of_arguments, return_type, dex_pc, method_idx, table_index);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100599 } else {
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000600 DCHECK(optimized_invoke_type == kDirect || optimized_invoke_type == kStatic);
601 // Sharpening to kDirect only works if we compile PIC.
602 DCHECK((optimized_invoke_type == invoke_type) || (optimized_invoke_type != kDirect)
603 || compiler_driver_->GetCompilerOptions().GetCompilePic());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000604 bool is_recursive =
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000605 (target_method.dex_method_index == dex_compilation_unit_->GetDexMethodIndex());
606 DCHECK(!is_recursive || (target_method.dex_file == dex_compilation_unit_->GetDexFile()));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000607 invoke = new (arena_) HInvokeStaticOrDirect(
608 arena_, number_of_arguments, return_type, dex_pc, target_method.dex_method_index,
Nicolas Geoffray79041292015-03-26 10:05:54 +0000609 is_recursive, invoke_type, optimized_invoke_type);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100610 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100611
612 size_t start_index = 0;
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000613 Temporaries temps(graph_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100614 if (is_instance_call) {
615 HInstruction* arg = LoadLocal(is_range ? register_index : args[0], Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000616 HNullCheck* null_check = new (arena_) HNullCheck(arg, dex_pc);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100617 current_block_->AddInstruction(null_check);
618 temps.Add(null_check);
619 invoke->SetArgumentAt(0, null_check);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100620 start_index = 1;
621 }
622
623 uint32_t descriptor_index = 1;
624 uint32_t argument_index = start_index;
625 for (size_t i = start_index; i < number_of_vreg_arguments; i++, argument_index++) {
626 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100627 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
628 if (!is_range && is_wide && args[i] + 1 != args[i + 1]) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100629 LOG(WARNING) << "Non sequential register pair in " << dex_compilation_unit_->GetSymbol()
Calin Juravle225ff812014-11-13 16:46:39 +0000630 << " at " << dex_pc;
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100631 // We do not implement non sequential register pair.
Calin Juravle48c2b032014-12-09 18:11:36 +0000632 MaybeRecordStat(MethodCompilationStat::kNotCompiledNonSequentialRegPair);
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100633 return false;
634 }
635 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
636 invoke->SetArgumentAt(argument_index, arg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100637 if (is_wide) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100638 i++;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100639 }
640 }
641
642 DCHECK_EQ(argument_index, number_of_arguments);
643 current_block_->AddInstruction(invoke);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100644 latest_result_ = invoke;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100645 return true;
646}
647
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100648bool HGraphBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000649 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100650 bool is_put) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100651 uint32_t source_or_dest_reg = instruction.VRegA_22c();
652 uint32_t obj_reg = instruction.VRegB_22c();
653 uint16_t field_index = instruction.VRegC_22c();
654
655 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierc7853442015-03-27 14:35:38 -0700656 ArtField* resolved_field =
657 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100658
Mathieu Chartierc7853442015-03-27 14:35:38 -0700659 if (resolved_field == nullptr) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000660 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedField);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100661 return false;
662 }
Calin Juravle52c48962014-12-16 17:02:57 +0000663
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100664 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100665
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100666 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000667 current_block_->AddInstruction(new (arena_) HNullCheck(object, dex_pc));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100668 if (is_put) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000669 Temporaries temps(graph_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100670 HInstruction* null_check = current_block_->GetLastInstruction();
671 // We need one temporary for the null check.
672 temps.Add(null_check);
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100673 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100674 current_block_->AddInstruction(new (arena_) HInstanceFieldSet(
675 null_check,
676 value,
Nicolas Geoffray39468442014-09-02 15:17:15 +0100677 field_type,
Calin Juravle52c48962014-12-16 17:02:57 +0000678 resolved_field->GetOffset(),
679 resolved_field->IsVolatile()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100680 } else {
681 current_block_->AddInstruction(new (arena_) HInstanceFieldGet(
682 current_block_->GetLastInstruction(),
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100683 field_type,
Calin Juravle52c48962014-12-16 17:02:57 +0000684 resolved_field->GetOffset(),
685 resolved_field->IsVolatile()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100686
687 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
688 }
689 return true;
690}
691
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000692mirror::Class* HGraphBuilder::GetOutermostCompilingClass() const {
693 ScopedObjectAccess soa(Thread::Current());
694 StackHandleScope<2> hs(soa.Self());
695 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
696 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
697 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
698 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
699 outer_compilation_unit_->GetClassLinker()->FindDexCache(outer_dex_file)));
700
701 return compiler_driver_->ResolveCompilingMethodsClass(
702 soa, outer_dex_cache, class_loader, outer_compilation_unit_);
703}
704
705bool HGraphBuilder::IsOutermostCompilingClass(uint16_t type_index) const {
706 ScopedObjectAccess soa(Thread::Current());
707 StackHandleScope<4> hs(soa.Self());
708 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
709 dex_compilation_unit_->GetClassLinker()->FindDexCache(*dex_compilation_unit_->GetDexFile())));
710 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
711 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
712 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
713 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
714 Handle<mirror::Class> compiling_class(hs.NewHandle(GetOutermostCompilingClass()));
715
716 return compiling_class.Get() == cls.Get();
717}
718
719
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100720bool HGraphBuilder::BuildStaticFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000721 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100722 bool is_put) {
723 uint32_t source_or_dest_reg = instruction.VRegA_21c();
724 uint16_t field_index = instruction.VRegB_21c();
725
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000726 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierc7853442015-03-27 14:35:38 -0700727 StackHandleScope<4> hs(soa.Self());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000728 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
729 dex_compilation_unit_->GetClassLinker()->FindDexCache(*dex_compilation_unit_->GetDexFile())));
730 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
731 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
Mathieu Chartierc7853442015-03-27 14:35:38 -0700732 ArtField* resolved_field = compiler_driver_->ResolveField(
733 soa, dex_cache, class_loader, dex_compilation_unit_, field_index, true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100734
Mathieu Chartierc7853442015-03-27 14:35:38 -0700735 if (resolved_field == nullptr) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000736 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedField);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100737 return false;
738 }
739
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000740 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
741 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
742 outer_compilation_unit_->GetClassLinker()->FindDexCache(outer_dex_file)));
743 Handle<mirror::Class> referrer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000744
745 // The index at which the field's class is stored in the DexCache's type array.
746 uint32_t storage_index;
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000747 bool is_referrer_class = (referrer_class.Get() == resolved_field->GetDeclaringClass());
748 if (is_referrer_class) {
749 storage_index = referrer_class->GetDexTypeIndex();
750 } else if (outer_dex_cache.Get() != dex_cache.Get()) {
751 // The compiler driver cannot currently understand multple dex caches involved. Just bailout.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000752 return false;
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000753 } else {
754 std::pair<bool, bool> pair = compiler_driver_->IsFastStaticField(
755 outer_dex_cache.Get(),
756 referrer_class.Get(),
Mathieu Chartierc7853442015-03-27 14:35:38 -0700757 resolved_field,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000758 field_index,
759 &storage_index);
760 bool can_easily_access = is_put ? pair.second : pair.first;
761 if (!can_easily_access) {
762 return false;
763 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000764 }
765
766 // TODO: find out why this check is needed.
767 bool is_in_dex_cache = compiler_driver_->CanAssumeTypeIsPresentInDexCache(
Nicolas Geoffray6a816cf2015-03-24 16:17:56 +0000768 *outer_compilation_unit_->GetDexFile(), storage_index);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000769 bool is_initialized = resolved_field->GetDeclaringClass()->IsInitialized() && is_in_dex_cache;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000770
771 HLoadClass* constant = new (arena_) HLoadClass(storage_index, is_referrer_class, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100772 current_block_->AddInstruction(constant);
773
774 HInstruction* cls = constant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000775 if (!is_initialized && !is_referrer_class) {
Calin Juravle225ff812014-11-13 16:46:39 +0000776 cls = new (arena_) HClinitCheck(constant, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100777 current_block_->AddInstruction(cls);
778 }
779
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000780 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100781 if (is_put) {
782 // We need to keep the class alive before loading the value.
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000783 Temporaries temps(graph_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100784 temps.Add(cls);
785 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
786 DCHECK_EQ(value->GetType(), field_type);
787 current_block_->AddInstruction(
Calin Juravle52c48962014-12-16 17:02:57 +0000788 new (arena_) HStaticFieldSet(cls, value, field_type, resolved_field->GetOffset(),
789 resolved_field->IsVolatile()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100790 } else {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000791 current_block_->AddInstruction(
Calin Juravle52c48962014-12-16 17:02:57 +0000792 new (arena_) HStaticFieldGet(cls, field_type, resolved_field->GetOffset(),
793 resolved_field->IsVolatile()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100794 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
795 }
796 return true;
797}
798
Calin Juravlebacfec32014-11-14 15:54:36 +0000799void HGraphBuilder::BuildCheckedDivRem(uint16_t out_vreg,
800 uint16_t first_vreg,
801 int64_t second_vreg_or_constant,
802 uint32_t dex_pc,
803 Primitive::Type type,
804 bool second_is_constant,
805 bool isDiv) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000806 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Calin Juravled0d48522014-11-04 16:40:20 +0000807
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000808 HInstruction* first = LoadLocal(first_vreg, type);
809 HInstruction* second = nullptr;
810 if (second_is_constant) {
811 if (type == Primitive::kPrimInt) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000812 second = graph_->GetIntConstant(second_vreg_or_constant);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000813 } else {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000814 second = graph_->GetLongConstant(second_vreg_or_constant);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000815 }
816 } else {
817 second = LoadLocal(second_vreg_or_constant, type);
818 }
819
820 if (!second_is_constant
821 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0)
822 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) {
823 second = new (arena_) HDivZeroCheck(second, dex_pc);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000824 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +0000825 current_block_->AddInstruction(second);
826 temps.Add(current_block_->GetLastInstruction());
827 }
828
Calin Juravlebacfec32014-11-14 15:54:36 +0000829 if (isDiv) {
830 current_block_->AddInstruction(new (arena_) HDiv(type, first, second, dex_pc));
831 } else {
832 current_block_->AddInstruction(new (arena_) HRem(type, first, second, dex_pc));
833 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000834 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
Calin Juravled0d48522014-11-04 16:40:20 +0000835}
836
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100837void HGraphBuilder::BuildArrayAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000838 uint32_t dex_pc,
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100839 bool is_put,
840 Primitive::Type anticipated_type) {
841 uint8_t source_or_dest_reg = instruction.VRegA_23x();
842 uint8_t array_reg = instruction.VRegB_23x();
843 uint8_t index_reg = instruction.VRegC_23x();
844
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100845 // We need one temporary for the null check, one for the index, and one for the length.
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000846 Temporaries temps(graph_);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100847
848 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000849 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100850 current_block_->AddInstruction(object);
851 temps.Add(object);
852
853 HInstruction* length = new (arena_) HArrayLength(object);
854 current_block_->AddInstruction(length);
855 temps.Add(length);
856 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt);
Calin Juravle225ff812014-11-13 16:46:39 +0000857 index = new (arena_) HBoundsCheck(index, length, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100858 current_block_->AddInstruction(index);
859 temps.Add(index);
860 if (is_put) {
861 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
862 // TODO: Insert a type check node if the type is Object.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100863 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +0000864 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100865 } else {
866 current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type));
867 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
868 }
Mingyao Yange4335eb2015-03-02 15:14:13 -0800869 graph_->SetHasArrayAccesses(true);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100870}
871
Calin Juravle225ff812014-11-13 16:46:39 +0000872void HGraphBuilder::BuildFilledNewArray(uint32_t dex_pc,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100873 uint32_t type_index,
874 uint32_t number_of_vreg_arguments,
875 bool is_range,
876 uint32_t* args,
877 uint32_t register_index) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000878 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments);
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +0000879 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
880 ? kQuickAllocArrayWithAccessCheck
881 : kQuickAllocArray;
882 HInstruction* object = new (arena_) HNewArray(length, dex_pc, type_index, entrypoint);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100883 current_block_->AddInstruction(object);
884
885 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
886 DCHECK_EQ(descriptor[0], '[') << descriptor;
887 char primitive = descriptor[1];
888 DCHECK(primitive == 'I'
889 || primitive == 'L'
890 || primitive == '[') << descriptor;
891 bool is_reference_array = (primitive == 'L') || (primitive == '[');
892 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
893
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000894 Temporaries temps(graph_);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100895 temps.Add(object);
896 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
897 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000898 HInstruction* index = graph_->GetIntConstant(i);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100899 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +0000900 new (arena_) HArraySet(object, index, value, type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100901 }
902 latest_result_ = object;
903}
904
905template <typename T>
906void HGraphBuilder::BuildFillArrayData(HInstruction* object,
907 const T* data,
908 uint32_t element_count,
909 Primitive::Type anticipated_type,
Calin Juravle225ff812014-11-13 16:46:39 +0000910 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100911 for (uint32_t i = 0; i < element_count; ++i) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000912 HInstruction* index = graph_->GetIntConstant(i);
913 HInstruction* value = graph_->GetIntConstant(data[i]);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100914 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +0000915 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100916 }
917}
918
Calin Juravle225ff812014-11-13 16:46:39 +0000919void HGraphBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000920 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +0000921 HInstruction* array = LoadLocal(instruction.VRegA_31t(), Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000922 HNullCheck* null_check = new (arena_) HNullCheck(array, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000923 current_block_->AddInstruction(null_check);
924 temps.Add(null_check);
925
926 HInstruction* length = new (arena_) HArrayLength(null_check);
927 current_block_->AddInstruction(length);
928
Calin Juravle225ff812014-11-13 16:46:39 +0000929 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
Calin Juravled0d48522014-11-04 16:40:20 +0000930 const Instruction::ArrayDataPayload* payload =
931 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_start_ + payload_offset);
932 const uint8_t* data = payload->data;
933 uint32_t element_count = payload->element_count;
934
935 // Implementation of this DEX instruction seems to be that the bounds check is
936 // done before doing any stores.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000937 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1);
Calin Juravle225ff812014-11-13 16:46:39 +0000938 current_block_->AddInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
Calin Juravled0d48522014-11-04 16:40:20 +0000939
940 switch (payload->element_width) {
941 case 1:
942 BuildFillArrayData(null_check,
943 reinterpret_cast<const int8_t*>(data),
944 element_count,
945 Primitive::kPrimByte,
Calin Juravle225ff812014-11-13 16:46:39 +0000946 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000947 break;
948 case 2:
949 BuildFillArrayData(null_check,
950 reinterpret_cast<const int16_t*>(data),
951 element_count,
952 Primitive::kPrimShort,
Calin Juravle225ff812014-11-13 16:46:39 +0000953 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000954 break;
955 case 4:
956 BuildFillArrayData(null_check,
957 reinterpret_cast<const int32_t*>(data),
958 element_count,
959 Primitive::kPrimInt,
Calin Juravle225ff812014-11-13 16:46:39 +0000960 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000961 break;
962 case 8:
963 BuildFillWideArrayData(null_check,
964 reinterpret_cast<const int64_t*>(data),
965 element_count,
Calin Juravle225ff812014-11-13 16:46:39 +0000966 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000967 break;
968 default:
969 LOG(FATAL) << "Unknown element width for " << payload->element_width;
970 }
971}
972
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100973void HGraphBuilder::BuildFillWideArrayData(HInstruction* object,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +0100974 const int64_t* data,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100975 uint32_t element_count,
Calin Juravle225ff812014-11-13 16:46:39 +0000976 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100977 for (uint32_t i = 0; i < element_count; ++i) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000978 HInstruction* index = graph_->GetIntConstant(i);
979 HInstruction* value = graph_->GetLongConstant(data[i]);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100980 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +0000981 object, index, value, Primitive::kPrimLong, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100982 }
983}
984
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000985bool HGraphBuilder::BuildTypeCheck(const Instruction& instruction,
986 uint8_t destination,
987 uint8_t reference,
988 uint16_t type_index,
Calin Juravle225ff812014-11-13 16:46:39 +0000989 uint32_t dex_pc) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000990 bool type_known_final;
991 bool type_known_abstract;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000992 // `CanAccessTypeWithoutChecks` will tell whether the method being
993 // built is trying to access its own class, so that the generated
994 // code can optimize for this case. However, the optimization does not
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000995 // work for inlining, so we use `IsOutermostCompilingClass` instead.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000996 bool dont_use_is_referrers_class;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000997 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
998 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000999 &type_known_final, &type_known_abstract, &dont_use_is_referrers_class);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001000 if (!can_access) {
Calin Juravle48c2b032014-12-09 18:11:36 +00001001 MaybeRecordStat(MethodCompilationStat::kNotCompiledCantAccesType);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001002 return false;
1003 }
1004 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot);
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001005 HLoadClass* cls = new (arena_) HLoadClass(
1006 type_index, IsOutermostCompilingClass(type_index), dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001007 current_block_->AddInstruction(cls);
1008 // The class needs a temporary before being used by the type check.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001009 Temporaries temps(graph_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001010 temps.Add(cls);
1011 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
1012 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001013 new (arena_) HInstanceOf(object, cls, type_known_final, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001014 UpdateLocal(destination, current_block_->GetLastInstruction());
1015 } else {
1016 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
1017 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001018 new (arena_) HCheckCast(object, cls, type_known_final, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001019 }
1020 return true;
1021}
1022
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001023bool HGraphBuilder::NeedsAccessCheck(uint32_t type_index) const {
1024 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
1025 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index);
1026}
1027
Calin Juravle48c2b032014-12-09 18:11:36 +00001028void HGraphBuilder::BuildPackedSwitch(const Instruction& instruction, uint32_t dex_pc) {
Andreas Gamped881df52014-11-24 23:28:39 -08001029 SwitchTable table(instruction, dex_pc, false);
1030
1031 // Value to test against.
1032 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
1033
Andreas Gampee4d4d322014-12-04 09:09:57 -08001034 uint16_t num_entries = table.GetNumEntries();
1035 // There should be at least one entry here.
1036 DCHECK_GT(num_entries, 0U);
1037
Andreas Gamped881df52014-11-24 23:28:39 -08001038 // Chained cmp-and-branch, starting from starting_key.
1039 int32_t starting_key = table.GetEntryAt(0);
1040
Andreas Gamped881df52014-11-24 23:28:39 -08001041 for (size_t i = 1; i <= num_entries; i++) {
Andreas Gampee4d4d322014-12-04 09:09:57 -08001042 BuildSwitchCaseHelper(instruction, i, i == num_entries, table, value, starting_key + i - 1,
1043 table.GetEntryAt(i), dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08001044 }
Andreas Gamped881df52014-11-24 23:28:39 -08001045}
1046
Calin Juravle48c2b032014-12-09 18:11:36 +00001047void HGraphBuilder::BuildSparseSwitch(const Instruction& instruction, uint32_t dex_pc) {
Andreas Gampee4d4d322014-12-04 09:09:57 -08001048 SwitchTable table(instruction, dex_pc, true);
1049
1050 // Value to test against.
1051 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
1052
1053 uint16_t num_entries = table.GetNumEntries();
Andreas Gampee4d4d322014-12-04 09:09:57 -08001054
1055 for (size_t i = 0; i < num_entries; i++) {
1056 BuildSwitchCaseHelper(instruction, i, i == static_cast<size_t>(num_entries) - 1, table, value,
1057 table.GetEntryAt(i), table.GetEntryAt(i + num_entries), dex_pc);
1058 }
Andreas Gampee4d4d322014-12-04 09:09:57 -08001059}
1060
1061void HGraphBuilder::BuildSwitchCaseHelper(const Instruction& instruction, size_t index,
1062 bool is_last_case, const SwitchTable& table,
1063 HInstruction* value, int32_t case_value_int,
1064 int32_t target_offset, uint32_t dex_pc) {
David Brazdil852eaff2015-02-02 15:23:05 +00001065 HBasicBlock* case_target = FindBlockStartingAt(dex_pc + target_offset);
1066 DCHECK(case_target != nullptr);
1067 PotentiallyAddSuspendCheck(case_target, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001068
1069 // The current case's value.
David Brazdil8d5b8b22015-03-24 10:51:52 +00001070 HInstruction* this_case_value = graph_->GetIntConstant(case_value_int);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001071
1072 // Compare value and this_case_value.
1073 HEqual* comparison = new (arena_) HEqual(value, this_case_value);
1074 current_block_->AddInstruction(comparison);
1075 HInstruction* ifinst = new (arena_) HIf(comparison);
1076 current_block_->AddInstruction(ifinst);
1077
1078 // Case hit: use the target offset to determine where to go.
Andreas Gampee4d4d322014-12-04 09:09:57 -08001079 current_block_->AddSuccessor(case_target);
1080
1081 // Case miss: go to the next case (or default fall-through).
1082 // When there is a next case, we use the block stored with the table offset representing this
1083 // case (that is where we registered them in ComputeBranchTargets).
1084 // When there is no next case, we use the following instruction.
1085 // TODO: Find a good way to peel the last iteration to avoid conditional, but still have re-use.
1086 if (!is_last_case) {
1087 HBasicBlock* next_case_target = FindBlockStartingAt(table.GetDexPcForIndex(index));
1088 DCHECK(next_case_target != nullptr);
1089 current_block_->AddSuccessor(next_case_target);
1090
1091 // Need to manually add the block, as there is no dex-pc transition for the cases.
1092 graph_->AddBlock(next_case_target);
1093
1094 current_block_ = next_case_target;
1095 } else {
1096 HBasicBlock* default_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
1097 DCHECK(default_target != nullptr);
1098 current_block_->AddSuccessor(default_target);
1099 current_block_ = nullptr;
1100 }
1101}
1102
David Brazdil852eaff2015-02-02 15:23:05 +00001103void HGraphBuilder::PotentiallyAddSuspendCheck(HBasicBlock* target, uint32_t dex_pc) {
1104 int32_t target_offset = target->GetDexPc() - dex_pc;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001105 if (target_offset <= 0) {
David Brazdil852eaff2015-02-02 15:23:05 +00001106 // DX generates back edges to the first encountered return. We can save
1107 // time of later passes by not adding redundant suspend checks.
David Brazdil2fd6aa52015-02-02 18:58:27 +00001108 HInstruction* last_in_target = target->GetLastInstruction();
1109 if (last_in_target != nullptr &&
1110 (last_in_target->IsReturn() || last_in_target->IsReturnVoid())) {
1111 return;
David Brazdil852eaff2015-02-02 15:23:05 +00001112 }
1113
1114 // Add a suspend check to backward branches which may potentially loop. We
1115 // can remove them after we recognize loops in the graph.
Calin Juravle225ff812014-11-13 16:46:39 +00001116 current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_pc));
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001117 }
1118}
1119
Calin Juravle225ff812014-11-13 16:46:39 +00001120bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001121 if (current_block_ == nullptr) {
1122 return true; // Dead code
1123 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001124
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001125 switch (instruction.Opcode()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001126 case Instruction::CONST_4: {
1127 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001128 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001129 UpdateLocal(register_index, constant);
1130 break;
1131 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001132
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001133 case Instruction::CONST_16: {
1134 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001135 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001136 UpdateLocal(register_index, constant);
1137 break;
1138 }
1139
Dave Allison20dfc792014-06-16 20:44:29 -07001140 case Instruction::CONST: {
1141 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001142 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i());
Dave Allison20dfc792014-06-16 20:44:29 -07001143 UpdateLocal(register_index, constant);
1144 break;
1145 }
1146
1147 case Instruction::CONST_HIGH16: {
1148 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001149 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16);
Dave Allison20dfc792014-06-16 20:44:29 -07001150 UpdateLocal(register_index, constant);
1151 break;
1152 }
1153
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001154 case Instruction::CONST_WIDE_16: {
1155 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001156 // Get 16 bits of constant value, sign extended to 64 bits.
1157 int64_t value = instruction.VRegB_21s();
1158 value <<= 48;
1159 value >>= 48;
David Brazdil8d5b8b22015-03-24 10:51:52 +00001160 HLongConstant* constant = graph_->GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001161 UpdateLocal(register_index, constant);
1162 break;
1163 }
1164
1165 case Instruction::CONST_WIDE_32: {
1166 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001167 // Get 32 bits of constant value, sign extended to 64 bits.
1168 int64_t value = instruction.VRegB_31i();
1169 value <<= 32;
1170 value >>= 32;
David Brazdil8d5b8b22015-03-24 10:51:52 +00001171 HLongConstant* constant = graph_->GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001172 UpdateLocal(register_index, constant);
1173 break;
1174 }
1175
1176 case Instruction::CONST_WIDE: {
1177 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001178 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001179 UpdateLocal(register_index, constant);
1180 break;
1181 }
1182
Dave Allison20dfc792014-06-16 20:44:29 -07001183 case Instruction::CONST_WIDE_HIGH16: {
1184 int32_t register_index = instruction.VRegA();
1185 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
David Brazdil8d5b8b22015-03-24 10:51:52 +00001186 HLongConstant* constant = graph_->GetLongConstant(value);
Dave Allison20dfc792014-06-16 20:44:29 -07001187 UpdateLocal(register_index, constant);
1188 break;
1189 }
1190
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001191 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001192 case Instruction::MOVE:
1193 case Instruction::MOVE_FROM16:
1194 case Instruction::MOVE_16: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001195 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001196 UpdateLocal(instruction.VRegA(), value);
1197 break;
1198 }
1199
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001200 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001201 case Instruction::MOVE_WIDE:
1202 case Instruction::MOVE_WIDE_FROM16:
1203 case Instruction::MOVE_WIDE_16: {
1204 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong);
1205 UpdateLocal(instruction.VRegA(), value);
1206 break;
1207 }
1208
1209 case Instruction::MOVE_OBJECT:
1210 case Instruction::MOVE_OBJECT_16:
1211 case Instruction::MOVE_OBJECT_FROM16: {
1212 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot);
1213 UpdateLocal(instruction.VRegA(), value);
1214 break;
1215 }
1216
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001217 case Instruction::RETURN_VOID: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001218 BuildReturn(instruction, Primitive::kPrimVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001219 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001220 }
1221
Dave Allison20dfc792014-06-16 20:44:29 -07001222#define IF_XX(comparison, cond) \
Calin Juravle225ff812014-11-13 16:46:39 +00001223 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
1224 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001225
Dave Allison20dfc792014-06-16 20:44:29 -07001226 IF_XX(HEqual, EQ);
1227 IF_XX(HNotEqual, NE);
1228 IF_XX(HLessThan, LT);
1229 IF_XX(HLessThanOrEqual, LE);
1230 IF_XX(HGreaterThan, GT);
1231 IF_XX(HGreaterThanOrEqual, GE);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001232
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001233 case Instruction::GOTO:
1234 case Instruction::GOTO_16:
1235 case Instruction::GOTO_32: {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001236 int32_t offset = instruction.GetTargetOffset();
Calin Juravle225ff812014-11-13 16:46:39 +00001237 HBasicBlock* target = FindBlockStartingAt(offset + dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001238 DCHECK(target != nullptr);
David Brazdil852eaff2015-02-02 15:23:05 +00001239 PotentiallyAddSuspendCheck(target, dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001240 current_block_->AddInstruction(new (arena_) HGoto());
1241 current_block_->AddSuccessor(target);
1242 current_block_ = nullptr;
1243 break;
1244 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001245
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001246 case Instruction::RETURN: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001247 DCHECK_NE(return_type_, Primitive::kPrimNot);
1248 DCHECK_NE(return_type_, Primitive::kPrimLong);
1249 DCHECK_NE(return_type_, Primitive::kPrimDouble);
1250 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001251 break;
1252 }
1253
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001254 case Instruction::RETURN_OBJECT: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001255 DCHECK(return_type_ == Primitive::kPrimNot);
1256 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001257 break;
1258 }
1259
1260 case Instruction::RETURN_WIDE: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001261 DCHECK(return_type_ == Primitive::kPrimDouble || return_type_ == Primitive::kPrimLong);
1262 BuildReturn(instruction, return_type_);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001263 break;
1264 }
1265
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001266 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001267 case Instruction::INVOKE_INTERFACE:
1268 case Instruction::INVOKE_STATIC:
1269 case Instruction::INVOKE_SUPER:
1270 case Instruction::INVOKE_VIRTUAL: {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001271 uint32_t method_idx = instruction.VRegB_35c();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001272 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001273 uint32_t args[5];
Ian Rogers29a26482014-05-02 15:27:29 -07001274 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00001275 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001276 number_of_vreg_arguments, false, args, -1)) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001277 return false;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001278 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001279 break;
1280 }
1281
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001282 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001283 case Instruction::INVOKE_INTERFACE_RANGE:
1284 case Instruction::INVOKE_STATIC_RANGE:
1285 case Instruction::INVOKE_SUPER_RANGE:
1286 case Instruction::INVOKE_VIRTUAL_RANGE: {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001287 uint32_t method_idx = instruction.VRegB_3rc();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001288 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1289 uint32_t register_index = instruction.VRegC();
Calin Juravle225ff812014-11-13 16:46:39 +00001290 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001291 number_of_vreg_arguments, true, nullptr, register_index)) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001292 return false;
1293 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001294 break;
1295 }
1296
Roland Levillain88cb1752014-10-20 16:36:47 +01001297 case Instruction::NEG_INT: {
1298 Unop_12x<HNeg>(instruction, Primitive::kPrimInt);
1299 break;
1300 }
1301
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001302 case Instruction::NEG_LONG: {
1303 Unop_12x<HNeg>(instruction, Primitive::kPrimLong);
1304 break;
1305 }
1306
Roland Levillain3dbcb382014-10-28 17:30:07 +00001307 case Instruction::NEG_FLOAT: {
1308 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat);
1309 break;
1310 }
1311
1312 case Instruction::NEG_DOUBLE: {
1313 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble);
1314 break;
1315 }
1316
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001317 case Instruction::NOT_INT: {
1318 Unop_12x<HNot>(instruction, Primitive::kPrimInt);
1319 break;
1320 }
1321
Roland Levillain70566432014-10-24 16:20:17 +01001322 case Instruction::NOT_LONG: {
1323 Unop_12x<HNot>(instruction, Primitive::kPrimLong);
1324 break;
1325 }
1326
Roland Levillaindff1f282014-11-05 14:15:05 +00001327 case Instruction::INT_TO_LONG: {
Roland Levillain624279f2014-12-04 11:54:28 +00001328 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong, dex_pc);
Roland Levillaindff1f282014-11-05 14:15:05 +00001329 break;
1330 }
1331
Roland Levillaincff13742014-11-17 14:32:17 +00001332 case Instruction::INT_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001333 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00001334 break;
1335 }
1336
1337 case Instruction::INT_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001338 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00001339 break;
1340 }
1341
Roland Levillain946e1432014-11-11 17:35:19 +00001342 case Instruction::LONG_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001343 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt, dex_pc);
Roland Levillain946e1432014-11-11 17:35:19 +00001344 break;
1345 }
1346
Roland Levillain6d0e4832014-11-27 18:31:21 +00001347 case Instruction::LONG_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001348 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimFloat, dex_pc);
Roland Levillain6d0e4832014-11-27 18:31:21 +00001349 break;
1350 }
1351
Roland Levillain647b9ed2014-11-27 12:06:00 +00001352 case Instruction::LONG_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001353 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble, dex_pc);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001354 break;
1355 }
1356
Roland Levillain3f8f9362014-12-02 17:45:01 +00001357 case Instruction::FLOAT_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001358 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimInt, dex_pc);
1359 break;
1360 }
1361
1362 case Instruction::FLOAT_TO_LONG: {
1363 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimLong, dex_pc);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001364 break;
1365 }
1366
Roland Levillain8964e2b2014-12-04 12:10:50 +00001367 case Instruction::FLOAT_TO_DOUBLE: {
1368 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimDouble, dex_pc);
1369 break;
1370 }
1371
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001372 case Instruction::DOUBLE_TO_INT: {
1373 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimInt, dex_pc);
1374 break;
1375 }
1376
1377 case Instruction::DOUBLE_TO_LONG: {
1378 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimLong, dex_pc);
1379 break;
1380 }
1381
Roland Levillain8964e2b2014-12-04 12:10:50 +00001382 case Instruction::DOUBLE_TO_FLOAT: {
1383 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimFloat, dex_pc);
1384 break;
1385 }
1386
Roland Levillain51d3fc42014-11-13 14:11:42 +00001387 case Instruction::INT_TO_BYTE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001388 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte, dex_pc);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001389 break;
1390 }
1391
Roland Levillain01a8d712014-11-14 16:27:39 +00001392 case Instruction::INT_TO_SHORT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001393 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort, dex_pc);
Roland Levillain01a8d712014-11-14 16:27:39 +00001394 break;
1395 }
1396
Roland Levillain981e4542014-11-14 11:47:14 +00001397 case Instruction::INT_TO_CHAR: {
Roland Levillain624279f2014-12-04 11:54:28 +00001398 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar, dex_pc);
Roland Levillain981e4542014-11-14 11:47:14 +00001399 break;
1400 }
1401
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001402 case Instruction::ADD_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001403 Binop_23x<HAdd>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001404 break;
1405 }
1406
1407 case Instruction::ADD_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001408 Binop_23x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001409 break;
1410 }
1411
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001412 case Instruction::ADD_DOUBLE: {
1413 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble);
1414 break;
1415 }
1416
1417 case Instruction::ADD_FLOAT: {
1418 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat);
1419 break;
1420 }
1421
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001422 case Instruction::SUB_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001423 Binop_23x<HSub>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001424 break;
1425 }
1426
1427 case Instruction::SUB_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001428 Binop_23x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001429 break;
1430 }
1431
Calin Juravle096cc022014-10-23 17:01:13 +01001432 case Instruction::SUB_FLOAT: {
1433 Binop_23x<HSub>(instruction, Primitive::kPrimFloat);
1434 break;
1435 }
1436
1437 case Instruction::SUB_DOUBLE: {
1438 Binop_23x<HSub>(instruction, Primitive::kPrimDouble);
1439 break;
1440 }
1441
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001442 case Instruction::ADD_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001443 Binop_12x<HAdd>(instruction, Primitive::kPrimInt);
1444 break;
1445 }
1446
Calin Juravle34bacdf2014-10-07 20:23:36 +01001447 case Instruction::MUL_INT: {
1448 Binop_23x<HMul>(instruction, Primitive::kPrimInt);
1449 break;
1450 }
1451
1452 case Instruction::MUL_LONG: {
1453 Binop_23x<HMul>(instruction, Primitive::kPrimLong);
1454 break;
1455 }
1456
Calin Juravleb5bfa962014-10-21 18:02:24 +01001457 case Instruction::MUL_FLOAT: {
1458 Binop_23x<HMul>(instruction, Primitive::kPrimFloat);
1459 break;
1460 }
1461
1462 case Instruction::MUL_DOUBLE: {
1463 Binop_23x<HMul>(instruction, Primitive::kPrimDouble);
1464 break;
1465 }
1466
Calin Juravled0d48522014-11-04 16:40:20 +00001467 case Instruction::DIV_INT: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001468 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1469 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravled0d48522014-11-04 16:40:20 +00001470 break;
1471 }
1472
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001473 case Instruction::DIV_LONG: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001474 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1475 dex_pc, Primitive::kPrimLong, false, true);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001476 break;
1477 }
1478
Calin Juravle7c4954d2014-10-28 16:57:40 +00001479 case Instruction::DIV_FLOAT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001480 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001481 break;
1482 }
1483
1484 case Instruction::DIV_DOUBLE: {
Calin Juravle225ff812014-11-13 16:46:39 +00001485 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001486 break;
1487 }
1488
Calin Juravlebacfec32014-11-14 15:54:36 +00001489 case Instruction::REM_INT: {
1490 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1491 dex_pc, Primitive::kPrimInt, false, false);
1492 break;
1493 }
1494
1495 case Instruction::REM_LONG: {
1496 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1497 dex_pc, Primitive::kPrimLong, false, false);
1498 break;
1499 }
1500
Calin Juravled2ec87d2014-12-08 14:24:46 +00001501 case Instruction::REM_FLOAT: {
1502 Binop_23x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
1503 break;
1504 }
1505
1506 case Instruction::REM_DOUBLE: {
1507 Binop_23x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
1508 break;
1509 }
1510
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001511 case Instruction::AND_INT: {
1512 Binop_23x<HAnd>(instruction, Primitive::kPrimInt);
1513 break;
1514 }
1515
1516 case Instruction::AND_LONG: {
1517 Binop_23x<HAnd>(instruction, Primitive::kPrimLong);
1518 break;
1519 }
1520
Calin Juravle9aec02f2014-11-18 23:06:35 +00001521 case Instruction::SHL_INT: {
1522 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt);
1523 break;
1524 }
1525
1526 case Instruction::SHL_LONG: {
1527 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong);
1528 break;
1529 }
1530
1531 case Instruction::SHR_INT: {
1532 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt);
1533 break;
1534 }
1535
1536 case Instruction::SHR_LONG: {
1537 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong);
1538 break;
1539 }
1540
1541 case Instruction::USHR_INT: {
1542 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt);
1543 break;
1544 }
1545
1546 case Instruction::USHR_LONG: {
1547 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong);
1548 break;
1549 }
1550
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001551 case Instruction::OR_INT: {
1552 Binop_23x<HOr>(instruction, Primitive::kPrimInt);
1553 break;
1554 }
1555
1556 case Instruction::OR_LONG: {
1557 Binop_23x<HOr>(instruction, Primitive::kPrimLong);
1558 break;
1559 }
1560
1561 case Instruction::XOR_INT: {
1562 Binop_23x<HXor>(instruction, Primitive::kPrimInt);
1563 break;
1564 }
1565
1566 case Instruction::XOR_LONG: {
1567 Binop_23x<HXor>(instruction, Primitive::kPrimLong);
1568 break;
1569 }
1570
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001571 case Instruction::ADD_LONG_2ADDR: {
1572 Binop_12x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001573 break;
1574 }
1575
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001576 case Instruction::ADD_DOUBLE_2ADDR: {
1577 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble);
1578 break;
1579 }
1580
1581 case Instruction::ADD_FLOAT_2ADDR: {
1582 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat);
1583 break;
1584 }
1585
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001586 case Instruction::SUB_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001587 Binop_12x<HSub>(instruction, Primitive::kPrimInt);
1588 break;
1589 }
1590
1591 case Instruction::SUB_LONG_2ADDR: {
1592 Binop_12x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001593 break;
1594 }
1595
Calin Juravle096cc022014-10-23 17:01:13 +01001596 case Instruction::SUB_FLOAT_2ADDR: {
1597 Binop_12x<HSub>(instruction, Primitive::kPrimFloat);
1598 break;
1599 }
1600
1601 case Instruction::SUB_DOUBLE_2ADDR: {
1602 Binop_12x<HSub>(instruction, Primitive::kPrimDouble);
1603 break;
1604 }
1605
Calin Juravle34bacdf2014-10-07 20:23:36 +01001606 case Instruction::MUL_INT_2ADDR: {
1607 Binop_12x<HMul>(instruction, Primitive::kPrimInt);
1608 break;
1609 }
1610
1611 case Instruction::MUL_LONG_2ADDR: {
1612 Binop_12x<HMul>(instruction, Primitive::kPrimLong);
1613 break;
1614 }
1615
Calin Juravleb5bfa962014-10-21 18:02:24 +01001616 case Instruction::MUL_FLOAT_2ADDR: {
1617 Binop_12x<HMul>(instruction, Primitive::kPrimFloat);
1618 break;
1619 }
1620
1621 case Instruction::MUL_DOUBLE_2ADDR: {
1622 Binop_12x<HMul>(instruction, Primitive::kPrimDouble);
1623 break;
1624 }
1625
Calin Juravle865fc882014-11-06 17:09:03 +00001626 case Instruction::DIV_INT_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001627 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1628 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravle865fc882014-11-06 17:09:03 +00001629 break;
1630 }
1631
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001632 case Instruction::DIV_LONG_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001633 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1634 dex_pc, Primitive::kPrimLong, false, true);
1635 break;
1636 }
1637
1638 case Instruction::REM_INT_2ADDR: {
1639 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1640 dex_pc, Primitive::kPrimInt, false, false);
1641 break;
1642 }
1643
1644 case Instruction::REM_LONG_2ADDR: {
1645 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1646 dex_pc, Primitive::kPrimLong, false, false);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001647 break;
1648 }
1649
Calin Juravled2ec87d2014-12-08 14:24:46 +00001650 case Instruction::REM_FLOAT_2ADDR: {
1651 Binop_12x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
1652 break;
1653 }
1654
1655 case Instruction::REM_DOUBLE_2ADDR: {
1656 Binop_12x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
1657 break;
1658 }
1659
Calin Juravle9aec02f2014-11-18 23:06:35 +00001660 case Instruction::SHL_INT_2ADDR: {
1661 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt);
1662 break;
1663 }
1664
1665 case Instruction::SHL_LONG_2ADDR: {
1666 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong);
1667 break;
1668 }
1669
1670 case Instruction::SHR_INT_2ADDR: {
1671 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt);
1672 break;
1673 }
1674
1675 case Instruction::SHR_LONG_2ADDR: {
1676 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong);
1677 break;
1678 }
1679
1680 case Instruction::USHR_INT_2ADDR: {
1681 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt);
1682 break;
1683 }
1684
1685 case Instruction::USHR_LONG_2ADDR: {
1686 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong);
1687 break;
1688 }
1689
Calin Juravle7c4954d2014-10-28 16:57:40 +00001690 case Instruction::DIV_FLOAT_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00001691 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001692 break;
1693 }
1694
1695 case Instruction::DIV_DOUBLE_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00001696 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001697 break;
1698 }
1699
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001700 case Instruction::AND_INT_2ADDR: {
1701 Binop_12x<HAnd>(instruction, Primitive::kPrimInt);
1702 break;
1703 }
1704
1705 case Instruction::AND_LONG_2ADDR: {
1706 Binop_12x<HAnd>(instruction, Primitive::kPrimLong);
1707 break;
1708 }
1709
1710 case Instruction::OR_INT_2ADDR: {
1711 Binop_12x<HOr>(instruction, Primitive::kPrimInt);
1712 break;
1713 }
1714
1715 case Instruction::OR_LONG_2ADDR: {
1716 Binop_12x<HOr>(instruction, Primitive::kPrimLong);
1717 break;
1718 }
1719
1720 case Instruction::XOR_INT_2ADDR: {
1721 Binop_12x<HXor>(instruction, Primitive::kPrimInt);
1722 break;
1723 }
1724
1725 case Instruction::XOR_LONG_2ADDR: {
1726 Binop_12x<HXor>(instruction, Primitive::kPrimLong);
1727 break;
1728 }
1729
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001730 case Instruction::ADD_INT_LIT16: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001731 Binop_22s<HAdd>(instruction, false);
1732 break;
1733 }
1734
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001735 case Instruction::AND_INT_LIT16: {
1736 Binop_22s<HAnd>(instruction, false);
1737 break;
1738 }
1739
1740 case Instruction::OR_INT_LIT16: {
1741 Binop_22s<HOr>(instruction, false);
1742 break;
1743 }
1744
1745 case Instruction::XOR_INT_LIT16: {
1746 Binop_22s<HXor>(instruction, false);
1747 break;
1748 }
1749
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001750 case Instruction::RSUB_INT: {
1751 Binop_22s<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001752 break;
1753 }
1754
Calin Juravle34bacdf2014-10-07 20:23:36 +01001755 case Instruction::MUL_INT_LIT16: {
1756 Binop_22s<HMul>(instruction, false);
1757 break;
1758 }
1759
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001760 case Instruction::ADD_INT_LIT8: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001761 Binop_22b<HAdd>(instruction, false);
1762 break;
1763 }
1764
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001765 case Instruction::AND_INT_LIT8: {
1766 Binop_22b<HAnd>(instruction, false);
1767 break;
1768 }
1769
1770 case Instruction::OR_INT_LIT8: {
1771 Binop_22b<HOr>(instruction, false);
1772 break;
1773 }
1774
1775 case Instruction::XOR_INT_LIT8: {
1776 Binop_22b<HXor>(instruction, false);
1777 break;
1778 }
1779
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001780 case Instruction::RSUB_INT_LIT8: {
1781 Binop_22b<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001782 break;
1783 }
1784
Calin Juravle34bacdf2014-10-07 20:23:36 +01001785 case Instruction::MUL_INT_LIT8: {
1786 Binop_22b<HMul>(instruction, false);
1787 break;
1788 }
1789
Calin Juravled0d48522014-11-04 16:40:20 +00001790 case Instruction::DIV_INT_LIT16:
1791 case Instruction::DIV_INT_LIT8: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001792 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1793 dex_pc, Primitive::kPrimInt, true, true);
1794 break;
1795 }
1796
1797 case Instruction::REM_INT_LIT16:
1798 case Instruction::REM_INT_LIT8: {
1799 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1800 dex_pc, Primitive::kPrimInt, true, false);
Calin Juravled0d48522014-11-04 16:40:20 +00001801 break;
1802 }
1803
Calin Juravle9aec02f2014-11-18 23:06:35 +00001804 case Instruction::SHL_INT_LIT8: {
1805 Binop_22b<HShl>(instruction, false);
1806 break;
1807 }
1808
1809 case Instruction::SHR_INT_LIT8: {
1810 Binop_22b<HShr>(instruction, false);
1811 break;
1812 }
1813
1814 case Instruction::USHR_INT_LIT8: {
1815 Binop_22b<HUShr>(instruction, false);
1816 break;
1817 }
1818
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001819 case Instruction::NEW_INSTANCE: {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001820 uint16_t type_index = instruction.VRegB_21c();
1821 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
1822 ? kQuickAllocObjectWithAccessCheck
1823 : kQuickAllocObject;
1824
1825 current_block_->AddInstruction(new (arena_) HNewInstance(dex_pc, type_index, entrypoint));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001826 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
1827 break;
1828 }
1829
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001830 case Instruction::NEW_ARRAY: {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001831 uint16_t type_index = instruction.VRegC_22c();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001832 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt);
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001833 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
1834 ? kQuickAllocArrayWithAccessCheck
1835 : kQuickAllocArray;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001836 current_block_->AddInstruction(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001837 new (arena_) HNewArray(length, dex_pc, type_index, entrypoint));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001838 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
1839 break;
1840 }
1841
1842 case Instruction::FILLED_NEW_ARRAY: {
1843 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
1844 uint32_t type_index = instruction.VRegB_35c();
1845 uint32_t args[5];
1846 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00001847 BuildFilledNewArray(dex_pc, type_index, number_of_vreg_arguments, false, args, 0);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001848 break;
1849 }
1850
1851 case Instruction::FILLED_NEW_ARRAY_RANGE: {
1852 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1853 uint32_t type_index = instruction.VRegB_3rc();
1854 uint32_t register_index = instruction.VRegC_3rc();
1855 BuildFilledNewArray(
Calin Juravle225ff812014-11-13 16:46:39 +00001856 dex_pc, type_index, number_of_vreg_arguments, true, nullptr, register_index);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001857 break;
1858 }
1859
1860 case Instruction::FILL_ARRAY_DATA: {
Calin Juravle225ff812014-11-13 16:46:39 +00001861 BuildFillArrayData(instruction, dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001862 break;
1863 }
1864
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001865 case Instruction::MOVE_RESULT:
Dave Allison20dfc792014-06-16 20:44:29 -07001866 case Instruction::MOVE_RESULT_WIDE:
1867 case Instruction::MOVE_RESULT_OBJECT:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001868 UpdateLocal(instruction.VRegA(), latest_result_);
1869 latest_result_ = nullptr;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001870 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001871
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001872 case Instruction::CMP_LONG: {
Calin Juravleddb7df22014-11-25 20:56:51 +00001873 Binop_23x_cmp(instruction, Primitive::kPrimLong, HCompare::kNoBias);
1874 break;
1875 }
1876
1877 case Instruction::CMPG_FLOAT: {
1878 Binop_23x_cmp(instruction, Primitive::kPrimFloat, HCompare::kGtBias);
1879 break;
1880 }
1881
1882 case Instruction::CMPG_DOUBLE: {
1883 Binop_23x_cmp(instruction, Primitive::kPrimDouble, HCompare::kGtBias);
1884 break;
1885 }
1886
1887 case Instruction::CMPL_FLOAT: {
1888 Binop_23x_cmp(instruction, Primitive::kPrimFloat, HCompare::kLtBias);
1889 break;
1890 }
1891
1892 case Instruction::CMPL_DOUBLE: {
1893 Binop_23x_cmp(instruction, Primitive::kPrimDouble, HCompare::kLtBias);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001894 break;
1895 }
1896
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001897 case Instruction::NOP:
1898 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001899
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001900 case Instruction::IGET:
1901 case Instruction::IGET_WIDE:
1902 case Instruction::IGET_OBJECT:
1903 case Instruction::IGET_BOOLEAN:
1904 case Instruction::IGET_BYTE:
1905 case Instruction::IGET_CHAR:
1906 case Instruction::IGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001907 if (!BuildInstanceFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001908 return false;
1909 }
1910 break;
1911 }
1912
1913 case Instruction::IPUT:
1914 case Instruction::IPUT_WIDE:
1915 case Instruction::IPUT_OBJECT:
1916 case Instruction::IPUT_BOOLEAN:
1917 case Instruction::IPUT_BYTE:
1918 case Instruction::IPUT_CHAR:
1919 case Instruction::IPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001920 if (!BuildInstanceFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001921 return false;
1922 }
1923 break;
1924 }
1925
1926 case Instruction::SGET:
1927 case Instruction::SGET_WIDE:
1928 case Instruction::SGET_OBJECT:
1929 case Instruction::SGET_BOOLEAN:
1930 case Instruction::SGET_BYTE:
1931 case Instruction::SGET_CHAR:
1932 case Instruction::SGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001933 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001934 return false;
1935 }
1936 break;
1937 }
1938
1939 case Instruction::SPUT:
1940 case Instruction::SPUT_WIDE:
1941 case Instruction::SPUT_OBJECT:
1942 case Instruction::SPUT_BOOLEAN:
1943 case Instruction::SPUT_BYTE:
1944 case Instruction::SPUT_CHAR:
1945 case Instruction::SPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001946 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001947 return false;
1948 }
1949 break;
1950 }
1951
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001952#define ARRAY_XX(kind, anticipated_type) \
1953 case Instruction::AGET##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00001954 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001955 break; \
1956 } \
1957 case Instruction::APUT##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00001958 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001959 break; \
1960 }
1961
1962 ARRAY_XX(, Primitive::kPrimInt);
1963 ARRAY_XX(_WIDE, Primitive::kPrimLong);
1964 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
1965 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
1966 ARRAY_XX(_BYTE, Primitive::kPrimByte);
1967 ARRAY_XX(_CHAR, Primitive::kPrimChar);
1968 ARRAY_XX(_SHORT, Primitive::kPrimShort);
1969
Nicolas Geoffray39468442014-09-02 15:17:15 +01001970 case Instruction::ARRAY_LENGTH: {
1971 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001972 // No need for a temporary for the null check, it is the only input of the following
1973 // instruction.
Calin Juravle225ff812014-11-13 16:46:39 +00001974 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001975 current_block_->AddInstruction(object);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001976 current_block_->AddInstruction(new (arena_) HArrayLength(object));
1977 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
1978 break;
1979 }
1980
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001981 case Instruction::CONST_STRING: {
Calin Juravle225ff812014-11-13 16:46:39 +00001982 current_block_->AddInstruction(new (arena_) HLoadString(instruction.VRegB_21c(), dex_pc));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001983 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
1984 break;
1985 }
1986
1987 case Instruction::CONST_STRING_JUMBO: {
Calin Juravle225ff812014-11-13 16:46:39 +00001988 current_block_->AddInstruction(new (arena_) HLoadString(instruction.VRegB_31c(), dex_pc));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001989 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
1990 break;
1991 }
1992
Nicolas Geoffray424f6762014-11-03 14:51:25 +00001993 case Instruction::CONST_CLASS: {
1994 uint16_t type_index = instruction.VRegB_21c();
1995 bool type_known_final;
1996 bool type_known_abstract;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001997 bool dont_use_is_referrers_class;
1998 // `CanAccessTypeWithoutChecks` will tell whether the method being
1999 // built is trying to access its own class, so that the generated
2000 // code can optimize for this case. However, the optimization does not
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002001 // work for inlining, so we use `IsOutermostCompilingClass` instead.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002002 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
2003 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002004 &type_known_final, &type_known_abstract, &dont_use_is_referrers_class);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002005 if (!can_access) {
Calin Juravle48c2b032014-12-09 18:11:36 +00002006 MaybeRecordStat(MethodCompilationStat::kNotCompiledCantAccesType);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002007 return false;
2008 }
2009 current_block_->AddInstruction(
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002010 new (arena_) HLoadClass(type_index, IsOutermostCompilingClass(type_index), dex_pc));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002011 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2012 break;
2013 }
2014
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002015 case Instruction::MOVE_EXCEPTION: {
2016 current_block_->AddInstruction(new (arena_) HLoadException());
2017 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
2018 break;
2019 }
2020
2021 case Instruction::THROW: {
2022 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00002023 current_block_->AddInstruction(new (arena_) HThrow(exception, dex_pc));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002024 // A throw instruction must branch to the exit block.
2025 current_block_->AddSuccessor(exit_block_);
2026 // We finished building this block. Set the current block to null to avoid
2027 // adding dead instructions to it.
2028 current_block_ = nullptr;
2029 break;
2030 }
2031
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002032 case Instruction::INSTANCE_OF: {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002033 uint8_t destination = instruction.VRegA_22c();
2034 uint8_t reference = instruction.VRegB_22c();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002035 uint16_t type_index = instruction.VRegC_22c();
Calin Juravle225ff812014-11-13 16:46:39 +00002036 if (!BuildTypeCheck(instruction, destination, reference, type_index, dex_pc)) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002037 return false;
2038 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002039 break;
2040 }
2041
2042 case Instruction::CHECK_CAST: {
2043 uint8_t reference = instruction.VRegA_21c();
2044 uint16_t type_index = instruction.VRegB_21c();
Calin Juravle225ff812014-11-13 16:46:39 +00002045 if (!BuildTypeCheck(instruction, -1, reference, type_index, dex_pc)) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002046 return false;
2047 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002048 break;
2049 }
2050
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002051 case Instruction::MONITOR_ENTER: {
2052 current_block_->AddInstruction(new (arena_) HMonitorOperation(
2053 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2054 HMonitorOperation::kEnter,
Calin Juravle225ff812014-11-13 16:46:39 +00002055 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002056 break;
2057 }
2058
2059 case Instruction::MONITOR_EXIT: {
2060 current_block_->AddInstruction(new (arena_) HMonitorOperation(
2061 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2062 HMonitorOperation::kExit,
Calin Juravle225ff812014-11-13 16:46:39 +00002063 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002064 break;
2065 }
2066
Andreas Gamped881df52014-11-24 23:28:39 -08002067 case Instruction::PACKED_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002068 BuildPackedSwitch(instruction, dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08002069 break;
2070 }
2071
Andreas Gampee4d4d322014-12-04 09:09:57 -08002072 case Instruction::SPARSE_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002073 BuildSparseSwitch(instruction, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08002074 break;
2075 }
2076
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002077 default:
Calin Juravle48c2b032014-12-09 18:11:36 +00002078 VLOG(compiler) << "Did not compile "
2079 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
2080 << " because of unhandled instruction "
2081 << instruction.Name();
2082 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnhandledInstruction);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002083 return false;
2084 }
2085 return true;
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00002086} // NOLINT(readability/fn_size)
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002087
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002088HLocal* HGraphBuilder::GetLocalAt(int register_index) const {
2089 return locals_.Get(register_index);
2090}
2091
2092void HGraphBuilder::UpdateLocal(int register_index, HInstruction* instruction) const {
2093 HLocal* local = GetLocalAt(register_index);
2094 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction));
2095}
2096
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002097HInstruction* HGraphBuilder::LoadLocal(int register_index, Primitive::Type type) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002098 HLocal* local = GetLocalAt(register_index);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002099 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type));
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002100 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002101}
2102
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002103} // namespace art