blob: fff2906a04cd299625e9624bbdc22df497a3fede [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
Calin Juravle27df7582015-04-17 19:12:31 +0100523static bool RequiresConstructorBarrier(const DexCompilationUnit& cu, const CompilerDriver& driver) {
524 Thread* self = Thread::Current();
525 return cu.IsConstructor()
526 && driver.RequiresConstructorBarrier(self, cu.GetDexFile(), cu.GetClassDefIndex());
527}
528
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100529void HGraphBuilder::BuildReturn(const Instruction& instruction, Primitive::Type type) {
530 if (type == Primitive::kPrimVoid) {
Calin Juravle27df7582015-04-17 19:12:31 +0100531 // Note that we might insert redundant barriers when inlining `super` calls.
532 // TODO: add a data flow analysis to get rid of duplicate barriers.
533 if (RequiresConstructorBarrier(*dex_compilation_unit_, *compiler_driver_)) {
534 current_block_->AddInstruction(new (arena_) HMemoryBarrier(kStoreStore));
535 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100536 current_block_->AddInstruction(new (arena_) HReturnVoid());
537 } else {
538 HInstruction* value = LoadLocal(instruction.VRegA(), type);
539 current_block_->AddInstruction(new (arena_) HReturn(value));
540 }
541 current_block_->AddSuccessor(exit_block_);
542 current_block_ = nullptr;
543}
544
545bool HGraphBuilder::BuildInvoke(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000546 uint32_t dex_pc,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100547 uint32_t method_idx,
548 uint32_t number_of_vreg_arguments,
549 bool is_range,
550 uint32_t* args,
551 uint32_t register_index) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100552 Instruction::Code opcode = instruction.Opcode();
553 InvokeType invoke_type;
554 switch (opcode) {
555 case Instruction::INVOKE_STATIC:
556 case Instruction::INVOKE_STATIC_RANGE:
557 invoke_type = kStatic;
558 break;
559 case Instruction::INVOKE_DIRECT:
560 case Instruction::INVOKE_DIRECT_RANGE:
561 invoke_type = kDirect;
562 break;
563 case Instruction::INVOKE_VIRTUAL:
564 case Instruction::INVOKE_VIRTUAL_RANGE:
565 invoke_type = kVirtual;
566 break;
567 case Instruction::INVOKE_INTERFACE:
568 case Instruction::INVOKE_INTERFACE_RANGE:
569 invoke_type = kInterface;
570 break;
571 case Instruction::INVOKE_SUPER_RANGE:
572 case Instruction::INVOKE_SUPER:
573 invoke_type = kSuper;
574 break;
575 default:
576 LOG(FATAL) << "Unexpected invoke op: " << opcode;
577 return false;
578 }
579
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100580 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
581 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(method_id.proto_idx_);
582 const char* descriptor = dex_file_->StringDataByIdx(proto_id.shorty_idx_);
583 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100584 bool is_instance_call = invoke_type != kStatic;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100585 const size_t number_of_arguments = strlen(descriptor) - (is_instance_call ? 0 : 1);
586
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000587 MethodReference target_method(dex_file_, method_idx);
588 uintptr_t direct_code;
589 uintptr_t direct_method;
590 int table_index;
591 InvokeType optimized_invoke_type = invoke_type;
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000592
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000593 if (!compiler_driver_->ComputeInvokeInfo(dex_compilation_unit_, dex_pc, true, true,
594 &optimized_invoke_type, &target_method, &table_index,
595 &direct_code, &direct_method)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000596 VLOG(compiler) << "Did not compile " << PrettyMethod(method_idx, *dex_file_)
597 << " because a method call could not be resolved";
598 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedMethod);
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000599 return false;
600 }
601 DCHECK(optimized_invoke_type != kSuper);
602
603 HInvoke* invoke = nullptr;
604 if (optimized_invoke_type == kVirtual) {
605 invoke = new (arena_) HInvokeVirtual(
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800606 arena_, number_of_arguments, return_type, dex_pc, method_idx, table_index);
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000607 } else if (optimized_invoke_type == kInterface) {
608 invoke = new (arena_) HInvokeInterface(
609 arena_, number_of_arguments, return_type, dex_pc, method_idx, table_index);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100610 } else {
Calin Juravlec7c8fe22014-12-02 11:42:34 +0000611 DCHECK(optimized_invoke_type == kDirect || optimized_invoke_type == kStatic);
612 // Sharpening to kDirect only works if we compile PIC.
613 DCHECK((optimized_invoke_type == invoke_type) || (optimized_invoke_type != kDirect)
614 || compiler_driver_->GetCompilerOptions().GetCompilePic());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000615 bool is_recursive =
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000616 (target_method.dex_method_index == dex_compilation_unit_->GetDexMethodIndex());
617 DCHECK(!is_recursive || (target_method.dex_file == dex_compilation_unit_->GetDexFile()));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000618 invoke = new (arena_) HInvokeStaticOrDirect(
619 arena_, number_of_arguments, return_type, dex_pc, target_method.dex_method_index,
Nicolas Geoffray79041292015-03-26 10:05:54 +0000620 is_recursive, invoke_type, optimized_invoke_type);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100621 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100622
623 size_t start_index = 0;
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000624 Temporaries temps(graph_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100625 if (is_instance_call) {
626 HInstruction* arg = LoadLocal(is_range ? register_index : args[0], Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000627 HNullCheck* null_check = new (arena_) HNullCheck(arg, dex_pc);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100628 current_block_->AddInstruction(null_check);
629 temps.Add(null_check);
630 invoke->SetArgumentAt(0, null_check);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100631 start_index = 1;
632 }
633
634 uint32_t descriptor_index = 1;
635 uint32_t argument_index = start_index;
636 for (size_t i = start_index; i < number_of_vreg_arguments; i++, argument_index++) {
637 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100638 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
639 if (!is_range && is_wide && args[i] + 1 != args[i + 1]) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100640 LOG(WARNING) << "Non sequential register pair in " << dex_compilation_unit_->GetSymbol()
Calin Juravle225ff812014-11-13 16:46:39 +0000641 << " at " << dex_pc;
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100642 // We do not implement non sequential register pair.
Calin Juravle48c2b032014-12-09 18:11:36 +0000643 MaybeRecordStat(MethodCompilationStat::kNotCompiledNonSequentialRegPair);
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100644 return false;
645 }
646 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
647 invoke->SetArgumentAt(argument_index, arg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100648 if (is_wide) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100649 i++;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100650 }
651 }
652
653 DCHECK_EQ(argument_index, number_of_arguments);
654 current_block_->AddInstruction(invoke);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100655 latest_result_ = invoke;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100656 return true;
657}
658
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100659bool HGraphBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000660 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100661 bool is_put) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100662 uint32_t source_or_dest_reg = instruction.VRegA_22c();
663 uint32_t obj_reg = instruction.VRegB_22c();
664 uint16_t field_index = instruction.VRegC_22c();
665
666 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierc7853442015-03-27 14:35:38 -0700667 ArtField* resolved_field =
668 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100669
Mathieu Chartierc7853442015-03-27 14:35:38 -0700670 if (resolved_field == nullptr) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000671 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedField);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100672 return false;
673 }
Calin Juravle52c48962014-12-16 17:02:57 +0000674
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100675 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100676
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100677 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000678 current_block_->AddInstruction(new (arena_) HNullCheck(object, dex_pc));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100679 if (is_put) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000680 Temporaries temps(graph_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100681 HInstruction* null_check = current_block_->GetLastInstruction();
682 // We need one temporary for the null check.
683 temps.Add(null_check);
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100684 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100685 current_block_->AddInstruction(new (arena_) HInstanceFieldSet(
686 null_check,
687 value,
Nicolas Geoffray39468442014-09-02 15:17:15 +0100688 field_type,
Calin Juravle52c48962014-12-16 17:02:57 +0000689 resolved_field->GetOffset(),
690 resolved_field->IsVolatile()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100691 } else {
692 current_block_->AddInstruction(new (arena_) HInstanceFieldGet(
693 current_block_->GetLastInstruction(),
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100694 field_type,
Calin Juravle52c48962014-12-16 17:02:57 +0000695 resolved_field->GetOffset(),
696 resolved_field->IsVolatile()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100697
698 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
699 }
700 return true;
701}
702
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000703mirror::Class* HGraphBuilder::GetOutermostCompilingClass() const {
704 ScopedObjectAccess soa(Thread::Current());
705 StackHandleScope<2> hs(soa.Self());
706 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
707 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
708 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
709 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
710 outer_compilation_unit_->GetClassLinker()->FindDexCache(outer_dex_file)));
711
712 return compiler_driver_->ResolveCompilingMethodsClass(
713 soa, outer_dex_cache, class_loader, outer_compilation_unit_);
714}
715
716bool HGraphBuilder::IsOutermostCompilingClass(uint16_t type_index) const {
717 ScopedObjectAccess soa(Thread::Current());
718 StackHandleScope<4> hs(soa.Self());
719 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
720 dex_compilation_unit_->GetClassLinker()->FindDexCache(*dex_compilation_unit_->GetDexFile())));
721 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
722 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
723 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
724 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
725 Handle<mirror::Class> compiling_class(hs.NewHandle(GetOutermostCompilingClass()));
726
727 return compiling_class.Get() == cls.Get();
728}
729
730
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100731bool HGraphBuilder::BuildStaticFieldAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000732 uint32_t dex_pc,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100733 bool is_put) {
734 uint32_t source_or_dest_reg = instruction.VRegA_21c();
735 uint16_t field_index = instruction.VRegB_21c();
736
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000737 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierc7853442015-03-27 14:35:38 -0700738 StackHandleScope<4> hs(soa.Self());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000739 Handle<mirror::DexCache> dex_cache(hs.NewHandle(
740 dex_compilation_unit_->GetClassLinker()->FindDexCache(*dex_compilation_unit_->GetDexFile())));
741 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
742 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
Mathieu Chartierc7853442015-03-27 14:35:38 -0700743 ArtField* resolved_field = compiler_driver_->ResolveField(
744 soa, dex_cache, class_loader, dex_compilation_unit_, field_index, true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100745
Mathieu Chartierc7853442015-03-27 14:35:38 -0700746 if (resolved_field == nullptr) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000747 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedField);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100748 return false;
749 }
750
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000751 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
752 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle(
753 outer_compilation_unit_->GetClassLinker()->FindDexCache(outer_dex_file)));
754 Handle<mirror::Class> referrer_class(hs.NewHandle(GetOutermostCompilingClass()));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000755
756 // The index at which the field's class is stored in the DexCache's type array.
757 uint32_t storage_index;
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000758 bool is_referrer_class = (referrer_class.Get() == resolved_field->GetDeclaringClass());
759 if (is_referrer_class) {
760 storage_index = referrer_class->GetDexTypeIndex();
761 } else if (outer_dex_cache.Get() != dex_cache.Get()) {
762 // The compiler driver cannot currently understand multple dex caches involved. Just bailout.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000763 return false;
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000764 } else {
765 std::pair<bool, bool> pair = compiler_driver_->IsFastStaticField(
766 outer_dex_cache.Get(),
767 referrer_class.Get(),
Mathieu Chartierc7853442015-03-27 14:35:38 -0700768 resolved_field,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000769 field_index,
770 &storage_index);
771 bool can_easily_access = is_put ? pair.second : pair.first;
772 if (!can_easily_access) {
773 return false;
774 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000775 }
776
777 // TODO: find out why this check is needed.
778 bool is_in_dex_cache = compiler_driver_->CanAssumeTypeIsPresentInDexCache(
Nicolas Geoffray6a816cf2015-03-24 16:17:56 +0000779 *outer_compilation_unit_->GetDexFile(), storage_index);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000780 bool is_initialized = resolved_field->GetDeclaringClass()->IsInitialized() && is_in_dex_cache;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000781
782 HLoadClass* constant = new (arena_) HLoadClass(storage_index, is_referrer_class, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100783 current_block_->AddInstruction(constant);
784
785 HInstruction* cls = constant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000786 if (!is_initialized && !is_referrer_class) {
Calin Juravle225ff812014-11-13 16:46:39 +0000787 cls = new (arena_) HClinitCheck(constant, dex_pc);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100788 current_block_->AddInstruction(cls);
789 }
790
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000791 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100792 if (is_put) {
793 // We need to keep the class alive before loading the value.
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000794 Temporaries temps(graph_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100795 temps.Add(cls);
796 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
797 DCHECK_EQ(value->GetType(), field_type);
798 current_block_->AddInstruction(
Calin Juravle52c48962014-12-16 17:02:57 +0000799 new (arena_) HStaticFieldSet(cls, value, field_type, resolved_field->GetOffset(),
800 resolved_field->IsVolatile()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100801 } else {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000802 current_block_->AddInstruction(
Calin Juravle52c48962014-12-16 17:02:57 +0000803 new (arena_) HStaticFieldGet(cls, field_type, resolved_field->GetOffset(),
804 resolved_field->IsVolatile()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100805 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
806 }
807 return true;
808}
809
Calin Juravlebacfec32014-11-14 15:54:36 +0000810void HGraphBuilder::BuildCheckedDivRem(uint16_t out_vreg,
811 uint16_t first_vreg,
812 int64_t second_vreg_or_constant,
813 uint32_t dex_pc,
814 Primitive::Type type,
815 bool second_is_constant,
816 bool isDiv) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000817 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Calin Juravled0d48522014-11-04 16:40:20 +0000818
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000819 HInstruction* first = LoadLocal(first_vreg, type);
820 HInstruction* second = nullptr;
821 if (second_is_constant) {
822 if (type == Primitive::kPrimInt) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000823 second = graph_->GetIntConstant(second_vreg_or_constant);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000824 } else {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000825 second = graph_->GetLongConstant(second_vreg_or_constant);
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000826 }
827 } else {
828 second = LoadLocal(second_vreg_or_constant, type);
829 }
830
831 if (!second_is_constant
832 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0)
833 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) {
834 second = new (arena_) HDivZeroCheck(second, dex_pc);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000835 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +0000836 current_block_->AddInstruction(second);
837 temps.Add(current_block_->GetLastInstruction());
838 }
839
Calin Juravlebacfec32014-11-14 15:54:36 +0000840 if (isDiv) {
841 current_block_->AddInstruction(new (arena_) HDiv(type, first, second, dex_pc));
842 } else {
843 current_block_->AddInstruction(new (arena_) HRem(type, first, second, dex_pc));
844 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000845 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
Calin Juravled0d48522014-11-04 16:40:20 +0000846}
847
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100848void HGraphBuilder::BuildArrayAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000849 uint32_t dex_pc,
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100850 bool is_put,
851 Primitive::Type anticipated_type) {
852 uint8_t source_or_dest_reg = instruction.VRegA_23x();
853 uint8_t array_reg = instruction.VRegB_23x();
854 uint8_t index_reg = instruction.VRegC_23x();
855
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100856 // We need one temporary for the null check, one for the index, and one for the length.
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000857 Temporaries temps(graph_);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100858
859 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000860 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100861 current_block_->AddInstruction(object);
862 temps.Add(object);
863
864 HInstruction* length = new (arena_) HArrayLength(object);
865 current_block_->AddInstruction(length);
866 temps.Add(length);
867 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt);
Calin Juravle225ff812014-11-13 16:46:39 +0000868 index = new (arena_) HBoundsCheck(index, length, dex_pc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100869 current_block_->AddInstruction(index);
870 temps.Add(index);
871 if (is_put) {
872 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
873 // TODO: Insert a type check node if the type is Object.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100874 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +0000875 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100876 } else {
877 current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type));
878 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
879 }
Mingyao Yange4335eb2015-03-02 15:14:13 -0800880 graph_->SetHasArrayAccesses(true);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100881}
882
Calin Juravle225ff812014-11-13 16:46:39 +0000883void HGraphBuilder::BuildFilledNewArray(uint32_t dex_pc,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100884 uint32_t type_index,
885 uint32_t number_of_vreg_arguments,
886 bool is_range,
887 uint32_t* args,
888 uint32_t register_index) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000889 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments);
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +0000890 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
891 ? kQuickAllocArrayWithAccessCheck
892 : kQuickAllocArray;
893 HInstruction* object = new (arena_) HNewArray(length, dex_pc, type_index, entrypoint);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100894 current_block_->AddInstruction(object);
895
896 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
897 DCHECK_EQ(descriptor[0], '[') << descriptor;
898 char primitive = descriptor[1];
899 DCHECK(primitive == 'I'
900 || primitive == 'L'
901 || primitive == '[') << descriptor;
902 bool is_reference_array = (primitive == 'L') || (primitive == '[');
903 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
904
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000905 Temporaries temps(graph_);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100906 temps.Add(object);
907 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
908 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000909 HInstruction* index = graph_->GetIntConstant(i);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100910 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +0000911 new (arena_) HArraySet(object, index, value, type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100912 }
913 latest_result_ = object;
914}
915
916template <typename T>
917void HGraphBuilder::BuildFillArrayData(HInstruction* object,
918 const T* data,
919 uint32_t element_count,
920 Primitive::Type anticipated_type,
Calin Juravle225ff812014-11-13 16:46:39 +0000921 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100922 for (uint32_t i = 0; i < element_count; ++i) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000923 HInstruction* index = graph_->GetIntConstant(i);
924 HInstruction* value = graph_->GetIntConstant(data[i]);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100925 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +0000926 object, index, value, anticipated_type, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100927 }
928}
929
Calin Juravle225ff812014-11-13 16:46:39 +0000930void HGraphBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000931 Temporaries temps(graph_);
Calin Juravled0d48522014-11-04 16:40:20 +0000932 HInstruction* array = LoadLocal(instruction.VRegA_31t(), Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +0000933 HNullCheck* null_check = new (arena_) HNullCheck(array, dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000934 current_block_->AddInstruction(null_check);
935 temps.Add(null_check);
936
937 HInstruction* length = new (arena_) HArrayLength(null_check);
938 current_block_->AddInstruction(length);
939
Calin Juravle225ff812014-11-13 16:46:39 +0000940 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
Calin Juravled0d48522014-11-04 16:40:20 +0000941 const Instruction::ArrayDataPayload* payload =
942 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_start_ + payload_offset);
943 const uint8_t* data = payload->data;
944 uint32_t element_count = payload->element_count;
945
946 // Implementation of this DEX instruction seems to be that the bounds check is
947 // done before doing any stores.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000948 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1);
Calin Juravle225ff812014-11-13 16:46:39 +0000949 current_block_->AddInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
Calin Juravled0d48522014-11-04 16:40:20 +0000950
951 switch (payload->element_width) {
952 case 1:
953 BuildFillArrayData(null_check,
954 reinterpret_cast<const int8_t*>(data),
955 element_count,
956 Primitive::kPrimByte,
Calin Juravle225ff812014-11-13 16:46:39 +0000957 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000958 break;
959 case 2:
960 BuildFillArrayData(null_check,
961 reinterpret_cast<const int16_t*>(data),
962 element_count,
963 Primitive::kPrimShort,
Calin Juravle225ff812014-11-13 16:46:39 +0000964 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000965 break;
966 case 4:
967 BuildFillArrayData(null_check,
968 reinterpret_cast<const int32_t*>(data),
969 element_count,
970 Primitive::kPrimInt,
Calin Juravle225ff812014-11-13 16:46:39 +0000971 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000972 break;
973 case 8:
974 BuildFillWideArrayData(null_check,
975 reinterpret_cast<const int64_t*>(data),
976 element_count,
Calin Juravle225ff812014-11-13 16:46:39 +0000977 dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000978 break;
979 default:
980 LOG(FATAL) << "Unknown element width for " << payload->element_width;
981 }
982}
983
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100984void HGraphBuilder::BuildFillWideArrayData(HInstruction* object,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +0100985 const int64_t* data,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100986 uint32_t element_count,
Calin Juravle225ff812014-11-13 16:46:39 +0000987 uint32_t dex_pc) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100988 for (uint32_t i = 0; i < element_count; ++i) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000989 HInstruction* index = graph_->GetIntConstant(i);
990 HInstruction* value = graph_->GetLongConstant(data[i]);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100991 current_block_->AddInstruction(new (arena_) HArraySet(
Calin Juravle225ff812014-11-13 16:46:39 +0000992 object, index, value, Primitive::kPrimLong, dex_pc));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100993 }
994}
995
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000996bool HGraphBuilder::BuildTypeCheck(const Instruction& instruction,
997 uint8_t destination,
998 uint8_t reference,
999 uint16_t type_index,
Calin Juravle225ff812014-11-13 16:46:39 +00001000 uint32_t dex_pc) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001001 bool type_known_final;
1002 bool type_known_abstract;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001003 // `CanAccessTypeWithoutChecks` will tell whether the method being
1004 // built is trying to access its own class, so that the generated
1005 // code can optimize for this case. However, the optimization does not
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001006 // work for inlining, so we use `IsOutermostCompilingClass` instead.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001007 bool dont_use_is_referrers_class;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001008 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
1009 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001010 &type_known_final, &type_known_abstract, &dont_use_is_referrers_class);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001011 if (!can_access) {
Calin Juravle48c2b032014-12-09 18:11:36 +00001012 MaybeRecordStat(MethodCompilationStat::kNotCompiledCantAccesType);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001013 return false;
1014 }
1015 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot);
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001016 HLoadClass* cls = new (arena_) HLoadClass(
1017 type_index, IsOutermostCompilingClass(type_index), dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001018 current_block_->AddInstruction(cls);
1019 // The class needs a temporary before being used by the type check.
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001020 Temporaries temps(graph_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001021 temps.Add(cls);
1022 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
1023 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001024 new (arena_) HInstanceOf(object, cls, type_known_final, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001025 UpdateLocal(destination, current_block_->GetLastInstruction());
1026 } else {
1027 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
1028 current_block_->AddInstruction(
Calin Juravle225ff812014-11-13 16:46:39 +00001029 new (arena_) HCheckCast(object, cls, type_known_final, dex_pc));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001030 }
1031 return true;
1032}
1033
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001034bool HGraphBuilder::NeedsAccessCheck(uint32_t type_index) const {
1035 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
1036 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index);
1037}
1038
Calin Juravle48c2b032014-12-09 18:11:36 +00001039void HGraphBuilder::BuildPackedSwitch(const Instruction& instruction, uint32_t dex_pc) {
Andreas Gamped881df52014-11-24 23:28:39 -08001040 SwitchTable table(instruction, dex_pc, false);
1041
1042 // Value to test against.
1043 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
1044
Andreas Gampee4d4d322014-12-04 09:09:57 -08001045 uint16_t num_entries = table.GetNumEntries();
1046 // There should be at least one entry here.
1047 DCHECK_GT(num_entries, 0U);
1048
Andreas Gamped881df52014-11-24 23:28:39 -08001049 // Chained cmp-and-branch, starting from starting_key.
1050 int32_t starting_key = table.GetEntryAt(0);
1051
Andreas Gamped881df52014-11-24 23:28:39 -08001052 for (size_t i = 1; i <= num_entries; i++) {
Andreas Gampee4d4d322014-12-04 09:09:57 -08001053 BuildSwitchCaseHelper(instruction, i, i == num_entries, table, value, starting_key + i - 1,
1054 table.GetEntryAt(i), dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08001055 }
Andreas Gamped881df52014-11-24 23:28:39 -08001056}
1057
Calin Juravle48c2b032014-12-09 18:11:36 +00001058void HGraphBuilder::BuildSparseSwitch(const Instruction& instruction, uint32_t dex_pc) {
Andreas Gampee4d4d322014-12-04 09:09:57 -08001059 SwitchTable table(instruction, dex_pc, true);
1060
1061 // Value to test against.
1062 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
1063
1064 uint16_t num_entries = table.GetNumEntries();
Andreas Gampee4d4d322014-12-04 09:09:57 -08001065
1066 for (size_t i = 0; i < num_entries; i++) {
1067 BuildSwitchCaseHelper(instruction, i, i == static_cast<size_t>(num_entries) - 1, table, value,
1068 table.GetEntryAt(i), table.GetEntryAt(i + num_entries), dex_pc);
1069 }
Andreas Gampee4d4d322014-12-04 09:09:57 -08001070}
1071
1072void HGraphBuilder::BuildSwitchCaseHelper(const Instruction& instruction, size_t index,
1073 bool is_last_case, const SwitchTable& table,
1074 HInstruction* value, int32_t case_value_int,
1075 int32_t target_offset, uint32_t dex_pc) {
David Brazdil852eaff2015-02-02 15:23:05 +00001076 HBasicBlock* case_target = FindBlockStartingAt(dex_pc + target_offset);
1077 DCHECK(case_target != nullptr);
1078 PotentiallyAddSuspendCheck(case_target, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001079
1080 // The current case's value.
David Brazdil8d5b8b22015-03-24 10:51:52 +00001081 HInstruction* this_case_value = graph_->GetIntConstant(case_value_int);
Andreas Gampee4d4d322014-12-04 09:09:57 -08001082
1083 // Compare value and this_case_value.
1084 HEqual* comparison = new (arena_) HEqual(value, this_case_value);
1085 current_block_->AddInstruction(comparison);
1086 HInstruction* ifinst = new (arena_) HIf(comparison);
1087 current_block_->AddInstruction(ifinst);
1088
1089 // Case hit: use the target offset to determine where to go.
Andreas Gampee4d4d322014-12-04 09:09:57 -08001090 current_block_->AddSuccessor(case_target);
1091
1092 // Case miss: go to the next case (or default fall-through).
1093 // When there is a next case, we use the block stored with the table offset representing this
1094 // case (that is where we registered them in ComputeBranchTargets).
1095 // When there is no next case, we use the following instruction.
1096 // TODO: Find a good way to peel the last iteration to avoid conditional, but still have re-use.
1097 if (!is_last_case) {
1098 HBasicBlock* next_case_target = FindBlockStartingAt(table.GetDexPcForIndex(index));
1099 DCHECK(next_case_target != nullptr);
1100 current_block_->AddSuccessor(next_case_target);
1101
1102 // Need to manually add the block, as there is no dex-pc transition for the cases.
1103 graph_->AddBlock(next_case_target);
1104
1105 current_block_ = next_case_target;
1106 } else {
1107 HBasicBlock* default_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits());
1108 DCHECK(default_target != nullptr);
1109 current_block_->AddSuccessor(default_target);
1110 current_block_ = nullptr;
1111 }
1112}
1113
David Brazdil852eaff2015-02-02 15:23:05 +00001114void HGraphBuilder::PotentiallyAddSuspendCheck(HBasicBlock* target, uint32_t dex_pc) {
1115 int32_t target_offset = target->GetDexPc() - dex_pc;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001116 if (target_offset <= 0) {
David Brazdil852eaff2015-02-02 15:23:05 +00001117 // DX generates back edges to the first encountered return. We can save
1118 // time of later passes by not adding redundant suspend checks.
David Brazdil2fd6aa52015-02-02 18:58:27 +00001119 HInstruction* last_in_target = target->GetLastInstruction();
1120 if (last_in_target != nullptr &&
1121 (last_in_target->IsReturn() || last_in_target->IsReturnVoid())) {
1122 return;
David Brazdil852eaff2015-02-02 15:23:05 +00001123 }
1124
1125 // Add a suspend check to backward branches which may potentially loop. We
1126 // can remove them after we recognize loops in the graph.
Calin Juravle225ff812014-11-13 16:46:39 +00001127 current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_pc));
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001128 }
1129}
1130
Calin Juravle225ff812014-11-13 16:46:39 +00001131bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001132 if (current_block_ == nullptr) {
1133 return true; // Dead code
1134 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001135
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001136 switch (instruction.Opcode()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001137 case Instruction::CONST_4: {
1138 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001139 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001140 UpdateLocal(register_index, constant);
1141 break;
1142 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001143
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001144 case Instruction::CONST_16: {
1145 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001146 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001147 UpdateLocal(register_index, constant);
1148 break;
1149 }
1150
Dave Allison20dfc792014-06-16 20:44:29 -07001151 case Instruction::CONST: {
1152 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001153 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i());
Dave Allison20dfc792014-06-16 20:44:29 -07001154 UpdateLocal(register_index, constant);
1155 break;
1156 }
1157
1158 case Instruction::CONST_HIGH16: {
1159 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001160 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16);
Dave Allison20dfc792014-06-16 20:44:29 -07001161 UpdateLocal(register_index, constant);
1162 break;
1163 }
1164
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001165 case Instruction::CONST_WIDE_16: {
1166 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001167 // Get 16 bits of constant value, sign extended to 64 bits.
1168 int64_t value = instruction.VRegB_21s();
1169 value <<= 48;
1170 value >>= 48;
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_32: {
1177 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -07001178 // Get 32 bits of constant value, sign extended to 64 bits.
1179 int64_t value = instruction.VRegB_31i();
1180 value <<= 32;
1181 value >>= 32;
David Brazdil8d5b8b22015-03-24 10:51:52 +00001182 HLongConstant* constant = graph_->GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001183 UpdateLocal(register_index, constant);
1184 break;
1185 }
1186
1187 case Instruction::CONST_WIDE: {
1188 int32_t register_index = instruction.VRegA();
David Brazdil8d5b8b22015-03-24 10:51:52 +00001189 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001190 UpdateLocal(register_index, constant);
1191 break;
1192 }
1193
Dave Allison20dfc792014-06-16 20:44:29 -07001194 case Instruction::CONST_WIDE_HIGH16: {
1195 int32_t register_index = instruction.VRegA();
1196 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
David Brazdil8d5b8b22015-03-24 10:51:52 +00001197 HLongConstant* constant = graph_->GetLongConstant(value);
Dave Allison20dfc792014-06-16 20:44:29 -07001198 UpdateLocal(register_index, constant);
1199 break;
1200 }
1201
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001202 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001203 case Instruction::MOVE:
1204 case Instruction::MOVE_FROM16:
1205 case Instruction::MOVE_16: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001206 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001207 UpdateLocal(instruction.VRegA(), value);
1208 break;
1209 }
1210
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001211 // Note that the SSA building will refine the types.
Dave Allison20dfc792014-06-16 20:44:29 -07001212 case Instruction::MOVE_WIDE:
1213 case Instruction::MOVE_WIDE_FROM16:
1214 case Instruction::MOVE_WIDE_16: {
1215 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong);
1216 UpdateLocal(instruction.VRegA(), value);
1217 break;
1218 }
1219
1220 case Instruction::MOVE_OBJECT:
1221 case Instruction::MOVE_OBJECT_16:
1222 case Instruction::MOVE_OBJECT_FROM16: {
1223 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot);
1224 UpdateLocal(instruction.VRegA(), value);
1225 break;
1226 }
1227
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001228 case Instruction::RETURN_VOID: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001229 BuildReturn(instruction, Primitive::kPrimVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001230 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001231 }
1232
Dave Allison20dfc792014-06-16 20:44:29 -07001233#define IF_XX(comparison, cond) \
Calin Juravle225ff812014-11-13 16:46:39 +00001234 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
1235 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001236
Dave Allison20dfc792014-06-16 20:44:29 -07001237 IF_XX(HEqual, EQ);
1238 IF_XX(HNotEqual, NE);
1239 IF_XX(HLessThan, LT);
1240 IF_XX(HLessThanOrEqual, LE);
1241 IF_XX(HGreaterThan, GT);
1242 IF_XX(HGreaterThanOrEqual, GE);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001243
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001244 case Instruction::GOTO:
1245 case Instruction::GOTO_16:
1246 case Instruction::GOTO_32: {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001247 int32_t offset = instruction.GetTargetOffset();
Calin Juravle225ff812014-11-13 16:46:39 +00001248 HBasicBlock* target = FindBlockStartingAt(offset + dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001249 DCHECK(target != nullptr);
David Brazdil852eaff2015-02-02 15:23:05 +00001250 PotentiallyAddSuspendCheck(target, dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001251 current_block_->AddInstruction(new (arena_) HGoto());
1252 current_block_->AddSuccessor(target);
1253 current_block_ = nullptr;
1254 break;
1255 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001256
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001257 case Instruction::RETURN: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001258 DCHECK_NE(return_type_, Primitive::kPrimNot);
1259 DCHECK_NE(return_type_, Primitive::kPrimLong);
1260 DCHECK_NE(return_type_, Primitive::kPrimDouble);
1261 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001262 break;
1263 }
1264
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001265 case Instruction::RETURN_OBJECT: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001266 DCHECK(return_type_ == Primitive::kPrimNot);
1267 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001268 break;
1269 }
1270
1271 case Instruction::RETURN_WIDE: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001272 DCHECK(return_type_ == Primitive::kPrimDouble || return_type_ == Primitive::kPrimLong);
1273 BuildReturn(instruction, return_type_);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001274 break;
1275 }
1276
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001277 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001278 case Instruction::INVOKE_INTERFACE:
1279 case Instruction::INVOKE_STATIC:
1280 case Instruction::INVOKE_SUPER:
1281 case Instruction::INVOKE_VIRTUAL: {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001282 uint32_t method_idx = instruction.VRegB_35c();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001283 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001284 uint32_t args[5];
Ian Rogers29a26482014-05-02 15:27:29 -07001285 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00001286 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00001287 number_of_vreg_arguments, false, args, -1)) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001288 return false;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001289 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001290 break;
1291 }
1292
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001293 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0d8db992014-11-11 14:40:10 +00001294 case Instruction::INVOKE_INTERFACE_RANGE:
1295 case Instruction::INVOKE_STATIC_RANGE:
1296 case Instruction::INVOKE_SUPER_RANGE:
1297 case Instruction::INVOKE_VIRTUAL_RANGE: {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001298 uint32_t method_idx = instruction.VRegB_3rc();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001299 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1300 uint32_t register_index = instruction.VRegC();
Calin Juravle225ff812014-11-13 16:46:39 +00001301 if (!BuildInvoke(instruction, dex_pc, method_idx,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001302 number_of_vreg_arguments, true, nullptr, register_index)) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001303 return false;
1304 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001305 break;
1306 }
1307
Roland Levillain88cb1752014-10-20 16:36:47 +01001308 case Instruction::NEG_INT: {
1309 Unop_12x<HNeg>(instruction, Primitive::kPrimInt);
1310 break;
1311 }
1312
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001313 case Instruction::NEG_LONG: {
1314 Unop_12x<HNeg>(instruction, Primitive::kPrimLong);
1315 break;
1316 }
1317
Roland Levillain3dbcb382014-10-28 17:30:07 +00001318 case Instruction::NEG_FLOAT: {
1319 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat);
1320 break;
1321 }
1322
1323 case Instruction::NEG_DOUBLE: {
1324 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble);
1325 break;
1326 }
1327
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001328 case Instruction::NOT_INT: {
1329 Unop_12x<HNot>(instruction, Primitive::kPrimInt);
1330 break;
1331 }
1332
Roland Levillain70566432014-10-24 16:20:17 +01001333 case Instruction::NOT_LONG: {
1334 Unop_12x<HNot>(instruction, Primitive::kPrimLong);
1335 break;
1336 }
1337
Roland Levillaindff1f282014-11-05 14:15:05 +00001338 case Instruction::INT_TO_LONG: {
Roland Levillain624279f2014-12-04 11:54:28 +00001339 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong, dex_pc);
Roland Levillaindff1f282014-11-05 14:15:05 +00001340 break;
1341 }
1342
Roland Levillaincff13742014-11-17 14:32:17 +00001343 case Instruction::INT_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001344 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00001345 break;
1346 }
1347
1348 case Instruction::INT_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001349 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble, dex_pc);
Roland Levillaincff13742014-11-17 14:32:17 +00001350 break;
1351 }
1352
Roland Levillain946e1432014-11-11 17:35:19 +00001353 case Instruction::LONG_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001354 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt, dex_pc);
Roland Levillain946e1432014-11-11 17:35:19 +00001355 break;
1356 }
1357
Roland Levillain6d0e4832014-11-27 18:31:21 +00001358 case Instruction::LONG_TO_FLOAT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001359 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimFloat, dex_pc);
Roland Levillain6d0e4832014-11-27 18:31:21 +00001360 break;
1361 }
1362
Roland Levillain647b9ed2014-11-27 12:06:00 +00001363 case Instruction::LONG_TO_DOUBLE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001364 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble, dex_pc);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001365 break;
1366 }
1367
Roland Levillain3f8f9362014-12-02 17:45:01 +00001368 case Instruction::FLOAT_TO_INT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001369 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimInt, dex_pc);
1370 break;
1371 }
1372
1373 case Instruction::FLOAT_TO_LONG: {
1374 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimLong, dex_pc);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001375 break;
1376 }
1377
Roland Levillain8964e2b2014-12-04 12:10:50 +00001378 case Instruction::FLOAT_TO_DOUBLE: {
1379 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimDouble, dex_pc);
1380 break;
1381 }
1382
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001383 case Instruction::DOUBLE_TO_INT: {
1384 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimInt, dex_pc);
1385 break;
1386 }
1387
1388 case Instruction::DOUBLE_TO_LONG: {
1389 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimLong, dex_pc);
1390 break;
1391 }
1392
Roland Levillain8964e2b2014-12-04 12:10:50 +00001393 case Instruction::DOUBLE_TO_FLOAT: {
1394 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimFloat, dex_pc);
1395 break;
1396 }
1397
Roland Levillain51d3fc42014-11-13 14:11:42 +00001398 case Instruction::INT_TO_BYTE: {
Roland Levillain624279f2014-12-04 11:54:28 +00001399 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte, dex_pc);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001400 break;
1401 }
1402
Roland Levillain01a8d712014-11-14 16:27:39 +00001403 case Instruction::INT_TO_SHORT: {
Roland Levillain624279f2014-12-04 11:54:28 +00001404 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort, dex_pc);
Roland Levillain01a8d712014-11-14 16:27:39 +00001405 break;
1406 }
1407
Roland Levillain981e4542014-11-14 11:47:14 +00001408 case Instruction::INT_TO_CHAR: {
Roland Levillain624279f2014-12-04 11:54:28 +00001409 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar, dex_pc);
Roland Levillain981e4542014-11-14 11:47:14 +00001410 break;
1411 }
1412
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001413 case Instruction::ADD_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001414 Binop_23x<HAdd>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001415 break;
1416 }
1417
1418 case Instruction::ADD_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001419 Binop_23x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001420 break;
1421 }
1422
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001423 case Instruction::ADD_DOUBLE: {
1424 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble);
1425 break;
1426 }
1427
1428 case Instruction::ADD_FLOAT: {
1429 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat);
1430 break;
1431 }
1432
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001433 case Instruction::SUB_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001434 Binop_23x<HSub>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001435 break;
1436 }
1437
1438 case Instruction::SUB_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001439 Binop_23x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001440 break;
1441 }
1442
Calin Juravle096cc022014-10-23 17:01:13 +01001443 case Instruction::SUB_FLOAT: {
1444 Binop_23x<HSub>(instruction, Primitive::kPrimFloat);
1445 break;
1446 }
1447
1448 case Instruction::SUB_DOUBLE: {
1449 Binop_23x<HSub>(instruction, Primitive::kPrimDouble);
1450 break;
1451 }
1452
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001453 case Instruction::ADD_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001454 Binop_12x<HAdd>(instruction, Primitive::kPrimInt);
1455 break;
1456 }
1457
Calin Juravle34bacdf2014-10-07 20:23:36 +01001458 case Instruction::MUL_INT: {
1459 Binop_23x<HMul>(instruction, Primitive::kPrimInt);
1460 break;
1461 }
1462
1463 case Instruction::MUL_LONG: {
1464 Binop_23x<HMul>(instruction, Primitive::kPrimLong);
1465 break;
1466 }
1467
Calin Juravleb5bfa962014-10-21 18:02:24 +01001468 case Instruction::MUL_FLOAT: {
1469 Binop_23x<HMul>(instruction, Primitive::kPrimFloat);
1470 break;
1471 }
1472
1473 case Instruction::MUL_DOUBLE: {
1474 Binop_23x<HMul>(instruction, Primitive::kPrimDouble);
1475 break;
1476 }
1477
Calin Juravled0d48522014-11-04 16:40:20 +00001478 case Instruction::DIV_INT: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001479 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1480 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravled0d48522014-11-04 16:40:20 +00001481 break;
1482 }
1483
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001484 case Instruction::DIV_LONG: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001485 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1486 dex_pc, Primitive::kPrimLong, false, true);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001487 break;
1488 }
1489
Calin Juravle7c4954d2014-10-28 16:57:40 +00001490 case Instruction::DIV_FLOAT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001491 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001492 break;
1493 }
1494
1495 case Instruction::DIV_DOUBLE: {
Calin Juravle225ff812014-11-13 16:46:39 +00001496 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001497 break;
1498 }
1499
Calin Juravlebacfec32014-11-14 15:54:36 +00001500 case Instruction::REM_INT: {
1501 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1502 dex_pc, Primitive::kPrimInt, false, false);
1503 break;
1504 }
1505
1506 case Instruction::REM_LONG: {
1507 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1508 dex_pc, Primitive::kPrimLong, false, false);
1509 break;
1510 }
1511
Calin Juravled2ec87d2014-12-08 14:24:46 +00001512 case Instruction::REM_FLOAT: {
1513 Binop_23x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
1514 break;
1515 }
1516
1517 case Instruction::REM_DOUBLE: {
1518 Binop_23x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
1519 break;
1520 }
1521
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001522 case Instruction::AND_INT: {
1523 Binop_23x<HAnd>(instruction, Primitive::kPrimInt);
1524 break;
1525 }
1526
1527 case Instruction::AND_LONG: {
1528 Binop_23x<HAnd>(instruction, Primitive::kPrimLong);
1529 break;
1530 }
1531
Calin Juravle9aec02f2014-11-18 23:06:35 +00001532 case Instruction::SHL_INT: {
1533 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt);
1534 break;
1535 }
1536
1537 case Instruction::SHL_LONG: {
1538 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong);
1539 break;
1540 }
1541
1542 case Instruction::SHR_INT: {
1543 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt);
1544 break;
1545 }
1546
1547 case Instruction::SHR_LONG: {
1548 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong);
1549 break;
1550 }
1551
1552 case Instruction::USHR_INT: {
1553 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt);
1554 break;
1555 }
1556
1557 case Instruction::USHR_LONG: {
1558 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong);
1559 break;
1560 }
1561
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001562 case Instruction::OR_INT: {
1563 Binop_23x<HOr>(instruction, Primitive::kPrimInt);
1564 break;
1565 }
1566
1567 case Instruction::OR_LONG: {
1568 Binop_23x<HOr>(instruction, Primitive::kPrimLong);
1569 break;
1570 }
1571
1572 case Instruction::XOR_INT: {
1573 Binop_23x<HXor>(instruction, Primitive::kPrimInt);
1574 break;
1575 }
1576
1577 case Instruction::XOR_LONG: {
1578 Binop_23x<HXor>(instruction, Primitive::kPrimLong);
1579 break;
1580 }
1581
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001582 case Instruction::ADD_LONG_2ADDR: {
1583 Binop_12x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001584 break;
1585 }
1586
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001587 case Instruction::ADD_DOUBLE_2ADDR: {
1588 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble);
1589 break;
1590 }
1591
1592 case Instruction::ADD_FLOAT_2ADDR: {
1593 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat);
1594 break;
1595 }
1596
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001597 case Instruction::SUB_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001598 Binop_12x<HSub>(instruction, Primitive::kPrimInt);
1599 break;
1600 }
1601
1602 case Instruction::SUB_LONG_2ADDR: {
1603 Binop_12x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001604 break;
1605 }
1606
Calin Juravle096cc022014-10-23 17:01:13 +01001607 case Instruction::SUB_FLOAT_2ADDR: {
1608 Binop_12x<HSub>(instruction, Primitive::kPrimFloat);
1609 break;
1610 }
1611
1612 case Instruction::SUB_DOUBLE_2ADDR: {
1613 Binop_12x<HSub>(instruction, Primitive::kPrimDouble);
1614 break;
1615 }
1616
Calin Juravle34bacdf2014-10-07 20:23:36 +01001617 case Instruction::MUL_INT_2ADDR: {
1618 Binop_12x<HMul>(instruction, Primitive::kPrimInt);
1619 break;
1620 }
1621
1622 case Instruction::MUL_LONG_2ADDR: {
1623 Binop_12x<HMul>(instruction, Primitive::kPrimLong);
1624 break;
1625 }
1626
Calin Juravleb5bfa962014-10-21 18:02:24 +01001627 case Instruction::MUL_FLOAT_2ADDR: {
1628 Binop_12x<HMul>(instruction, Primitive::kPrimFloat);
1629 break;
1630 }
1631
1632 case Instruction::MUL_DOUBLE_2ADDR: {
1633 Binop_12x<HMul>(instruction, Primitive::kPrimDouble);
1634 break;
1635 }
1636
Calin Juravle865fc882014-11-06 17:09:03 +00001637 case Instruction::DIV_INT_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001638 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1639 dex_pc, Primitive::kPrimInt, false, true);
Calin Juravle865fc882014-11-06 17:09:03 +00001640 break;
1641 }
1642
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001643 case Instruction::DIV_LONG_2ADDR: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001644 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1645 dex_pc, Primitive::kPrimLong, false, true);
1646 break;
1647 }
1648
1649 case Instruction::REM_INT_2ADDR: {
1650 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1651 dex_pc, Primitive::kPrimInt, false, false);
1652 break;
1653 }
1654
1655 case Instruction::REM_LONG_2ADDR: {
1656 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
1657 dex_pc, Primitive::kPrimLong, false, false);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001658 break;
1659 }
1660
Calin Juravled2ec87d2014-12-08 14:24:46 +00001661 case Instruction::REM_FLOAT_2ADDR: {
1662 Binop_12x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
1663 break;
1664 }
1665
1666 case Instruction::REM_DOUBLE_2ADDR: {
1667 Binop_12x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
1668 break;
1669 }
1670
Calin Juravle9aec02f2014-11-18 23:06:35 +00001671 case Instruction::SHL_INT_2ADDR: {
1672 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt);
1673 break;
1674 }
1675
1676 case Instruction::SHL_LONG_2ADDR: {
1677 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong);
1678 break;
1679 }
1680
1681 case Instruction::SHR_INT_2ADDR: {
1682 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt);
1683 break;
1684 }
1685
1686 case Instruction::SHR_LONG_2ADDR: {
1687 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong);
1688 break;
1689 }
1690
1691 case Instruction::USHR_INT_2ADDR: {
1692 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt);
1693 break;
1694 }
1695
1696 case Instruction::USHR_LONG_2ADDR: {
1697 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong);
1698 break;
1699 }
1700
Calin Juravle7c4954d2014-10-28 16:57:40 +00001701 case Instruction::DIV_FLOAT_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00001702 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001703 break;
1704 }
1705
1706 case Instruction::DIV_DOUBLE_2ADDR: {
Calin Juravle225ff812014-11-13 16:46:39 +00001707 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001708 break;
1709 }
1710
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001711 case Instruction::AND_INT_2ADDR: {
1712 Binop_12x<HAnd>(instruction, Primitive::kPrimInt);
1713 break;
1714 }
1715
1716 case Instruction::AND_LONG_2ADDR: {
1717 Binop_12x<HAnd>(instruction, Primitive::kPrimLong);
1718 break;
1719 }
1720
1721 case Instruction::OR_INT_2ADDR: {
1722 Binop_12x<HOr>(instruction, Primitive::kPrimInt);
1723 break;
1724 }
1725
1726 case Instruction::OR_LONG_2ADDR: {
1727 Binop_12x<HOr>(instruction, Primitive::kPrimLong);
1728 break;
1729 }
1730
1731 case Instruction::XOR_INT_2ADDR: {
1732 Binop_12x<HXor>(instruction, Primitive::kPrimInt);
1733 break;
1734 }
1735
1736 case Instruction::XOR_LONG_2ADDR: {
1737 Binop_12x<HXor>(instruction, Primitive::kPrimLong);
1738 break;
1739 }
1740
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001741 case Instruction::ADD_INT_LIT16: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001742 Binop_22s<HAdd>(instruction, false);
1743 break;
1744 }
1745
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001746 case Instruction::AND_INT_LIT16: {
1747 Binop_22s<HAnd>(instruction, false);
1748 break;
1749 }
1750
1751 case Instruction::OR_INT_LIT16: {
1752 Binop_22s<HOr>(instruction, false);
1753 break;
1754 }
1755
1756 case Instruction::XOR_INT_LIT16: {
1757 Binop_22s<HXor>(instruction, false);
1758 break;
1759 }
1760
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001761 case Instruction::RSUB_INT: {
1762 Binop_22s<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001763 break;
1764 }
1765
Calin Juravle34bacdf2014-10-07 20:23:36 +01001766 case Instruction::MUL_INT_LIT16: {
1767 Binop_22s<HMul>(instruction, false);
1768 break;
1769 }
1770
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001771 case Instruction::ADD_INT_LIT8: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001772 Binop_22b<HAdd>(instruction, false);
1773 break;
1774 }
1775
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001776 case Instruction::AND_INT_LIT8: {
1777 Binop_22b<HAnd>(instruction, false);
1778 break;
1779 }
1780
1781 case Instruction::OR_INT_LIT8: {
1782 Binop_22b<HOr>(instruction, false);
1783 break;
1784 }
1785
1786 case Instruction::XOR_INT_LIT8: {
1787 Binop_22b<HXor>(instruction, false);
1788 break;
1789 }
1790
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001791 case Instruction::RSUB_INT_LIT8: {
1792 Binop_22b<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001793 break;
1794 }
1795
Calin Juravle34bacdf2014-10-07 20:23:36 +01001796 case Instruction::MUL_INT_LIT8: {
1797 Binop_22b<HMul>(instruction, false);
1798 break;
1799 }
1800
Calin Juravled0d48522014-11-04 16:40:20 +00001801 case Instruction::DIV_INT_LIT16:
1802 case Instruction::DIV_INT_LIT8: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001803 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1804 dex_pc, Primitive::kPrimInt, true, true);
1805 break;
1806 }
1807
1808 case Instruction::REM_INT_LIT16:
1809 case Instruction::REM_INT_LIT8: {
1810 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
1811 dex_pc, Primitive::kPrimInt, true, false);
Calin Juravled0d48522014-11-04 16:40:20 +00001812 break;
1813 }
1814
Calin Juravle9aec02f2014-11-18 23:06:35 +00001815 case Instruction::SHL_INT_LIT8: {
1816 Binop_22b<HShl>(instruction, false);
1817 break;
1818 }
1819
1820 case Instruction::SHR_INT_LIT8: {
1821 Binop_22b<HShr>(instruction, false);
1822 break;
1823 }
1824
1825 case Instruction::USHR_INT_LIT8: {
1826 Binop_22b<HUShr>(instruction, false);
1827 break;
1828 }
1829
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001830 case Instruction::NEW_INSTANCE: {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001831 uint16_t type_index = instruction.VRegB_21c();
1832 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
1833 ? kQuickAllocObjectWithAccessCheck
1834 : kQuickAllocObject;
1835
1836 current_block_->AddInstruction(new (arena_) HNewInstance(dex_pc, type_index, entrypoint));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001837 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
1838 break;
1839 }
1840
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001841 case Instruction::NEW_ARRAY: {
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001842 uint16_t type_index = instruction.VRegC_22c();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001843 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt);
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001844 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
1845 ? kQuickAllocArrayWithAccessCheck
1846 : kQuickAllocArray;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001847 current_block_->AddInstruction(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001848 new (arena_) HNewArray(length, dex_pc, type_index, entrypoint));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001849 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
1850 break;
1851 }
1852
1853 case Instruction::FILLED_NEW_ARRAY: {
1854 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
1855 uint32_t type_index = instruction.VRegB_35c();
1856 uint32_t args[5];
1857 instruction.GetVarArgs(args);
Calin Juravle225ff812014-11-13 16:46:39 +00001858 BuildFilledNewArray(dex_pc, type_index, number_of_vreg_arguments, false, args, 0);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001859 break;
1860 }
1861
1862 case Instruction::FILLED_NEW_ARRAY_RANGE: {
1863 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1864 uint32_t type_index = instruction.VRegB_3rc();
1865 uint32_t register_index = instruction.VRegC_3rc();
1866 BuildFilledNewArray(
Calin Juravle225ff812014-11-13 16:46:39 +00001867 dex_pc, type_index, number_of_vreg_arguments, true, nullptr, register_index);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001868 break;
1869 }
1870
1871 case Instruction::FILL_ARRAY_DATA: {
Calin Juravle225ff812014-11-13 16:46:39 +00001872 BuildFillArrayData(instruction, dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001873 break;
1874 }
1875
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001876 case Instruction::MOVE_RESULT:
Dave Allison20dfc792014-06-16 20:44:29 -07001877 case Instruction::MOVE_RESULT_WIDE:
1878 case Instruction::MOVE_RESULT_OBJECT:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001879 UpdateLocal(instruction.VRegA(), latest_result_);
1880 latest_result_ = nullptr;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001881 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001882
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001883 case Instruction::CMP_LONG: {
Calin Juravleddb7df22014-11-25 20:56:51 +00001884 Binop_23x_cmp(instruction, Primitive::kPrimLong, HCompare::kNoBias);
1885 break;
1886 }
1887
1888 case Instruction::CMPG_FLOAT: {
1889 Binop_23x_cmp(instruction, Primitive::kPrimFloat, HCompare::kGtBias);
1890 break;
1891 }
1892
1893 case Instruction::CMPG_DOUBLE: {
1894 Binop_23x_cmp(instruction, Primitive::kPrimDouble, HCompare::kGtBias);
1895 break;
1896 }
1897
1898 case Instruction::CMPL_FLOAT: {
1899 Binop_23x_cmp(instruction, Primitive::kPrimFloat, HCompare::kLtBias);
1900 break;
1901 }
1902
1903 case Instruction::CMPL_DOUBLE: {
1904 Binop_23x_cmp(instruction, Primitive::kPrimDouble, HCompare::kLtBias);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001905 break;
1906 }
1907
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001908 case Instruction::NOP:
1909 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001910
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001911 case Instruction::IGET:
1912 case Instruction::IGET_WIDE:
1913 case Instruction::IGET_OBJECT:
1914 case Instruction::IGET_BOOLEAN:
1915 case Instruction::IGET_BYTE:
1916 case Instruction::IGET_CHAR:
1917 case Instruction::IGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001918 if (!BuildInstanceFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001919 return false;
1920 }
1921 break;
1922 }
1923
1924 case Instruction::IPUT:
1925 case Instruction::IPUT_WIDE:
1926 case Instruction::IPUT_OBJECT:
1927 case Instruction::IPUT_BOOLEAN:
1928 case Instruction::IPUT_BYTE:
1929 case Instruction::IPUT_CHAR:
1930 case Instruction::IPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001931 if (!BuildInstanceFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001932 return false;
1933 }
1934 break;
1935 }
1936
1937 case Instruction::SGET:
1938 case Instruction::SGET_WIDE:
1939 case Instruction::SGET_OBJECT:
1940 case Instruction::SGET_BOOLEAN:
1941 case Instruction::SGET_BYTE:
1942 case Instruction::SGET_CHAR:
1943 case Instruction::SGET_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001944 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001945 return false;
1946 }
1947 break;
1948 }
1949
1950 case Instruction::SPUT:
1951 case Instruction::SPUT_WIDE:
1952 case Instruction::SPUT_OBJECT:
1953 case Instruction::SPUT_BOOLEAN:
1954 case Instruction::SPUT_BYTE:
1955 case Instruction::SPUT_CHAR:
1956 case Instruction::SPUT_SHORT: {
Calin Juravle225ff812014-11-13 16:46:39 +00001957 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001958 return false;
1959 }
1960 break;
1961 }
1962
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001963#define ARRAY_XX(kind, anticipated_type) \
1964 case Instruction::AGET##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00001965 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001966 break; \
1967 } \
1968 case Instruction::APUT##kind: { \
Calin Juravle225ff812014-11-13 16:46:39 +00001969 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001970 break; \
1971 }
1972
1973 ARRAY_XX(, Primitive::kPrimInt);
1974 ARRAY_XX(_WIDE, Primitive::kPrimLong);
1975 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
1976 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
1977 ARRAY_XX(_BYTE, Primitive::kPrimByte);
1978 ARRAY_XX(_CHAR, Primitive::kPrimChar);
1979 ARRAY_XX(_SHORT, Primitive::kPrimShort);
1980
Nicolas Geoffray39468442014-09-02 15:17:15 +01001981 case Instruction::ARRAY_LENGTH: {
1982 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001983 // No need for a temporary for the null check, it is the only input of the following
1984 // instruction.
Calin Juravle225ff812014-11-13 16:46:39 +00001985 object = new (arena_) HNullCheck(object, dex_pc);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001986 current_block_->AddInstruction(object);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001987 current_block_->AddInstruction(new (arena_) HArrayLength(object));
1988 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
1989 break;
1990 }
1991
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001992 case Instruction::CONST_STRING: {
Calin Juravle225ff812014-11-13 16:46:39 +00001993 current_block_->AddInstruction(new (arena_) HLoadString(instruction.VRegB_21c(), dex_pc));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001994 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
1995 break;
1996 }
1997
1998 case Instruction::CONST_STRING_JUMBO: {
Calin Juravle225ff812014-11-13 16:46:39 +00001999 current_block_->AddInstruction(new (arena_) HLoadString(instruction.VRegB_31c(), dex_pc));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002000 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
2001 break;
2002 }
2003
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002004 case Instruction::CONST_CLASS: {
2005 uint16_t type_index = instruction.VRegB_21c();
2006 bool type_known_final;
2007 bool type_known_abstract;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002008 bool dont_use_is_referrers_class;
2009 // `CanAccessTypeWithoutChecks` will tell whether the method being
2010 // built is trying to access its own class, so that the generated
2011 // code can optimize for this case. However, the optimization does not
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002012 // work for inlining, so we use `IsOutermostCompilingClass` instead.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002013 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
2014 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002015 &type_known_final, &type_known_abstract, &dont_use_is_referrers_class);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002016 if (!can_access) {
Calin Juravle48c2b032014-12-09 18:11:36 +00002017 MaybeRecordStat(MethodCompilationStat::kNotCompiledCantAccesType);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002018 return false;
2019 }
2020 current_block_->AddInstruction(
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002021 new (arena_) HLoadClass(type_index, IsOutermostCompilingClass(type_index), dex_pc));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002022 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2023 break;
2024 }
2025
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002026 case Instruction::MOVE_EXCEPTION: {
2027 current_block_->AddInstruction(new (arena_) HLoadException());
2028 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
2029 break;
2030 }
2031
2032 case Instruction::THROW: {
2033 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot);
Calin Juravle225ff812014-11-13 16:46:39 +00002034 current_block_->AddInstruction(new (arena_) HThrow(exception, dex_pc));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002035 // A throw instruction must branch to the exit block.
2036 current_block_->AddSuccessor(exit_block_);
2037 // We finished building this block. Set the current block to null to avoid
2038 // adding dead instructions to it.
2039 current_block_ = nullptr;
2040 break;
2041 }
2042
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002043 case Instruction::INSTANCE_OF: {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002044 uint8_t destination = instruction.VRegA_22c();
2045 uint8_t reference = instruction.VRegB_22c();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002046 uint16_t type_index = instruction.VRegC_22c();
Calin Juravle225ff812014-11-13 16:46:39 +00002047 if (!BuildTypeCheck(instruction, destination, reference, type_index, dex_pc)) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002048 return false;
2049 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002050 break;
2051 }
2052
2053 case Instruction::CHECK_CAST: {
2054 uint8_t reference = instruction.VRegA_21c();
2055 uint16_t type_index = instruction.VRegB_21c();
Calin Juravle225ff812014-11-13 16:46:39 +00002056 if (!BuildTypeCheck(instruction, -1, reference, type_index, dex_pc)) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002057 return false;
2058 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002059 break;
2060 }
2061
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002062 case Instruction::MONITOR_ENTER: {
2063 current_block_->AddInstruction(new (arena_) HMonitorOperation(
2064 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2065 HMonitorOperation::kEnter,
Calin Juravle225ff812014-11-13 16:46:39 +00002066 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002067 break;
2068 }
2069
2070 case Instruction::MONITOR_EXIT: {
2071 current_block_->AddInstruction(new (arena_) HMonitorOperation(
2072 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2073 HMonitorOperation::kExit,
Calin Juravle225ff812014-11-13 16:46:39 +00002074 dex_pc));
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002075 break;
2076 }
2077
Andreas Gamped881df52014-11-24 23:28:39 -08002078 case Instruction::PACKED_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002079 BuildPackedSwitch(instruction, dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -08002080 break;
2081 }
2082
Andreas Gampee4d4d322014-12-04 09:09:57 -08002083 case Instruction::SPARSE_SWITCH: {
Calin Juravle48c2b032014-12-09 18:11:36 +00002084 BuildSparseSwitch(instruction, dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -08002085 break;
2086 }
2087
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002088 default:
Calin Juravle48c2b032014-12-09 18:11:36 +00002089 VLOG(compiler) << "Did not compile "
2090 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
2091 << " because of unhandled instruction "
2092 << instruction.Name();
2093 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnhandledInstruction);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002094 return false;
2095 }
2096 return true;
Nicolas Geoffraydadf3172014-11-07 16:36:02 +00002097} // NOLINT(readability/fn_size)
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002098
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002099HLocal* HGraphBuilder::GetLocalAt(int register_index) const {
2100 return locals_.Get(register_index);
2101}
2102
2103void HGraphBuilder::UpdateLocal(int register_index, HInstruction* instruction) const {
2104 HLocal* local = GetLocalAt(register_index);
2105 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction));
2106}
2107
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002108HInstruction* HGraphBuilder::LoadLocal(int register_index, Primitive::Type type) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002109 HLocal* local = GetLocalAt(register_index);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002110 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type));
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002111 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002112}
2113
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002114} // namespace art